Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: jac/aspects/gui/web/PrimitiveFieldEditor.java


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 jac.aspects.gui.web;
20  
21  import jac.aspects.gui.GuiAC;
22  import jac.core.rtti.FieldItem;
23  import java.io.PrintWriter;
24  import java.lang.reflect.Constructor;
25  
26  
27  /**
28   * HTML editor for primitive types (int, long, float, double, short) and String
29   *
30   */
31  public class PrimitiveFieldEditor  extends AbstractFieldEditor 
32     implements HTMLEditor
33  {
34     Class type;
35     boolean password;
36  
37     public PrimitiveFieldEditor(Object substance, FieldItem field,
38                                 Class type, boolean password) {
39        super(substance,field);
40        this.type = type;
41        this.password = password;
42        width = 15;
43     }
44  
45     // HTMLEditor interface
46  
47     public void genHTML(PrintWriter out) {
48        out.println("<INPUT type=\""+(password?"password":"text")+"\""+
49                    " class=\"editor\""+
50                    " name=\""+label+"\""+
51                    " style=\"width:"+width+"ex\""+
52                    " value=\""+ ( value==null ? "" : value.toString() ) +"\">");
53     }
54  
55     public boolean readValue(Object parameter) {
56        String string = (String)parameter;
57        if ( type == int.class || type == Integer.class ) {
58           setValue(new Integer (string));
59        } else if ( type == boolean.class || type == Boolean.class) {
60           setValue(new Boolean (string));
61        } else if ( type == long.class || type == Long.class ) {
62           setValue(new Long (string));
63        } else if ( type == float.class || type == Float.class ) {
64           setValue(new Float (string));
65        } else if ( type == double.class || type == Double.class ) {
66           setValue(new Double (string));
67        } else if ( type == short.class || type == Short.class ) {
68           setValue(new Short (string));
69        } else if ( type == byte.class || type == Byte.class ) {
70           setValue(new Byte(string));
71        } else if ( type == char.class || type == Character.class ) {
72           setValue(new Character(string.charAt(0)));
73        } else if ( type == String.class ) {
74           setValue(string);
75        } else {
76           try {
77              // trying to construct the object from its textual 
78              // representation (I think that any class should have
79              // a constructor taking a string... this is so helpful...)
80              // of course, this will raise an exception most of the time :-(
81              Constructor c = type.getConstructor(new Class[] {String.class});
82              setValue(c.newInstance(new Object[] {string}));
83           } catch( Exception e ) {
84              e.printStackTrace();
85              throw new RuntimeException("Unhandled type "+type.getName());
86           }
87        }
88        return true;
89     }
90  }
91