KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > contexts > LaunchSuspendTrigger


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 /**
30  * @since 3.2
31  */

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     /* (non-Javadoc)
55      * @see org.eclipse.debug.ui.contexts.ISuspendTrigger#addSuspendTriggerListener(org.eclipse.debug.ui.contexts.ISuspendTriggerListener)
56      */

57     public void addSuspendTriggerListener(ISuspendTriggerListener listener) {
58         if (fListeners != null) {
59             fListeners.add(listener);
60         }
61     }
62
63     /* (non-Javadoc)
64      * @see org.eclipse.debug.ui.contexts.ISuspendTrigger#removeSuspendTriggerListener(org.eclipse.debug.ui.contexts.ISuspendTriggerListener)
65      */

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     /* (non-Javadoc)
76      * @see org.eclipse.debug.core.IDebugEventSetListener#handleDebugEvents(org.eclipse.debug.core.DebugEvent[])
77      */

78     public void handleDebugEvents(DebugEvent[] events) {
79         // open the debugger if this is a suspend event and the debug view is not yet open
80
// and the preferences are set to switch
81
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 // Don't switch perspective for evaluations or stepping
85
Object JavaDoc 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                         // only notify for this launch
91
notifySuspend(event);
92                     }
93                 }
94
95             }
96         }
97     }
98
99     /**
100      * @param event
101      */

102     private void notifySuspend(DebugEvent event) {
103         Object JavaDoc source = event.getSource();
104         if (source instanceof IDebugElement) {
105             final ILaunch launch = ((IDebugElement)source).getLaunch();
106             Object JavaDoc 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 JavaDoc temp = context;
116             ListenerList list = fListeners;
117             if (list != null) {
118                 Object JavaDoc[] 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 JavaDoc {
123                             listener.suspended(launch, temp);
124                         }
125                     
126                         public void handleException(Throwable JavaDoc exception) {
127                             DebugUIPlugin.log(exception);
128                         }
129                     
130                     });
131                 }
132             }
133
134         }
135         
136     }
137
138 }
139
Popular Tags