001    /*
002      Copyright (C) 2003 Laurent Martelli <laurent@aopsys.com>
003      
004      This program is free software; you can redistribute it and/or modify
005      it under the terms of the GNU Lesser General Public License as
006      published by the Free Software Foundation; either version 2 of the
007      License, or (at your option) any later version.
008    
009      This program is distributed in the hope that it will be useful, but
010      WITHOUT ANY WARRANTY; without even the implied warranty of
011      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
012      Lesser General Public License for more details.
013    
014      You should have received a copy of the GNU Lesser General Public
015      License along with this program; if not, write to the Free Software
016      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
017      USA */
018    
019    package org.objectweb.jac.aspects.gui.swing;
020    
021    import java.awt.Dimension;
022    import java.text.ParsePosition;
023    import javax.swing.JTextField;
024    import org.objectweb.jac.aspects.gui.FieldEditor;
025    import org.objectweb.jac.core.rtti.FieldItem;
026    import org.objectweb.jac.util.WrappedThrowableException;
027    import org.objectweb.jac.aspects.gui.Format;
028    
029    public abstract class FormatEditor extends AbstractFieldEditor
030        implements FieldEditor
031    {
032        /** Stores the default format of the date */
033        protected Format format;
034        protected JTextField textField;
035    
036        /**
037        * Constructs a new date editor. */
038    
039        public FormatEditor(Object substance, FieldItem field) {
040            super(substance,field);
041            textField = new JTextField(10);
042            // swing "bug" workaround : prevent the textField from extending
043            // vertically.
044            textField.setMaximumSize(new Dimension(1000,20));
045            textField.addFocusListener(this);
046            add(textField);
047            initFormat(field);
048        }
049    
050        protected abstract void initFormat(FieldItem field);
051    
052        public void setField(FieldItem field) {
053            super.setField(field);
054            initFormat(field);
055        }
056    
057        public Object getValue() {
058            try {
059                if (textField.getText().equals("")) {
060                    return null;
061                } else {
062                    return parse(textField.getText());
063                }
064            } catch (Exception e) {
065                throw new WrappedThrowableException(e);
066            }
067        }
068    
069        protected Object parse(String s) {
070            ParsePosition pos = new ParsePosition(0);
071            return format.parse(s,pos);        
072        }
073    
074        public void setValue(Object value) {
075            super.setValue(value);
076            if( value != null )
077                textField.setText(format.format(value));
078        }
079    
080        public void setWidth(int width) {
081            super.setWidth(width);
082            textField.setColumns(width);
083        }
084    
085    }