1 19 20 package org.openide.explorer.propertysheet; 21 22 import org.openide.explorer.propertysheet.editors.EnhancedCustomPropertyEditor; 23 import org.openide.nodes.Node.*; 24 import org.openide.util.NbBundle; 25 26 import java.awt.Component ; 27 import java.awt.Container ; 28 import java.awt.event.*; 29 30 import java.beans.PropertyChangeEvent ; 31 import java.beans.PropertyChangeListener ; 32 import java.beans.PropertyEditor ; 33 import java.beans.PropertyVetoException ; 34 import java.beans.VetoableChangeListener ; 35 36 import java.lang.reflect.InvocationTargetException ; 37 38 import java.text.MessageFormat ; 39 import java.util.logging.Level ; 40 import java.util.logging.Logger ; 41 42 import javax.swing.*; 43 import javax.swing.event.*; 44 import org.netbeans.modules.openide.explorer.UIException; 45 46 51 final class CustomEditorDisplayer implements PropertyDisplayer_Editable { 52 private int updatePolicy = UPDATE_ON_CONFIRMATION; 53 private Property prop; 54 private PropertyEnv env = null; 55 private PropertyEditor editor = null; 56 private Component customEditor = null; 57 boolean ignoreChanges = false; 58 private PropertyChangeListener editorListener = null; 59 private EnvListener envListener = null; 60 private PropertyModel model = null; 61 private Object originalValue = null; 62 63 66 private javax.swing.event.EventListenerList listenerList = null; 67 private boolean ignoreChanges2 = false; 68 69 private PropertyChangeListener remoteEnvListener = null; 71 private VetoableChangeListener remotevEnvListener = null; 72 73 74 public CustomEditorDisplayer(Property prop) { 75 this.prop = prop; 76 } 77 78 public CustomEditorDisplayer(Property prop, PropertyModel mdl) { 79 this(prop); 80 model = mdl; 81 } 82 83 public void setUpdatePolicy(int i) { 84 this.updatePolicy = i; 85 86 if (env != null) { 87 env.setChangeImmediate(i != UPDATE_ON_EXPLICIT_REQUEST); 88 } 89 } 90 91 private Component getCustomEditor() { 92 if (customEditor == null) { 93 customEditor = getPropertyEditor().getCustomEditor(); 94 } 95 96 return customEditor; 97 } 98 99 PropertyEditor getPropertyEditor() { 101 if (editor == null) { 102 setPropertyEditor(PropUtils.getPropertyEditor(getProperty())); 103 } 104 105 return editor; 106 } 107 108 private void setPropertyEditor(PropertyEditor editor) { 109 if (this.editor != null) { 110 detachFromPropertyEditor(this.editor); 111 112 ignoreChanges = true; 115 } 116 117 this.editor = editor; 118 119 try { 120 if (editor != null) { 121 if (!editor.supportsCustomEditor()) { 122 throw new IllegalArgumentException ( 123 "Property editor " + editor + " for property " + getProperty() + 124 " does not support a custom editor." 125 ); } 127 128 try { 129 originalValue = editor.getValue(); 130 } catch (Exception e) { 131 } 133 134 PropertyEnv env = new PropertyEnv(); 138 139 env.setFeatureDescriptor(EditorPropertyDisplayer.findFeatureDescriptor(this)); 142 setPropertyEnv(env); 143 144 if (editor instanceof ExPropertyEditor) { 145 ((ExPropertyEditor) editor).attachEnv(env); 146 } 147 148 attachToPropertyEditor(editor); 149 } 150 } finally { 151 ignoreChanges = false; 152 } 153 } 154 155 private void setPropertyEnv(PropertyEnv env) { 156 if (this.env != null) { 157 detachFromEnv(this.env); 158 } 159 160 this.env = env; 161 162 if (env != null) { 163 env.setChangeImmediate(getUpdatePolicy() != UPDATE_ON_EXPLICIT_REQUEST); 164 attachToEnv(env); 165 } 166 } 167 168 private void attachToEnv(PropertyEnv env) { 169 env.addPropertyChangeListener(getEnvListener()); 170 env.addVetoableChangeListener(getEnvListener()); 171 env.setBeans(EditorPropertyDisplayer.findBeans(this)); 172 } 173 174 private void detachFromEnv(PropertyEnv env) { 175 env.removePropertyChangeListener(getEnvListener()); 176 env.removeVetoableChangeListener(getEnvListener()); 177 } 178 179 private void attachToPropertyEditor(PropertyEditor editor) { 180 editor.addPropertyChangeListener(getEditorListener()); 182 } 183 184 private void detachFromPropertyEditor(PropertyEditor editor) { 185 editor.removePropertyChangeListener(getEditorListener()); 186 } 187 188 private PropertyChangeListener getEditorListener() { 189 if (editorListener == null) { 190 editorListener = new EditorListener(); 191 } 192 193 return editorListener; 194 } 195 196 private EnvListener getEnvListener() { 197 if (envListener == null) { 198 envListener = new EnvListener(); 199 } 200 201 return envListener; 202 } 203 204 public boolean commit() throws IllegalArgumentException { 205 try { 207 ignoreChanges = true; 208 209 PropertyEditor editor = getPropertyEditor(); 210 Object entered = getEnteredValue(); 211 212 try { 214 if ((entered != null) && entered.equals(getProperty().getValue())) { 215 return false; 217 } 218 } catch (Exception e) { 219 Logger.getLogger(CustomEditorDisplayer.class.getName()).log(Level.WARNING, null, e); 222 223 try { 224 if (getProperty().canRead()) { 225 editor.setValue(model.getValue()); 226 } 227 } catch (ProxyNode.DifferentValuesException dve) { 228 } catch (Exception ex) { 230 PropertyDialogManager.notify(ex); 231 } 232 233 return false; 234 } 235 236 PropertyEnv env = getPropertyEnv(); 237 238 Exception exception = PropUtils.updatePropertyEditor(editor, entered); 239 240 if (exception == null) { 241 if ((env != null) && PropertyEnv.STATE_NEEDS_VALIDATION.equals(env.getState())) { 242 String msg = env.silentlySetState(env.STATE_VALID, entered); 243 244 System.err.println(" result of silent set state: " + msg); 245 246 if ((msg != null) && !PropertyEnv.STATE_VALID.equals(env.getState())) { 248 IllegalArgumentException iae = new IllegalArgumentException ("Error setting value"); 250 UIException.annotateUser(iae, null, msg, null, null); 251 252 if (!env.STATE_INVALID.equals(env.getState())) { 254 env.silentlySetState(env.STATE_INVALID, null); 255 } 256 257 throw iae; 258 } 259 } 260 } 261 262 Object res = Boolean.FALSE; 263 264 if (exception == null) { 265 res = PropUtils.noDlgUpdateProp(getModel(), editor); 266 originalValue = editor.getValue(); 267 268 if (res instanceof Exception && (!(res instanceof ProxyNode.DifferentValuesException))) { 269 exception = (Exception ) res; 270 } 271 272 if (res instanceof InvocationTargetException || res instanceof IllegalAccessException ) { 273 PropertyDialogManager.notify((Exception ) res); 274 } 275 } 276 277 if (exception != null) { 278 if (exception instanceof IllegalArgumentException ) { 279 throw (IllegalArgumentException ) exception; 280 } else { 281 PropertyDialogManager.notify(exception); 282 283 IllegalArgumentException iae = new IllegalArgumentException ("Error setting value"); UIException.annotateUser(iae, null, 285 PropUtils.findLocalizedMessage(exception, 286 entered, 287 getProperty().getDisplayName()), 288 exception, null); 289 throw iae; 290 } 291 } 292 293 boolean result = Boolean.TRUE.equals(res); 294 295 if (result) { 296 fireActionPerformed(); 297 } 298 299 return result; 300 } finally { 301 ignoreChanges = false; 302 } 303 } 304 305 PropertyModel getModel() { 306 if (model == null) { 307 return new NodePropertyModel(getProperty(), null); 308 } else { 309 return model; 310 } 311 } 312 313 void setModel(PropertyModel mdl) { 314 model = mdl; 315 } 316 317 public PropertyEnv getPropertyEnv() { 318 return env; 319 } 320 321 public Component getComponent() { 322 return getCustomEditor(); 323 } 324 325 public Object getEnteredValue() { 326 PropertyEditor editor = getPropertyEditor(); 327 Object result; 328 329 if (customEditor instanceof EnhancedCustomPropertyEditor) { 330 result = ((EnhancedCustomPropertyEditor) customEditor ).getPropertyValue(); 331 } else { 332 result = editor.getValue(); } 334 335 return result; 336 } 337 338 public Property getProperty() { 339 return prop; 340 } 341 342 public int getUpdatePolicy() { 343 return updatePolicy; 344 } 345 346 public String isModifiedValueLegal() { 347 boolean legal = true; 348 String msg = null; 349 PropertyEditor editor = getPropertyEditor(); 350 351 if (env != null) { 353 legal = env.getState() != env.STATE_INVALID; 354 355 System.err.println(" Attempting to validate env"); 356 357 if (legal && env.STATE_NEEDS_VALIDATION.equals(env.getState())) { 358 msg = env.silentlySetState(env.STATE_VALID, getEnteredValue()); 359 360 legal = msg == null; 362 } 363 } else if (editor instanceof EnhancedCustomPropertyEditor) { 364 Object entered = ((EnhancedCustomPropertyEditor) editor).getPropertyValue(); 365 366 try { 367 editor.setValue(entered); 368 } catch (IllegalStateException ise) { 369 legal = false; 370 msg = PropUtils.findLocalizedMessage(ise, entered, getProperty().getDisplayName()); 371 } 372 } 373 374 if (!legal && (msg == null)) { 375 msg = MessageFormat.format( 377 NbBundle.getMessage(CustomEditorDisplayer.class, "FMT_CannotUpdateProperty"), 378 new Object [] { editor.getValue(), getProperty().getDisplayName() } 379 ); } 381 382 return msg; 383 } 384 385 public boolean isValueModified() { 386 PropertyEditor editor = getPropertyEditor(); 387 boolean result = editor.getValue() != originalValue; 388 389 if (!result && editor instanceof EnhancedCustomPropertyEditor) { 390 Object entered = ((EnhancedCustomPropertyEditor) editor).getPropertyValue(); 391 392 if (entered != null) { 393 result = entered.equals(originalValue); 394 } else { 395 result = originalValue == null; 396 } 397 } 398 399 return result; 400 } 401 402 public void refresh() { 403 } 405 406 public void reset() { 407 try { 408 originalValue = getProperty().getValue(); 409 getPropertyEditor().setValue(originalValue); 410 } catch (Exception e) { 411 Logger.getLogger(CustomEditorDisplayer.class.getName()).log(Level.WARNING, null, e); 413 } 414 } 415 416 421 public void setEnabled(boolean enabled) { 422 Component custEditor = (Container ) getComponent(); 424 425 if (custEditor instanceof Container ) { 426 setEnabled((Container ) custEditor, enabled); 427 } 428 429 custEditor.setEnabled(enabled); 430 } 431 432 public void setEnabled(Container c, boolean enabled) { 433 Component [] comp = c.getComponents(); 434 435 for (int i = 0; i < comp.length; i++) { 436 if (!(comp[i] instanceof JScrollBar)) { 437 comp[i].setEnabled(false); 438 } else { 439 ((JScrollBar) comp[i]).setFocusable(enabled); 440 } 441 442 if (comp[i] instanceof Container ) { 443 boolean ignore = false; 444 445 if (comp[i] instanceof JComponent) { 446 Boolean val = (Boolean ) ((JComponent) comp[i]).getClientProperty("dontEnableMe"); 452 if (val != null) { 453 ignore = val.booleanValue(); 454 } 455 } 456 457 if (!ignore) { 458 setEnabled((Container ) comp[i], enabled); 459 } 460 } 461 } 462 463 c.setEnabled(enabled); 464 } 465 466 public void setEnteredValue(Object o) { 467 PropUtils.updatePropertyEditor(getPropertyEditor(), o); 468 } 469 470 public void setActionCommand(String val) { 471 } 472 473 public String getActionCommand() { 474 return null; 475 } 476 477 481 public synchronized void addActionListener(java.awt.event.ActionListener listener) { 482 if (listenerList == null) { 483 listenerList = new EventListenerList(); 484 } 485 486 listenerList.add(ActionListener.class, listener); 487 } 488 489 493 public synchronized void removeActionListener(java.awt.event.ActionListener listener) { 494 listenerList.remove(ActionListener.class, listener); 495 } 496 497 502 private void fireActionPerformed() { 503 ActionEvent event = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "userChangedValue"); 505 if (listenerList == null) { 506 return; 507 } 508 509 Object [] listeners = listenerList.getListenerList(); 510 511 for (int i = listeners.length - 2; i >= 0; i -= 2) { 512 if (listeners[i] == ActionListener.class) { 513 ((ActionListener) listeners[i + 1]).actionPerformed(event); 514 } 515 } 516 } 517 518 522 public synchronized void addChangeListener(ChangeListener listener) { 523 if (listenerList == null) { 524 listenerList = new EventListenerList(); 525 } 526 527 listenerList.add(ChangeListener .class, listener); 528 } 529 530 534 public synchronized void removeChangeListener(ChangeListener listener) { 535 listenerList.remove(ChangeListener .class, listener); 536 } 537 538 543 private void fireStateChanged() { 544 ChangeEvent event = new ChangeEvent (this); 545 546 if (listenerList == null) { 547 return; 548 } 549 550 Object [] listeners = listenerList.getListenerList(); 551 552 for (int i = listeners.length - 2; i >= 0; i -= 2) { 553 if (listeners[i] == ChangeListener .class) { 554 ((ChangeListener ) listeners[i + 1]).stateChanged(event); 555 } 556 } 557 } 558 559 void setRemoteEnvListener(PropertyChangeListener l) { 560 remoteEnvListener = l; 562 } 563 564 void setRemoteEnvVetoListener(VetoableChangeListener vl) { 565 remotevEnvListener = vl; 566 } 567 568 private class EnvListener implements PropertyChangeListener , VetoableChangeListener { 569 public void propertyChange(PropertyChangeEvent evt) { 570 fireStateChanged(); 571 572 if (remoteEnvListener != null) { 575 remoteEnvListener.propertyChange(evt); 576 577 } 580 } 581 582 public void vetoableChange(PropertyChangeEvent evt) 583 throws PropertyVetoException { 584 if (remotevEnvListener != null) { 585 remotevEnvListener.vetoableChange(evt); 586 } 587 } 588 } 589 590 private class EditorListener implements PropertyChangeListener { 591 public void propertyChange(PropertyChangeEvent evt) { 592 if (ignoreChanges) { 594 return; 596 } 597 598 if (ExPropertyEditor.PROP_VALUE_VALID.equals(evt.getPropertyName())) { 599 return; 601 } 602 603 if (ignoreChanges2) { 604 return; 605 } 606 607 ignoreChanges2 = true; 608 609 if (getUpdatePolicy() != UPDATE_ON_EXPLICIT_REQUEST) { 610 commit(); 611 612 } 615 616 fireStateChanged(); 617 ignoreChanges2 = false; 618 } 619 } 620 621 } 622 | Popular Tags |