KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   Copyright (C) 2001-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,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

17
18 package org.objectweb.jac.aspects.gui.web;
19
20 import java.lang.IllegalAccessException JavaDoc;
21 import org.apache.log4j.Logger;
22 import org.objectweb.jac.aspects.gui.FieldEditor;
23 import org.objectweb.jac.aspects.gui.FieldUpdate;
24 import org.objectweb.jac.aspects.gui.GuiAC;
25 import org.objectweb.jac.aspects.gui.Length;
26 import org.objectweb.jac.aspects.gui.Utils;
27 import org.objectweb.jac.core.Collaboration;
28 import org.objectweb.jac.core.rtti.ClassItem;
29 import org.objectweb.jac.core.rtti.FieldItem;
30 import org.objectweb.jac.core.rtti.MethodItem;
31 import org.objectweb.jac.aspects.gui.Unit;
32
33 /**
34  * Base class for field editors
35  */

36 public abstract class AbstractFieldEditor extends AbstractView
37     implements FieldEditor, FieldUpdate
38 {
39     static Logger logger = Logger.getLogger("gui.editor");
40
41     // substance and field are required so that we can register and
42
// unregister ourself from fieldUpdated events on close()
43
protected Object JavaDoc substance;
44     protected FieldItem field;
45     protected ClassItem type;
46     protected MethodItem setter;
47     protected Object JavaDoc value;
48     Object JavaDoc oldValue;
49     protected boolean isEmbedded;
50     boolean isValueSet = false;
51
52     public AbstractFieldEditor(Object JavaDoc substance, FieldItem field) {
53         this.substance = substance;
54         setField(field);
55         width = new Length("10ex");
56     }
57
58     public Object JavaDoc getValue() {
59         return value;
60     }
61
62     public void setValue(Object JavaDoc value) {
63         logger.debug(this+".setValue "+oldValue+"->"+value);
64         this.value = value;
65         if (!isValueSet) {
66             oldValue = value;
67             isValueSet = true;
68         }
69     }
70
71     public void setSubstance(Object JavaDoc substance) {
72         this.substance = substance;
73     }
74
75     public Object JavaDoc getSubstance() {
76         return substance;
77     }
78
79     public void setEditedType(ClassItem type) {
80         this.type = type;
81     }
82
83     public void setField(FieldItem field) {
84         Utils.unregisterField(substance,this.field,this);
85         this.field = field;
86         if (field!=null)
87             this.setter = field.getSetter();
88         Utils.registerField(substance,field,this);
89     }
90
91     public FieldItem getField() {
92         return field;
93     }
94
95     public void setAutoUpdate(boolean autoUpdate) {
96     }
97
98     public void setEmbedded(boolean isEmbedded) {
99         this.isEmbedded = isEmbedded;
100     }
101
102     public void close(boolean validate) {
103         if (validate) {
104             Object JavaDoc paramValue =
105                 WebDisplay.getRequest().getParameter(getLabel());
106             logger.debug("reading value for "+getLabel()+": "+paramValue);
107             readValue(paramValue);
108             if (isEmbedded)
109                 commit();
110         }
111         Utils.unregisterField(substance,field,this);
112         context.removeEditor(this);
113     }
114
115     public void onSetFocus(Object JavaDoc param) {
116     }
117
118     public boolean readValue(Object JavaDoc parameter) {
119         if (disabled)
120             return false;
121         else
122             return doReadValue(parameter);
123     }
124    
125     protected abstract boolean doReadValue(Object JavaDoc parameter);
126
127     /**
128      * Commit editing by calling the setter method.
129      */

130     public void commit() {
131         if (setter!=null && valueHasChanged()) {
132             logger.debug(this+": "+field.getName()+
133                          "'s value changed: "+getValue());
134             try {
135                 field.setThroughWriter(substance,getValue());
136             } catch (IllegalAccessException JavaDoc e) {
137                 logger.error("Failed to commit value "+getValue()+
138                              " for field "+substance+"."+field,e);
139             }
140             oldValue = this.value;
141         }
142     }
143
144     /**
145     * Tells wether the value in the editor was changed
146     */

147     boolean valueHasChanged() {
148         boolean ret;
149         if( oldValue == null && value != null ) {
150             ret = true;
151         } else if( oldValue == null ) {
152             ret = false;
153         } else {
154             ret = ! oldValue.equals(value);
155         }
156         logger.debug("valueHasChanged("+field.getName()+") "+
157                   oldValue+" / "+value+" -> "+ret);
158         return ret;
159     }
160
161     public void fieldUpdated(Object JavaDoc substance, FieldItem field,
162                              Object JavaDoc value, Object JavaDoc param) {
163         setValue(value);
164     }
165
166     // When disabled == true, readValue() does nothing
167
boolean disabled = false;
168     public boolean isEnabled() {
169         return !disabled;
170     }
171     public void setEnabled(boolean enabled) {
172         logger.debug((enabled?"enable ":"disable ")+this);
173         this.disabled = !enabled;
174     }
175
176     protected String JavaDoc sizeSpec() {
177         String JavaDoc size = "";
178         if (width!=null && width.unit==Unit.EX)
179             size += " size=\""+width+"\"";
180         size += " style=\"width:"+width+"\"";
181         return size;
182      }
183 }
184
Popular Tags