KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.suberic.util.gui.propedit;
2 import javax.swing.*;
3 import net.suberic.util.*;
4 import net.suberic.util.gui.IconManager;
5 import java.util.*;
6 import java.awt.Container JavaDoc;
7 import java.awt.Component JavaDoc;
8 import java.awt.Frame JavaDoc;
9 import java.awt.Dialog JavaDoc;
10 import java.awt.Dimension JavaDoc;
11 import java.awt.Point JavaDoc;
12 import javax.help.HelpBroker;
13
14 /**
15  * A factory which can be used to create PropertyEditorUI's.
16  */

17 public class PropertyEditorFactory {
18   // the property that defines the different editor classes for the
19
// registry.
20
public static String JavaDoc SOURCE_PROPERTY = "PropertyEditor";
21
22   // the VariableBundle that holds both the properties and the editor
23
// definitions.
24
VariableBundle sourceBundle;
25
26   // the IconManager used for PropertyEditors that use icons.
27
IconManager iconManager;
28
29   // the HelpBroker
30
HelpBroker helpBroker;
31
32   // the propertyType to className mapping
33
Map typeToClassMap = new HashMap();
34
35   /**
36    * Creates a PropertyEditorFactory using the given VariableBundle as
37    * a source.
38    */

39   public PropertyEditorFactory(VariableBundle bundle, IconManager manager, HelpBroker broker) {
40     sourceBundle = bundle;
41     iconManager = manager;
42     helpBroker = broker;
43     createTypeToClassMap();
44   }
45
46   /**
47    * Creates the typeToClassMap.
48    */

49   private void createTypeToClassMap() {
50
51     try {
52       Class JavaDoc parentClass = Class.forName("net.suberic.util.gui.propedit.SwingPropertyEditor");
53
54       Vector propertyTypes = sourceBundle.getPropertyAsVector(SOURCE_PROPERTY, "");
55       for (int i = 0; i < propertyTypes.size(); i++) {
56         String JavaDoc currentType = (String JavaDoc) propertyTypes.get(i);
57         String JavaDoc className = sourceBundle.getProperty(SOURCE_PROPERTY + "." + currentType + ".class", "");
58         try {
59           Class JavaDoc currentClass = Class.forName(className);
60           if (parentClass.isAssignableFrom(currentClass)) {
61             typeToClassMap.put(currentType, currentClass);
62           }
63         } catch (Exception JavaDoc e) {
64           System.out.println("error registering class for property type " + currentType + ": " + e);
65         }
66       }
67     } catch (Exception JavaDoc e) {
68       System.out.println("caught exception initializing PropertyEditorFactory: " + e);
69       e.printStackTrace();
70     }
71   }
72
73   /**
74    * Shows an error message.
75    */

76   public void showError(Object JavaDoc component, String JavaDoc errorMessage) {
77     JOptionPane.showMessageDialog((Component JavaDoc) component, errorMessage);
78   }
79
80   /**
81    * Shows an input dialog.
82    */

83   public String JavaDoc showInputDialog(SwingPropertyEditor dpe, String JavaDoc query) {
84     return JOptionPane.showInputDialog(dpe, query);
85   }
86
87   /**
88    * Creates and displays an editor window.
89    */

90   public void showNewEditorWindow(String JavaDoc title, String JavaDoc property) {
91     showNewEditorWindow(title, property, property);
92   }
93
94   /**
95    * Creates and displays an editor window.
96    */

97   public void showNewEditorWindow(String JavaDoc title, String JavaDoc property, String JavaDoc template) {
98     showNewEditorWindow(title, property, template, new PropertyEditorManager(sourceBundle, this, iconManager));
99   }
100
101   /**
102    * Creates and displays an editor window.
103    */

104   public void showNewEditorWindow(String JavaDoc title, String JavaDoc property, String JavaDoc template, PropertyEditorManager mgr) {
105     showNewEditorWindow(title, property, template, mgr, null);
106   }
107
108   /**
109    * Creates and displays an editor window.
110    */

111   public void showNewEditorWindow(String JavaDoc title, String JavaDoc property, String JavaDoc template, PropertyEditorManager mgr, Container JavaDoc window) {
112     showNewEditorWindow(title, property, template, property, mgr, window);
113   }
114
115   public void showNewEditorWindow(String JavaDoc title, String JavaDoc property, String JavaDoc template, String JavaDoc propertyBase, PropertyEditorManager mgr, Container JavaDoc window) {
116     showNewEditorWindow(title, createEditor(property, template, propertyBase, mgr), window);
117   }
118
119   /**
120    * Creates and displays an editor window.
121    */

122   public void showNewEditorWindow(String JavaDoc title, PropertyEditorUI editor) {
123     showNewEditorWindow(title, editor, null);
124   }
125   /**
126    * Creates and displays an editor window.
127    */

128   public void showNewEditorWindow(String JavaDoc title, PropertyEditorUI editor, Container JavaDoc window) {
129     JDialog jd = (JDialog) createEditorWindow(title, editor, window);
130     if (window != null) {
131       Point JavaDoc location = window.getLocationOnScreen();
132       Dimension JavaDoc windowSize = window.getSize();
133       Dimension JavaDoc editorWindowSize = jd.getSize();
134       int yValue = ((windowSize.height - editorWindowSize.height) / 2) + location.y;
135       int xValue = ((windowSize.width - editorWindowSize.width) / 2) + location.x;
136       jd.setLocation(new Point JavaDoc(xValue, yValue));
137     }
138     jd.setVisible(true);
139   }
140
141   /**
142    * This method returns an EditorWindow (a JDialog in this
143    * implementation) which has an editor for each property in the
144    * property List. The title string is the title of the
145    * JInternalFrame.
146    */

147   public Container JavaDoc createEditorWindow(String JavaDoc title, String JavaDoc property) {
148     return createEditorWindow(title, property, property, new PropertyEditorManager(sourceBundle, this, iconManager));
149   }
150
151   /**
152    * This method returns an EditorWindow (a JDialog in this
153    * implementation) which has an editor for each property in the
154    * property List. The title string is the title of the
155    * JDialog.
156    */

157   public Container JavaDoc createEditorWindow(String JavaDoc title, String JavaDoc property, String JavaDoc template ) {
158     return createEditorWindow(title, property, template, new PropertyEditorManager(sourceBundle, this, iconManager));
159   }
160
161   /**
162    * This method returns an EditorWindow (a JDialog in this
163    * implementation) which has an editor for each property in the
164    * property List. The title string is the title of the
165    * JDialog.
166    */

167   public Container JavaDoc createEditorWindow(String JavaDoc title, String JavaDoc property, String JavaDoc template, Container JavaDoc window ) {
168     return createEditorWindow(title, property, template, new PropertyEditorManager(sourceBundle, this, iconManager), window);
169   }
170
171   /**
172    * This method returns an EditorWindow (a JDialog in this
173    * implementation) which has an editor for each property in the
174    * property Vector. The title string is the title of the
175    * JInternalFrame.
176    */

177   public Container JavaDoc createEditorWindow(String JavaDoc title, String JavaDoc property, String JavaDoc template, PropertyEditorManager mgr) {
178     return createEditorWindow(title, property, template, mgr, null);
179   }
180   /**
181    * This method returns an EditorWindow (a JDialog in this
182    * implementation) which has an editor for each property in the
183    * property Vector. The title string is the title of the
184    * JInternalFrame.
185    */

186   public Container JavaDoc createEditorWindow(String JavaDoc title, String JavaDoc property, String JavaDoc template, PropertyEditorManager mgr, Container JavaDoc window) {
187     return createEditorWindow(title, property, template, property, mgr, window);
188   }
189
190   public Container JavaDoc createEditorWindow(String JavaDoc title, String JavaDoc property, String JavaDoc template, String JavaDoc propertyBase, PropertyEditorManager mgr, Container JavaDoc window) {
191     return createEditorWindow(title, createEditor(property, template, propertyBase, mgr), window);
192   }
193
194   public Container JavaDoc createEditorWindow(String JavaDoc title, PropertyEditorUI editor, Container JavaDoc window) {
195     JDialog jd = null;
196     if (window instanceof Dialog JavaDoc) {
197       jd = new JDialog((Dialog JavaDoc) window, title, Dialog.ModalityType.APPLICATION_MODAL);
198     } else if (window instanceof Frame JavaDoc) {
199       jd = new JDialog((Frame JavaDoc) window, title, Dialog.ModalityType.APPLICATION_MODAL);
200     } else {
201       jd = new JDialog();
202       jd.setTitle(title);
203       jd.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
204     }
205     PropertyEditorPane pep = createPropertyEditorPane(editor.getManager(), (SwingPropertyEditor) editor, jd);
206     jd.getContentPane().add(pep);
207     jd.getRootPane().setDefaultButton(pep.getDefaultButton());
208     jd.pack();
209     return jd;
210   }
211
212
213   /**
214    * Creates an appropriate PropertyEditorUI for the given property and
215    * editorTemplate, using the given PropertyEditorManager.
216    */

217   public PropertyEditorUI createEditor(String JavaDoc property, String JavaDoc editorTemplate, PropertyEditorManager mgr) {
218
219     return createEditor(property, editorTemplate, editorTemplate, mgr);
220   }
221
222   /**
223    * Creates an appropriate PropertyEditorUI for the given property and
224    * editorTemplate, using the given PropertyEditorManager.
225    */

226   public PropertyEditorUI createEditor(String JavaDoc property, String JavaDoc editorTemplate, String JavaDoc propertyBase, PropertyEditorManager mgr) {
227     String JavaDoc type = sourceBundle.getProperty(editorTemplate + ".propertyType", "");
228     return createEditor(property, editorTemplate, propertyBase, type, mgr);
229   }
230   /**
231    * Creates an appropriate PropertyEditorUI for the given property and
232    * editorTemplate, using the given PropertyEditorManager.
233    */

234   public PropertyEditorUI createEditor(String JavaDoc property, String JavaDoc editorTemplate, String JavaDoc propertyBase, String JavaDoc type, PropertyEditorManager mgr) {
235
236     //System.err.println("creating editor for property '" + property + "', template '" + editorTemplate + "', propertyBase '" + propertyBase + "', type '" + type + "'");
237
Class JavaDoc editorClass = (Class JavaDoc) typeToClassMap.get(type);
238     if (editorClass == null) {
239       editorClass = (Class JavaDoc) typeToClassMap.get("String");
240     }
241
242     PropertyEditorUI returnValue = null;
243     try {
244       returnValue = (PropertyEditorUI) editorClass.newInstance();
245     } catch (Exception JavaDoc e) {
246       System.err.println("error creating editor for property " + property + ": " + e);
247       returnValue = new StringEditorPane();
248     }
249     returnValue.configureEditor(property, editorTemplate, propertyBase, mgr);
250     return returnValue;
251   }
252
253   /**
254    * Creates the PropertyEditoPane for this editor.
255    */

256   public PropertyEditorPane createPropertyEditorPane(PropertyEditorManager manager, SwingPropertyEditor editor, Container JavaDoc container) {
257     boolean commit = ! editor.getManager().createdEditorPane;
258     String JavaDoc template = editor.getEditorTemplate();
259     PropertyEditorPane returnValue = null;
260     if (manager.getProperty(template + ".editorType", "").equalsIgnoreCase("wizard")) {
261       returnValue = new WizardPropertyEditor(manager, editor, container, commit);
262     } else {
263       returnValue = new PropertyEditorPane(manager, editor, container, commit);
264     }
265     manager.createdEditorPane = true;
266     return returnValue;
267   }
268
269
270   /**
271    * Gets the source bundle for this factory.
272    */

273   public VariableBundle getSourceBundle() {
274     return sourceBundle;
275   }
276
277   /**
278    * Gets the IconManager for this factory.
279    */

280   public IconManager getIconManager() {
281     return iconManager;
282   }
283
284   /**
285    * Returns the HelpBroker for this PropertyEditorManager.
286    */

287   public HelpBroker getHelpBroker() {
288     return helpBroker;
289   }
290 }
291
292
293
294
295
296
297
298
Popular Tags