001    /*
002      Copyright (C) 2002-2003 Laurent Martelli <laurent@aopsys.com>
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.web;
020    
021    import org.objectweb.jac.aspects.gui.GuiAC;
022    import org.objectweb.jac.core.rtti.FieldItem;
023    import java.io.PrintWriter;
024    import java.lang.reflect.Constructor;
025    
026    
027    /**
028     * HTML editor for primitive types (int, long, float, double, short) and String
029     *
030     */
031    public class PrimitiveFieldEditor  extends AbstractFieldEditor 
032        implements HTMLEditor
033    {
034        boolean password;
035    
036        public PrimitiveFieldEditor(Object substance, FieldItem field,
037                                    boolean password) {
038            super(substance,field);
039            this.password = password;
040            width = 15;
041        }
042    
043        public PrimitiveFieldEditor(Object substance, FieldItem field) {
044            super(substance,field);
045            width = 15;
046        }
047    
048        // HTMLEditor interface
049    
050        public void genHTML(PrintWriter out) {
051            boolean hasDefault = field!=null && GuiAC.hasDefaultValue(field);
052            out.print("<INPUT type=\""+(password?"password":"text")+"\""+
053                      " class=\"editor\""+
054                      " id=\""+label+"\""+
055                      " name=\""+label+"\""+
056                      " style=\"width:"+width+"ex\""+
057                      " value=\""+displayedValue()+"\"");
058            printAttributes(out);
059            out.print(">");
060        }
061    
062        protected String displayedValue() {
063            return value==null ? "" : value.toString();
064        }
065    
066        protected boolean doReadValue(Object parameter) {
067            if (parameter==null)
068                return false;
069            String string = (String)parameter;
070            Class cl = type.getActualClass();
071            if (cl == int.class || cl == Integer.class) {
072                setValue(new Integer (string));
073            } else if (cl == boolean.class || cl == Boolean.class) {
074                setValue(Boolean.valueOf(string));
075            } else if (cl == long.class || cl == Long.class) {
076                setValue(new Long (string));
077            } else if (cl == float.class || cl == Float.class) {
078                setValue(new Float (string));
079            } else if (cl == double.class || cl == Double.class) {
080                setValue(new Double (string));
081            } else if (cl == short.class || cl == Short.class) {
082                setValue(new Short (string));
083            } else if (cl == byte.class || cl == Byte.class) {
084                setValue(new Byte(string));
085            } else if (cl == char.class || cl == Character.class) {
086                setValue(new Character(string.charAt(0)));
087            } else if (cl == String.class) {
088                setValue(string);
089            } else {
090                try {
091                    // trying to construct the object from its textual 
092                    // representation (I think that any class should have
093                    // a constructor taking a string... this is so helpful...)
094                    // of course, this will raise an exception most of the time :-(
095                    Constructor c = cl.getConstructor(new Class[] {String.class});
096                    setValue(c.newInstance(new Object[] {string}));
097                } catch (Exception e) {
098                    e.printStackTrace();
099                    throw new RuntimeException("Unhandled type "+type.getName());
100                }
101            }
102            return true;
103        }
104    }
105