KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > util > gui > propedit > SwingPropertyEditor


1 package net.suberic.util.gui.propedit;
2 import net.suberic.util.*;
3 import javax.swing.*;
4 import java.awt.Dimension JavaDoc;
5 import java.util.HashSet JavaDoc;
6 import java.util.List JavaDoc;
7 import java.util.LinkedList JavaDoc;
8 import java.util.logging.Logger JavaDoc;
9 import java.util.Set JavaDoc;
10
11 /**
12  * A Swing implementation of the PropertyEditorUI.
13  */

14 public abstract class SwingPropertyEditor extends JPanel implements PropertyEditorUI {
15   // debug flag
16
protected boolean debug = false;
17
18   // a set of disable flags
19
protected Set JavaDoc disableMaskSet = new HashSet JavaDoc();
20
21   // the property being edited.
22
protected String JavaDoc property;
23
24   // the template to use
25
protected String JavaDoc editorTemplate;
26
27   // the property base to use
28
protected String JavaDoc propertyBase;
29
30   // the original value of the property.
31
protected String JavaDoc originalValue;
32
33   // the PorpertyEditorManager for this instance.
34
protected PropertyEditorManager manager;
35
36   // the logger
37
protected static Logger JavaDoc sLogger = Logger.getLogger("editors.debug");
38
39   /**
40    * Creates a new SwingPropertyEditor, in this case a JPanel with a
41    * SpringLayout. Note that configureEditor() will need to get called
42    * on this component in order to make it useful.
43    */

44   public SwingPropertyEditor() {
45     super();
46     this.setLayout(new java.awt.GridBagLayout JavaDoc());
47   }
48
49   /**
50    * Creates a SwingPropertyEditor using the given property and manager.
51    *
52    * @param propertyName The property to be edited. This property will
53    * also be used to define the layout of this Editor.
54    * @param template The template to be used for this property
55    * @param baseProperty The base property to be used for other scoped
56    * properties.
57    * @param newManager The PropertyEditorManager that will manage the
58    * changes.
59    */

60   public SwingPropertyEditor(String JavaDoc propertyName, String JavaDoc template, String JavaDoc baseProperty, PropertyEditorManager newManager) {
61     configureEditor(propertyName, template, baseProperty, newManager);
62   }
63
64   /**
65    * Creates a SwingPropertyEditor using the given property and manager.
66    *
67    * @param propertyName The property to be edited. This property will
68    * also be used to define the layout of this Editor.
69    * @param newManager The PropertyEditorManager that will manage the
70    * changes.
71    */

72   public SwingPropertyEditor(String JavaDoc propertyName, PropertyEditorManager newManager) {
73     configureEditor(propertyName, propertyName, newManager);
74   }
75
76   /**
77    * Creates a SwingPropertyEditor using the given property and manager.
78    *
79    * @param propertyName The property to be edited. This property will
80    * also be used to define the layout of this Editor.
81    * @param newManager The PropertyEditorManager that will manage the
82    * changes.
83    */

84   public void configureEditor(String JavaDoc propertyName, PropertyEditorManager newManager) {
85     configureEditor(propertyName, propertyName, newManager);
86   }
87
88   /**
89    * Creates a SwingPropertyEditor using the given property and manager.
90    *
91    * @param propertyName The property to be edited. This property will
92    * also be used to define the layout of this Editor.
93    * @param propertyName The template to use for this Property.
94    * @param newManager The PropertyEditorManager that will manage the
95    * changes.
96    */

97   public void configureEditor(String JavaDoc propertyName, String JavaDoc template, PropertyEditorManager manager) {
98     configureEditor(propertyName, template, propertyName, manager);
99   }
100
101   /**
102    * Loads the basic properties for all SwingPropertyEditors.
103    */

104   public void configureBasic(String JavaDoc propertyName, String JavaDoc template, String JavaDoc propertyBaseName, PropertyEditorManager newManager) {
105     manager=newManager;
106     propertyBase=propertyBaseName;
107     editorTemplate = template;
108     /*
109     if (propertyBaseName == null || propertyBaseName.length() == 0 || propertyBaseName.equals(propertyName)) {
110       property = propertyName;
111     } else {
112       property = propertyBaseName + "." + propertyName;
113     }
114     */

115     property = propertyName;
116     addDefaultListeners();
117     originalValue = manager.getProperty(property, manager.getProperty(editorTemplate, ""));
118     manager.registerPropertyEditor(property, this);
119     firePropertyInitializedEvent(originalValue);
120
121   }
122
123   /**
124    * Adds a disable mask to the PropertyEditorUI.
125    */

126   public void addDisableMask(Object JavaDoc key) {
127     disableMaskSet.add(key);
128     updateEditorEnabled();
129   }
130
131   /**
132    * Removes the disable mask keyed by this Object.
133    */

134   public void removeDisableMask(Object JavaDoc key) {
135     disableMaskSet.remove(key);
136     updateEditorEnabled();
137   }
138
139   /**
140    * Returns whether or not this Editor is currently enabled.
141    */

142   public boolean isEditorEnabled() {
143     return disableMaskSet.isEmpty();
144   }
145
146   /**
147    * Run when the PropertyEditor may have changed enabled states.
148    */

149   protected abstract void updateEditorEnabled();
150
151   /**
152    * Gets the PropertyEditorManager
153    */

154   public PropertyEditorManager getManager() {
155     return manager;
156   }
157
158   /**
159    * Adds a PropertyEditorListener to the ListenerList.
160    */

161   public void addPropertyEditorListener(PropertyEditorListener pel) {
162     manager.addPropertyEditorListener(getProperty(), pel);
163   }
164
165   /**
166    * Removes a PropertyEditorListener from the ListenerList.
167    */

168   public void removePropertyEditorListener(PropertyEditorListener pel) {
169     manager.removePropertyEditorListener(getProperty(), pel);
170   }
171
172
173   /**
174    * Fires a propertyChanging event to all of the PropertyEditorListeners.
175    * If any of the listeners veto the new value, then this returns false.
176    * Otherwise, returns true.
177    */

178   public void firePropertyChangingEvent(String JavaDoc newValue) throws PropertyValueVetoException {
179     manager.firePropertyChangingEvent(this, newValue);
180   }
181
182   /**
183    * Fires a propertyChanged event to all of the PropertyEditorListeners.
184    */

185   public void firePropertyChangedEvent(String JavaDoc newValue) {
186     manager.firePropertyChangedEvent(this, newValue);
187   }
188
189   /**
190    * Fires a propertyCommitting event to all of the PropertyEditorListeners.
191    */

192   public void firePropertyCommittingEvent(String JavaDoc newValue) throws PropertyValueVetoException {
193     manager.firePropertyCommittingEvent(this, newValue);
194   }
195
196   /**
197    * Fires a propertyInitialized event to all of the PropertyEditorListeners.
198    */

199   public void firePropertyInitializedEvent(String JavaDoc newValue) {
200     manager.firePropertyInitializedEvent(this, newValue);
201   }
202
203   /**
204    * Gets the parent PropertyEditorPane for the given component.
205    */

206   public abstract PropertyEditorPane getPropertyEditorPane();
207
208   /**
209    * Gets the parent PropertyEditorPane for the given component.
210    */

211   protected PropertyEditorPane getPropertyEditorPane(java.awt.Component JavaDoc component) {
212     try {
213       Class JavaDoc pepClass = Class.forName("net.suberic.util.gui.propedit.PropertyEditorPane");
214       if (pepClass != null) {
215         PropertyEditorPane pep = (PropertyEditorPane) SwingUtilities.getAncestorOfClass(pepClass, component);
216         return pep;
217       }
218     } catch (Exception JavaDoc e) {
219     }
220
221     return null;
222   }
223
224   /**
225    * Adds the appropriate listeners.
226    */

227   public void addDefaultListeners() {
228     List JavaDoc propertyListenerList = manager.getPropertyAsList(editorTemplate + "._listeners", "");
229     java.util.Iterator JavaDoc it = propertyListenerList.iterator();
230     while (it.hasNext()) {
231       String JavaDoc current = (String JavaDoc)it.next();
232       PropertyEditorListener pel = manager.createListener(current, property, propertyBase, editorTemplate);
233       if (pel != null) {
234         addPropertyEditorListener(pel);
235       }
236     }
237   }
238
239   /**
240    * Returns the currently edited property.
241    */

242   public String JavaDoc getProperty() {
243     return property;
244   }
245
246   /**
247    * Sets the original value.
248    */

249   public void setOriginalValue(String JavaDoc pOriginalValue) {
250     originalValue = pOriginalValue;
251   }
252
253   /**
254    * Returns the template for the current property.
255    */

256   public String JavaDoc getEditorTemplate() {
257     return editorTemplate;
258   }
259
260   /**
261    * Returns the helpId for this editor.
262    */

263   public String JavaDoc getHelpID() {
264     return getEditorTemplate();
265   }
266
267   /**
268    * Gets the Logger for this Editor.
269    *
270    */

271   public Logger JavaDoc getLogger() {
272     return sLogger;
273   }
274
275   /**
276    * Removes the PropertyEditor.
277    */

278   public void remove() {
279     manager.removePropertyEditorListeners(getProperty());
280   }
281
282   /**
283    * Accepts or rejects the initial focus for this component.
284    */

285   public boolean acceptDefaultFocus() {
286     return false;
287   }
288 }
289
Popular Tags