KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > threadgroups > JavaThreadEventHandler


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 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.jdt.internal.debug.ui.threadgroups;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.debug.core.DebugEvent;
19 import org.eclipse.debug.core.DebugException;
20 import org.eclipse.debug.core.DebugPlugin;
21 import org.eclipse.debug.core.ILaunch;
22 import org.eclipse.debug.core.model.IStackFrame;
23 import org.eclipse.debug.core.model.IThread;
24 import org.eclipse.debug.internal.ui.viewers.model.provisional.IModelDelta;
25 import org.eclipse.debug.internal.ui.viewers.model.provisional.ModelDelta;
26 import org.eclipse.debug.internal.ui.viewers.provisional.AbstractModelProxy;
27 import org.eclipse.debug.internal.ui.viewers.update.ThreadEventHandler;
28 import org.eclipse.jdt.debug.core.IJavaDebugTarget;
29 import org.eclipse.jdt.debug.core.IJavaStackFrame;
30 import org.eclipse.jdt.debug.core.IJavaThread;
31 import org.eclipse.jdt.debug.core.IJavaThreadGroup;
32 import org.eclipse.jdt.debug.ui.IJavaDebugUIConstants;
33 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
34 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPreferenceInitializer;
35 import org.eclipse.jdt.internal.debug.ui.monitors.JavaElementContentProvider;
36 import org.eclipse.jdt.internal.debug.ui.snippeteditor.ScrapbookLauncher;
37 import org.eclipse.jface.preference.IPreferenceStore;
38 import org.eclipse.jface.util.IPropertyChangeListener;
39 import org.eclipse.jface.util.PropertyChangeEvent;
40 import org.eclipse.jface.viewers.Viewer;
41 import org.eclipse.swt.events.TreeEvent;
42 import org.eclipse.swt.events.TreeListener;
43 import org.eclipse.swt.widgets.Control;
44 import org.eclipse.swt.widgets.Tree;
45 import org.eclipse.swt.widgets.TreeItem;
46 import org.eclipse.swt.widgets.Widget;
47
48 /**
49  * @since 3.2
50  *
51  */

