KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > gui > swing > 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.swing;
19
20 import java.awt.event.FocusEvent JavaDoc;
21 import java.awt.event.FocusListener JavaDoc;
22 import java.lang.IllegalAccessException JavaDoc;
23 import org.apache.log4j.Logger;
24 import org.objectweb.jac.aspects.gui.CommitException;
25 import org.objectweb.jac.aspects.gui.FieldEditor;
26 import org.objectweb.jac.aspects.gui.FieldUpdate;
27 import org.objectweb.jac.aspects.gui.GuiAC;
28 import org.objectweb.jac.aspects.gui.Utils;
29 import org.objectweb.jac.core.Collaboration;
30 import org.objectweb.jac.core.rtti.ClassItem;
31 import org.objectweb.jac.core.rtti.FieldItem;
32 import org.objectweb.jac.core.rtti.MethodItem;
33
34 public abstract class AbstractFieldEditor extends AbstractView
35     implements FocusListener JavaDoc, FieldEditor, FieldUpdate
36 {
37     static Logger logger = Logger.getLogger("gui.editor");
38     static Logger loggerFocus = Logger.getLogger("gui.focus");
39
40     // substance and field are required so that we can register and
41
// unregister ourself from fieldUpdated events on close()
42
Object JavaDoc substance;
43     FieldItem field;
44     MethodItem setter;
45     Object JavaDoc value;
46     boolean isEmbedded;
47     protected ClassItem type;
48
49     public AbstractFieldEditor(Object JavaDoc substance, FieldItem field) {
50         this.substance = substance;
51         setField(field);
52         addFocusListener(this);
53     }
54
55     public void fieldUpdated(Object JavaDoc object, FieldItem field,
56                              Object JavaDoc value, Object JavaDoc param) {
57         setValue(value);
58     }
59
60     public abstract Object JavaDoc getValue();
61
62     public void setValue(Object JavaDoc value) {
63         this.value = value;
64     }
65
66     public void setField(FieldItem field) {
67         Utils.unregisterField(substance,this.field,this);
68         this.field = field;
69         Utils.registerField(substance,this.field,this);
70         if (field!=null)
71             this.setter = field.getSetter();
72     }
73
74     public FieldItem getField() {
75         return field;
76     }
77
78     public void setSubstance(Object JavaDoc substance) {
79         logger.debug("setSubstance("+substance+")");
80         this.substance = substance;
81     }
82
83     /**
84     * Returns the object that holds the field, if any
85     */

86     public Object JavaDoc getSubstance() {
87         return substance;
88     }
89
90     public void setEditedType(ClassItem type) {
91         this.type = type;
92     }
93
94     public void setEmbedded(boolean isEmbedded) {
95         this.isEmbedded = isEmbedded;
96         if (isEmbedded)
97             Utils.registerField(substance,field,this);
98         else
99             Utils.unregisterField(substance,field,this);
100     }
101
102     public void setAutoUpdate(boolean autoUpdate) {
103         // TODO
104
}
105
106     public void close(boolean validate) {
107         Utils.unregisterField(substance,field,this);
108         if (validate)
109             commit();
110         substance = null;
111         super.close(validate);
112     }
113
114     public void onSetFocus(Object JavaDoc extraOption) {}
115
116     /**
117     * Commit editing by calling the setter method.
118     */

119     public void commit() {
120         if (setter!=null && valueHasChanged()) {
121             logger.debug("value changed for "+substance+"."+field.getName());
122             try {
123                 field.setThroughWriter(substance,getValue());
124             } catch (IllegalAccessException JavaDoc e) {
125                 logger.error("Failed to commit value "+getValue()+
126                              " for field "+substance+"."+field,e);
127             } catch (Exception JavaDoc e) {
128                 throw new CommitException(e,substance,field);
129             }
130             value = getValue();
131         }
132     }
133
134     /**
135     * Tells wether the value in the editor was changed
136     */

137     boolean valueHasChanged() {
138         Object JavaDoc newValue = getValue();
139         boolean ret;
140         if (value == null && newValue != null) {
141             ret = true;
142         } else if (value == null) {
143             ret = false;
144         } else {
145             ret = ! value.equals(newValue);
146         }
147         logger.debug("valueHasChanged("+field.getName()+") "+
148                      value+" / "+newValue+" -> "+ret);
149         return ret;
150     }
151
152     // FocusListener interface
153

154     public void focusGained(FocusEvent JavaDoc e) {
155         /** do nothing */
156         loggerFocus.debug("focus gained on "+getClass().getName());
157     }
158
159     public void focusLost(FocusEvent JavaDoc e) {
160         /** Update the object's field if needed. */
161         loggerFocus.debug("focus lost on "+getClass().getName());
162         // We must ignore the event if closed==true because susbtance
163
// is set to null
164
if (field!=null && isEmbedded && !closed) {
165             invokeInContext(this,"commit", new Object JavaDoc[]{});
166         } else {
167             loggerFocus.debug("ignoring focusLost event");
168         }
169     }
170
171 }
172
Popular Tags