KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > core > WatchExpression


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.debug.internal.core;
12
13 import org.eclipse.core.runtime.Platform;
14 import org.eclipse.debug.core.DebugEvent;
15 import org.eclipse.debug.core.DebugException;
16 import org.eclipse.debug.core.DebugPlugin;
17 import org.eclipse.debug.core.ILaunch;
18 import org.eclipse.debug.core.ILaunchConfiguration;
19 import org.eclipse.debug.core.model.IDebugElement;
20 import org.eclipse.debug.core.model.IDebugTarget;
21 import org.eclipse.debug.core.model.IValue;
22 import org.eclipse.debug.core.model.IWatchExpression;
23 import org.eclipse.debug.core.model.IWatchExpressionDelegate;
24 import org.eclipse.debug.core.model.IWatchExpressionListener;
25 import org.eclipse.debug.core.model.IWatchExpressionResult;
26
27 /**
28  * Base watch expression implementation.
29  *
30  * @since 3.0
31  */

32 public class WatchExpression implements IWatchExpression {
33     
34     protected String JavaDoc fExpressionText;
35     protected IWatchExpressionResult fResult;
36     protected IDebugElement fCurrentContext;
37     private boolean fEnabled= true;
38     private boolean fPending= false;
39     
40     /**
41      * Creates a new watch expression with the given expression
42      * text.
43      * @param expression the text of the expression to be evaluated.
44      */

45     public WatchExpression(String JavaDoc expression) {
46         fExpressionText= expression;
47     }
48
49     /**
50      * Creates a new watch expression with the given expression
51      * and the given enablement;
52      *
53      * @param expressionText the text of the expression to be evaluated
54      * @param enabled whether or not the new expression should be enabled
55      */

56     public WatchExpression(String JavaDoc expressionText, boolean enabled) {
57         this(expressionText);
58         fEnabled= enabled;
59     }
60     
61     /**
62      * @see org.eclipse.debug.core.model.IWatchExpression#evaluate()
63      */

64     public void evaluate() {
65         IDebugElement context= fCurrentContext;
66         if (context == null) {
67             return;
68         }
69             
70         IWatchExpressionListener listener= new IWatchExpressionListener() {
71             /* (non-Javadoc)
72              * @see org.eclipse.debug.core.model.IWatchExpressionListener#watchEvaluationFinished(org.eclipse.debug.core.model.IWatchExpressionResult)
73              */

74             public void watchEvaluationFinished(IWatchExpressionResult result) {
75                 setPending(false);
76                 setResult(result);
77             }
78         };
79         setPending(true);
80         IWatchExpressionDelegate delegate= DebugPlugin.getDefault().getExpressionManager().newWatchExpressionDelegate(context.getModelIdentifier());
81         if (delegate != null) {
82             delegate.evaluateExpression(getExpressionText(), context, listener);
83         } else {
84             // No delegate provided
85
listener.watchEvaluationFinished(new IWatchExpressionResult() {
86                 public IValue getValue() {
87                     return null;
88                 }
89                 public boolean hasErrors() {
90                     return true;
91                 }
92                 public String JavaDoc[] getErrorMessages() {
93                     return new String JavaDoc[] { DebugCoreMessages.WatchExpression_0 };
94                 }
95                 public String JavaDoc getExpressionText() {
96                     return WatchExpression.this.getExpressionText();
97                 }
98                 public DebugException getException() {
99                     return null;
100                 }
101             });
102         }
103     }
104
105     /* (non-Javadoc)
106      * @see org.eclipse.debug.core.model.IWatchExpression#setExpressionContext(org.eclipse.debug.core.model.IDebugElement)
107      */

108     public void setExpressionContext(IDebugElement context) {
109         fCurrentContext= context;
110         if (context == null) {
111             setResult(null);
112             return;
113         }
114         if (!isEnabled()) {
115             return;
116         }
117         
118         evaluate();
119     }
120
121     /**
122      * Sets the result of the last expression and fires notification that
123      * this expression's value has changed.
124      *
125      * @param result result of a watch expression
126      */

127     public void setResult(IWatchExpressionResult result) {
128         fResult= result;
129         fireEvent(new DebugEvent(this, DebugEvent.CHANGE, DebugEvent.CONTENT));
130     }
131
132     /**
133      * Fires the given debug event
134      * @param event
135      */

136     protected void fireEvent(DebugEvent event) {
137         DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[] {event});
138     }
139     
140     /**
141      * Notifies the expression manager that this watch expression's
142      * values have changed so the manager can update the
143      * persisted expression.
144      *
145      * @param persist whether to persist the expression
146      */

