001: package net.suberic.util.gui.propedit;
002:
003: import net.suberic.util.VariableBundle;
004: import java.util.*;
005:
006: /**
007: * This manages a set of PropertyEditors. Basically, this acts as a
008: * Transaction for the PropertyEditors.
009: */
010: public class PropertyEditorManager {
011:
012: protected HashMap editorMap = new HashMap();
013:
014: protected VariableBundle sourceBundle;
015:
016: protected PropertyEditorFactory propertyFactory;
017:
018: protected HashMap pendingListenerMap = new HashMap();
019:
020: protected boolean writeChanges = true;
021:
022: protected Properties localProps = new Properties();
023:
024: /**
025: * Creates a new PropertyEditorManager.
026: */
027: protected PropertyEditorManager() {
028: }
029:
030: /**
031: * Creates a PropertyEditorManager using the given VariableBundle and
032: * PropertyEditorFactory.
033: */
034: public PropertyEditorManager(VariableBundle vb,
035: PropertyEditorFactory factory) {
036: sourceBundle = vb;
037: propertyFactory = factory;
038: }
039:
040: /**
041: * Gets the PropertyEditor for the given Property.
042: */
043: public PropertyEditorUI getPropertyEditor(String propertyName) {
044: return (PropertyEditorUI) editorMap.get(propertyName);
045: }
046:
047: /**
048: * Registers the given PropertyEditorUI as the editor for the given
049: * Property.
050: */
051: public void registerPropertyEditor(String property,
052: PropertyEditorUI editor) {
053: List listenerList = (List) pendingListenerMap.get(property);
054: if (listenerList != null) {
055: Iterator it = listenerList.iterator();
056: while (it.hasNext()) {
057: editor
058: .addPropertyEditorListener((PropertyEditorListener) it
059: .next());
060: }
061: }
062:
063: editorMap.put(property, editor);
064: }
065:
066: /**
067: * Gets the PropertyEditorFactory for this manager.
068: */
069: public PropertyEditorFactory getFactory() {
070: return propertyFactory;
071: }
072:
073: /**
074: * Gets the value of the given property.
075: */
076: public String getProperty(String property, String defaultValue) {
077: // check the localProps first
078: if (!writeChanges) {
079: String tmpValue = (String) localProps.get(property);
080: if (tmpValue != null)
081: return tmpValue;
082: }
083: return sourceBundle.getProperty(property, defaultValue);
084: }
085:
086: /**
087: * Gets the value of the given property.
088: */
089: public List getPropertyAsList(String property, String defaultValue) {
090: // check the localProps first
091: if (!writeChanges) {
092: String tmpValue = (String) localProps.get(property);
093: if (tmpValue != null) {
094: return VariableBundle.convertToVector(tmpValue);
095: }
096: }
097: return sourceBundle.getPropertyAsVector(property, defaultValue);
098: }
099:
100: /**
101: * Sets the given property to the given value.
102: */
103: public void setProperty(String property, String value) {
104: if (!writeChanges) {
105: localProps.setProperty(property, value);
106: } else {
107: sourceBundle.setProperty(property, value);
108: }
109: }
110:
111: /**
112: * Removes the given property.
113: */
114: public void removeProperty(String property) {
115: if (writeChanges)
116: sourceBundle.removeProperty(property);
117: }
118:
119: /**
120: * Creates an appropriate PropertyEditorUI for the given property and
121: * editorTemplate, using this PropertyEditorManager.
122: */
123: public PropertyEditorUI createEditor(String property,
124: String editorTemplate) {
125: return getFactory()
126: .createEditor(property, editorTemplate, this );
127: }
128:
129: /**
130: * Creates an appropriate PropertyEditorUI for the given properties and
131: * editorTemplates, using this PropertyEditorManager.
132: */
133: public PropertyEditorUI createEditor(List properties,
134: List editorTemplates) {
135: return getFactory().createEditor(properties, editorTemplates,
136: this );
137: }
138:
139: /**
140: * Commits the changes to the underlying VariableBundle.
141: */
142: public void commit() {
143: if (writeChanges)
144: sourceBundle.saveProperties();
145: }
146:
147: /**
148: * Adds the given PropertyEditorListener as a listener to the editor
149: * for the given property. If no editor exists yet, then the listener
150: * is saved until a PropertyEditorUI is registered.
151: */
152: public void addPropertyEditorListener(String property,
153: PropertyEditorListener listener) {
154: PropertyEditorUI editor = (PropertyEditorUI) editorMap
155: .get(property);
156: if (editor != null) {
157: editor.addPropertyEditorListener(listener);
158: } else {
159: List listenerList = (List) pendingListenerMap.get(property);
160: if (listenerList == null) {
161: listenerList = new ArrayList();
162: }
163: listenerList.add(listener);
164: pendingListenerMap.put(property, listenerList);
165: }
166: }
167:
168: /**
169: * Creates an appropriate PropertyEditorListener from the given
170: * String.
171: */
172: public PropertyEditorListener createListener(String key) {
173: try {
174: Class pelClass = Class.forName(getProperty(key + ".class",
175: ""));
176: ConfigurablePropertyEditorListener pel = (ConfigurablePropertyEditorListener) pelClass
177: .newInstance();
178: pel.configureListener(key, this );
179: return pel;
180: } catch (Exception e) {
181:
182: }
183:
184: return null;
185: }
186:
187: /**
188: * Sets whether or not this PEM should write its changes to the source
189: * VariableBundle.
190: */
191: public void setWriteChanges(boolean newValue) {
192: writeChanges = newValue;
193: }
194:
195: }
|