52 public class JavaThreadEventHandler extends ThreadEventHandler implements IPropertyChangeListener, TreeListener {
53     
54     private boolean fDisplayMonitors;
55     private Tree fTree;
56
57     /**
58      * Constructs and event handler for a Java thread.
59      *
60      * @param proxy
61      */

62     public JavaThreadEventHandler(AbstractModelProxy proxy) {
63         super(proxy);
64         IPreferenceStore preferenceStore = JDIDebugUIPlugin.getDefault().getPreferenceStore();
65         preferenceStore.addPropertyChangeListener(this);
66         fDisplayMonitors= preferenceStore.getBoolean(IJavaDebugUIConstants.PREF_SHOW_MONITOR_THREAD_INFO);
67     }
68     
69     protected void init(Viewer viewer) {
70         Control control = viewer.getControl();
71         if (control instanceof Tree) {
72             fTree = (Tree) control;
73             fTree.getDisplay().asyncExec(new Runnable JavaDoc() {
74                 public void run() {
75                     fTree.addTreeListener(JavaThreadEventHandler.this);
76                 }
77             });
78         }
79     }
80     
81     /* (non-Javadoc)
82      * @see org.eclipse.debug.internal.ui.viewers.update.ThreadEventHandler#dispose()
83      */

84     public synchronized void dispose() {
85         if (fTree != null) {
86             fTree.removeTreeListener(this);
87         }
88         IPreferenceStore preferenceStore = JDIDebugUIPlugin.getDefault().getPreferenceStore();
89         preferenceStore.removePropertyChangeListener(this);
90         super.dispose();
91     }
92
93     protected ModelDelta addPathToThread(ModelDelta delta, IThread thread) {
94         if (JavaElementContentProvider.isDisplayThreadGroups()) {
95             ILaunch launch = thread.getLaunch();
96             ILaunch[] launches = DebugPlugin.getDefault().getLaunchManager().getLaunches();
97             Object JavaDoc[] launchChildren = launch.getChildren();
98             delta = delta.addNode(launch, indexOf(launches, launch), IModelDelta.NO_CHANGE, launchChildren.length);
99             IJavaDebugTarget debugTarget = (IJavaDebugTarget) thread.getDebugTarget();
100             List JavaDoc groups = new ArrayList JavaDoc();
101             try{
102                 delta = delta.addNode(debugTarget, indexOf(launchChildren, debugTarget), IModelDelta.NO_CHANGE, debugTarget.getRootThreadGroups().length);
103                 IJavaThread javaThread = (IJavaThread) thread;
104                 IJavaThreadGroup threadGroup = javaThread.getThreadGroup();
105                 while (threadGroup != null) {
106                     groups.add(0, threadGroup);
107                     threadGroup = threadGroup.getThreadGroup();
108                 }
109                 Iterator JavaDoc iterator = groups.iterator();
110                 while (iterator.hasNext()) {
111                     IJavaThreadGroup group = (IJavaThreadGroup) iterator.next();
112                     int index = -1;
113                     IJavaThreadGroup parent = group.getThreadGroup();
114                     if (parent != null) {
115                         index = indexOf(parent.getThreadGroups(), group);
116                         if (index >= 0) {
117                             // threads are displayed first
118
index += parent.getThreads().length;
119                         }
120                     } else {
121                         index = indexOf(debugTarget.getRootThreadGroups(), group);
122                     }
123                     delta = delta.addNode(group, index, IModelDelta.NO_CHANGE, group.getThreadGroups().length + group.getThreads().length);
124                 }
125                 } catch (DebugException e) {
126                     JDIDebugUIPlugin.log(e);
127                 }
128             return delta;
129         } else {
130             return super.addPathToThread(delta, thread);
131         }
132     }
133     
134     public void propertyChange(PropertyChangeEvent event) {
135         if (event.getProperty().equals(IJavaDebugUIConstants.PREF_SHOW_MONITOR_THREAD_INFO)) {
136             fDisplayMonitors= JDIDebugUIPreferenceInitializer.getBoolean(event);
137         }
138     }
139
140     protected boolean isDisplayMonitors() {
141         return fDisplayMonitors;
142     }
143     
144     /* (non-Javadoc)
145      * @see org.eclipse.debug.internal.ui.viewers.update.ThreadEventHandler#indexOf(org.eclipse.debug.core.model.IStackFrame)
146      */

147     protected int indexOf(IStackFrame frame) {
148         if (isDisplayMonitors()) {
149             if (((IJavaDebugTarget)frame.getDebugTarget()).supportsMonitorInformation()) {
150                 IJavaThread thread = (IJavaThread)frame.getThread();
151                 int index = 0;
152                 try {
153                     index = thread.getOwnedMonitors().length;
154                     if (thread.getContendedMonitor() != null) {
155                         index++;
156                     }
157                 } catch (DebugException e) {
158                 }
159                 return index;
160             } else {
161                 // make room for the 'no monitor info' element
162
return 1;
163             }
164         } else {
165             return super.indexOf(frame);
166         }
167     }
168     
169     /**
170      * Returns the number of children the given thread has in the view.
171      *
172      * @param thread thread
173      * @return number of children
174      */

175     protected int childCount(IThread thread) {
176         try {
177             IJavaThread jThread = (IJavaThread) thread;
178             int count = jThread.getFrameCount();
179             if (isDisplayMonitors()) {
180                 if (((IJavaDebugTarget)thread.getDebugTarget()).supportsMonitorInformation()) {
181                     count += jThread.getOwnedMonitors().length;
182                     if (jThread.getContendedMonitor() != null) {
183                         count++;
184                     }
185                 } else {
186                     // make room for the 'no monitor info' element
187
count++;
188                 }
189             }
190             return count;
191         } catch (DebugException e) {
192         }
193         return -1;
194     }
195     
196
197     /* (non-Javadoc)
198      * @see org.eclipse.debug.internal.ui.viewers.update.ThreadEventHandler#indexOf(org.eclipse.debug.core.model.IThread)
199      */

200     protected int indexOf(IThread thread) {
201         if (JavaElementContentProvider.isDisplayThreadGroups()) {
202             IJavaThread javaThread = (IJavaThread) thread;
203             try {
204                 return indexOf(javaThread.getThreadGroup().getThreads(), javaThread);
205             } catch (CoreException e) {
206                 return -1;
207             }
208         }
209         return super.indexOf(thread);
210     }
211
212     /* (non-Javadoc)
213      * @see org.eclipse.debug.internal.ui.viewers.update.ThreadEventHandler#handlesEvent(org.eclipse.debug.core.DebugEvent)
214      */

215     protected boolean handlesEvent(DebugEvent event) {
216         if (super.handlesEvent(event)) {
217             Object JavaDoc source = event.getSource();
218             if (source instanceof IJavaThread) {
219                 IJavaThread thread = (IJavaThread) source;
220                 ILaunch launch = thread.getLaunch();
221                 if (launch != null) {
222                     if (launch.getAttribute(ScrapbookLauncher.SCRAPBOOK_LAUNCH) != null) {
223                         if (event.getKind() == DebugEvent.SUSPEND) {
224                             try {
225                                 IJavaStackFrame frame = (IJavaStackFrame) thread.getTopStackFrame();
226                                 if (frame.getDeclaringTypeName().startsWith("org.eclipse.jdt.internal.debug.ui.snippeteditor.ScrapbookMain")) { //$NON-NLS-1$
227
return false;
228                                 }
229                             } catch (DebugException e) {
230                             }
231                         }
232                     }
233                 }
234             }
235         } else {
236             return false;
237         }
238         return true;
239     }
240
241     /* (non-Javadoc)
242      * @see org.eclipse.swt.events.TreeListener#treeCollapsed(org.eclipse.swt.events.TreeEvent)
243      */

244     public void treeCollapsed(TreeEvent e) {
245         // when the user collapses a thread, remove it from the 'next thread to select queue'
246
Widget widget = e.item;
247         if (widget instanceof TreeItem) {
248             TreeItem item = (TreeItem) widget;
249             Object JavaDoc data = item.getData();
250             if (data instanceof IJavaThread) {
251                 removeQueuedThread((IJavaThread)data);
252             }
253         }
254     }
255
256     /* (non-Javadoc)
257      * @see org.eclipse.swt.events.TreeListener#treeExpanded(org.eclipse.swt.events.TreeEvent)
258      */

259     public void treeExpanded(TreeEvent e) {
260         // when the expands a thread, add it back to the 'next thread to select queue'
261
Widget widget = e.item;
262         if (widget instanceof TreeItem) {
263             TreeItem item = (TreeItem) widget;
264             Object JavaDoc data = item.getData();
265             if (data instanceof IJavaThread) {
266                 queueSuspendedThread((IJavaThread)data);
267             }
268         }
269     }
270
271 }
272
Popular Tags