KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > gui > swing > PasswordFieldEditor


1 /*
2   Copyright (C) 2001 Renaud Pawlak, Laurent Martelli
3   
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful, but
10   WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public
15   License along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17   USA */

18
19 package org.objectweb.jac.aspects.gui.swing;
20
21 import java.awt.Dimension JavaDoc;
22 import javax.swing.JLabel JavaDoc;
23 import javax.swing.JPasswordField JavaDoc;
24 import javax.swing.JTextField JavaDoc;
25 import org.objectweb.jac.aspects.gui.FieldEditor;
26 import org.objectweb.jac.aspects.gui.GuiAC;
27 import org.objectweb.jac.aspects.gui.Length;
28 import org.objectweb.jac.core.rtti.FieldItem;
29 import org.objectweb.jac.core.rtti.NamingConventions;
30
31 /**
32  * A Swing editor component for fields values (password types).
33  */

34
35 public class PasswordFieldEditor extends AbstractFieldEditor
36     implements FieldEditor
37 {
38     JTextField textField;
39     JTextField confirmField;
40
41     /**
42      * Constructs a new password field editor. */

43
44     public PasswordFieldEditor(Object JavaDoc substance, FieldItem field)
45     {
46         super(substance,field);
47         textField = new JPasswordField JavaDoc();
48         confirmField = new JPasswordField JavaDoc();
49         // swing "bug" workaround : prevent the textField from extending
50
// vertically.
51
Dimension JavaDoc minSize = textField.getPreferredSize();
52         Dimension JavaDoc maxSize = textField.getMaximumSize();
53         textField.setMaximumSize(new Dimension JavaDoc(maxSize.width,minSize.height));
54
55         // textField.addFocusListener(this);
56
add(textField);
57
58         confirmField.setMaximumSize(new Dimension JavaDoc(maxSize.width,minSize.height));
59         confirmField.addFocusListener(this);
60         add(new JLabel JavaDoc("Retype "+NamingConventions.textForName(field.getName())));
61         add(confirmField);
62
63     }
64
65     // FieldEditor interface
66

67     public Object JavaDoc getValue() {
68         if (!(confirmField.getText().equals(textField.getText())))
69             throw new RuntimeException JavaDoc(NamingConventions.textForName(field.getName())+" and its confirmation are different");
70         return( textField.getText() );
71     }
72
73     public void setValue(Object JavaDoc value) {
74         super.setValue(value);
75         if( value == null ) value="";
76         textField.setText(GuiAC.toString(value));
77         Dimension JavaDoc minSize = textField.getPreferredSize();
78         minSize.width = 100;
79         textField.setMinimumSize(minSize);
80     }
81
82     public void setSize(Length width, Length height) {
83         super.setSize(width,height);
84         SwingUtils.setColumns(textField,width);
85         SwingUtils.setColumns(confirmField,width);
86     }
87
88     /**
89      * Set the focus on the textField
90      */

91     public void requestFocus() {
92         textField.requestFocus();
93     }
94
95 }
96
Popular Tags