KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > views > expression > ExpressionInformationControl


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.ui.views.expression;
12
13 import java.util.Iterator JavaDoc;
14 import java.util.Map JavaDoc;
15
16 import org.eclipse.debug.core.DebugException;
17 import org.eclipse.debug.core.DebugPlugin;
18 import org.eclipse.debug.core.model.IExpression;
19 import org.eclipse.debug.core.model.IValue;
20 import org.eclipse.debug.core.model.IVariable;
21 import org.eclipse.debug.internal.ui.DebugUIPlugin;
22 import org.eclipse.debug.internal.ui.IInternalDebugUIConstants;
23 import org.eclipse.debug.internal.ui.VariablesViewModelPresentation;
24 import org.eclipse.debug.internal.ui.views.DebugUIViewsMessages;
25 import org.eclipse.debug.internal.ui.views.variables.IndexedVariablePartition;
26 import org.eclipse.debug.internal.ui.views.variables.VariablesView;
27 import org.eclipse.debug.internal.ui.views.variables.VariablesViewer;
28 import org.eclipse.debug.ui.IDebugModelPresentation;
29 import org.eclipse.debug.ui.IDebugUIConstants;
30 import org.eclipse.debug.ui.IValueDetailListener;
31 import org.eclipse.jface.dialogs.IDialogSettings;
32 import org.eclipse.jface.viewers.StructuredViewer;
33 import org.eclipse.jface.viewers.ViewerFilter;
34 import org.eclipse.swt.SWT;
35 import org.eclipse.swt.custom.SashForm;
36 import org.eclipse.swt.custom.StyledText;
37 import org.eclipse.swt.events.SelectionEvent;
38 import org.eclipse.swt.events.SelectionListener;
39 import org.eclipse.swt.graphics.Color;
40 import org.eclipse.swt.graphics.GC;
41 import org.eclipse.swt.graphics.Image;
42 import org.eclipse.swt.graphics.Point;
43 import org.eclipse.swt.layout.GridData;
44 import org.eclipse.swt.layout.GridLayout;
45 import org.eclipse.swt.widgets.Composite;
46 import org.eclipse.swt.widgets.Control;
47 import org.eclipse.swt.widgets.Display;
48 import org.eclipse.swt.widgets.Tree;
49 import org.eclipse.swt.widgets.TreeItem;
50 import org.eclipse.ui.IViewPart;
51 import org.eclipse.ui.IWorkbenchPage;
52 import org.eclipse.ui.IWorkbenchPartSite;
53 import org.eclipse.ui.PartInitException;
54
55 /**
56  * A popup that displays an expression with a details area.
57  * <p>
58  * Clients may instantiate this class; this class is not intended
59  * to be subclassed.
60  * </p>
61  * @since 3.0
62  */

