001    /*
002      Copyright (C) 2001 Renaud Pawlak, Laurent Martelli
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 org.objectweb.jac.aspects.gui.FieldEditor;
022    import org.objectweb.jac.aspects.gui.GuiAC;
023    import org.objectweb.jac.core.rtti.FieldItem;
024    import org.objectweb.jac.core.rtti.NamingConventions;
025    import java.awt.Dimension;
026    import javax.swing.JPasswordField;
027    import javax.swing.JTextField;
028    import javax.swing.JLabel;
029    
030    /**
031     * A Swing editor component for fields values (password types).
032     */
033    
034    public class PasswordFieldEditor extends AbstractFieldEditor
035        implements FieldEditor
036    {
037        JTextField textField;
038        JTextField confirmField;
039    
040        /**
041         * Constructs a new password field editor. */
042    
043        public PasswordFieldEditor(Object substance, FieldItem field)
044        {
045            super(substance,field);
046            textField = new JPasswordField();
047            confirmField = new JPasswordField();
048            // swing "bug" workaround : prevent the textField from extending
049            // vertically.
050            Dimension minSize = textField.getPreferredSize();
051            Dimension maxSize = textField.getMaximumSize();
052            textField.setMaximumSize(new Dimension(maxSize.width,minSize.height));
053    
054            // textField.addFocusListener(this);
055            add(textField);
056    
057            confirmField.setMaximumSize(new Dimension(maxSize.width,minSize.height));
058            confirmField.addFocusListener(this);
059            add(new JLabel("Retype "+NamingConventions.textForName(field.getName())));
060            add(confirmField);
061    
062        }
063    
064        // FieldEditor interface
065    
066        public Object getValue() {
067            if (!(confirmField.getText().equals(textField.getText())))
068                throw new RuntimeException(NamingConventions.textForName(field.getName())+" and its confirmation are different");
069            return( textField.getText() );
070        }
071    
072        public void setValue(Object value) {
073            super.setValue(value);
074            if( value == null ) value="";
075            textField.setText(GuiAC.toString(value));
076            Dimension minSize = textField.getPreferredSize();
077            minSize.width = 100;
078            textField.setMinimumSize(minSize);
079        }
080    
081        public void setWidth(int width) {
082            textField.setColumns(width);
083        }
084    
085        /**
086         * Set the focus on the textField
087         */
088        public void requestFocus() {
089            textField.requestFocus();
090        }
091    
092    }