1 /*******************************************************************************
2  * Copyright (c) 2000, 2015 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.jdt.internal.debug.ui.actions;
15 
16 
17 import org.eclipse.debug.core.DebugException;
18 import org.eclipse.debug.core.model.IValue;
19 import org.eclipse.debug.ui.DebugUITools;
20 import org.eclipse.debug.ui.IDebugUIConstants;
21 import org.eclipse.debug.ui.IValueDetailListener;
22 import org.eclipse.jdt.debug.core.IJavaType;
23 import org.eclipse.jdt.debug.core.IJavaValue;
24 import org.eclipse.jdt.debug.eval.IEvaluationResult;
25 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
26 import org.eclipse.jdt.internal.debug.ui.display.IDataDisplay;
27 import org.eclipse.jdt.internal.debug.ui.snippeteditor.JavaSnippetEditor;
28 import org.eclipse.osgi.util.NLS;
29 import org.eclipse.swt.widgets.Display;
30 import org.eclipse.ui.IWorkbenchPart;
31 
32 /**
33  * Displays the result of an evaluation in the display view
34  */
35 public class DisplayAction extends EvaluateAction {
36 
37 	/**
38 	 * @see EvaluateAction#displayResult(IEvaluationResult)
39 	 */
40 	@Override
displayResult(final IEvaluationResult evaluationResult)41 	protected void displayResult(final IEvaluationResult evaluationResult) {
42 		if (evaluationResult.hasErrors()) {
43 			final Display display = JDIDebugUIPlugin.getStandardDisplay();
44 			display.asyncExec(new Runnable() {
45 				@Override
46 				public void run() {
47 					if (display.isDisposed()) {
48 						return;
49 					}
50 					reportErrors(evaluationResult);
51 					evaluationCleanup();
52 				}
53 			});
54 			return;
55 		}
56 
57 		final String snippet= evaluationResult.getSnippet();
58 		IJavaValue resultValue= evaluationResult.getValue();
59 		try {
60 			String sig= null;
61 			IJavaType type= resultValue.getJavaType();
62 			if (type != null) {
63 				sig= type.getSignature();
64 			}
65 			if ("V".equals(sig)) { //$NON-NLS-1$
66 				displayStringResult(snippet, ActionMessages.DisplayAction_no_result_value);
67 			} else {
68 				final String resultString;
69 				if (sig != null) {
70 					resultString= NLS.bind(ActionMessages.DisplayAction_type_name_pattern, new Object[] { resultValue.getReferenceTypeName() });
71 				} else {
72 					resultString= ""; //$NON-NLS-1$
73 				}
74 				getDebugModelPresentation().computeDetail(resultValue, new IValueDetailListener() {
75 					@Override
76 					public void detailComputed(IValue value, String result) {
77 						displayStringResult(snippet, NLS.bind(ActionMessages.DisplayAction_result_pattern, new Object[] { resultString, trimDisplayResult(result)}));
78 					}
79 				});
80 			}
81 		} catch (DebugException x) {
82 			displayStringResult(snippet, getExceptionMessage(x));
83 		}
84 	}
85 
displayStringResult(final String snippet,final String resultString)86 	protected void displayStringResult(final String snippet,final String resultString) {
87 		final IDataDisplay directDisplay= getDirectDataDisplay();
88 		final Display display= JDIDebugUIPlugin.getStandardDisplay();
89 		display.asyncExec(new Runnable() {
90 			@Override
91 			public void run() {
92 				if (!display.isDisposed()) {
93 					IDataDisplay dataDisplay= getDataDisplay();
94 					if (dataDisplay != null) {
95 						if (directDisplay == null) {
96 							dataDisplay.displayExpression(snippet);
97 						}
98 						dataDisplay.displayExpressionValue(trimDisplayResult(resultString));
99 					}
100 				}
101 				evaluationCleanup();
102 			}
103 		});
104 	}
105 
106 	@Override
run()107 	protected void run() {
108 		IWorkbenchPart part= getTargetPart();
109 		if (part instanceof JavaSnippetEditor) {
110 			((JavaSnippetEditor) part).evalSelection(JavaSnippetEditor.RESULT_DISPLAY);
111 			return;
112 		}
113 		super.run();
114 	}
115 
116     /**
117      * Trims the result based on the preference of how long the
118      * variable details should be.
119      *
120      * TODO: illegal internal reference to IInternalDebugUIConstants
121      */
trimDisplayResult(String result)122     public static String trimDisplayResult(String result) {
123         int max = DebugUITools.getPreferenceStore().getInt(IDebugUIConstants.PREF_MAX_DETAIL_LENGTH);
124         if (max > 0 && result.length() > max) {
125             result = result.substring(0, max) + "..."; //$NON-NLS-1$
126         }
127         return result;
128     }
129 
130 }
131