KickJava   Java API By Example, From Geeks To Geeks.

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


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

11 package org.eclipse.jdt.internal.debug.ui.monitors;
12
13
14 import org.eclipse.jdt.debug.core.IJavaObject;
15 import org.eclipse.jdt.debug.core.IJavaThread;
16 import org.eclipse.jface.viewers.ITreeContentProvider;
17 import org.eclipse.jface.viewers.TreeViewer;
18 import org.eclipse.jface.viewers.Viewer;
19
20 /**
21  * Provides the tree data for the thread view
22  */

23 public class ThreadsViewContentProvider implements ITreeContentProvider {
24
25     /**
26      * MonitorWrapper for the thread view
27      * We use it to know the state of the monitor we display: owned or in contention
28      */

29     public class MonitorWrapper{
30         public static final int OWNED_MONITOR = 1;
31         public static final int CONTENDED_MONITOR = 2;
32         public IJavaObject monitor;
33         public int state;
34     }
35
36     /**
37      * ThreadWrapper for the monitor view
38      * We use it to know the state of the thread we display: caught in a deadlock or not
39      */

40     public class ThreadWrapper{
41         public IJavaThread thread;
42         public boolean isCaughtInDeadlock;
43     }
44         
45     protected TreeViewer fViewer= null;
46
47     /**
48      * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(Object)
49      */

50     public Object JavaDoc[] getChildren(Object JavaDoc parentElement) {
51         
52         //the parent will be ThreadWrapper
53
if (parentElement instanceof ThreadWrapper) {
54             IJavaThread thread= ((ThreadWrapper)parentElement).thread;
55             
56             //owned monitors
57
IJavaObject[] ownedMonitors= MonitorManager.getDefault().getOwnedMonitors(thread);
58             
59             //contended monitor
60
IJavaObject contendedMonitor= MonitorManager.getDefault().getContendedMonitor(thread);
61             if (ownedMonitors == null && contendedMonitor == null) {
62                 return null;
63             }
64             
65             //adding the monitors to the result
66
int size= 0;
67             if (ownedMonitors != null) {
68                 size= ownedMonitors.length;
69             }
70             if (contendedMonitor != null) {
71                 size= size + 1;
72             }
73             //transforming the result to MonitorWrapper, setting the type
74
Object JavaDoc[] children= new Object JavaDoc[size];
75             if (ownedMonitors != null) {
76                 for (int i = 0; i < ownedMonitors.length; i++) {
77                     MonitorWrapper mw = new MonitorWrapper();
78                     mw.monitor = ownedMonitors[i];
79                     mw.state = MonitorWrapper.OWNED_MONITOR;
80                     children[i]= mw;
81                 }
82             }
83             if (contendedMonitor != null) {
84                 MonitorWrapper mw = new MonitorWrapper();
85                 mw.monitor = contendedMonitor;
86                 mw.state = MonitorWrapper.CONTENDED_MONITOR;
87                 children[children.length - 1]= mw;
88             }
89             return children;
90         }
91                 
92         return null;
93     }
94
95     /**
96      * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(Object)
97      */

98     public Object JavaDoc getParent(Object JavaDoc element) {
99         
100         if (element instanceof IJavaThread) {
101             return MonitorManager.getDefault().getOwnedMonitors((IJavaThread)element);
102         } else if (element instanceof IJavaObject) {
103             return MonitorManager.getDefault().getOwningThread((IJavaObject)element);
104         }
105         return null;
106     }
107
108     /**
109      * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(Object)
110      */

111     public boolean hasChildren(Object JavaDoc element) {
112             
113         if (element instanceof IJavaThread) {
114             IJavaThread thread= (IJavaThread)element;
115             IJavaObject[] ownedMonitors= MonitorManager.getDefault().getOwnedMonitors(thread);
116             IJavaObject contendedMonitor= MonitorManager.getDefault().getContendedMonitor(thread);
117             if (ownedMonitors == null && contendedMonitor == null) {
118                 return false;
119             } else {
120                 return true;
121             }
122         }
123         return false;
124     }
125
126     /**
127      * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(Object)
128      */

129     public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
130         
131         //the root elements are ThreadWrapper
132
IJavaThread[] allThreads= MonitorManager.getDefault().getThreads();
133         Object JavaDoc[] res = new Object JavaDoc[allThreads.length];
134         for (int i = 0; i < allThreads.length; i++) {
135             ThreadWrapper tw = new ThreadWrapper();
136             tw.thread= allThreads[i];
137             if(MonitorManager.getDefault().isCaughtInDeadlock(allThreads[i])) {
138                 tw.isCaughtInDeadlock = true;
139             } else {
140                 tw.isCaughtInDeadlock = false;
141             }
142             res[i] = tw;
143         }
144         return res;
145     }
146
147     /**
148      * @see org.eclipse.jface.viewers.IContentProvider#dispose()
149      */

150     public void dispose() {
151         fViewer= null;
152     }
153
154     /**
155      * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(Viewer, Object, Object)
156      */

157     public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
158         fViewer= (TreeViewer)viewer;
159     }
160 }
161
Popular Tags