1 19 20 package org.netbeans.modules.form; 21 22 import java.util.*; 23 import java.beans.*; 24 import java.awt.event.*; 25 26 import org.openide.*; 27 import org.openide.nodes.*; 28 import org.openide.util.Utilities; 29 30 36 37 class EventProperty extends PropertySupport.ReadWrite { 38 39 private static String NO_EVENT; 40 41 private static boolean somethingChanged; private static boolean invalidValueTried; 44 private Event event; 45 46 private String selectedEventHandler; 47 48 EventProperty(Event event, String eventId) { 49 super(eventId, 50 String .class, 51 event.getListenerMethod().getName(), 52 event.getListenerMethod().getName()); 53 this.event = event; 54 setShortDescription( 55 event.getEventSetDescriptor().getListenerType().getName()); 56 } 57 58 Event getEvent() { 59 return event; 60 } 61 62 private FormEvents getFormEvents() { 63 return event.getComponent().getFormModel().getFormEvents(); 64 } 65 66 private java.lang.reflect.Method getListenerMethod() { 67 return event.getListenerMethod(); 68 } 69 70 String [] getEventHandlers() { 71 return event.getEventHandlers(); 72 } 73 74 76 80 public Object getValue() { 81 if (selectedEventHandler == null && event.hasEventHandlers()) 82 selectedEventHandler = (String ) event.getEventHandlerList().get(0); 83 return selectedEventHandler; 84 } 85 86 91 public void setValue(Object val) { 92 Change change = null; 93 String newSelectedHandler = null; 94 95 if (val instanceof Change) { 96 change = (Change) val; 97 } 98 else if (val instanceof String ) { 99 String [] handlers = getEventHandlers(); 100 if (handlers.length > 0) { 101 String current = selectedEventHandler != null ? 103 selectedEventHandler : handlers[0]; 104 105 if ("".equals(val)) { change = new Change(); 108 change.getRemoved().add(current); 109 for (int i=0; i < handlers.length; i++) 110 if (!handlers[i].equals(current)) { 111 newSelectedHandler = handlers[i]; 112 break; 113 } 114 } 115 else { newSelectedHandler = (String ) val; 117 118 boolean ignore = false; 119 for (int i=0; i < handlers.length; i++) 120 if (handlers[i].equals(val)) { ignore = true; 122 break; 123 } 124 125 if (!ignore) { change = new Change(); 127 change.getRenamedNewNames().add(val); 128 change.getRenamedOldNames().add(current); 129 } 130 } 131 } 132 else { if (!"".equals(val)) { 134 change = new Change(); 135 change.getAdded().add((String )val); 136 newSelectedHandler = (String ) val; 137 } 138 } 139 } 140 else if (val == null) { 141 if (selectedEventHandler == null) 142 return; 143 } 144 else throw new IllegalArgumentException (); 145 146 if (change != null) { 147 somethingChanged = true; 149 FormEvents formEvents = getFormEvents(); 150 151 if (change.hasRemoved()) for (Iterator it=change.getRemoved().iterator(); it.hasNext(); ) 153 formEvents.detachEvent(event, (String ) it.next()); 154 155 if (change.hasRenamed()) for (int i=0; i < change.getRenamedOldNames().size(); i++) { 157 String oldName = (String )change.getRenamedOldNames().get(i); 158 String newName = (String )change.getRenamedNewNames().get(i); 159 160 try { 161 formEvents.renameEventHandler(oldName, newName); 162 163 Event[] events = formEvents.getEventsForHandler(newName); 165 for (int j=0 ; j < events.length; j++) { 166 Node.Property prop = events[j].getComponent() 167 .getPropertyByName(getName()); 168 if (prop != null && prop != this) { 169 try { 170 if (oldName.equals(prop.getValue())) 171 prop.setValue(newName); 172 } 173 catch (Exception ex) { ex.printStackTrace(); 175 } 176 } 177 } 178 } 179 catch (IllegalArgumentException ex) { ErrorManager.getDefault().notify(ErrorManager.WARNING, ex); 181 newSelectedHandler = null; 182 } 183 } 184 185 if (change.hasAdded()) for (Iterator it=change.getAdded().iterator(); it.hasNext(); ) { 187 try { 188 formEvents.attachEvent(event, (String ) it.next(), null); 189 } 190 catch (IllegalArgumentException ex) { ErrorManager.getDefault().notify(ErrorManager.WARNING, ex); 192 newSelectedHandler = null; 193 } 194 } 195 } 196 197 selectedEventHandler = newSelectedHandler; 198 199 RADComponentNode node = event.getComponent().getNodeReference(); 200 if (node != null) 201 node.firePropertyChangeHelper(getName(), null, null); 202 } 203 204 public Object getValue(String key) { 205 if ("canEditAsText".equals(key)) return Boolean.TRUE; 207 208 if ("initialEditValue".equals(key)) { somethingChanged = false; invalidValueTried = false; 211 return selectedEventHandler != null ? null : 212 getFormEvents().findFreeHandlerName(event, event.getComponent()); 213 } 214 215 if ("postSetAction".equals(key)) return new javax.swing.AbstractAction () { 217 public void actionPerformed(ActionEvent ev) { 218 if (!somethingChanged && !invalidValueTried) 221 getFormEvents().attachEvent(event, 222 selectedEventHandler, 223 null); 224 } 225 }; 226 227 return super.getValue(key); 228 } 229 230 231 public boolean canWrite() { 239 return !isReadOnly(); 240 } 241 242 private boolean isReadOnly() { 243 return event.getComponent().isReadOnly(); 244 } 245 246 249 public PropertyEditor getPropertyEditor() { 250 return new EventEditor(); 251 } 252 253 255 257 static class Change { 258 boolean hasAdded() { 259 return added != null && added.size() > 0; 260 } 261 boolean hasRemoved() { 262 return removed != null && removed.size() > 0; 263 } 264 boolean hasRenamed() { 265 return renamedOldName != null && renamedOldName.size() > 0; 266 } 267 List getAdded() { 268 if (added == null) 269 added = new ArrayList(); 270 return added; 271 } 272 List getRemoved() { 273 if (removed == null) 274 removed = new ArrayList(); 275 return removed; 276 } 277 List getRenamedOldNames() { 278 if (renamedOldName == null) 279 renamedOldName = new ArrayList(); 280 return renamedOldName; 281 } 282 List getRenamedNewNames() { 283 if (renamedNewName == null) 284 renamedNewName = new ArrayList(); 285 return renamedNewName; 286 } 287 private List added; 288 private List removed; 289 private List renamedOldName; 290 private List renamedNewName; 291 } 292 293 295 private class EventEditor extends PropertyEditorSupport { 296 297 public String getAsText() { 298 if (this.getValue() == null) { 299 if (NO_EVENT == null) 300 NO_EVENT = FormUtils.getBundleString("CTL_NoEvent"); return NO_EVENT; 302 } 303 return this.getValue().toString(); 304 } 305 306 public void setAsText(String txt) { 307 if (!"".equals(txt) && !Utilities.isJavaIdentifier(txt)) { invalidValueTried = true; 310 IllegalArgumentException iae = new IllegalArgumentException (); 311 String annotation = FormUtils.getFormattedBundleString( 312 "FMT_MSG_InvalidJavaIdentifier", new Object [] { txt } ); 314 ErrorManager.getDefault().annotate( 315 iae, ErrorManager.ERROR, "Not a java identifier", annotation, null, null); 317 throw iae; 318 } 319 if ("".equals(txt) && (this.getValue() == null)) { 320 invalidValueTried = true; 322 IllegalArgumentException iae = new IllegalArgumentException (); 323 String emptyStringTxt = FormUtils.getBundleString("FMT_MSG_EmptyString"); String annotation = FormUtils.getFormattedBundleString( 325 "FMT_MSG_InvalidJavaIdentifier", new Object [] { emptyStringTxt } ); 327 ErrorManager.getDefault().annotate( 328 iae, ErrorManager.ERROR, "Not a java identifier", annotation, null, null); 330 throw iae; 331 } 332 invalidValueTried = false; 333 this.setValue(txt); 334 } 335 336 public String [] getTags() { 337 String [] handlers = getEventHandlers(); 338 return handlers.length > 1 ? handlers : null; 339 } 340 341 public boolean supportsCustomEditor() { 342 return isReadOnly() ? false : true; 343 } 344 345 public java.awt.Component getCustomEditor() { 346 if (isReadOnly()) 347 return null; 348 349 final EventCustomEditor ed = new EventCustomEditor(EventProperty.this); 350 DialogDescriptor dd = new DialogDescriptor( 351 ed, 352 FormUtils.getFormattedBundleString( 353 "FMT_MSG_HandlersFor", new Object [] { getListenerMethod().getName() }), 355 true, 356 new ActionListener() { 357 public void actionPerformed(ActionEvent evt) { 358 if (evt.getSource().equals(DialogDescriptor.OK_OPTION)) { 359 ed.doChanges(); 360 } 361 } 362 }); 363 364 return DialogDisplayer.getDefault().createDialog(dd); 365 } 366 } 367 } 368 | Popular Tags |