001: package net.suberic.util.gui.propedit;
002:
003: import javax.swing.*;
004: import net.suberic.util.*;
005: import java.util.*;
006: import java.awt.Container;
007: import java.awt.Component;
008:
009: /**
010: * A factory which can be used to create PropertyEditorUI's.
011: */
012: public class PropertyEditorFactory {
013: // the property that defines the different editor classes for the
014: // registry.
015: public static String SOURCE_PROPERTY = "PropertyEditor";
016:
017: // the VariableBundle that holds both the properties and the editor
018: // definitions.
019:
020: VariableBundle sourceBundle;
021:
022: // the propertyType to className mapping
023: Map typeToClassMap = new HashMap();
024:
025: /**
026: * Creates a PropertyEditorFactory using the given VariableBundle as
027: * a source.
028: */
029: public PropertyEditorFactory(VariableBundle bundle) {
030: sourceBundle = bundle;
031: createTypeToClassMap();
032: }
033:
034: /**
035: * Creates the typeToClassMap.
036: */
037: private void createTypeToClassMap() {
038:
039: try {
040: Class parentClass = Class
041: .forName("net.suberic.util.gui.propedit.SwingPropertyEditor");
042:
043: Vector propertyTypes = sourceBundle.getPropertyAsVector(
044: SOURCE_PROPERTY, "");
045: for (int i = 0; i < propertyTypes.size(); i++) {
046: String currentType = (String) propertyTypes.get(i);
047: String className = sourceBundle.getProperty(
048: SOURCE_PROPERTY + "." + currentType + ".class",
049: "");
050: try {
051: Class currentClass = Class.forName(className);
052: if (parentClass.isAssignableFrom(currentClass)) {
053: typeToClassMap.put(currentType, currentClass);
054: }
055: } catch (Exception e) {
056: System.out
057: .println("error registering class for property type "
058: + currentType + ": " + e);
059: }
060: }
061: } catch (Exception e) {
062: System.out
063: .println("caught exception initializing PropertyEditorFactory: "
064: + e);
065: e.printStackTrace();
066: }
067: }
068:
069: /**
070: * Shows an error message.
071: */
072: public void showError(Object component, String errorMessage) {
073: JOptionPane.showMessageDialog((Component) component,
074: errorMessage);
075: }
076:
077: /**
078: * Shows an input dialog.
079: */
080: public String showInputDialog(SwingPropertyEditor dpe, String query) {
081: return JOptionPane.showInputDialog(dpe, query);
082: }
083:
084: /**
085: * Creates and displays an editor window.
086: */
087: public void showNewEditorWindow(String title, List properties) {
088: showNewEditorWindow(title, properties, properties);
089: }
090:
091: /**
092: * Creates and displays an editor window.
093: */
094: public void showNewEditorWindow(String title, List properties,
095: List templates) {
096: showNewEditorWindow(title, properties, templates, null);
097: }
098:
099: /**
100: * Creates and displays an editor window.
101: */
102: public void showNewEditorWindow(String title, List properties,
103: List templates, PropertyEditorManager mgr) {
104: JFrame jf = (JFrame) createEditorWindow(title, properties,
105: templates, mgr);
106: jf.show();
107: }
108:
109: /**
110: * Creates and displays an editor window.
111: */
112: public void showNewEditorWindow(String title,
113: PropertyEditorUI editor) {
114: JFrame jf = new JFrame(title);
115: jf.getContentPane().add(
116: new PropertyEditorPane(editor.getManager(),
117: (SwingPropertyEditor) editor, jf));
118: jf.setSize(200, 200);
119: jf.pack();
120: jf.show();
121: }
122:
123: /**
124: * This method returns an EditorWindow (a JFrame in this
125: * implementation) which has an editor for each property in the
126: * properties List. The title string is the title of the
127: * JInternalFrame.
128: */
129: public Container createEditorWindow(String title, List properties) {
130: return createEditorWindow(title, properties, properties,
131: new PropertyEditorManager(sourceBundle, this ));
132: }
133:
134: /**
135: * This method returns an EditorWindow (a JFrame in this
136: * implementation) which has an editor for each property in the
137: * properties List. The title string is the title of the
138: * JFrame.
139: */
140: public Container createEditorWindow(String title, List properties,
141: List templates) {
142: return createEditorWindow(title, properties, templates,
143: new PropertyEditorManager(sourceBundle, this ));
144: }
145:
146: /**
147: * This method returns an EditorWindow (a JFrame in this
148: * implementation) which has an editor for each property in the
149: * properties Vector. The title string is the title of the
150: * JInternalFrame.
151: */
152: public Container createEditorWindow(String title, List properties,
153: List templates, PropertyEditorManager mgr) {
154: JFrame jf = new JFrame(title);
155: jf.getContentPane().add(
156: new PropertyEditorPane(mgr, properties, templates, jf));
157: jf.setSize(200, 200);
158: jf.pack();
159: return jf;
160: }
161:
162: /**
163: * Creates an appropriate PropertyEditorUI for the given property and
164: * editorTemplate, using the given PropertyEditorManager.
165: */
166: public PropertyEditorUI createEditor(String property,
167: String editorTemplate, PropertyEditorManager mgr) {
168:
169: return createEditor(property, editorTemplate, mgr, true);
170: }
171:
172: /**
173: * Creates an appropriate PropertyEditorUI for the given property and
174: * editorTemplate, using the given PropertyEditorManager.
175: */
176: public PropertyEditorUI createEditor(String property,
177: String editorTemplate, PropertyEditorManager mgr,
178: boolean enabled) {
179: String type = sourceBundle.getProperty(editorTemplate
180: + ".propertyType", "");
181: return createEditor(property, editorTemplate, type, mgr,
182: enabled);
183: }
184:
185: /**
186: * Creates an appropriate PropertyEditorUI for the given property and
187: * editorTemplate, using the given PropertyEditorManager.
188: */
189: public PropertyEditorUI createEditor(String property,
190: String editorTemplate, String type,
191: PropertyEditorManager mgr, boolean enabled) {
192:
193: //System.err.println("creating editor for property " + property + ", template " + editorTemplate + ", type " + type);
194:
195: Class editorClass = (Class) typeToClassMap.get(type);
196: if (editorClass == null) {
197: editorClass = (Class) typeToClassMap.get("String");
198: }
199:
200: PropertyEditorUI returnValue = null;
201: try {
202: returnValue = (PropertyEditorUI) editorClass.newInstance();
203: } catch (Exception e) {
204: System.err.println("error creating editor for property "
205: + property + ": " + e);
206: returnValue = new StringEditorPane();
207: }
208: returnValue.configureEditor(property, editorTemplate, mgr,
209: enabled);
210: return returnValue;
211: }
212:
213: /**
214: * Creates an appropriate PropertyEditorUI for the given property and
215: * editorTemplate, using the given PropertyEditorManager.
216: */
217: public PropertyEditorUI createEditor(List properties,
218: List editorTemplates, PropertyEditorManager mgr) {
219: return new CompositeEditorPane(properties, editorTemplates, mgr);
220: }
221:
222: /**
223: * Gets the source bundle for this factory.
224: */
225: public VariableBundle getSourceBundle() {
226: return sourceBundle;
227: }
228:
229: }
|