KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > monitors > JavaThreadContentProvider


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.monitors;
12
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.core.runtime.jobs.ISchedulingRule;
15 import org.eclipse.debug.core.DebugException;
16 import org.eclipse.debug.core.model.IDebugElement;
17 import org.eclipse.debug.core.model.IStackFrame;
18 import org.eclipse.debug.internal.ui.viewers.model.provisional.IChildrenCountUpdate;
19 import org.eclipse.debug.internal.ui.viewers.model.provisional.IChildrenUpdate;
20 import org.eclipse.debug.internal.ui.viewers.model.provisional.IHasChildrenUpdate;
21 import org.eclipse.debug.internal.ui.viewers.model.provisional.IPresentationContext;
22 import org.eclipse.debug.internal.ui.viewers.model.provisional.IViewerUpdate;
23 import org.eclipse.jdt.debug.core.IJavaDebugTarget;
24 import org.eclipse.jdt.debug.core.IJavaThread;
25 import org.eclipse.jdt.debug.ui.JavaDebugUtils;
26 import org.eclipse.jdt.internal.debug.core.model.JDIThread;
27
28 /**
29  * Java thread presentation adapter.
30  *
31  * @since 3.3
32  */

33 public class JavaThreadContentProvider extends JavaElementContentProvider {
34
35     /* (non-Javadoc)
36      * @see org.eclipse.debug.internal.ui.viewers.model.provisional.elements.ElementContentProvider#getChildCount(java.lang.Object, org.eclipse.debug.internal.ui.viewers.provisional.IPresentationContext)
37      */

38     protected int getChildCount(Object JavaDoc element, IPresentationContext context, IViewerUpdate monitor) throws CoreException {
39         IJavaThread thread = (IJavaThread)element;
40         if (!thread.isSuspended()) {
41             return 0;
42         }
43         int childCount = thread.getFrameCount();
44         if (isDisplayMonitors()) {
45             if (((IJavaDebugTarget) thread.getDebugTarget()).supportsMonitorInformation()) {
46                 childCount+= thread.getOwnedMonitors().length;
47                 if (thread.getContendedMonitor() != null) {
48                     childCount++;
49                 }
50             } else {
51                 // unavailable notice
52
childCount++;
53             }
54         }
55         return childCount;
56     }
57
58     /* (non-Javadoc)
59      * @see org.eclipse.debug.internal.ui.model.elements.ElementContentProvider#getChildren(java.lang.Object, int, int, org.eclipse.debug.internal.ui.viewers.model.provisional.IPresentationContext, org.eclipse.core.runtime.IProgressMonitor)
60      */

61     protected Object JavaDoc[] getChildren(Object JavaDoc parent, int index, int length, IPresentationContext context, IViewerUpdate monitor) throws CoreException {
62         IJavaThread thread = (IJavaThread)parent;
63         if (!thread.isSuspended()) {
64             return EMPTY;
65         }
66         return getElements(getChildren(thread), index, length);
67     }
68     
69     protected Object JavaDoc[] getChildren(IJavaThread thread) {
70         try {
71             IStackFrame[] frames = thread.getStackFrames();
72             if (!isDisplayMonitors()) {
73                 return frames;
74             }
75
76             Object JavaDoc[] children;
77             int length = frames.length;
78             if (((IJavaDebugTarget) thread.getDebugTarget()).supportsMonitorInformation()) {
79                 IDebugElement[] ownedMonitors = JavaDebugUtils.getOwnedMonitors(thread);
80                 IDebugElement contendedMonitor = JavaDebugUtils.getContendedMonitor(thread);
81
82                 if (ownedMonitors != null) {
83                     length += ownedMonitors.length;
84                 }
85                 if (contendedMonitor != null) {
86                     length++;
87                 }
88                 children = new Object JavaDoc[length];
89                 if (ownedMonitors != null && ownedMonitors.length > 0) {
90                     System.arraycopy(ownedMonitors, 0, children, 0, ownedMonitors.length);
91                 }
92                 if (contendedMonitor != null) {
93                     // Insert the contended monitor after the owned monitors
94
children[ownedMonitors.length] = contendedMonitor;
95                 }
96             } else {
97                 children = new Object JavaDoc[length + 1];
98                 children[0] = new NoMonitorInformationElement(thread.getDebugTarget());
99             }
100             int offset = children.length - frames.length;
101             System.arraycopy(frames, 0, children, offset, frames.length);
102             return children;
103         } catch (DebugException e) {
104             return EMPTY;
105         }
106     }
107
108     /* (non-Javadoc)
109      * @see org.eclipse.debug.internal.ui.model.elements.ElementContentProvider#hasChildren(java.lang.Object, org.eclipse.debug.internal.ui.viewers.model.provisional.IPresentationContext, org.eclipse.core.runtime.IProgressMonitor)
110      */

111     protected boolean hasChildren(Object JavaDoc element, IPresentationContext context, IViewerUpdate monitor) throws CoreException {
112         return ((IJavaThread)element).hasStackFrames() ||
113             (isDisplayMonitors() && ((IJavaThread)element).hasOwnedMonitors());
114     }
115
116     /* (non-Javadoc)
117      * @see org.eclipse.debug.internal.ui.model.elements.ElementContentProvider#getRule(org.eclipse.debug.internal.ui.viewers.model.provisional.IChildrenCountUpdate[])
118      */

119     protected ISchedulingRule getRule(IChildrenCountUpdate[] updates) {
120         return getThreadRule(updates);
121     }
122
123     /* (non-Javadoc)
124      * @see org.eclipse.debug.internal.ui.model.elements.ElementContentProvider#getRule(org.eclipse.debug.internal.ui.viewers.model.provisional.IChildrenUpdate[])
125      */

126     protected ISchedulingRule getRule(IChildrenUpdate[] updates) {
127         return getThreadRule(updates);
128     }
129
130     /* (non-Javadoc)
131      * @see org.eclipse.debug.internal.ui.model.elements.ElementContentProvider#getRule(org.eclipse.debug.internal.ui.viewers.model.provisional.IHasChildrenUpdate[])
132      */

133     protected ISchedulingRule getRule(IHasChildrenUpdate[] updates) {
134         return getThreadRule(updates);
135     }
136     
137     /**
138      * Returns a scheduling rule to ensure we aren't trying to get thread content
139      * while executing an implicit evaluation (like toString() for the details
140      * pane).
141      *
142      * @param updates viewer updates
143      * @return scheduling rule or <code>null</code>
144      */

145     private ISchedulingRule getThreadRule(IViewerUpdate[] updates) {
146         if (updates.length > 0) {
147             Object JavaDoc element = updates[0].getElement();
148             if (element instanceof JDIThread) {
149                 return ((JDIThread)element).getThreadRule();
150             }
151         }
152         return null;
153     }
154     
155
156 }
157
Popular Tags