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 10 public class PropertyEditorManager { 11 12 protected HashMap<String ,PropertyEditorUI> editorMap = new HashMap<String ,PropertyEditorUI>(); 13 14 protected VariableBundle sourceBundle; 15 16 protected PropertyEditorFactory propertyFactory; 17 18 protected HashMap<String ,List<PropertyEditorListener>> listenerMap = new HashMap<String ,List<PropertyEditorListener>>(); 19 20 protected boolean writeChanges = true; 21 22 protected Properties localProps = new Properties(); 23 protected Properties tempProps = new Properties(); 24 25 27 protected IconManager iconManager; 28 29 public boolean createdEditorPane = false; 31 32 35 protected PropertyEditorManager() { 36 } 37 38 42 public PropertyEditorManager(VariableBundle vb, PropertyEditorFactory factory, IconManager manager) { 43 sourceBundle = vb; 44 propertyFactory = factory; 45 iconManager = manager; 46 } 47 48 51 public PropertyEditorUI getPropertyEditor(String propertyName) { 52 return (PropertyEditorUI) editorMap.get(propertyName); 53 } 54 55 59 public void registerPropertyEditor(String property, PropertyEditorUI editor) { 60 editorMap.put(property, editor); 61 } 62 63 66 public PropertyEditorFactory getFactory() { 67 return propertyFactory; 68 } 69 70 73 public IconManager getIconManager() { 74 return iconManager; 75 } 76 77 80 public String getProperty(String property, String defaultValue) { 81 String tmpValue = (String ) tempProps.get(property); 83 if (tmpValue != null) 84 return tmpValue; 85 tmpValue = (String ) localProps.get(property); 87 if (tmpValue != null) 88 return tmpValue; 89 return sourceBundle.getProperty(property, defaultValue); 90 } 91 92 95 public String getCurrentProperty(String property, String defaultValue) { 96 String tmpValue = null; 97 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 tmpValue = (String ) tempProps.get(property); 107 if (tmpValue != null) 108 return tmpValue; 109 tmpValue = (String ) localProps.get(property); 111 if (tmpValue != null) 112 return tmpValue; 113 return sourceBundle.getProperty(property, defaultValue); 114 } 115 116 119 public List<String > getPropertyAsList(String property, String defaultValue) { 120 String tmpValue = (String ) tempProps.get(property); 122 if (tmpValue != null) { 123 return VariableBundle.convertToVector(tmpValue); 124 } 125 tmpValue = (String ) localProps.get(property); 127 if (tmpValue != null) { 128 return VariableBundle.convertToVector(tmpValue); 129 } 130 return sourceBundle.getPropertyAsList(property, defaultValue); 131 } 132 133 136 public Set<String > getPropertyNamesStartingWith(String startsWith) { 137 Set<String > returnValue = new HashSet<String >(); 138 Set<String > tProps = tempProps.stringPropertyNames(); 140 for (String prop: tProps) { 141 if (prop.startsWith(startsWith)) 142 returnValue.add(prop); 143 } 144 Set<String > lProps = localProps.stringPropertyNames(); 146 for (String prop: lProps) { 147 if (prop.startsWith(startsWith)) 148 returnValue.add(prop); 149 } 150 returnValue.addAll(sourceBundle.getPropertyNamesStartingWith(startsWith)); 151 return returnValue; 152 } 153 154 157 public void setProperty(String property, String value) { 158 tempProps.remove(property); 159 localProps.setProperty(property, value); 160 167 } 168 169 172 public void setTemporaryProperty(String property, String value) { 173 tempProps.setProperty(property, value); 174 } 175 176 179 public void removeProperty(String property) { 180 setProperty(property, ""); 181 } 184 185 189 public PropertyEditorUI createEditor(String property, String editorTemplate, String propertyBase) { 190 return getFactory().createEditor(property, editorTemplate, propertyBase, this); 191 } 192 193 198 public String formatMessage(String key, Object ... arguments) { 199 return sourceBundle.formatMessage(key, arguments); 200 } 201 202 205 public void commit() { 206 if (writeChanges) { 207 212 213 sourceBundle.setAllProperties(localProps); 214 215 sourceBundle.saveProperties(); 216 217 clearValues(); 218 } 219 } 220 221 224 public void clearValues() { 225 localProps = new Properties(); 226 tempProps = new Properties(); 227 } 229 230 234 public PropertyEditorListener createListener(String key, String property, String propertyBase, String editorTemplate) { 235 try { 236 Class 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 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 252 public void setWriteChanges(boolean newValue) { 253 writeChanges = newValue; 254 } 255 256 259 public void addPropertyEditorListener(String 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 274 public void removePropertyEditorListener(String 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 288 public void removePropertyEditorListeners(String property) { 289 if (property != null) { 290 listenerMap.remove(property); 291 } 292 } 293 294 299 public void firePropertyChangingEvent(PropertyEditorUI propertyEditor, String newValue) throws PropertyValueVetoException { 300 String 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 312 public void firePropertyChangedEvent(PropertyEditorUI propertyEditor, String newValue) { 313 String 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 325 public void firePropertyCommittingEvent(PropertyEditorUI propertyEditor, String newValue) throws PropertyValueVetoException { 326 String 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 338 public void firePropertyInitializedEvent(PropertyEditorUI propertyEditor, String newValue) { 339 String 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 |