1 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 32 public class WatchExpression implements IWatchExpression { 33 34 protected String fExpressionText; 35 protected IWatchExpressionResult fResult; 36 protected IDebugElement fCurrentContext; 37 private boolean fEnabled= true; 38 private boolean fPending= false; 39 40 45 public WatchExpression(String expression) { 46 fExpressionText= expression; 47 } 48 49 56 public WatchExpression(String expressionText, boolean enabled) { 57 this(expressionText); 58 fEnabled= enabled; 59 } 60 61 64 public void evaluate() { 65 IDebugElement context= fCurrentContext; 66 if (context == null) { 67 return; 68 } 69 70 IWatchExpressionListener listener= new IWatchExpressionListener() { 71 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 listener.watchEvaluationFinished(new IWatchExpressionResult() { 86 public IValue getValue() { 87 return null; 88 } 89 public boolean hasErrors() { 90 return true; 91 } 92 public String [] getErrorMessages() { 93 return new String [] { DebugCoreMessages.WatchExpression_0 }; 94 } 95 public String getExpressionText() { 96 return WatchExpression.this.getExpressionText(); 97 } 98 public DebugException getException() { 99 return null; 100 } 101 }); 102 } 103 } 104 105 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 127 public void setResult(IWatchExpressionResult result) { 128 fResult= result; 129 fireEvent(new DebugEvent(this, DebugEvent.CHANGE, DebugEvent.CONTENT)); 130 } 131 132 136 protected void fireEvent(DebugEvent event) { 137 DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[] {event}); 138 } 139 140 147 private void watchExpressionChanged(boolean persist) { 148 ((ExpressionManager)DebugPlugin.getDefault().getExpressionManager()).watchExpressionChanged(this, persist); 149 } 150 151 154 public String getExpressionText() { 155 return fExpressionText; 156 } 157 158 161 public IValue getValue() { 162 if (fResult == null) { 163 return null; 164 } 165 return fResult.getValue(); 166 } 167 168 171 public IDebugTarget getDebugTarget() { 172 IDebugElement element = fCurrentContext; 173 if (element != null) { 174 return element.getDebugTarget(); 175 } 176 return null; 177 } 178 179 182 public void dispose() { 183 } 184 185 188 public String getModelIdentifier() { 189 if (fCurrentContext != null) { 190 return fCurrentContext.getModelIdentifier(); 191 } 192 return DebugPlugin.getUniqueIdentifier(); 193 } 194 195 198 public ILaunch getLaunch() { 199 IDebugTarget debugTarget = getDebugTarget(); 200 if (debugTarget != null) { 201 return debugTarget.getLaunch(); 202 } 203 return null; 204 } 205 206 209 public Object getAdapter(Class adapter) { 210 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 223 public void setEnabled(boolean enabled) { 224 fEnabled= enabled; 225 watchExpressionChanged(true); 226 evaluate(); 227 } 228 229 232 public void setExpressionText(String expression) { 233 fExpressionText= expression; 234 watchExpressionChanged(true); 235 evaluate(); 236 } 237 238 243 public boolean isEnabled() { 244 return fEnabled; 245 } 246 247 250 public boolean isPending() { 251 return fPending; 252 } 253 254 260 protected void setPending(boolean pending) { 261 fPending= pending; 262 fireEvent(new DebugEvent(this, DebugEvent.CHANGE, DebugEvent.STATE)); 263 } 264 265 268 public boolean hasErrors() { 269 return fResult != null && fResult.hasErrors(); 270 } 271 272 275 public String [] getErrorMessages() { 276 if (fResult == null) { 277 return new String [0]; 278 } 279 return fResult.getErrorMessages(); 280 } 281 282 } 283 | Popular Tags |