KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2004, 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.PlatformObject;
14 import org.eclipse.debug.core.DebugException;
15 import org.eclipse.debug.core.ILaunch;
16 import org.eclipse.debug.core.ILaunchConfiguration;
17 import org.eclipse.debug.core.model.IDebugElement;
18 import org.eclipse.debug.core.model.IDebugTarget;
19 import org.eclipse.debug.core.model.ISuspendResume;
20 import org.eclipse.debug.core.model.ITerminate;
21 import org.eclipse.debug.core.model.IThread;
22
23 /**
24  * Object used to display owned monitor in the debug launch view.
25  * In this case, the monitor is waited by the waiting threads, and owned
26  * by the parent thread.
27  */

28 public class JavaOwnedMonitor extends PlatformObject implements IDebugElement, ITerminate, ISuspendResume {
29     
30     /**
31      * The monitor object in the thread and monitor model.
32      */

33     private JavaMonitor fMonitor;
34     
35     /**
36      * The threads waiting for this monitor.
37      */

38     private JavaWaitingThread[] fWaitingThreads;
39     /**
40      * The parent, in the debug view tree.
41      */

42     private JavaWaitingThread fParent;
43
44     /**
45      * Constructor
46      * @param monitor
47      * @param parent
48      */

49     public JavaOwnedMonitor(JavaMonitor monitor, JavaWaitingThread parent) {
50         fMonitor= monitor;
51         monitor.addElement(this);
52         fParent= parent;
53     }
54     
55     /**
56      * Returns the monitor
57      * @return the monitor
58      */

59     public JavaMonitor getMonitor() {
60         return fMonitor;
61     }
62     
63     /**
64      * Returns the original <code>IThread</code> or the parent thread
65      * @return the original <code>IThread</code> of the parent thread
66      */

67     public Object JavaDoc getParent() {
68         if (fParent.getParent() == null) {
69             return fParent.getThread().getOriginalThread();
70         }
71         return fParent;
72     }
73     
74     /**
75      * Returns an array of all of the threads waiting on this monitor
76      * @return the array of <code>JavaWaitingThread</code>s waiting on this monitor
77      */

78     public JavaWaitingThread[] getWaitingThreads() {
79         JavaMonitorThread[] waitingThreads= fMonitor.getWaitingThreads0();
80         JavaWaitingThread[] tmp= new JavaWaitingThread[waitingThreads.length];
81         if (fWaitingThreads == null) {
82             // the list was empty, creating new objects
83
for (int i= 0; i < waitingThreads.length; i++) {
84                 tmp[i]= new JavaWaitingThread(waitingThreads[i], this);
85             }
86         } else {
87             // trying to reuse the objects from the previous list
88
outer: for (int i= 0; i < waitingThreads.length; i++) {
89                 JavaMonitorThread waitingThread= waitingThreads[i];
90                 for (int j= 0; j < fWaitingThreads.length; j++) {
91                     if (fWaitingThreads[j].getThread() == waitingThread) {
92                         tmp[i]= fWaitingThreads[j];
93                         continue outer;
94                     }
95                 }
96                 tmp[i]= new JavaWaitingThread(waitingThread, this);
97             }
98         }
99         fWaitingThreads = tmp;
100         return fWaitingThreads;
101     }
102     
103     /* (non-Javadoc)
104      * @see org.eclipse.debug.core.model.IDebugElement#getModelIdentifier()
105      */

106     public String JavaDoc getModelIdentifier() {
107         return fMonitor.getModelIdentifier();
108     }
109
110     /* (non-Javadoc)
111      * @see org.eclipse.debug.core.model.IDebugElement#getDebugTarget()
112      */

113     public IDebugTarget getDebugTarget() {
114         return fMonitor.getDebugTarget();
115     }
116
117     /* (non-Javadoc)
118      * @see org.eclipse.debug.core.model.IDebugElement#getLaunch()
119      */

120     public ILaunch getLaunch() {
121         return fMonitor.getLaunch();
122     }
123
124     /* (non-Javadoc)
125      * @see org.eclipse.core.runtime.PlatformObject#getAdapter(java.lang.Class)
126      */

127     public Object JavaDoc getAdapter(Class JavaDoc adapter) {
128         if(adapter == IDebugTarget.class) {
129             return getDebugTarget();
130         }
131         //CONTEXTLAUNCHING
132
if(adapter.equals(ILaunchConfiguration.class)) {
133             return getLaunch().getLaunchConfiguration();
134         }
135         return super.getAdapter(adapter);
136     }
137
138     /**
139      * returns the parent thread of this monitor
140      * @return the parent <code>IThread</code> that owns this monitor
141      */

142     protected IThread getParentThread() {
143         Object JavaDoc parent = getParent();
144         IThread thread = null;
145         if(parent instanceof IThread) {
146             thread = (IThread) parent;
147         }
148         else if(parent instanceof JavaWaitingThread) {
149             thread = ((JavaWaitingThread)parent).getThread().getOriginalThread();
150         }
151         return thread;
152     }
153     
154     /**
155      * @see org.eclipse.debug.core.model.ITerminate#canTerminate()
156      */

157     public boolean canTerminate() {
158         return getDebugTarget().canTerminate();
159     }
160
161     /**
162      * @see org.eclipse.debug.core.model.ITerminate#isTerminated()
163      */

164     public boolean isTerminated() {
165         return getDebugTarget().isTerminated();
166     }
167
168     /**
169      * @see org.eclipse.debug.core.model.ITerminate#terminate()
170      */

171     public void terminate() throws DebugException {
172         getDebugTarget().terminate();
173     }
174     
175     /**
176      * @see org.eclipse.debug.core.model.ISuspendResume#canResume()
177      */

178     public boolean canResume() {
179         IThread thread = getParentThread();
180         if(thread != null) {
181             return thread.canResume();
182         }
183         return false;
184     }
185
186     /**
187      * @see org.eclipse.debug.core.model.ISuspendResume#canSuspend()
188      */

189     public boolean canSuspend() {
190         IThread thread = getParentThread();
191         if(thread != null) {
192             return thread.canSuspend();
193         }
194         return false;
195     }
196
197     /**
198      * @see org.eclipse.debug.core.model.ISuspendResume#isSuspended()
199      */

200     public boolean isSuspended() {
201         IThread thread = getParentThread();
202         if(thread != null) {
203             return thread.isSuspended();
204         }
205         return false;
206     }
207
208     /**
209      * @see org.eclipse.debug.core.model.ISuspendResume#resume()
210      */

211     public void resume() throws DebugException {
212         IThread thread = getParentThread();
213         if(thread != null) {
214             thread.resume();
215         }
216     }
217
218     /**
219      * @see org.eclipse.debug.core.model.ISuspendResume#suspend()
220      */

221     public void suspend() throws DebugException {
222         IThread thread = getParentThread();
223         if(thread != null) {
224             thread.suspend();
225         }
226     }
227 }
228
Popular Tags