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 ; 7 import java.awt.Component ; 8 import java.awt.Frame ; 9 import java.awt.Dialog ; 10 import java.awt.Dimension ; 11 import java.awt.Point ; 12 import javax.help.HelpBroker; 13 14 17 public class PropertyEditorFactory { 18 public static String SOURCE_PROPERTY = "PropertyEditor"; 21 22 VariableBundle sourceBundle; 25 26 IconManager iconManager; 28 29 HelpBroker helpBroker; 31 32 Map typeToClassMap = new HashMap(); 34 35 39 public PropertyEditorFactory(VariableBundle bundle, IconManager manager, HelpBroker broker) { 40 sourceBundle = bundle; 41 iconManager = manager; 42 helpBroker = broker; 43 createTypeToClassMap(); 44 } 45 46 49 private void createTypeToClassMap() { 50 51 try { 52 Class 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 currentType = (String ) propertyTypes.get(i); 57 String className = sourceBundle.getProperty(SOURCE_PROPERTY + "." + currentType + ".class", ""); 58 try { 59 Class currentClass = Class.forName(className); 60 if (parentClass.isAssignableFrom(currentClass)) { 61 typeToClassMap.put(currentType, currentClass); 62 } 63 } catch (Exception e) { 64 System.out.println("error registering class for property type " + currentType + ": " + e); 65 } 66 } 67 } catch (Exception e) { 68 System.out.println("caught exception initializing PropertyEditorFactory: " + e); 69 e.printStackTrace(); 70 } 71 } 72 73 76 public void showError(Object component, String errorMessage) { 77 JOptionPane.showMessageDialog((Component ) component, errorMessage); 78 } 79 80 83 public String showInputDialog(SwingPropertyEditor dpe, String query) { 84 return JOptionPane.showInputDialog(dpe, query); 85 } 86 87 90 public void showNewEditorWindow(String title, String property) { 91 showNewEditorWindow(title, property, property); 92 } 93 94 97 public void showNewEditorWindow(String title, String property, String template) { 98 showNewEditorWindow(title, property, template, new PropertyEditorManager(sourceBundle, this, iconManager)); 99 } 100 101 104 public void showNewEditorWindow(String title, String property, String template, PropertyEditorManager mgr) { 105 showNewEditorWindow(title, property, template, mgr, null); 106 } 107 108 111 public void showNewEditorWindow(String title, String property, String template, PropertyEditorManager mgr, Container window) { 112 showNewEditorWindow(title, property, template, property, mgr, window); 113 } 114 115 public void showNewEditorWindow(String title, String property, String template, String propertyBase, PropertyEditorManager mgr, Container window) { 116 showNewEditorWindow(title, createEditor(property, template, propertyBase, mgr), window); 117 } 118 119 122 public void showNewEditorWindow(String title, PropertyEditorUI editor) { 123 showNewEditorWindow(title, editor, null); 124 } 125 128 public void showNewEditorWindow(String title, PropertyEditorUI editor, Container window) { 129 JDialog jd = (JDialog) createEditorWindow(title, editor, window); 130 if (window != null) { 131 Point location = window.getLocationOnScreen(); 132 Dimension windowSize = window.getSize(); 133 Dimension 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 (xValue, yValue)); 137 } 138 jd.setVisible(true); 139 } 140 141 147 public Container createEditorWindow(String title, String property) { 148 return createEditorWindow(title, property, property, new PropertyEditorManager(sourceBundle, this, iconManager)); 149 } 150 151 157 public Container createEditorWindow(String title, String property, String template ) { 158 return createEditorWindow(title, property, template, new PropertyEditorManager(sourceBundle, this, iconManager)); 159 } 160 161 167 public Container createEditorWindow(String title, String property, String template, Container window ) { 168 return createEditorWindow(title, property, template, new PropertyEditorManager(sourceBundle, this, iconManager), window); 169 } 170 171 177 public Container createEditorWindow(String title, String property, String template, PropertyEditorManager mgr) { 178 return createEditorWindow(title, property, template, mgr, null); 179 } 180 186 public Container createEditorWindow(String title, String property, String template, PropertyEditorManager mgr, Container window) { 187 return createEditorWindow(title, property, template, property, mgr, window); 188 } 189 190 public Container createEditorWindow(String title, String property, String template, String propertyBase, PropertyEditorManager mgr, Container window) { 191 return createEditorWindow(title, createEditor(property, template, propertyBase, mgr), window); 192 } 193 194 public Container createEditorWindow(String title, PropertyEditorUI editor, Container window) { 195 JDialog jd = null; 196 if (window instanceof Dialog ) { 197 jd = new JDialog((Dialog ) window, title, Dialog.ModalityType.APPLICATION_MODAL); 198 } else if (window instanceof Frame ) { 199 jd = new JDialog((Frame ) 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 217 public PropertyEditorUI createEditor(String property, String editorTemplate, PropertyEditorManager mgr) { 218 219 return createEditor(property, editorTemplate, editorTemplate, mgr); 220 } 221 222 226 public PropertyEditorUI createEditor(String property, String editorTemplate, String propertyBase, PropertyEditorManager mgr) { 227 String type = sourceBundle.getProperty(editorTemplate + ".propertyType", ""); 228 return createEditor(property, editorTemplate, propertyBase, type, mgr); 229 } 230 234 public PropertyEditorUI createEditor(String property, String editorTemplate, String propertyBase, String type, PropertyEditorManager mgr) { 235 236 Class editorClass = (Class ) typeToClassMap.get(type); 238 if (editorClass == null) { 239 editorClass = (Class ) typeToClassMap.get("String"); 240 } 241 242 PropertyEditorUI returnValue = null; 243 try { 244 returnValue = (PropertyEditorUI) editorClass.newInstance(); 245 } catch (Exception 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 256 public PropertyEditorPane createPropertyEditorPane(PropertyEditorManager manager, SwingPropertyEditor editor, Container container) { 257 boolean commit = ! editor.getManager().createdEditorPane; 258 String 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 273 public VariableBundle getSourceBundle() { 274 return sourceBundle; 275 } 276 277 280 public IconManager getIconManager() { 281 return iconManager; 282 } 283 284 287 public HelpBroker getHelpBroker() { 288 return helpBroker; 289 } 290 } 291 292 293 294 295 296 297 298 | Popular Tags |