147     private void watchExpressionChanged(boolean persist) {
148         ((ExpressionManager)DebugPlugin.getDefault().getExpressionManager()).watchExpressionChanged(this, persist);
149     }
150
151     /**
152      * @see org.eclipse.debug.core.model.IExpression#getExpressionText()
153      */

154     public String JavaDoc getExpressionText() {
155         return fExpressionText;
156     }
157
158     /**
159      * @see org.eclipse.debug.core.model.IExpression#getValue()
160      */

161     public IValue getValue() {
162         if (fResult == null) {
163             return null;
164         }
165         return fResult.getValue();
166     }
167
168     /**
169      * @see org.eclipse.debug.core.model.IDebugElement#getDebugTarget()
170      */

171     public IDebugTarget getDebugTarget() {
172         IDebugElement element = fCurrentContext;
173         if (element != null) {
174             return element.getDebugTarget();
175         }
176         return null;
177     }
178
179     /**
180      * @see org.eclipse.debug.core.model.IExpression#dispose()
181      */

182     public void dispose() {
183     }
184
185     /**
186      * @see org.eclipse.debug.core.model.IDebugElement#getModelIdentifier()
187      */

188     public String JavaDoc getModelIdentifier() {
189         if (fCurrentContext != null) {
190             return fCurrentContext.getModelIdentifier();
191         }
192         return DebugPlugin.getUniqueIdentifier();
193     }
194
195     /**
196      * @see org.eclipse.debug.core.model.IDebugElement#getLaunch()
197      */

198     public ILaunch getLaunch() {
199         IDebugTarget debugTarget = getDebugTarget();
200         if (debugTarget != null) {
201             return debugTarget.getLaunch();
202         }
203         return null;
204     }
205
206     /**
207      * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
208      */

209     public Object JavaDoc getAdapter(Class JavaDoc adapter) {
210         //CONTEXTLAUNCHING
211
if(adapter.equals(ILaunchConfiguration.class)) {
212             ILaunch launch = getLaunch();
213             if(launch != null) {
214                 return launch.getLaunchConfiguration();
215             }
216         }
217         return Platform.getAdapterManager().getAdapter(this, adapter);
218     }
219
220     /**
221      * @param enabled
222      */

223     public void setEnabled(boolean enabled) {
224         fEnabled= enabled;
225         watchExpressionChanged(true);
226         evaluate();
227     }
228
229     /**
230      * @param expression
231      */

232     public void setExpressionText(String JavaDoc expression) {
233         fExpressionText= expression;
234         watchExpressionChanged(true);
235         evaluate();
236     }
237
238     /**
239      * @return Whether or not this watch expression is currently enabled.
240      * Enabled watch expressions will continue to update their value
241      * automatically. Disabled expressions require a manual update.
242      */

243     public boolean isEnabled() {
244         return fEnabled;
245     }
246
247     /**
248      * @see org.eclipse.debug.core.model.IWatchExpression#isPending()
249      */

250     public boolean isPending() {
251         return fPending;
252     }
253     
254     /**
255      * Sets the pending state of this expression.
256      *
257      * @param pending whether or not this expression should be
258      * flagged as pending
259      */

260     protected void setPending(boolean pending) {
261         fPending= pending;
262         fireEvent(new DebugEvent(this, DebugEvent.CHANGE, DebugEvent.STATE));
263     }
264
265     /* (non-Javadoc)
266      * @see org.eclipse.debug.core.model.IErrorReportingExpression#hasErrors()
267      */

268     public boolean hasErrors() {
269         return fResult != null && fResult.hasErrors();
270     }
271     
272     /* (non-Javadoc)
273      * @see org.eclipse.debug.core.model.IErrorReportingExpression#getErrorMessages()
274      */

275     public String JavaDoc[] getErrorMessages() {
276         if (fResult == null) {
277             return new String JavaDoc[0];
278         }
279         return fResult.getErrorMessages();
280     }
281
282 }
283
Popular Tags