1 11 package org.eclipse.jdt.internal.debug.ui.threadgroups; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 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 52 public class JavaThreadEventHandler extends ThreadEventHandler implements IPropertyChangeListener, TreeListener { 53 54 private boolean fDisplayMonitors; 55 private Tree fTree; 56 57 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 () { 74 public void run() { 75 fTree.addTreeListener(JavaThreadEventHandler.this); 76 } 77 }); 78 } 79 } 80 81 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 [] launchChildren = launch.getChildren(); 98 delta = delta.addNode(launch, indexOf(launches, launch), IModelDelta.NO_CHANGE, launchChildren.length); 99 IJavaDebugTarget debugTarget = (IJavaDebugTarget) thread.getDebugTarget(); 100 List groups = new ArrayList (); 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 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 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 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 return 1; 163 } 164 } else { 165 return super.indexOf(frame); 166 } 167 } 168 169 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 count++; 188 } 189 } 190 return count; 191 } catch (DebugException e) { 192 } 193 return -1; 194 } 195 196 197 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 215 protected boolean handlesEvent(DebugEvent event) { 216 if (super.handlesEvent(event)) { 217 Object 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")) { return false; 228 } 229 } catch (DebugException e) { 230 } 231 } 232 } 233 } 234 } 235 } else { 236 return false; 237 } 238 return true; 239 } 240 241 244 public void treeCollapsed(TreeEvent e) { 245 Widget widget = e.item; 247 if (widget instanceof TreeItem) { 248 TreeItem item = (TreeItem) widget; 249 Object data = item.getData(); 250 if (data instanceof IJavaThread) { 251 removeQueuedThread((IJavaThread)data); 252 } 253 } 254 } 255 256 259 public void treeExpanded(TreeEvent e) { 260 Widget widget = e.item; 262 if (widget instanceof TreeItem) { 263 TreeItem item = (TreeItem) widget; 264 Object data = item.getData(); 265 if (data instanceof IJavaThread) { 266 queueSuspendedThread((IJavaThread)data); 267 } 268 } 269 } 270 271 } 272 | Popular Tags |