1 19 20 package org.netbeans.modules.form; 21 22 import java.util.*; 23 import java.lang.reflect.Method ; 24 25 33 34 public class FormEvents { 35 36 static final Event[] NO_EVENTS = {}; 37 38 private Map usedListeners = new HashMap(); 40 41 private Map usedMethods = new HashMap(); 43 44 private Map eventHandlers = new HashMap(); 46 47 private FormModel formModel; 48 49 FormEvents(FormModel formModel) { 50 this.formModel = formModel; 51 } 52 53 56 public boolean hasEventsInCEDL() { 57 return !usedListeners.isEmpty(); 58 } 59 60 public Class [] getCEDLTypes() { 61 Collection infoValues = usedListeners.values(); 62 Class [] listenerTypes = new Class [infoValues.size()]; 63 int i = 0; 64 Iterator it = infoValues.iterator(); 65 while (it.hasNext()) 66 listenerTypes[i++] = ((ListenerInfo)it.next()).listenerType; 67 68 return listenerTypes; 69 } 70 71 public Event[] getEventsForCEDLMethod(Method listenerMethod) { 72 List eventList = (List) usedMethods.get(fullMethodName(listenerMethod)); 73 if (eventList == null) 74 return NO_EVENTS; 75 76 Event[] eventArray = new Event[eventList.size()]; 77 eventList.toArray(eventArray); 78 return eventArray; 79 } 80 81 public Event[] getEventsForHandler(String handlerName) { 82 List handlerEventList = (List) eventHandlers.get(handlerName); 83 Event[] events = new Event[handlerEventList.size()]; 84 handlerEventList.toArray(events); 85 return events; 86 } 87 88 public Method getOriginalListenerMethod(String handlerName) { 89 List handlerEventList = (List) eventHandlers.get(handlerName); 90 return handlerEventList != null ? 91 ((Event)handlerEventList.get(0)).getListenerMethod() : null; 92 } 93 94 public String [] getAllEventHandlers() { 95 Set nameSet = eventHandlers.keySet(); 96 String [] names = new String [nameSet.size()]; 97 nameSet.toArray(names); 98 return names; 99 } 100 101 104 public void attachEvent(Event event, String handlerName, String handlerText) 105 { 106 boolean newHandler = false; 108 if (handlerName == null && event.hasEventHandlers()) { handlerName = (String ) event.getEventHandlerList().get(0); 110 event = null; 111 } 112 else { List handlerEventList; 114 if (handlerName != null) { 115 handlerEventList = (List) eventHandlers.get(handlerName); 116 if (handlerEventList != null) checkCompatibility(event, (Event) handlerEventList.get(0)); 118 } 119 else { handlerEventList = null; 121 handlerName = findFreeHandlerName(event, 122 event.getComponent()); 123 } 124 125 if (handlerEventList == null) { handlerEventList = new ArrayList(3); 127 eventHandlers.put(handlerName, handlerEventList); 128 newHandler = true; 129 } 130 131 if (!event.isInCEDL()) 132 registerEventInCEDL(event); 133 134 if (event.addEventHandler(handlerName)) 135 handlerEventList.add(event); 136 else event = null; 138 } 139 140 formModel.fireEventHandlerAdded(event, 141 handlerName, handlerText, newHandler); 142 } 143 144 public void detachEvent(Event event, String handlerName) { 145 if (event.removeEventHandler(handlerName)) { 146 if (!event.hasEventHandlers()) 147 unregisterEventFromCEDL(event); 148 149 detachEventHandler(event, handlerName); 150 } 151 } 152 153 public void detachEvent(Event event) { 154 unregisterEventFromCEDL(event); 155 String [] handlerNames = event.getEventHandlers(); 156 for (int i=0; i < handlerNames.length; i++) { 157 event.removeEventHandler(handlerNames[i]); 158 detachEventHandler(event, handlerNames[i]); 159 } 160 } 161 162 public void renameEventHandler(String oldHandlerName, String newHandlerName) 163 { 164 if (oldHandlerName == null || newHandlerName == null 165 || oldHandlerName.equals(newHandlerName)) 166 return; 167 168 List handlerEventList = (List) eventHandlers.get(oldHandlerName); 169 if (handlerEventList == null) 170 return; 172 if (eventHandlers.get(newHandlerName) == null) { 173 for (int j=0; j < handlerEventList.size(); j++) { 174 Event event = (Event) handlerEventList.get(j); 175 event.renameEventHandler(oldHandlerName, newHandlerName); 176 } 177 eventHandlers.remove(oldHandlerName); 178 eventHandlers.put(newHandlerName, handlerEventList); 179 formModel.fireEventHandlerRenamed(oldHandlerName, newHandlerName); 180 } 181 else { 182 IllegalArgumentException iae = 183 new IllegalArgumentException ("Cannot rename handler"); org.openide.ErrorManager.getDefault().annotate( 185 iae, FormUtils.getBundleString("MSG_CannotRename")); throw iae; 187 } 188 } 189 190 public String findFreeHandlerName(Event event, RADComponent metacomp) { 191 String componentName = metacomp == formModel.getTopRADComponent() ? 192 "form" : metacomp.getName(); String methodName = event.getListenerMethod().getName(); 194 195 return findFreeHandlerName(componentName 196 + methodName.substring(0, 1).toUpperCase() 197 + methodName.substring(1)); 198 } 199 200 public String findFreeHandlerName(String baseName) { 201 String name = baseName; 202 int n = 0; 203 while (eventHandlers.get(name) != null) 204 name = baseName + (++n); 205 return name; 206 } 207 208 211 static String getEventIdName(Method eventMethod) { 212 StringBuffer buf = new StringBuffer (64); 213 214 buf.append("$"); buf.append(eventMethod.getDeclaringClass().getName()); 216 buf.append("."); buf.append(eventMethod.getName()); 218 buf.append("("); 220 Class [] parameterTypes = eventMethod.getParameterTypes(); 221 for (int i=0; i < parameterTypes.length; i++) { 222 buf.append(parameterTypes[i].getName()); 223 if (i+1 < parameterTypes.length) 224 buf.append(", "); } 226 227 buf.append(")"); return buf.toString(); 229 } 230 231 234 private void registerEventInCEDL(Event event) { 235 Class listenerType = event.getEventSetDescriptor().getListenerType(); 237 if (!listenerType.isInterface()) 238 return; 239 240 Class [] parameters = event.getListenerMethod().getParameterTypes(); 242 if (parameters.length == 0 243 || !java.util.EventObject .class.isAssignableFrom(parameters[0])) 244 return; 245 246 if (!addEventToMethod(event)) 247 return; 249 String listenerTypeName = listenerType.getName(); 250 ListenerInfo lInfo = (ListenerInfo) 251 usedListeners.get(listenerTypeName); 252 if (lInfo == null) { 253 lInfo = new ListenerInfo(listenerType); 254 usedListeners.put(listenerTypeName, lInfo); 255 } 256 else lInfo.listenerType = listenerType; 257 lInfo.useCount++; 258 259 event.setInCEDL(true); 260 } 261 262 private void unregisterEventFromCEDL(Event event) { 263 if (removeEventFromMethod(event)) { 264 String listenerTypeName = event.getEventSetDescriptor() 265 .getListenerType().getName(); 266 ListenerInfo lInfo = (ListenerInfo) 267 usedListeners.get(listenerTypeName); 268 if (lInfo != null && --lInfo.useCount == 0) 269 usedListeners.remove(listenerTypeName); 270 271 event.setInCEDL(false); 272 } 273 } 274 275 private boolean addEventToMethod(Event event) { 276 String methodName = fullMethodName(event.getListenerMethod()); 277 List eventList = (List) usedMethods.get(methodName); 278 279 if (eventList == null) { 280 eventList = new ArrayList(); 281 eventList.add(event); 282 usedMethods.put(methodName, eventList); 283 } 284 else { 285 for (Iterator it=eventList.iterator(); it.hasNext(); ) { 286 Event e = (Event) it.next(); 287 if (e.getComponent() == event.getComponent()) 288 return false; } 291 eventList.add(event); 292 } 293 294 return true; 295 } 296 297 private boolean removeEventFromMethod(Event event) { 298 boolean removed; 299 String methodName = fullMethodName(event.getListenerMethod()); 300 List eventList = (List) usedMethods.get(methodName); 301 302 if (eventList != null) { 303 removed = eventList.remove(event); 304 if (eventList.size() == 0) 305 usedMethods.remove(methodName); 306 } 307 else removed = false; 308 309 return removed; 310 } 311 312 private void detachEventHandler(Event event, String handlerName) { 313 List handlerEventList = (List) eventHandlers.get(handlerName); 314 handlerEventList.remove(event); 315 if (handlerEventList.size() == 0) 316 eventHandlers.remove(handlerName); 318 formModel.fireEventHandlerRemoved(event, 319 handlerName, 320 handlerEventList.size() == 0); 321 } 322 323 private void checkCompatibility(Event event1, Event event2) { 324 Method m1 = event1.getListenerMethod(); 325 Method m2 = event2.getListenerMethod(); 326 Class [] params1 = m1.getParameterTypes(); 327 Class [] params2 = m2.getParameterTypes(); 328 boolean ok; 329 330 if (params1.length == params2.length) { 331 ok = true; 332 for (int i=0; i < params1.length; i++) 333 if (!params1[i].getName().equals(params2[i].getName())) { 334 ok = false; 335 break; 336 } 337 if (ok) 338 ok = m1.getReturnType().equals(m2.getReturnType()); 339 } 340 else ok = false; 341 342 if (!ok) { 343 IllegalArgumentException iae = 344 new IllegalArgumentException ("Incompatible event"); org.openide.ErrorManager.getDefault().annotate( 346 iae, FormUtils.getBundleString("MSG_CannotAttach")); throw iae; 348 } 349 } 350 351 private static String fullMethodName(Method m) { 352 StringBuffer name = new StringBuffer (); 353 name.append(m.getName()); 354 name.append("("); 356 Class [] params = m.getParameterTypes(); 357 for (int i=0; i < params.length; i++) { 358 name.append(params[i].getName()); 359 if (i+1 < params.length) 360 name.append(", "); } 362 363 name.append(")"); return name.toString(); 365 } 366 367 369 private static class ListenerInfo { 370 Class listenerType; 371 int useCount; 372 ListenerInfo(Class listenerType) { 373 this.listenerType = listenerType; 374 } 375 } 376 } 377 | Popular Tags |