1 19 20 package org.netbeans.api.debugger; 21 22 import java.beans.*; 23 import java.io.*; 24 import java.util.*; 25 26 import org.netbeans.spi.debugger.ActionsProvider; 27 import org.netbeans.spi.debugger.ActionsProviderListener; 28 import org.openide.util.Cancellable; 29 import org.openide.util.Task; 30 31 37 public final class ActionsManager { 38 39 40 41 public static final Object ACTION_STEP_OVER = "stepOver"; 42 43 44 public static final Object ACTION_RUN_INTO_METHOD = "runIntoMethod"; 45 46 47 public static final Object ACTION_STEP_INTO = "stepInto"; 48 49 50 public static final Object ACTION_STEP_OUT = "stepOut"; 51 52 53 public static final Object ACTION_STEP_OPERATION = "stepOperation"; 54 55 56 public static final Object ACTION_CONTINUE = "continue"; 57 58 59 public static final Object ACTION_START = "start"; 60 61 62 public static final Object ACTION_KILL= "kill"; 63 64 65 public static final Object ACTION_MAKE_CALLER_CURRENT = "makeCallerCurrent"; 66 67 68 public static final Object ACTION_MAKE_CALLEE_CURRENT = "makeCalleeCurrent"; 69 70 71 public static final Object ACTION_PAUSE = "pause"; 72 73 74 public static final Object ACTION_RUN_TO_CURSOR = "runToCursor"; 75 76 77 public static final Object ACTION_POP_TOPMOST_CALL = "popTopmostCall"; 78 79 80 public static final Object ACTION_FIX = "fix"; 81 82 83 public static final Object ACTION_RESTART = "restart"; 84 85 86 public static final Object ACTION_TOGGLE_BREAKPOINT = "toggleBreakpoint"; 87 88 89 91 private Vector listener = new Vector (); 92 private HashMap listeners = new HashMap (); 93 private HashMap actionProviders; 94 private Object actionProvidersLock = new Object (); 95 private MyActionListener actionListener = new MyActionListener (); 96 private Lookup lookup; 97 private boolean doiingDo = false; 98 private boolean destroy = false; 99 100 101 106 ActionsManager (Lookup lookup) { 107 this.lookup = lookup; 108 } 109 110 111 113 120 public final void doAction (final Object action) { 121 doiingDo = true; 122 ArrayList l; 123 synchronized (actionProvidersLock) { 124 if (actionProviders == null) initActionImpls (); 125 l = (ArrayList) actionProviders.get (action); 126 if (l != null) { 127 l = (ArrayList) l.clone (); 128 } 129 } 130 boolean done = false; 131 if (l != null) { 132 int i, k = l.size (); 133 for (i = 0; i < k; i++) { 134 if (((ActionsProvider) l.get (i)).isEnabled (action)) { 135 done = true; 136 ((ActionsProvider) l.get (i)).doAction (action); 137 } 138 } 139 } 140 if (done) { 141 fireActionDone (action); 142 } 143 doiingDo = false; 144 if (destroy) destroyIn (); 145 } 146 147 164 public final Task postAction(final Object action) { 165 doiingDo = true; 166 ArrayList l; 167 synchronized (actionProvidersLock) { 168 if (actionProviders == null) initActionImpls (); 169 l = (ArrayList) actionProviders.get (action); 170 if (l != null) { 171 l = (ArrayList) l.clone (); 172 } 173 } 174 boolean posted = false; 175 int k; 176 if (l != null) { 177 k = l.size (); 178 } else { 179 k = 0; 180 } 181 List postedActions = new ArrayList(k); 182 final AsynchActionTask task = new AsynchActionTask(postedActions); 183 if (l != null) { 184 int i; 185 for (i = 0; i < k; i++) { 186 ActionsProvider ap = (ActionsProvider) l.get (i); 187 if (ap.isEnabled (action)) { 188 postedActions.add(ap); 189 posted = true; 190 } 191 } 192 if (posted) { 193 final int[] count = new int[] { 0 }; 194 Runnable notifier = new Runnable () { 195 public void run() { 196 synchronized (count) { 197 if (--count[0] == 0) { 198 task.actionDone(); 199 fireActionDone (action); 200 doiingDo = false; 201 if (destroy) destroyIn (); 202 } 203 } 204 } 205 }; 206 count[0] = k = postedActions.size(); 207 for (i = 0; i < k; i++) { 208 ((ActionsProvider) postedActions.get (i)).postAction ( 209 action, notifier 210 ); 211 } 212 } 213 } 214 if (!posted) { 215 doiingDo = false; 216 if (destroy) destroyIn (); 217 task.actionDone(); 218 } 219 return task; 220 } 221 222 229 public final boolean isEnabled (final Object action) { 230 ArrayList l; 231 synchronized (actionProvidersLock) { 232 if (actionProviders == null) initActionImpls (); 233 l = (ArrayList) actionProviders.get (action); 234 if (l != null) { 235 l = (ArrayList) l.clone (); 236 } 237 } 238 if (l != null) { 239 int i, k = l.size (); 240 for (i = 0; i < k; i++) 241 if (((ActionsProvider) l.get (i)).isEnabled (action)) 242 return true; 243 } 244 return false; 245 } 246 247 250 public void destroy () { 251 if (!doiingDo) destroyIn (); 252 destroy = true; 253 } 254 255 256 258 263 public void addActionsManagerListener (ActionsManagerListener l) { 264 listener.addElement (l); 265 } 266 267 272 public void removeActionsManagerListener (ActionsManagerListener l) { 273 listener.removeElement (l); 274 } 275 276 282 public void addActionsManagerListener ( 283 String propertyName, 284 ActionsManagerListener l 285 ) { 286 Vector listener = (Vector) listeners.get (propertyName); 287 if (listener == null) { 288 listener = new Vector (); 289 listeners.put (propertyName, listener); 290 } 291 listener.addElement (l); 292 } 293 294 300 public void removeActionsManagerListener ( 301 String propertyName, 302 ActionsManagerListener l 303 ) { 304 Vector listener = (Vector) listeners.get (propertyName); 305 if (listener == null) return; 306 listener.removeElement (l); 307 if (listener.size () == 0) 308 listeners.remove (propertyName); 309 } 310 311 312 314 324 private void fireActionDone ( 325 final Object action 326 ) { 327 initListeners (); 328 Vector l = (Vector) listener.clone (); 329 Vector l1 = (Vector) listeners.get ( 330 ActionsManagerListener.PROP_ACTION_PERFORMED 331 ); 332 if (l1 != null) 333 l1 = (Vector) l1.clone (); 334 int i, k = l.size (); 335 for (i = 0; i < k; i++) 336 ((ActionsManagerListener) l.elementAt (i)).actionPerformed ( 337 action 338 ); 339 if (l1 != null) { 340 k = l1.size (); 341 for (i = 0; i < k; i++) 342 ((ActionsManagerListener) l1.elementAt (i)).actionPerformed 343 (action); 344 } 345 } 346 347 357 private void fireActionStateChanged ( 358 final Object action 359 ) { 360 boolean enabled = isEnabled (action); 361 initListeners (); 362 Vector l = (Vector) listener.clone (); 363 Vector l1 = (Vector) listeners.get ( 364 ActionsManagerListener.PROP_ACTION_STATE_CHANGED 365 ); 366 if (l1 != null) 367 l1 = (Vector) l1.clone (); 368 int i, k = l.size (); 369 for (i = 0; i < k; i++) 370 ((ActionsManagerListener) l.elementAt (i)).actionStateChanged ( 371 action, enabled 372 ); 373 if (l1 != null) { 374 k = l1.size (); 375 for (i = 0; i < k; i++) 376 ((ActionsManagerListener) l1.elementAt (i)).actionStateChanged 377 (action, enabled); 378 } 379 } 380 381 382 384 private void registerActionsProvider (Object action, ActionsProvider p) { 385 synchronized (actionProvidersLock) { 386 ArrayList l = (ArrayList) actionProviders.get (action); 387 if (l == null) { 388 l = new ArrayList (); 389 actionProviders.put (action, l); 390 } 391 l.add (p); 392 } 393 fireActionStateChanged (action); 394 p.addActionsProviderListener (actionListener); 395 } 396 397 private void initActionImpls () { 398 actionProviders = new HashMap (); 399 Iterator i = lookup.lookup (null, ActionsProvider.class).iterator (); 400 while (i.hasNext ()) { 401 ActionsProvider ap = (ActionsProvider) i.next (); 402 Iterator ii = ap.getActions ().iterator (); 403 while (ii.hasNext ()) 404 registerActionsProvider (ii.next (), ap); 405 } 406 } 407 408 private boolean listerersLoaded = false; 409 private List lazyListeners; 410 411 private void initListeners () { 412 if (listerersLoaded) return; 413 listerersLoaded = true; 414 lazyListeners = lookup.lookup (null, LazyActionsManagerListener.class); 415 int i, k = lazyListeners.size (); 416 for (i = 0; i < k; i++) { 417 LazyActionsManagerListener l = (LazyActionsManagerListener) 418 lazyListeners.get (i); 419 String [] props = l.getProperties (); 420 if (props == null) { 421 addActionsManagerListener (l); 422 continue; 423 } 424 int j, jj = props.length; 425 for (j = 0; j < jj; j++) { 426 addActionsManagerListener (props [j], l); 427 } 428 } 429 } 430 431 private synchronized void destroyIn () { 432 int i, k = lazyListeners.size (); 433 for (i = 0; i < k; i++) { 434 LazyActionsManagerListener l = (LazyActionsManagerListener) 435 lazyListeners.get (i); 436 String [] props = l.getProperties (); 437 if (props == null) { 438 removeActionsManagerListener (l); 439 continue; 440 } 441 int j, jj = props.length; 442 for (j = 0; j < jj; j++) 443 removeActionsManagerListener (props [j], l); 444 l.destroy (); 445 } 446 lazyListeners = new ArrayList (); 447 } 448 449 450 452 private static class AsynchActionTask extends Task implements Cancellable { 453 454 private Collection postedActions; 455 456 public AsynchActionTask(Collection postedActions) { 457 this.postedActions = postedActions; 458 } 459 460 void actionDone() { 461 notifyFinished(); 462 } 463 464 public boolean cancel() { 465 for (Iterator it = postedActions.iterator(); it.hasNext(); ) { 466 Object action = it.next(); 467 if (action instanceof Cancellable) { 468 if (!((Cancellable) action).cancel()) { 469 return false; 470 } 471 } else { 472 return false; 473 } 474 } 475 return true; 476 } 477 } 478 479 class MyActionListener implements ActionsProviderListener { 480 public void actionStateChange (Object action, boolean enabled) { 481 fireActionStateChanged (action); 482 } 483 } 484 } 485 486 | Popular Tags |