KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 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 API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.debug.ui.actions;
12
13
14 import com.ibm.icu.text.MessageFormat;
15
16 import org.eclipse.debug.core.DebugException;
17 import org.eclipse.debug.core.model.IValue;
18 import org.eclipse.debug.ui.DebugUITools;
19 import org.eclipse.debug.ui.IDebugUIConstants;
20 import org.eclipse.debug.ui.IValueDetailListener;
21 import org.eclipse.jdt.debug.core.IJavaType;
22 import org.eclipse.jdt.debug.core.IJavaValue;
23 import org.eclipse.jdt.debug.eval.IEvaluationResult;
24 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
25 import org.eclipse.jdt.internal.debug.ui.display.IDataDisplay;
26 import org.eclipse.jdt.internal.debug.ui.snippeteditor.JavaSnippetEditor;
27 import org.eclipse.swt.widgets.Display;
28 import org.eclipse.ui.IWorkbenchPart;
29
30 /**
31  * Displays the result of an evaluation in the display view
32  */

33 public class DisplayAction extends EvaluateAction {
34
35     /**
36      * @see EvaluateAction#displayResult(IEvaluationResult)
37      */

38     protected void displayResult(final IEvaluationResult evaluationResult) {
39         if (evaluationResult.hasErrors()) {
40             final Display display = JDIDebugUIPlugin.getStandardDisplay();
41             display.asyncExec(new Runnable JavaDoc() {
42                 public void run() {
43                     if (display.isDisposed()) {
44                         return;
45                     }
46                     reportErrors(evaluationResult);
47                     evaluationCleanup();
48                 }
49             });
50             return;
51         }
52         
53         final String JavaDoc snippet= evaluationResult.getSnippet();
54         IJavaValue resultValue= evaluationResult.getValue();
55         try {
56             String JavaDoc sig= null;
57             IJavaType type= resultValue.getJavaType();
58             if (type != null) {
59                 sig= type.getSignature();
60             }
61             if ("V".equals(sig)) { //$NON-NLS-1$
62
displayStringResult(snippet, ActionMessages.DisplayAction_no_result_value);
63             } else {
64                 final String JavaDoc resultString;
65                 if (sig != null) {
66                     resultString= MessageFormat.format(ActionMessages.DisplayAction_type_name_pattern, new Object JavaDoc[] { resultValue.getReferenceTypeName() });
67                 } else {
68                     resultString= ""; //$NON-NLS-1$
69
}
70                 getDebugModelPresentation().computeDetail(resultValue, new IValueDetailListener() {
71                     public void detailComputed(IValue value, String JavaDoc result) {
72                         displayStringResult(snippet, MessageFormat.format(ActionMessages.DisplayAction_result_pattern, new Object JavaDoc[] { resultString, trimDisplayResult(result)}));
73                     }
74                 });
75             }
76         } catch (DebugException x) {
77             displayStringResult(snippet, getExceptionMessage(x));
78         }
79     }
80
81     protected void displayStringResult(final String JavaDoc snippet,final String JavaDoc resultString) {
82         final IDataDisplay directDisplay= getDirectDataDisplay();
83         final Display display= JDIDebugUIPlugin.getStandardDisplay();
84         display.asyncExec(new Runnable JavaDoc() {
85             public void run() {
86                 if (!display.isDisposed()) {
87                     IDataDisplay dataDisplay= getDataDisplay();
88                     if (dataDisplay != null) {
89                         if (directDisplay == null) {
90                             dataDisplay.displayExpression(snippet);
91                         }
92                         dataDisplay.displayExpressionValue(trimDisplayResult(resultString));
93                     }
94                 }
95                 evaluationCleanup();
96             }
97         });
98     }
99
100     protected void run() {
101         IWorkbenchPart part= getTargetPart();
102         if (part instanceof JavaSnippetEditor) {
103             ((JavaSnippetEditor) part).evalSelection(JavaSnippetEditor.RESULT_DISPLAY);
104             return;
105         }
106         super.run();
107     }
108     
109     /**
110      * Trims the result based on the preference of how long the
111      * variable details should be.
112      *
113      * TODO: illegal internal reference to IInternalDebugUIConstants
114      */

115     public static String JavaDoc trimDisplayResult(String JavaDoc result) {
116         int max = DebugUITools.getPreferenceStore().getInt(IDebugUIConstants.PREF_MAX_DETAIL_LENGTH);
117         if (max > 0 && result.length() > max) {
118             result = result.substring(0, max) + "..."; //$NON-NLS-1$
119
}
120         return result;
121     }
122
123 }
124
Popular Tags