KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > actions > JavaObjectValueEditor


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 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 implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.debug.ui.actions;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.core.runtime.IAdaptable;
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.core.runtime.IStatus;
19 import org.eclipse.core.runtime.Status;
20 import org.eclipse.debug.core.DebugEvent;
21 import org.eclipse.debug.core.DebugException;
22 import org.eclipse.debug.core.ILaunch;
23 import org.eclipse.debug.core.model.ISourceLocator;
24 import org.eclipse.debug.core.model.IValue;
25 import org.eclipse.debug.core.model.IVariable;
26 import org.eclipse.debug.ui.DebugUITools;
27 import org.eclipse.debug.ui.IDebugUIConstants;
28 import org.eclipse.debug.ui.actions.IVariableValueEditor;
29 import org.eclipse.jdt.core.IJavaElement;
30 import org.eclipse.jdt.core.IJavaProject;
31 import org.eclipse.jdt.debug.core.IJavaDebugTarget;
32 import org.eclipse.jdt.debug.core.IJavaStackFrame;
33 import org.eclipse.jdt.debug.core.IJavaThread;
34 import org.eclipse.jdt.debug.core.IJavaVariable;
35 import org.eclipse.jdt.debug.eval.IAstEvaluationEngine;
36 import org.eclipse.jdt.debug.eval.IEvaluationListener;
37 import org.eclipse.jdt.debug.eval.IEvaluationResult;
38 import org.eclipse.jdt.internal.debug.core.JDIDebugPlugin;
39 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
40 import org.eclipse.jface.operation.IRunnableWithProgress;
41 import org.eclipse.jface.window.Window;
42 import org.eclipse.swt.widgets.Shell;
43 import org.eclipse.ui.PlatformUI;
44 import org.eclipse.ui.progress.IProgressService;
45
46 import com.ibm.icu.text.MessageFormat;
47 import com.sun.jdi.InvalidTypeException;
48 import com.sun.jdi.InvocationException;
49 import com.sun.jdi.ObjectReference;
50
51 /**
52  * A variable value editor which prompts the user to enter an expression
53  * for evaluation. The result of the evaluation is assigned to the variable.
54  */

