KickJava   Java API By Example, From Geeks To Geeks.

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


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 contended monitor in the debug launch view.
25  * In this case, the monitor is owned by the owning thread, and waited
26  * by the parent thread.
27  */

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

33     private JavaMonitor fMonitor;
34     /**
35      * The thread which owns this monitor.
36      */

37     private JavaOwningThread fOwningThread;
38     /**
39      * The parent, in the debug view tree.
40      */

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

48     public JavaContendedMonitor(JavaMonitor monitor, JavaOwningThread parent) {
49         fMonitor= monitor;
50         monitor.addElement(this);
51         fParent= parent;
52     }
53     
54     /**
55      * returns the monitor that is in contention
56      * @return the monitor that is in contention
57      */

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

66     public Object JavaDoc getParent() {
67         if (fParent.getParent() == null) {
68             return fParent.getThread().getOriginalThread();
69         }
70         return fParent;
71     }
72     
73     /**
74      * returns the <code>JavaOwningThread</code> that owns this monitor
75      * @return the <code>JavaOwningThread</code> that owns this monitor
76      */

77     public JavaOwningThread getOwningThread() {
78         JavaMonitorThread owningThread= fMonitor.getOwningThread0();
79         if (owningThread == null) {
80             fOwningThread= null;
81         } else if (fOwningThread == null || fOwningThread.getThread() != owningThread) {
82             // create a new object only if thread from the model changed
83
fOwningThread= new JavaOwningThread(owningThread, this);
84         }
85         return fOwningThread;
86     }
87
88     /* (non-Javadoc)
89      * @see org.eclipse.debug.core.model.IDebugElement#getModelIdentifier()
90      */

91     public String JavaDoc getModelIdentifier() {
92         return fMonitor.getModelIdentifier();
93     }
94
95     /* (non-Javadoc)
96      * @see org.eclipse.debug.core.model.IDebugElement#getDebugTarget()
97      */

98     public IDebugTarget getDebugTarget() {
99         return fMonitor.getDebugTarget();
100     }
101
102     /* (non-Javadoc)
103      * @see org.eclipse.debug.core.model.IDebugElement#getLaunch()
104      */

105     public ILaunch getLaunch() {
106         return fMonitor.getLaunch();
107     }
108
109     /* (non-Javadoc)
110      * @see org.eclipse.core.runtime.PlatformObject#getAdapter(java.lang.Class)
111      */

112     public Object JavaDoc getAdapter(Class JavaDoc adapter) {
113         if(adapter == IDebugTarget.class) {
114             return getDebugTarget();
115         }
116         //CONTEXTLAUNCHING
117
if(adapter.equals(ILaunchConfiguration.class)) {
118             return getLaunch().getLaunchConfiguration();
119         }
120         return super.getAdapter(adapter);
121     }
122
123     /**
124      * returns the parent thread of this monitor
125      * @return the parent <code>IThread</code> that owns this monitor
126      */

127     protected IThread getParentThread() {
128         Object JavaDoc parent = getParent();
129         IThread thread = null;
130         if(parent instanceof IThread) {
131             thread = (IThread) parent;
132         }
133         else if(parent instanceof JavaOwningThread) {
134             thread = ((JavaOwningThread)parent).getThread().getOriginalThread();
135         }
136         return thread;
137     }
138     
139     /**
140      * @see org.eclipse.debug.core.model.ITerminate#canTerminate()
141      */

142     public boolean canTerminate() {
143         return getDebugTarget().canTerminate();
144     }
145
146     /**
147      * @see org.eclipse.debug.core.model.ITerminate#isTerminated()
148      */

149     public boolean isTerminated() {
150         return getDebugTarget().isTerminated();
151     }
152
153     /**
154      * @see org.eclipse.debug.core.model.ITerminate#terminate()
155      */

156     public void terminate() throws DebugException {
157         getDebugTarget().terminate();
158     }
159
160     /**
161      * @see org.eclipse.debug.core.model.ISuspendResume#canResume()
162      */

163     public boolean canResume() {
164         if(getOwningThread() != null) {
165             return getOwningThread().getThread().getOriginalThread().canResume();
166         }
167         return false;
168     }
169
170     /**
171      * @see org.eclipse.debug.core.model.ISuspendResume#canSuspend()
172      */

173     public boolean canSuspend() {
174         return false;
175     }
176
177     /**
178      * @see org.eclipse.debug.core.model.ISuspendResume#isSuspended()
179      */

180     public boolean isSuspended() {
181         if(getOwningThread() != null) {
182             return getOwningThread().getThread().getOriginalThread().isSuspended();
183         }
184         return false;
185     }
186
187     /**
188      * @see org.eclipse.debug.core.model.ISuspendResume#resume()
189      */

190     public void resume() throws DebugException {
191         getOwningThread().getThread().getOriginalThread().resume();
192     }
193
194     /**
195      * @see org.eclipse.debug.core.model.ISuspendResume#suspend()
196      */

197     public void suspend() throws DebugException {
198         getOwningThread().getThread().getOriginalThread().suspend();
199     }
200 }
201
Popular Tags