KickJava   Java API By Example, From Geeks To Geeks.

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


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

26 public class MonitorsViewContentProvider implements ITreeContentProvider {
27     
28     protected TreeViewer fViewer= null;
29         
30     /**
31      * ThreadWrapper for the monitor view
32      * We use it to know the state of the thread we display: owning or waiting
33      */

34     public class ThreadWrapper{
35         public static final int OWNING_THREAD = 1;
36         public static final int IN_CONTENTION_FOR_MONITOR = 2;
37         public IJavaThread thread;
38         public int state;
39     }
40
41     /**
42      * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(Object)
43      */

44     public Object JavaDoc[] getChildren(Object JavaDoc parentElement) {
45
46         //the parents will be monitors
47
if (parentElement instanceof IJavaObject) {
48             IJavaObject monitor= (IJavaObject)parentElement;
49             //owning thread
50
IJavaThread owningThread = MonitorManager.getDefault().getOwningThread(monitor);
51             
52             //contending threads
53
List JavaDoc contendingThreads = MonitorManager.getDefault().getContendingThreads(monitor);
54             if (owningThread == null && contendingThreads == null) {
55                 return null;
56             }
57             
58             //adding the threads to the result
59
int size= 0;
60             if (contendingThreads != null) {
61                 size= contendingThreads.size();
62             }
63             if (owningThread != null) {
64                 size= size + 1;
65             }
66             //transforming the result to ThreadWrapper, setting the type
67
Object JavaDoc[] children= new Object JavaDoc[size];
68             if (contendingThreads != null) {
69                 List JavaDoc wrappedThreads = new ArrayList JavaDoc();
70                 for (int i = 0; i < contendingThreads.size(); i++) {
71                     ThreadWrapper tw = new ThreadWrapper();
72                     tw.thread = (IJavaThread) contendingThreads.get(i);
73                     tw.state = ThreadWrapper.IN_CONTENTION_FOR_MONITOR;
74                     wrappedThreads.add(tw);
75                 }
76                 wrappedThreads.toArray(children);
77                 wrappedThreads.toArray(children);
78             }
79             if (owningThread != null) {
80                 ThreadWrapper tw = new ThreadWrapper();
81                 tw.thread = owningThread;
82                 tw.state = ThreadWrapper.OWNING_THREAD;
83                 children[children.length - 1] = tw;
84             }
85             return children;
86         }
87                 
88         return null;
89     }
90
91     /**
92      * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(Object)
93      */

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

107     public boolean hasChildren(Object JavaDoc element) {
108         
109         if (element instanceof IJavaObject) {
110             IJavaObject monitor= (IJavaObject)element;
111             IJavaThread owningThread = MonitorManager.getDefault().getOwningThread(monitor);
112             List JavaDoc contendingThreads = MonitorManager.getDefault().getContendingThreads(monitor);
113             if (owningThread == null && contendingThreads == null) {
114                 return false;
115             } else {
116                 return true;
117             }
118         }
119         return false;
120     }
121
122     /**
123      * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(Object)
124      */

125     public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
126         return MonitorManager.getDefault().getMonitors();
127     }
128
129     /**
130      * @see org.eclipse.jface.viewers.IContentProvider#dispose()
131      */

132     public void dispose() {
133         fViewer= null;
134     }
135
136     /**
137      * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(Viewer, Object, Object)
138      */

139     public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
140         fViewer= (TreeViewer)viewer;
141     }
142 }
143
Popular Tags