KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.ArrayList JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
18
19 import org.eclipse.jdt.debug.core.IJavaObject;
20 import org.eclipse.jdt.debug.core.IJavaThread;
21 import org.eclipse.jface.viewers.ITreeContentProvider;
22 import org.eclipse.jface.viewers.Viewer;
23
24 /**
25  * Provides the tree data for the deadlock view
26  */

27 public class DeadLocksViewContentProvider implements ITreeContentProvider {
28     
29     Object JavaDoc[] fRoots= null;
30     
31     /**
32      * ThreadWrapper for the deadlock view
33      * Uses the data of ThreadWrapper
34      * We use this to differentiate the same thread in a deadlock list
35      * (the first and the last element in the list)
36      * @see ThreadWrapper
37      */

38     public class ContentThreadWrapper {
39         public IJavaThread fThread;
40         public Object JavaDoc fParent= null;
41         public boolean caughtInADeadLock;
42         public Object JavaDoc fChild= null;
43         
44         protected ContentThreadWrapper(IJavaThread thread, Object JavaDoc parent) {
45             fThread= thread;
46             fParent= parent;
47         }
48         
49         /**
50          * @see java.lang.Object#equals(java.lang.Object)
51          */

52         public boolean equals(Object JavaDoc obj) {
53             if (!(obj instanceof ContentThreadWrapper)) {
54                 return false;
55             }
56             ContentThreadWrapper other= (ContentThreadWrapper)obj;
57             
58             return other.fThread.equals(fThread);
59         }
60
61         /**
62          * @see java.lang.Object#hashCode()
63          */

64         public int hashCode() {
65             return fThread.hashCode();
66         }
67
68     }
69
70     /**
71      * MonitorWrapper for the deadlock view
72      */

73     public class ContentMonitorWrapper {
74         public IJavaObject fMonitor;
75         public Object JavaDoc fParent= null;
76         public Object JavaDoc fChild= null;
77         
78         protected ContentMonitorWrapper(IJavaObject monitor, Object JavaDoc parent) {
79             fMonitor= monitor;
80             fParent= parent;
81         }
82         
83         /**
84          * @see java.lang.Object#equals(java.lang.Object)
85          */

86         public boolean equals(Object JavaDoc obj) {
87             if (!(obj instanceof ContentMonitorWrapper)) {
88                 return false;
89             }
90             ContentMonitorWrapper other= (ContentMonitorWrapper)obj;
91             
92             return other.fMonitor.equals(fMonitor);
93         }
94
95         /**
96          * @see java.lang.Object#hashCode()
97          */

98         public int hashCode() {
99             return fMonitor.hashCode();
100         }
101     }
102     
103     /**
104      * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(Object)
105      */

106     public Object JavaDoc[] getChildren(Object JavaDoc parentElement) {
107         Object JavaDoc object= null;
108         if (parentElement instanceof ContentThreadWrapper) {
109             object= ((ContentThreadWrapper)parentElement).fChild;
110         } else if (parentElement instanceof ContentMonitorWrapper) {
111             object= ((ContentMonitorWrapper)parentElement).fChild;
112         }
113         if (object != null) {
114             return new Object JavaDoc[]{object};
115         }
116         return null;
117     }
118
119     /**
120      * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(Object)
121      */

122     public Object JavaDoc getParent(Object JavaDoc element) {
123         if (element instanceof ContentThreadWrapper) {
124             return ((ContentThreadWrapper)element).fParent;
125         } else if (element instanceof ContentMonitorWrapper) {
126             return ((ContentMonitorWrapper)element).fParent;
127         }
128         
129         return null;
130     }
131
132     /**
133      * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(Object)
134      */

135     public boolean hasChildren(Object JavaDoc element) {
136         
137         if (element instanceof ContentThreadWrapper) {
138             return ((ContentThreadWrapper)element).fChild != null;
139         } else if (element instanceof ContentMonitorWrapper) {
140             return ((ContentMonitorWrapper)element).fChild != null;
141         }
142         
143         return false;
144     }
145
146     /**
147      * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(Object)
148      */

149     public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
150         if (fRoots == null) {
151             MonitorManager manager= (MonitorManager) inputElement;
152             int numDeadLocks= manager.getNumberOfDeadlocks();
153             //the list of roots elements
154
fRoots= new Object JavaDoc[numDeadLocks];
155             
156             for (int i = 0; i < numDeadLocks; i++) {
157                 //all the root elements are ContentThreadWrapper
158
ContentThreadWrapper rootWrapper = new ContentThreadWrapper(manager.getStartThread(i), null);
159                 List JavaDoc deadlockList = manager.getDeadlockList(i);
160                 Map JavaDoc tree= new HashMap JavaDoc(deadlockList.size());
161                 tree.put(rootWrapper, rootWrapper);
162                 buildDeadlockTree(rootWrapper, tree, rootWrapper, deadlockList);
163                 fRoots[i] = rootWrapper;
164             }
165         }
166         return fRoots;
167     }
168
169     protected void buildDeadlockTree(ContentThreadWrapper ctw, Map JavaDoc tree, Object JavaDoc parent, List JavaDoc deadlockList) {
170         Object JavaDoc next;
171         Object JavaDoc object;
172         Object JavaDoc inTree;
173         List JavaDoc childFinder= new ArrayList JavaDoc(deadlockList.size());
174         for (int j= 1; j < deadlockList.size(); j++) {
175             next= deadlockList.get(j);
176             
177             if (next instanceof IJavaObject) {
178                 object= new ContentMonitorWrapper((IJavaObject)next, parent);
179             } else {
180                 object= new ContentThreadWrapper((IJavaThread)next, parent);
181             }
182             if (j == 1) {
183                 ctw.fChild= object;
184             }
185             inTree= tree.get(object);
186             if (inTree instanceof ContentThreadWrapper) {
187                 ((ContentThreadWrapper)inTree).caughtInADeadLock= true;
188                 ((ContentThreadWrapper)object).caughtInADeadLock= true;
189             } else if (inTree == null){
190                 tree.put(object, object);
191             }
192             parent= object;
193             childFinder.add(object);
194         }
195         
196         for (int j = 0; j < childFinder.size() - 1; j++) {
197             Object JavaDoc element = childFinder.get(j);
198             if (element instanceof ContentMonitorWrapper) {
199                 ((ContentMonitorWrapper)element).fChild= childFinder.get(j+1);
200             } else {
201                 ((ContentThreadWrapper)element).fChild= childFinder.get(j+1);
202             }
203         }
204     }
205
206     /**
207      * @see org.eclipse.jface.viewers.IContentProvider#dispose()
208      */

209     public void dispose() {
210         MonitorManager.getDefault().removeDeadlockUpdateListener();
211     }
212
213     /**
214      * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(Viewer, Object, Object)
215      */

216     public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
217         MonitorManager.getDefault().addDeadlockUpdateListener(this);
218     }
219     
220     protected void clearDeadlockInformation() {
221         fRoots= null;
222     }
223 }
224
Popular Tags