KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   Copyright (C) 2001-2003 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 java.lang.reflect.Constructor JavaDoc;
23 import javax.swing.JPasswordField JavaDoc;
24 import org.objectweb.jac.aspects.gui.FieldEditor;
25 import org.objectweb.jac.aspects.gui.GuiAC;
26 import org.objectweb.jac.aspects.gui.Length;
27 import org.objectweb.jac.core.rtti.FieldItem;
28
29 /**
30  * A Swing editor component for fields values (primitive types).
31  */

32
33 public class PrimitiveFieldEditor extends TextFieldEditor
34     implements FieldEditor
35 {
36     /**
37      * Constructs a new primitive field editor.
38      */

39     public PrimitiveFieldEditor(Object JavaDoc substance, FieldItem field,
40                                 boolean password)
41     {
42         super(substance,field);
43         init(password);
44         checkType();
45     }
46
47     public PrimitiveFieldEditor(Object JavaDoc substance, FieldItem field)
48     {
49         super(substance,field);
50         init(false);
51         checkType();
52     }
53
54     protected void checkType() {
55         if (field!=null && field.getType().isArray())
56             throw new RuntimeException JavaDoc("PrimitiveFieldEditor cannot handle arrays");
57     }
58
59     protected void init(boolean password)
60     {
61         if (password)
62             textField = new JPasswordField JavaDoc();
63         else
64             textField = new JTextField();
65         setSize(new Length("15ex"),null);
66         textField.addFocusListener(this);
67         add(textField);
68     }
69
70     // FieldEditor interface
71

72     public Object JavaDoc getValue() {
73         Class JavaDoc cl = type.getActualClass();
74         if (cl == int.class || cl == Integer JavaDoc.class) {
75             return new Integer JavaDoc(textField.getText());
76         } else if (cl == boolean.class || cl == Boolean JavaDoc.class) {
77             return( Boolean.valueOf(textField.getText()));
78         } else if (cl == long.class || cl == Long JavaDoc.class) {
79             return new Long JavaDoc(textField.getText());
80         } else if (cl == float.class || cl == Float JavaDoc.class) {
81             return new Float JavaDoc(textField.getText());
82         } else if (cl == double.class || cl == Double JavaDoc.class) {
83             return new Double JavaDoc(textField.getText());
84         } else if (cl == short.class || cl == Short JavaDoc.class) {
85             return new Short JavaDoc (textField.getText());
86         } else if (cl == byte.class || cl == Byte JavaDoc.class) {
87             return new Byte JavaDoc(textField.getText());
88         } else if (cl == char.class || cl == Character JavaDoc.class) {
89             return new Character JavaDoc(textField.getText().charAt(0));
90         } else if (cl == String JavaDoc.class) {
91             return textField.getText();
92         } else if (!cl.isArray()) {
93             try {
94                 // trying to construct the object from its textual
95
// representation (I think that any class should have
96
// a constructor taking a string... this is so helpful...)
97
// of course, this will raise an exception most of the time :-(
98
Constructor JavaDoc c = cl.getConstructor(new Class JavaDoc[] {String JavaDoc.class});
99                 return c.newInstance( new Object JavaDoc[] {textField.getText()} );
100             } catch( Exception JavaDoc e ) {
101                 logger.error("Failed to instantiate "+cl.getName(),e);
102                 throw new RuntimeException JavaDoc("Unhandled type "+type.getName());
103             }
104         } else {
105             throw new RuntimeException JavaDoc("Unhandled type "+type.getName());
106         }
107     }
108
109     public void setValue(Object JavaDoc value) {
110         super.setValue(value);
111         if( value == null ) value="";
112         textField.setText(GuiAC.toString(value));
113         Dimension JavaDoc minSize = textField.getPreferredSize();
114         minSize.width = 100;
115         textField.setMinimumSize(minSize);
116     }
117
118     public void setAutoUpdate(boolean autoUpdate) {
119         // TODO
120
}
121 }
122
Popular Tags