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.*; 27 import java.awt.event.ActionEvent ; 28 import java.awt.event.WindowAdapter ; 29 import java.awt.event.WindowEvent ; 30 import java.awt.event.WindowListener ; 31 32 import java.beans.*; 33 34 import java.lang.ref.WeakReference ; 35 36 import javax.swing.AbstractAction ; 37 import javax.swing.JDialog ; 38 import org.openide.util.Exceptions; 39 40 41 45 class CustomEditorAction extends AbstractAction { 46 private Invoker invoker; 47 private WeakReference <PropertyModel> modelRef = null; 48 49 50 public CustomEditorAction(Invoker invoker) { 51 this.invoker = invoker; 52 putValue(SMALL_ICON, PropUtils.getCustomButtonIcon()); 53 } 54 55 public CustomEditorAction(Invoker invoker, PropertyModel mdl) { 56 this(invoker); 57 58 if (mdl != null) { 59 modelRef = new WeakReference <PropertyModel>(mdl); 61 } 62 } 63 64 public void actionPerformed(ActionEvent ae) { 65 if (PropUtils.isLoggable(CustomEditorAction.class)) { 66 PropUtils.log(CustomEditorAction.class, "CustomEditorAction invoked " + ae); } 68 69 if (!invoker.allowInvoke()) { 70 if (PropUtils.isLoggable(CustomEditorAction.class)) { 71 PropUtils.log( 72 CustomEditorAction.class, 73 "Invoker (" + invoker.getClass() + " allowInvoke() returned false. Aborting." 74 ); } 76 77 return; 78 } 79 80 PropertyModel refd = (modelRef != null) ? modelRef.get() : null; 81 82 FeatureDescriptor fd = invoker.getSelection(); 84 85 final Property p = (fd instanceof Property) ? (Property) fd : null; 86 87 if (p == null) { 89 if (PropUtils.isLoggable(CustomEditorAction.class)) { 90 PropUtils.log( 91 CustomEditorAction.class, 92 "Cant invoke custom " + "editor on " + fd + " it is null or not a Property." + "Aborting." 93 ); } 95 96 Toolkit.getDefaultToolkit().beep(); 98 99 return; 100 } 101 102 final java.beans.PropertyEditor editor = PropUtils.getPropertyEditor(p); 103 104 PropertyEnv env = null; 106 107 if (editor instanceof ExPropertyEditor) { 108 if (PropUtils.isLoggable(CustomEditorAction.class)) { 109 PropUtils.log(CustomEditorAction.class, "Editor is an " + "ExPropertyEditor, attaching a PropertyEnv"); } 111 112 env = new PropertyEnv(); 113 env.setFeatureDescriptor(fd); 114 115 if (invoker instanceof SheetTable) { 116 if (PropUtils.isLoggable(CustomEditorAction.class)) { 117 PropUtils.log( 118 CustomEditorAction.class, "env.setBeans to " + invoker.getReusablePropertyEnv().getBeans() 119 ); } 121 122 env.setBeans(invoker.getReusablePropertyEnv().getBeans()); 123 } 124 125 ((ExPropertyEditor) editor).attachEnv(env); 127 } 128 129 if (!editor.supportsCustomEditor()) { 131 if (PropUtils.isLoggable(CustomEditorAction.class)) { 132 PropUtils.log( 133 CustomEditorAction.class, 134 "Cant invoke custom " + "editor for editor " + editor + " - it returns false " + 135 "from supportsCustomEditor()." 136 ); } 138 139 Toolkit.getDefaultToolkit().beep(); 141 142 return; 143 } 144 145 final Component curComp = invoker.getCursorChangeComponent(); 146 147 Cursor cur = Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR); 148 curComp.setCursor(cur); 149 try { 151 Object partialValue = invoker.getPartialValue(); 153 154 if (partialValue != null) { 158 try { 159 if ((editor.getValue() == null) ||!(partialValue.toString().equals(editor.getAsText()))) { 161 if (!(editor instanceof PropUtils.DifferentValuesEditor)) { 162 editor.setAsText(partialValue.toString()); 163 } 164 } 165 } catch (ProxyNode.DifferentValuesException dve) { 166 } catch (Exception ite) { 168 } 170 } 171 172 final PropertyModel mdl = (refd == null) ? new NodePropertyModel(p, null) : refd; 174 String fdName; 175 176 if ((mdl instanceof ExPropertyModel && (((ExPropertyModel) mdl).getFeatureDescriptor() != null))) { 177 fdName = ((ExPropertyModel) mdl).getFeatureDescriptor().getDisplayName(); 178 } else { 179 fdName = null; 180 } 181 182 String suppliedTitle = (String ) p.getValue("title"); final String title = (suppliedTitle == null) 185 ? ((fd.getDisplayName() == null) 186 ? NbBundle.getMessage(CustomEditorAction.class, "FMT_CUSTOM_DLG_NOPROPNAME_TITLE", 188 fdName == null ? invoker.getBeanName() : fdName 189 ) 190 : ((fd.getDisplayName().equals(invoker.getBeanName())) ? invoker.getBeanName() : 191 NbBundle.getMessage(CustomEditorAction.class, "FMT_CUSTOM_DLG_TITLE", 192 invoker.getBeanName(), fd.getDisplayName()) 193 )) : suppliedTitle; 195 final PropertyDialogManager pdm = new PropertyDialogManager( 196 NbBundle.getMessage( 197 CustomEditorAction.class, "PS_EditorTitle", (title == null) ? "" : title, p.getValueType() 200 ), true, editor, mdl, env 201 ); 202 203 boolean shouldListen = !(pdm.getComponent() instanceof EnhancedCustomPropertyEditor) && 204 (p.canWrite() && (invoker.wantAllChanges() || ((env == null) || env.isChangeImmediate()))); 205 206 final PropertyChangeListener pcl = (!shouldListen) ? null 207 : (new PropertyChangeListener() { 208 private boolean updating = false; 209 210 public void propertyChange(PropertyChangeEvent pce) { 211 if (updating) { 212 return; 213 } 214 215 updating = true; 216 217 try { 218 boolean success = PropUtils.updateProp(mdl, editor, title); 219 220 if (success) { 221 invoker.valueChanged(editor); 222 } else { 223 invoker.failed(); 224 } 225 } finally { 226 updating = false; 227 } 228 } 229 }); 230 231 if (pcl != null) { 232 editor.addPropertyChangeListener(pcl); 233 } 234 235 final java.awt.Window w = pdm.getDialog(); 236 237 WindowListener wl = new WindowAdapter () { 238 public void windowClosed(WindowEvent e) { 239 if (pdm.getComponent() instanceof EnhancedCustomPropertyEditor) { 240 if (!pdm.wasCancelled() && !closedOption && pdm.wasOK() && !pdm.wasReset()) { 241 try { 242 invoker.valueChanged(pdm.getEditor()); 243 } catch (Exception ex) { 244 } 246 } 247 } 248 249 invoker.editorClosed(); 250 w.removeWindowListener(this); 251 252 if (pcl != null) { 253 editor.removePropertyChangeListener(pcl); 254 } 255 256 } 258 259 public void windowOpened(WindowEvent e) { 260 invoker.editorOpened(); 261 curComp.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); 262 } 263 264 public void windowClosing(WindowEvent ev) { 266 if (PropUtils.isLoggable(CustomEditorAction.class)) { 267 PropUtils.log(CustomEditorAction.class, "CustomerEditorAction windowClosing event"); 268 } 269 270 closedOption = true; 271 } 272 273 boolean closedOption = false; 275 }; 276 277 if (w instanceof JDialog ) { 280 JDialog jd = (JDialog ) w; 281 jd.getAccessibleContext().setAccessibleName(title); 282 283 if (fd.getShortDescription() != null) { 284 jd.getAccessibleContext().setAccessibleDescription(fd.getShortDescription()); 285 } 286 287 w.addWindowListener(wl); 288 } else if (w instanceof Frame) { 289 ((Frame) w).addWindowListener(wl); 290 } 291 292 invoker.editorOpening(); 293 294 try { 295 PropUtils.addExternallyEdited(p); 296 w.setVisible(true); 297 PropUtils.removeExternallyEdited(p); 298 } catch (Exception ex) { 299 Exceptions.printStackTrace(ex); 300 } 301 302 } finally { curComp.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); 304 } 305 } 306 307 static interface Invoker { 308 309 public FeatureDescriptor getSelection(); 310 311 313 public Object getPartialValue(); 314 315 318 public Component getCursorChangeComponent(); 319 320 322 public String getBeanName(); 323 324 326 public void editorOpening(); 327 328 329 public void editorOpened(); 330 331 336 public void editorClosed(); 337 338 339 public void valueChanged(PropertyEditor editor); 340 341 343 public boolean allowInvoke(); 344 345 346 public void failed(); 347 348 351 public boolean wantAllChanges(); 352 353 public ReusablePropertyEnv getReusablePropertyEnv(); 354 } 355 } 356 | Popular Tags |