KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.suberic.util.gui.propedit;
2 import net.suberic.util.VariableBundle;
3 import net.suberic.util.gui.IconManager;
4 import java.util.*;
5
6 /**
7  * This manages a set of PropertyEditors. Basically, this acts as a
8  * Transaction for the PropertyEditors.
9  */

10 public class PropertyEditorManager {
11
12   protected HashMap<String JavaDoc,PropertyEditorUI> editorMap = new HashMap<String JavaDoc,PropertyEditorUI>();
13
14   protected VariableBundle sourceBundle;
15
16   protected PropertyEditorFactory propertyFactory;
17
18   protected HashMap<String JavaDoc,List<PropertyEditorListener>> listenerMap = new HashMap<String JavaDoc,List<PropertyEditorListener>>();
19
20   protected boolean writeChanges = true;
21
22   protected Properties localProps = new Properties();
23   protected Properties tempProps = new Properties();
24
25   //protected HashSet<String> removeProps = new HashSet<String>();
26

27   protected IconManager iconManager;
28
29   // whether or not we've created a PropertyEditorPane for this Manager.
30
public boolean createdEditorPane = false;
31
32   /**
33    * Creates a new PropertyEditorManager.
34    */

35   protected PropertyEditorManager() {
36   }
37
38   /**
39    * Creates a PropertyEditorManager using the given VariableBundle,
40    * PropertyEditorFactory, and IconManager.
41    */

42   public PropertyEditorManager(VariableBundle vb, PropertyEditorFactory factory, IconManager manager) {
43     sourceBundle = vb;
44     propertyFactory = factory;
45     iconManager = manager;
46   }
47
48   /**
49    * Gets the PropertyEditor for the given Property.
50    */

51   public PropertyEditorUI getPropertyEditor(String JavaDoc propertyName) {
52     return (PropertyEditorUI) editorMap.get(propertyName);
53   }
54
55   /**
56    * Registers the given PropertyEditorUI as the editor for the given
57    * Property.
58    */

59   public void registerPropertyEditor(String JavaDoc property, PropertyEditorUI editor) {
60     editorMap.put(property, editor);
61   }
62
63   /**
64    * Gets the PropertyEditorFactory for this manager.
65    */

66   public PropertyEditorFactory getFactory() {
67     return propertyFactory;
68   }
69
70   /**
71    * Gets the IconManager for this PropertyEditorManager.
72    */

73   public IconManager getIconManager() {
74     return iconManager;
75   }
76
77   /**
78    * Gets the value of the given property.
79    */

80   public String JavaDoc getProperty(String JavaDoc property, String JavaDoc defaultValue) {
81     // check the tempProps first
82
String JavaDoc tmpValue = (String JavaDoc) tempProps.get(property);
83     if (tmpValue != null)
84       return tmpValue;
85     // then check the localProps
86
tmpValue = (String JavaDoc) localProps.get(property);
87     if (tmpValue != null)
88       return tmpValue;
89     return sourceBundle.getProperty(property, defaultValue);
90   }
91
92   /**
93    * Gets the value of the given property.
94    */

95   public String JavaDoc getCurrentProperty(String JavaDoc property, String JavaDoc defaultValue) {
96     String JavaDoc tmpValue = null;
97     // check the PropertyEditor first.
98
PropertyEditorUI editor = getPropertyEditor(property);
99     if (editor != null) {
100       Properties value = editor.getValue();
101       tmpValue = value.getProperty(property);
102       if (tmpValue != null)
103         return tmpValue;
104     }
105     // then check the tempProps
106
tmpValue = (String JavaDoc) tempProps.get(property);
107     if (tmpValue != null)
108       return tmpValue;
109     // then check the localProps
110
tmpValue = (String JavaDoc) localProps.get(property);
111     if (tmpValue != null)
112       return tmpValue;
113     return sourceBundle.getProperty(property, defaultValue);
114   }
115
116   /**
117    * Gets the value of the given property.
118    */

119   public List<String JavaDoc> getPropertyAsList(String JavaDoc property, String JavaDoc defaultValue) {
120     // check the tempProps first
121
String JavaDoc tmpValue = (String JavaDoc) tempProps.get(property);
122     if (tmpValue != null) {
123       return VariableBundle.convertToVector(tmpValue);
124     }
125     // check the localProps second
126
tmpValue = (String JavaDoc) localProps.get(property);
127     if (tmpValue != null) {
128       return VariableBundle.convertToVector(tmpValue);
129     }
130     return sourceBundle.getPropertyAsList(property, defaultValue);
131   }
132
133   /**
134    * Gets the value of the given property.
135    */

136   public Set<String JavaDoc> getPropertyNamesStartingWith(String JavaDoc startsWith) {
137     Set<String JavaDoc> returnValue = new HashSet<String JavaDoc>();
138     // check temporary properties first.
139
Set<String JavaDoc> tProps = tempProps.stringPropertyNames();
140     for (String JavaDoc prop: tProps) {
141       if (prop.startsWith(startsWith))
142         returnValue.add(prop);
143     }
144     // check local properties next
145
Set<String JavaDoc> lProps = localProps.stringPropertyNames();
146     for (String JavaDoc prop: lProps) {
147       if (prop.startsWith(startsWith))
148         returnValue.add(prop);
149     }
150     returnValue.addAll(sourceBundle.getPropertyNamesStartingWith(startsWith));
151     return returnValue;
152   }
153
154   /**
155    * Sets the given property to the given value.
156    */

157   public void setProperty(String JavaDoc property, String JavaDoc value) {
158     tempProps.remove(property);
159     localProps.setProperty(property, value);
160     /*
161     if (value != null && value.length() > 0) {
162       removeProps.remove(property);
163     } else {
164       removeProps.add(property);
165     }
166     */

167   }
168
169   /**
170    * Sets the given property to the given value.
171    */

172   public void setTemporaryProperty(String JavaDoc property, String JavaDoc value) {
173     tempProps.setProperty(property, value);
174   }
175
176   /**
177    * Removes the given property.
178    */

179   public void removeProperty(String JavaDoc property) {
180     setProperty(property, "");
181     //removeProps.add(property);
182
//localProps.remove(property);
183
}
184
185   /**
186    * Creates an appropriate PropertyEditorUI for the given property and
187    * editorTemplate, using this PropertyEditorManager.
188    */

189   public PropertyEditorUI createEditor(String JavaDoc property, String JavaDoc editorTemplate, String JavaDoc propertyBase) {
190     return getFactory().createEditor(property, editorTemplate, propertyBase, this);
191   }
192
193   /**
194    * Returns a formatted message using the given key and the appropriate
195    * objects. If no message corresponding to the given key exists, uses
196    * the key string as the pattern instead.
197    */

198   public String JavaDoc formatMessage(String JavaDoc key, Object JavaDoc... arguments) {
199     return sourceBundle.formatMessage(key, arguments);
200   }
201
202   /**
203    * Commits the changes to the underlying VariableBundle.
204    */

205   public void commit() {
206     if (writeChanges) {
207       /*
208       for (String removeProp: removeProps) {
209         sourceBundle.setProperty(removeProp, "");
210       }
211       */

212
213       sourceBundle.setAllProperties(localProps);
214
215       sourceBundle.saveProperties();
216
217       clearValues();
218     }
219   }
220
221   /**
222    * Clears modified values.
223    */

224   public void clearValues() {
225     localProps = new Properties();
226     tempProps = new Properties();
227     //removeProps = new HashSet<String>();
228
}
229
230   /**
231    * Creates an appropriate PropertyEditorListener from the given
232    * String.
233    */

234   public PropertyEditorListener createListener(String JavaDoc key, String JavaDoc property, String JavaDoc propertyBase, String JavaDoc editorTemplate) {
235     try {
236       Class JavaDoc pelClass = Class.forName(getProperty(key + ".class", ""));
237       ConfigurablePropertyEditorListener pel = (ConfigurablePropertyEditorListener) pelClass.newInstance();
238       pel.configureListener(key, property, propertyBase, editorTemplate, this);
239       return pel;
240     } catch (Exception JavaDoc e) {
241       System.err.println("error configuring listener from key " + key + " for property " + property);
242       e.printStackTrace();
243     }
244
245     return null;
246   }
247
248   /**
249    * Sets whether or not this PEM should write its changes to the source
250    * VariableBundle.
251    */

252   public void setWriteChanges(boolean newValue) {
253     writeChanges = newValue;
254   }
255
256   /**
257    * Adds a PropertyEditorListener to the ListenerList.
258    */

259   public void addPropertyEditorListener(String JavaDoc property, PropertyEditorListener pel) {
260     if (property != null) {
261       List<PropertyEditorListener> listenerList = listenerMap.get(property);
262       if (listenerList == null) {
263         listenerList = new ArrayList<PropertyEditorListener>();
264         listenerMap.put(property, listenerList);
265       }
266       if (pel != null && ! listenerList.contains(pel))
267         listenerList.add(pel);
268     }
269   }
270
271   /**
272    * Removes a PropertyEditorListener from the ListenerList.
273    */

274   public void removePropertyEditorListener(String JavaDoc property, PropertyEditorListener pel) {
275     if (property != null) {
276       List<PropertyEditorListener> listenerList = listenerMap.get(property);
277       if (listenerList != null) {
278         if (pel != null && listenerList.contains(pel))
279           listenerList.remove(pel);
280       }
281     }
282   }
283
284   /**
285    * Removes all PropertyEditorListeners from the ListenerList for this
286    * property.
287    */

288   public void removePropertyEditorListeners(String JavaDoc property) {
289     if (property != null) {
290       listenerMap.remove(property);
291     }
292   }
293
294   /**
295    * Fires a propertyChanging event to all of the PropertyEditorListeners.
296    * If any of the listeners veto the new value, then this returns false.
297    * Otherwise, returns true.
298    */

299   public void firePropertyChangingEvent(PropertyEditorUI propertyEditor, String JavaDoc newValue) throws PropertyValueVetoException {
300     String JavaDoc property = propertyEditor.getProperty();
301     List<PropertyEditorListener> listenerList = listenerMap.get(property);
302     if (listenerList != null) {
303       for (PropertyEditorListener current: listenerList) {
304         current.propertyChanging(propertyEditor, property, newValue);
305       }
306     }
307   }
308
309   /**
310    * Fires a propertyChanged event to all of the PropertyEditorListeners.
311    */

312   public void firePropertyChangedEvent(PropertyEditorUI propertyEditor, String JavaDoc newValue) {
313     String JavaDoc property = propertyEditor.getProperty();
314     List<PropertyEditorListener> listenerList = listenerMap.get(property);
315     if (listenerList != null) {
316       for (PropertyEditorListener current: listenerList) {
317         current.propertyChanged(propertyEditor, property, newValue);
318       }
319     }
320   }
321
322   /**
323    * Fires a propertyCommitting event to all of the PropertyEditorListeners.
324    */

325   public void firePropertyCommittingEvent(PropertyEditorUI propertyEditor, String JavaDoc newValue) throws PropertyValueVetoException {
326     String JavaDoc property = propertyEditor.getProperty();
327     List<PropertyEditorListener> listenerList = listenerMap.get(property);
328     if (listenerList != null) {
329       for (PropertyEditorListener current: listenerList) {
330         current.propertyCommitting(propertyEditor, property, newValue);
331       }
332     }
333   }
334
335   /**
336    * Fires a propertyInitialized event to all of the PropertyEditorListeners.
337    */

338   public void firePropertyInitializedEvent(PropertyEditorUI propertyEditor, String JavaDoc newValue) {
339     String JavaDoc property = propertyEditor.getProperty();
340     List<PropertyEditorListener> listenerList = listenerMap.get(property);
341     if (listenerList != null) {
342       for (PropertyEditorListener current: listenerList) {
343         current.propertyInitialized(propertyEditor, property, newValue);
344       }
345     }
346   }
347
348
349
350 }
351
352
Popular Tags