55 public class JavaObjectValueEditor implements IVariableValueEditor {
56
57     /* (non-Javadoc)
58      * @see org.eclipse.debug.ui.actions.IVariableValueEditor#editVariable(org.eclipse.debug.core.model.IVariable, org.eclipse.swt.widgets.Shell)
59      */

60     public boolean editVariable(IVariable variable, Shell shell) {
61         try {
62             IJavaVariable javaVariable = (IJavaVariable) variable;
63             String JavaDoc signature = javaVariable.getSignature();
64             if ("Ljava/lang/String;".equals(signature)) { //$NON-NLS-1$
65
StringValueInputDialog dialog= new StringValueInputDialog(shell, javaVariable);
66                 if (dialog.open() == Window.OK) {
67                     String JavaDoc result = dialog.getResult();
68                     if (dialog.isUseLiteralValue()) {
69                         variable.setValue(result);
70                     } else {
71                         setValue(variable, result);
72                     }
73                 }
74             } else {
75                 ExpressionInputDialog dialog= new ExpressionInputDialog(shell, javaVariable);
76                 if (dialog.open() == Window.OK) {
77                     String JavaDoc result = dialog.getResult();
78                     setValue(variable, result);
79                 }
80             }
81         } catch (DebugException e) {
82             handleException(e);
83         }
84         return true;
85     }
86
87     /* (non-Javadoc)
88      * @see org.eclipse.debug.ui.actions.IVariableValueEditor#saveVariable(org.eclipse.debug.core.model.IVariable, java.lang.String, org.eclipse.swt.widgets.Shell)
89      */

90     public boolean saveVariable(IVariable variable, String JavaDoc expression, Shell shell) {
91         IJavaVariable javaVariable = (IJavaVariable) variable;
92         String JavaDoc signature= null;
93         try {
94             signature = javaVariable.getSignature();
95             if ("Ljava/lang/String;".equals(signature)) { //$NON-NLS-1$
96
return false;
97             }
98             setValue(variable, expression);
99         } catch (DebugException e) {
100             handleException(e);
101         }
102         return true;
103     }
104
105     /**
106      * Evaluates the given expression and sets the given variable's value
107      * using the result.
108      *
109      * @param variable the variable whose value should be set
110      * @param expression the expression to evaluate
111      * @throws DebugException if an exception occurs evaluating the expression
112      * or setting the variable's value
113      */

114     protected void setValue(final IVariable variable, final String JavaDoc expression) throws DebugException {
115         IProgressService service= PlatformUI.getWorkbench().getProgressService();
116         
117         IRunnableWithProgress runnable = new IRunnableWithProgress() {
118             public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc {
119                 try {
120                 IValue newValue = evaluate(expression);
121                 if (newValue != null) {
122                     variable.setValue(newValue);
123                 }
124                 } catch (DebugException de) {
125                     throw new InvocationTargetException JavaDoc(de);
126                 }
127             }
128         };
129         
130         try {
131             service.busyCursorWhile(runnable);
132         } catch (InvocationTargetException JavaDoc e) {
133             if (e.getTargetException() instanceof DebugException) {
134                 throw (DebugException)e.getTargetException();
135             }
136         } catch (InterruptedException JavaDoc e) {
137         }
138     }
139
140     /**
141      * Handles the given exception, which occurred during edit/save.
142      */

143     protected void handleException(DebugException e) {
144         Throwable JavaDoc cause = e.getStatus().getException();
145         if (cause instanceof InvalidTypeException) {
146             IStatus status = new Status(IStatus.ERROR, JDIDebugUIPlugin.getUniqueIdentifier(), IDebugUIConstants.INTERNAL_ERROR, cause.getMessage(), null);
147             JDIDebugUIPlugin.statusDialog(ActionMessages.JavaObjectValueEditor_3, status);
148         } else {
149             JDIDebugUIPlugin.statusDialog(e.getStatus());
150         }
151     }
152
153     /**
154      * Evaluates the given snippet. Reports any errors to the user.
155      * @param stringValue the snippet to evaluate
156      * @return the value that was computed or <code>null</code> if any errors occurred.
157      */

158     private IValue evaluate(String JavaDoc stringValue) throws DebugException {
159         IAdaptable adaptable = DebugUITools.getDebugContext();
160         IJavaStackFrame frame= (IJavaStackFrame) adaptable.getAdapter(IJavaStackFrame.class);
161         if (frame != null) {
162             IJavaThread thread = (IJavaThread) frame.getThread();
163             IJavaProject project= getProject(frame);
164             if (project != null) {
165                 final IEvaluationResult[] results= new IEvaluationResult[1];
166                 IAstEvaluationEngine engine = JDIDebugPlugin.getDefault().getEvaluationEngine(project, (IJavaDebugTarget) thread.getDebugTarget());
167                 IEvaluationListener listener= new IEvaluationListener() {
168                     public void evaluationComplete(IEvaluationResult result) {
169                         synchronized (JavaObjectValueEditor.this) {
170                             results[0]= result;
171                             JavaObjectValueEditor.this.notifyAll();
172                         }
173                     }
174                 };
175                 synchronized(this) {
176                     engine.evaluate(stringValue, frame, listener, DebugEvent.EVALUATION_IMPLICIT, false);
177                     try {
178                         this.wait();
179                     } catch (InterruptedException JavaDoc e) {
180                     }
181                 }
182                 IEvaluationResult result= results[0];
183                 if (result == null) {
184                     return null;
185                 }
186                 if (result.hasErrors()) {
187                     DebugException exception = result.getException();
188                     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
189                     if (exception == null) {
190                         String JavaDoc[] messages = result.getErrorMessages();
191                         for (int i = 0; i < messages.length; i++) {
192                             buffer.append(messages[i]).append("\n "); //$NON-NLS-1$
193
}
194                     } else {
195                         buffer.append(EvaluateAction.getExceptionMessage(exception));
196                     }
197                     IStatus status= new Status(IStatus.ERROR, JDIDebugUIPlugin.getUniqueIdentifier(), IStatus.ERROR, buffer.toString(), null);
198                     throw new DebugException(status);
199                 }
200                 return result.getValue();
201             }
202         }
203         return null;
204     }
205     
206     /**
207      * (copied from EvaluateAction)
208      */

209     protected String JavaDoc getExceptionMessage(Throwable JavaDoc exception) {
210         if (exception instanceof CoreException) {
211             CoreException ce = (CoreException)exception;
212             Throwable JavaDoc throwable= ce.getStatus().getException();
213             if (throwable instanceof com.sun.jdi.InvocationException) {
214                 return getInvocationExceptionMessage((com.sun.jdi.InvocationException)throwable);
215             } else if (throwable instanceof CoreException) {
216                 // Traverse nested CoreExceptions
217
return getExceptionMessage(throwable);
218             }
219             return ce.getStatus().getMessage();
220         }
221         String JavaDoc message= MessageFormat.format(ActionMessages.Evaluate_error_message_direct_exception, new Object JavaDoc[] { exception.getClass() });
222         if (exception.getMessage() != null) {
223             message= MessageFormat.format(ActionMessages.Evaluate_error_message_exception_pattern, new Object JavaDoc[] { message, exception.getMessage() });
224         }
225         return message;
226     }
227
228     /**
229      * Returns a message for the exception wrapped in an invocation exception
230      */

231     protected String JavaDoc getInvocationExceptionMessage(com.sun.jdi.InvocationException exception) {
232             InvocationException ie= exception;
233             ObjectReference ref= ie.exception();
234             return MessageFormat.format(ActionMessages.Evaluate_error_message_wrapped_exception, new Object JavaDoc[] { ref.referenceType().name() });
235     }
236     
237     /**
238      * Return the project associated with the given stack frame.
239      * (copied from JavaWatchExpressionDelegate)
240      */

241     private IJavaProject getProject(IJavaStackFrame javaStackFrame) {
242         ILaunch launch = javaStackFrame.getLaunch();
243         if (launch == null) {
244             return null;
245         }
246         ISourceLocator locator= launch.getSourceLocator();
247         if (locator == null) {
248             return null;
249         }
250
251         Object JavaDoc sourceElement = locator.getSourceElement(javaStackFrame);
252         if (!(sourceElement instanceof IJavaElement) && sourceElement instanceof IAdaptable) {
253             sourceElement = ((IAdaptable)sourceElement).getAdapter(IJavaElement.class);
254         }
255         if (sourceElement instanceof IJavaElement) {
256             return ((IJavaElement) sourceElement).getJavaProject();
257         }
258         return null;
259     }
260 }
261
Popular Tags