63 public class ExpressionInformationControl extends PopupInformationControl {
64     private static final int[] DEFAULT_SASH_WEIGHTS = new int[] {90, 10};
65     private static final String JavaDoc SASH_KEY = "SASH_WEIGHT"; //$NON-NLS-1$
66

67     private IWorkbenchPage page;
68     private IExpression exp;
69     private VariablesViewer viewer;
70     private IDebugModelPresentation modelPresentation;
71     private StyledText valueDisplay;
72     private SashForm sashForm;
73     private Tree tree;
74
75     /**
76      * Constructs a popup to display an expression. A label and handler
77      * are provided to move the expression to the Expressions view when
78      * dismissed with the given command.
79      *
80      * @param page the workbench page on which the popup should be displayed
81      * @param exp the expression to display
82      * @param commandId identifier of the command used to dismiss the popup
83      */

84     public ExpressionInformationControl(IWorkbenchPage page, IExpression exp, String JavaDoc commandId) {
85         super(page.getWorkbenchWindow().getShell(), DebugUIViewsMessages.ExpressionInformationControl_5, commandId); //$NON-NLS-1$
86
this.page = page;
87         this.exp = exp;
88     }
89
90     /* (non-Javadoc)
91      * @see org.eclipse.jface.text.IInformationControl#setInformation(String)
92      */

93     public void setInformation(String JavaDoc information) {
94         VariablesView view = getViewToEmulate();
95         viewer.getContentProvider();
96         if (view != null) {
97             StructuredViewer structuredViewer = (StructuredViewer) view.getViewer();
98             if (structuredViewer != null) {
99                 ViewerFilter[] filters = structuredViewer.getFilters();
100                 for (int i = 0; i < filters.length; i++) {
101                     viewer.addFilter(filters[i]);
102                 }
103             }
104             ((RemoteExpressionsContentProvider)viewer.getContentProvider()).setShowLogicalStructure(view.isShowLogicalStructure());
105             Map JavaDoc map = view.getPresentationAttributes(exp.getModelIdentifier());
106             Iterator JavaDoc iterator = map.keySet().iterator();
107             while (iterator.hasNext()) {
108                 String JavaDoc key = (String JavaDoc)iterator.next();
109                 modelPresentation.setAttribute(key, map.get(key));
110             }
111         }
112         viewer.setInput(new Object JavaDoc[]{exp});
113         viewer.expandToLevel(2);
114     }
115
116     private VariablesView getViewToEmulate() {
117         VariablesView expressionsView = (VariablesView)page.findView(IDebugUIConstants.ID_EXPRESSION_VIEW);
118         if (expressionsView != null && expressionsView.isVisible()) {
119             return expressionsView;
120         }
121         VariablesView variablesView = (VariablesView)page.findView(IDebugUIConstants.ID_VARIABLE_VIEW);
122         if (variablesView != null && variablesView.isVisible()) {
123             return variablesView;
124         }
125         if (expressionsView != null) {
126             return expressionsView;
127         }
128         return variablesView;
129     }
130
131     /*
132      * TODO: This method not used yet
133      */

134     protected int[] getInitialSashWeights() {
135         IDialogSettings settings = getDialogSettings();
136         int[] sashes = new int[2];
137         try {
138             sashes[0] = settings.getInt(SASH_KEY+"_ONE"); //$NON-NLS-1$
139
sashes[1] = settings.getInt(SASH_KEY+"_TWO"); //$NON-NLS-1$
140
return sashes;
141         } catch (NumberFormatException JavaDoc nfe) {
142         }
143         
144         return DEFAULT_SASH_WEIGHTS;
145     }
146     
147     private void updateValueDisplay(IValue val) {
148         IValueDetailListener valueDetailListener = new IValueDetailListener() {
149             public void detailComputed(IValue value, final String JavaDoc result) {
150                 Display.getDefault().asyncExec(new Runnable JavaDoc() {
151                     public void run() {
152                         if (!valueDisplay.isDisposed()) {
153                             String JavaDoc text = result;
154                             int max = DebugUIPlugin.getDefault().getPreferenceStore().getInt(IInternalDebugUIConstants.PREF_MAX_DETAIL_LENGTH);
155                             if (max > 0 && result.length() > max) {
156                                 text = result.substring(0, max) + "..."; //$NON-NLS-1$
157
}
158                             valueDisplay.setText(text);
159                         }
160                     }
161                 });
162             }
163         };
164         modelPresentation.computeDetail(val, valueDetailListener);
165     }
166
167     /* (non-Javadoc)
168      * @see org.eclipse.debug.ui.actions.PopupInformationControl#createControl(org.eclipse.swt.widgets.Composite)
169      */

170     protected Control createControl(Composite parent) {
171         Composite composite = new Composite(parent, parent.getStyle());
172         GridLayout layout = new GridLayout();
173         composite.setLayout(layout);
174         composite.setLayoutData(new GridData(GridData.FILL_BOTH));
175
176         sashForm = new SashForm(composite, parent.getStyle());
177         sashForm.setOrientation(SWT.VERTICAL);
178         sashForm.setLayoutData(new GridData(GridData.FILL_BOTH));
179         
180         page = DebugUIPlugin.getActiveWorkbenchWindow().getActivePage();
181         VariablesView view = getViewToEmulate();
182         IWorkbenchPartSite site = null;
183         if (view != null) {
184             site = view.getSite();
185         } else {
186             site = page.getActivePart().getSite();
187         }
188                
189         viewer = new VariablesViewer(sashForm, SWT.NO_TRIM, null);
190         viewer.setContentProvider(new ExpressionPopupContentProvider(viewer, site, view));
191         modelPresentation = new VariablesViewModelPresentation();
192         viewer.setLabelProvider(modelPresentation);
193         
194         valueDisplay = new StyledText(sashForm, SWT.NO_TRIM | SWT.WRAP | SWT.V_SCROLL);
195         valueDisplay.setEditable(false);
196         
197         tree = viewer.getTree();
198         tree.addSelectionListener(new SelectionListener() {
199             public void widgetSelected(SelectionEvent e) {
200                 try {
201                     TreeItem[] selections = tree.getSelection();
202                     Object JavaDoc data = selections[selections.length-1].getData();
203                     
204                     IValue val = null;
205                     if (data instanceof IndexedVariablePartition) {
206                         // no details for parititions
207
return;
208                     }
209                     if (data instanceof IVariable) {
210                         val = ((IVariable)data).getValue();
211                     } else if (data instanceof IExpression) {
212                         val = ((IExpression)data).getValue();
213                     }
214                     if (val == null) {
215                         return;
216                     }
217                     
218                     updateValueDisplay(val);
219                 } catch (DebugException ex) {
220                     DebugUIPlugin.log(ex);
221                 }
222                 
223             }
224
225             public void widgetDefaultSelected(SelectionEvent e) {
226             }
227         });
228         
229         Color background = parent.getDisplay().getSystemColor(SWT.COLOR_INFO_BACKGROUND);
230         Color foreground = parent.getDisplay().getSystemColor(SWT.COLOR_INFO_FOREGROUND);
231         tree.setForeground(foreground);
232         tree.setBackground(background);
233         composite.setForeground(foreground);
234         composite.setBackground(background);
235         valueDisplay.setForeground(foreground);
236         valueDisplay.setBackground(background);
237         
238         //sashForm.setWeights(getInitialSashWeights());
239
sashForm.setWeights(DEFAULT_SASH_WEIGHTS);
240         
241         return tree;
242     }
243
244     /* (non-Javadoc)
245      * @see org.eclipse.jface.text.IInformationControl#computeSizeHint()
246      */

247     public Point computeSizeHint() {
248         Point persistedSize = getInitialSize();
249         if (persistedSize != null) {
250             return persistedSize;
251         }
252         
253         int height = 0;
254         int width = 0;
255         int itemCount = 0;
256         
257         TreeItem[] items = tree.getItems();
258         GC gc = new GC(tree);
259         for (int i=0; i<items.length; i++) {
260             width = Math.max (width, calculateWidth(items[i], gc));
261             itemCount++;
262             
263             // do the same for the children because we expand the first level.
264
TreeItem[] children = items[i].getItems();
265             for (int j = 0; j < children.length; j++) {
266                 width = Math.max(width, calculateWidth(children[j], gc));
267                 itemCount++;
268             }
269             
270         }
271         gc.dispose ();
272         width += 40; // give a little extra space
273

274         height = itemCount * tree.getItemHeight() + 90;
275         if (width > 300) {
276             width = 300;
277         }
278         if (height > 300) {
279             height = 300;
280         }
281         return shell.computeSize(width, height, true);
282     }
283     
284     private int calculateWidth (TreeItem item, GC gc) {
285         int width = 0;
286         Image image = item.getImage ();
287         String JavaDoc text = item.getText ();
288         if (image != null) width = image.getBounds ().width + 2;
289         if (text != null && text.length () > 0) width += gc.stringExtent (text).x;
290         return width;
291     }
292     
293     /* (non-Javadoc)
294      * @see org.eclipse.jface.text.IInformationControlExtension#hasContents()
295      */

296     public boolean hasContents() {
297         return (viewer != null);
298     }
299
300     /* (non-Javadoc)
301      * @see org.eclipse.debug.ui.actions.PopupInformationControl#performCommand()
302      */

303     protected void performCommand() {
304         DebugPlugin.getDefault().getExpressionManager().addExpression(exp);
305         
306         // set exp to null since this dialog does not own the expression anymore
307
// the expression now belongs to the Expression View
308
exp = null;
309         
310         IViewPart part = page.findView(IDebugUIConstants.ID_EXPRESSION_VIEW);
311         if (part == null) {
312             try {
313                 page.showView(IDebugUIConstants.ID_EXPRESSION_VIEW);
314             } catch (PartInitException e) {
315             }
316         } else {
317             page.bringToTop(part);
318         }
319     }
320     /* (non-Javadoc)
321      * @see org.eclipse.jface.text.IInformationControl#dispose()
322      */

323     public void dispose() {
324         super.dispose();
325         if (modelPresentation != null) {
326             modelPresentation.dispose();
327         }
328         
329         // expression added to Expression View
330
// the expression will be disposed when the expression is
331
// removed from the view
332
if (exp != null)
333             exp.dispose();
334     }
335 }
336
Popular Tags