KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   Copyright (C) 2002-2003 Laurent Martelli <laurent@aopsys.com>
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.web;
20
21 import java.io.PrintWriter JavaDoc;
22 import java.lang.reflect.Constructor JavaDoc;
23 import org.objectweb.jac.aspects.gui.GuiAC;
24 import org.objectweb.jac.aspects.gui.Length;
25 import org.objectweb.jac.core.rtti.FieldItem;
26
27
28 /**
29  * HTML editor for primitive types (int, long, float, double, short) and String
30  *
31  */

32 public class PrimitiveFieldEditor extends AbstractFieldEditor
33     implements HTMLEditor
34 {
35     boolean password;
36
37     public PrimitiveFieldEditor(Object JavaDoc substance, FieldItem field,
38                                 boolean password) {
39         super(substance,field);
40         this.password = password;
41         width = new Length("15ex");
42     }
43
44     public PrimitiveFieldEditor(Object JavaDoc substance, FieldItem field) {
45         super(substance,field);
46         width = new Length("15ex");
47     }
48
49     // HTMLEditor interface
50

51     public void genHTML(PrintWriter JavaDoc out) {
52         boolean hasDefault = field!=null && GuiAC.hasDefaultValue(field);
53         out.print("<INPUT type=\""+(password?"password":"text")+"\""+
54                   " class=\"editor\""+
55                   " id=\""+label+"\""+
56                   " name=\""+label+"\""+
57                   sizeSpec()+
58                   " value=\""+displayedValue()+"\"");
59         printAttributes(out);
60         out.print(">");
61     }
62
63     protected String JavaDoc displayedValue() {
64         return value==null ? "" : value.toString();
65     }
66
67     protected boolean doReadValue(Object JavaDoc parameter) {
68         if (parameter==null)
69             return false;
70         String JavaDoc string = (String JavaDoc)parameter;
71         Class JavaDoc cl = type.getActualClass();
72         if (cl == int.class || cl == Integer JavaDoc.class) {
73             setValue(new Integer JavaDoc (string));
74         } else if (cl == boolean.class || cl == Boolean JavaDoc.class) {
75             setValue(Boolean.valueOf(string));
76         } else if (cl == long.class || cl == Long JavaDoc.class) {
77             setValue(new Long JavaDoc (string));
78         } else if (cl == float.class || cl == Float JavaDoc.class) {
79             setValue(new Float JavaDoc (string));
80         } else if (cl == double.class || cl == Double JavaDoc.class) {
81             setValue(new Double JavaDoc (string));
82         } else if (cl == short.class || cl == Short JavaDoc.class) {
83             setValue(new Short JavaDoc (string));
84         } else if (cl == byte.class || cl == Byte JavaDoc.class) {
85             setValue(new Byte JavaDoc(string));
86         } else if (cl == char.class || cl == Character JavaDoc.class) {
87             setValue(new Character JavaDoc(string.charAt(0)));
88         } else if (cl == String JavaDoc.class) {
89             setValue(string);
90         } else {
91             try {
92                 // trying to construct the object from its textual
93
// representation (I think that any class should have
94
// a constructor taking a string... this is so helpful...)
95
// of course, this will raise an exception most of the time :-(
96
Constructor JavaDoc c = cl.getConstructor(new Class JavaDoc[] {String JavaDoc.class});
97                 setValue(c.newInstance(new Object JavaDoc[] {string}));
98             } catch (Exception JavaDoc e) {
99                 e.printStackTrace();
100                 throw new RuntimeException JavaDoc("Unhandled type "+type.getName());
101             }
102         }
103         return true;
104     }
105 }
106
107
Popular Tags