1 11 package org.eclipse.debug.internal.ui.contexts; 12 13 import org.eclipse.core.runtime.IAdaptable; 14 import org.eclipse.core.runtime.ISafeRunnable; 15 import org.eclipse.core.runtime.ListenerList; 16 import org.eclipse.core.runtime.SafeRunner; 17 import org.eclipse.debug.core.DebugEvent; 18 import org.eclipse.debug.core.DebugException; 19 import org.eclipse.debug.core.DebugPlugin; 20 import org.eclipse.debug.core.IDebugEventSetListener; 21 import org.eclipse.debug.core.ILaunch; 22 import org.eclipse.debug.core.model.IDebugElement; 23 import org.eclipse.debug.core.model.IDebugTarget; 24 import org.eclipse.debug.core.model.IThread; 25 import org.eclipse.debug.internal.ui.DebugUIPlugin; 26 import org.eclipse.debug.ui.contexts.ISuspendTrigger; 27 import org.eclipse.debug.ui.contexts.ISuspendTriggerListener; 28 29 32 public class LaunchSuspendTrigger implements ISuspendTrigger, IDebugEventSetListener { 33 34 private ListenerList fListeners = new ListenerList(); 35 private SuspendTriggerAdapterFactory fFactory = null; 36 private ILaunch fLaunch = null; 37 38 public LaunchSuspendTrigger(ILaunch launch, SuspendTriggerAdapterFactory factory) { 39 fFactory = factory; 40 fLaunch = launch; 41 DebugPlugin.getDefault().addDebugEventListener(this); 42 } 43 44 public ILaunch getLaunch() { 45 return fLaunch; 46 } 47 48 protected void dispose() { 49 DebugPlugin.getDefault().removeDebugEventListener(this); 50 fListeners = null; 51 fFactory.dispose(this); 52 } 53 54 57 public void addSuspendTriggerListener(ISuspendTriggerListener listener) { 58 if (fListeners != null) { 59 fListeners.add(listener); 60 } 61 } 62 63 66 public void removeSuspendTriggerListener(ISuspendTriggerListener listener) { 67 if (fListeners != null) { 68 fListeners.remove(listener); 69 } 70 if (fListeners.size() == 0) { 71 dispose(); 72 } 73 } 74 75 78 public void handleDebugEvents(DebugEvent[] events) { 79 for (int i = 0; i < events.length; i++) { 82 DebugEvent event = events[i]; 83 if (event.getKind() == DebugEvent.SUSPEND && !event.isEvaluation() && event.getDetail() != DebugEvent.STEP_END) { 84 Object source = event.getSource(); 86 if (source instanceof IAdaptable) { 87 IAdaptable adaptable = (IAdaptable) source; 88 ILaunch launch = (ILaunch) adaptable.getAdapter(ILaunch.class); 89 if (fLaunch.equals(launch)) { 90 notifySuspend(event); 92 } 93 } 94 95 } 96 } 97 } 98 99 102 private void notifySuspend(DebugEvent event) { 103 Object source = event.getSource(); 104 if (source instanceof IDebugElement) { 105 final ILaunch launch = ((IDebugElement)source).getLaunch(); 106 Object context = null; 107 if (source instanceof IThread) { 108 try { 109 context = ((IThread)source).getTopStackFrame(); 110 } catch (DebugException e) { 111 } 112 } else if (source instanceof IDebugTarget) { 113 context = source; 114 } 115 final Object temp = context; 116 ListenerList list = fListeners; 117 if (list != null) { 118 Object [] listeners = list.getListeners(); 119 for (int i = 0; i < listeners.length; i++) { 120 final ISuspendTriggerListener listener = (ISuspendTriggerListener) listeners[i]; 121 SafeRunner.run(new ISafeRunnable() { 122 public void run() throws Exception { 123 listener.suspended(launch, temp); 124 } 125 126 public void handleException(Throwable exception) { 127 DebugUIPlugin.log(exception); 128 } 129 130 }); 131 } 132 } 133 134 } 135 136 } 137 138 } 139 | Popular Tags |