KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > ui > InspectPopupDialog


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 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.ui;
12
13 import java.util.List JavaDoc;
14
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.debug.core.DebugPlugin;
17 import org.eclipse.debug.core.model.IExpression;
18 import org.eclipse.debug.internal.ui.DebugUIPlugin;
19 import org.eclipse.debug.internal.ui.SWTFactory;
20 import org.eclipse.debug.internal.ui.model.elements.ElementContentProvider;
21 import org.eclipse.debug.internal.ui.viewers.model.provisional.IHasChildrenUpdate;
22 import org.eclipse.debug.internal.ui.viewers.model.provisional.IPresentationContext;
23 import org.eclipse.debug.internal.ui.viewers.model.provisional.IViewerUpdate;
24 import org.eclipse.debug.internal.ui.viewers.model.provisional.IViewerUpdateListener;
25 import org.eclipse.debug.internal.ui.viewers.model.provisional.PresentationContext;
26 import org.eclipse.debug.internal.ui.viewers.model.provisional.TreeModelViewer;
27 import org.eclipse.debug.internal.ui.views.DebugUIViewsMessages;
28 import org.eclipse.debug.internal.ui.views.variables.VariablesView;
29 import org.eclipse.debug.internal.ui.views.variables.details.DefaultDetailPane;
30 import org.eclipse.debug.internal.ui.views.variables.details.DetailPaneProxy;
31 import org.eclipse.debug.internal.ui.views.variables.details.IDetailPaneContainer;
32 import org.eclipse.jface.viewers.IStructuredSelection;
33 import org.eclipse.jface.viewers.StructuredViewer;
34 import org.eclipse.jface.viewers.ViewerFilter;
35 import org.eclipse.swt.SWT;
36 import org.eclipse.swt.custom.SashForm;
37 import org.eclipse.swt.events.SelectionEvent;
38 import org.eclipse.swt.events.SelectionListener;
39 import org.eclipse.swt.graphics.Point;
40 import org.eclipse.swt.layout.GridData;
41 import org.eclipse.swt.layout.GridLayout;
42 import org.eclipse.swt.widgets.Composite;
43 import org.eclipse.swt.widgets.Control;
44 import org.eclipse.swt.widgets.Shell;
45 import org.eclipse.swt.widgets.Tree;
46 import org.eclipse.ui.IViewPart;
47 import org.eclipse.ui.IWorkbenchPage;
48 import org.eclipse.ui.IWorkbenchPartSite;
49 import org.eclipse.ui.PartInitException;
50
51 /**
52  * A <code>DebugPopup</code> that can be used to inspect an
53  * <code>IExpression</code> object.
54  * <p>
55  * This class is not intended to be subclassed.
56  * </p>
57  * @since 3.2
58  */

59 public class InspectPopupDialog extends DebugPopup {
60     
61     private static final String JavaDoc PREF_INSPECT_POPUP_SASH_WEIGHTS = DebugUIPlugin.getUniqueIdentifier() + "inspectPopupSashWeights"; //$NON-NLS-1$
62

63     private static final int[] DEFAULT_SASH_WEIGHTS = new int[] { 90, 10 };
64     private static final int MIN_WIDTH = 250;
65     private static final int MIN_HEIGHT = 200;
66
67     private TreeModelViewer fViewer;
68     private SashForm fSashForm;
69     private Composite fDetailPaneComposite;
70     private DetailPaneProxy fDetailPane;
71     private Tree fTree;
72     private IExpression fExpression;
73     
74     /**
75      * Creates a new inspect popup.
76      *
77      * @param shell The parent shell
78      * @param anchor point at which to anchor the popup in Display coordinates. Since
79      * 3.3 <code>null</code> indicates a default location should be used.
80      * @param commandId The command id to be used for persistence of
81      * the dialog (possibly <code>null</code>)
82      * @param expression The expression being inspected
83      */

84     public InspectPopupDialog(Shell shell, Point anchor, String JavaDoc commandId, IExpression expression) {
85         super(shell, anchor, commandId);
86         fExpression = expression;
87     }
88
89     /* (non-Javadoc)
90      * @see org.eclipse.debug.ui.DebugPopup#createDialogArea(org.eclipse.swt.widgets.Composite)
91      */

92     protected Control createDialogArea(Composite parent) {
93         Composite composite = new Composite(parent, parent.getStyle());
94         GridLayout layout = new GridLayout();
95         composite.setLayout(layout);
96         composite.setLayoutData(new GridData(GridData.FILL_BOTH));
97
98         fSashForm = new SashForm(composite, parent.getStyle());
99         fSashForm.setOrientation(SWT.VERTICAL);
100         fSashForm.setLayoutData(new GridData(GridData.FILL_BOTH));
101
102         VariablesView view = getViewToEmulate();
103         IPresentationContext context;
104         if (view == null) {
105             context = new PresentationContext(IDebugUIConstants.ID_VARIABLE_VIEW);
106         } else {
107             context = ((TreeModelViewer)view.getViewer()).getPresentationContext();
108         }
109         fViewer = new TreeModelViewer(fSashForm, SWT.NO_TRIM | SWT.MULTI | SWT.VIRTUAL, context);
110
111         fDetailPaneComposite = SWTFactory.createComposite(fSashForm, 1, 1, GridData.FILL_BOTH);
112         
113         fDetailPane = new DetailPaneProxy(new DetailPaneContainer());
114         fDetailPane.display(null); // Bring up the default pane so the user doesn't see an empty composite
115

116         fTree = fViewer.getTree();
117         fTree.addSelectionListener(new SelectionListener() {
118             public void widgetSelected(SelectionEvent e) {
119                 fDetailPane.display((IStructuredSelection)fViewer.getSelection());
120             }
121             public void widgetDefaultSelected(SelectionEvent e) {}
122         });
123
124         initSashWeights();
125       
126         fViewer.getContentProvider();
127         if (view != null) {
128             StructuredViewer structuredViewer = (StructuredViewer) view.getViewer();
129             if (structuredViewer != null) {
130                 ViewerFilter[] filters = structuredViewer.getFilters();
131                 for (int i = 0; i < filters.length; i++) {
132                     fViewer.addFilter(filters[i]);
133                 }
134             }
135         }
136                
137         TreeRoot treeRoot = new TreeRoot();
138         fViewer.addViewerUpdateListener(new ViewerListener(fExpression));
139         fViewer.setInput(treeRoot);
140
141         return fTree;
142     }
143     
144     /**
145      * Initializes the sash form weights from the preference store (using default values if
146      * no sash weights were stored previously).
147      */

148     protected void initSashWeights(){
149         String JavaDoc prefWeights = DebugUIPlugin.getDefault().getPreferenceStore().getString(PREF_INSPECT_POPUP_SASH_WEIGHTS);
150         if (prefWeights.length() > 0){
151             String JavaDoc[] weights = prefWeights.split(":"); //$NON-NLS-1$
152
if (weights.length == 2){
153                 try{
154                     int[] intWeights = new int[2];
155                     intWeights[0] = Integer.parseInt(weights[0]);
156                     intWeights[1] = Integer.parseInt(weights[1]);
157                     fSashForm.setWeights(intWeights);
158                     return;
159                 } catch (NumberFormatException JavaDoc e){}
160             }
161         }
162         fSashForm.setWeights(DEFAULT_SASH_WEIGHTS);
163     }
164     
165     /* (non-Javadoc)
166      * @see org.eclipse.jface.dialogs.PopupDialog#saveDialogBounds(org.eclipse.swt.widgets.Shell)
167      */

168     protected void saveDialogBounds(Shell shell) {
169         super.saveDialogBounds(shell);
170         if (fSashForm != null && !fSashForm.isDisposed()){
171             int[] weights = fSashForm.getWeights();
172             if (weights.length == 2){
173                 String JavaDoc weightString = weights[0] + ":" + weights[1]; //$NON-NLS-1$
174
DebugUIPlugin.getDefault().getPluginPreferences().setValue(PREF_INSPECT_POPUP_SASH_WEIGHTS, weightString);
175             }
176         }
177     }
178     
179     /**
180      * Creates the content for the root element of the tree viewer in the inspect
181      * popup dialog. Always has one child, the expression this popup is displaying.
182      *
183      */

184     private class TreeRoot extends ElementContentProvider {
185         /* (non-Javadoc)
186          * @see org.eclipse.debug.internal.ui.viewers.model.provisional.elements.ElementContentProvider#getChildCount(java.lang.Object, org.eclipse.debug.internal.ui.viewers.provisional.IPresentationContext)
187          */

188         protected int getChildCount(Object JavaDoc element, IPresentationContext context, IViewerUpdate monitor) throws CoreException {
189             return 1;
190         }
191         /* (non-Javadoc)
192          * @see org.eclipse.debug.internal.ui.viewers.model.provisional.elements.ElementContentProvider#getChildren(java.lang.Object, int, int, org.eclipse.debug.internal.ui.viewers.provisional.IPresentationContext)
193          */

194         protected Object JavaDoc[] getChildren(Object JavaDoc parent, int index, int length, IPresentationContext context, IViewerUpdate monitor) throws CoreException {
195             return new Object JavaDoc[] { fExpression };
196         }
197         
198         /* (non-Javadoc)
199          * @see org.eclipse.debug.internal.ui.viewers.model.provisional.elements.ElementContentProvider#supportsContextId(java.lang.String)
200          */

201         protected boolean supportsContextId(String JavaDoc id) {
202             return true;
203         }
204     }
205        
206     /**
207      * Attempts to find an appropriate view to emulate, this will either be the
208      * variables view or the expressions view.
209      * @return a view to emulate or <code>null</code>
210      */

211     private VariablesView getViewToEmulate() {
212         IWorkbenchPage page = DebugUIPlugin.getActiveWorkbenchWindow().getActivePage();
213         VariablesView expressionsView = (VariablesView) page.findView(IDebugUIConstants.ID_EXPRESSION_VIEW);
214         if (expressionsView != null && expressionsView.isVisible()) {
215             return expressionsView;
216         }
217         VariablesView variablesView = (VariablesView) page.findView(IDebugUIConstants.ID_VARIABLE_VIEW);
218         if (variablesView != null && variablesView.isVisible()) {
219             return variablesView;
220         }
221         if (expressionsView != null) {
222             return expressionsView;
223         }
224         return variablesView;
225     }
226     
227     /* (non-Javadoc)
228      * @see org.eclipse.debug.ui.DebugPopup#close()
229      */

230     public boolean close() {
231         if (!wasPersisted()) {
232             fExpression.dispose();
233         }
234         fDetailPane.dispose();
235         return super.close();
236     }
237
238     /* (non-Javadoc)
239      * @see org.eclipse.debug.ui.DebugPopup#getActionText()
240      */

241     protected String JavaDoc getActionText() {
242         return DebugUIViewsMessages.InspectPopupDialog_0;
243     }
244
245     /* (non-Javadoc)
246      * @see org.eclipse.debug.ui.DebugPopup#persist()
247      */

248     protected void persist() {
249         super.persist();
250         DebugPlugin.getDefault().getExpressionManager().addExpression(fExpression);
251
252         fExpression = null;
253         IWorkbenchPage page = DebugUIPlugin.getActiveWorkbenchWindow().getActivePage();
254         IViewPart part = page.findView(IDebugUIConstants.ID_EXPRESSION_VIEW);
255         if (part == null) {
256             try {
257                 page.showView(IDebugUIConstants.ID_EXPRESSION_VIEW);
258             } catch (PartInitException e) {
259             }
260         } else {
261             page.bringToTop(part);
262         }
263     }
264
265     /* (non-Javadoc)
266      * @see org.eclipse.jface.dialogs.PopupDialog#getInitialSize()
267      */

268     protected Point getInitialSize() {
269         Point initialSize = super.getInitialSize();
270         initialSize.x = Math.max(initialSize.x, MIN_WIDTH);
271         initialSize.y = Math.max(initialSize.y, MIN_HEIGHT);
272         return initialSize;
273     }
274
275     /* (non-Javadoc)
276      * @see org.eclipse.jface.dialogs.PopupDialog#getBackgroundColorExclusions()
277      */

278     protected List JavaDoc getBackgroundColorExclusions() {
279         List JavaDoc list = super.getBackgroundColorExclusions();
280         list.add(fSashForm);
281         return list;
282     }
283
284     /**
285      * Inner class implementing IDetailPaneContainer methods. Handles changes to detail
286      * pane and provides limited access to the detail pane proxy.
287      */

288     private class DetailPaneContainer implements IDetailPaneContainer{
289     
290         /* (non-Javadoc)
291          * @see org.eclipse.debug.internal.ui.views.variables.details.IDetailPaneContainer#getCurrentPaneID()
292          */

293         public String JavaDoc getCurrentPaneID() {
294             return fDetailPane.getCurrentPaneID();
295         }
296     
297         /* (non-Javadoc)
298          * @see org.eclipse.debug.internal.ui.views.variables.details.IDetailPaneContainer#getCurrentSelection()
299          */

300         public IStructuredSelection getCurrentSelection() {
301             return (IStructuredSelection)fViewer.getSelection();
302         }
303     
304         /* (non-Javadoc)
305          * @see org.eclipse.debug.internal.ui.views.variables.details.IDetailPaneContainer#refreshDetailPaneContents()
306          */

307         public void refreshDetailPaneContents() {
308             fDetailPane.display(getCurrentSelection());
309         }
310     
311         /* (non-Javadoc)
312          * @see org.eclipse.debug.internal.ui.views.variables.details.IDetailPaneContainer#getParentComposite()
313          */

314         public Composite getParentComposite() {
315             return fDetailPaneComposite;
316         }
317     
318         /* (non-Javadoc)
319          * @see org.eclipse.debug.internal.ui.views.variables.details.IDetailPaneContainer#getWorkbenchPartSite()
320          */

321         public IWorkbenchPartSite getWorkbenchPartSite() {
322             return null;
323         }
324     
325         /* (non-Javadoc)
326          * @see org.eclipse.debug.internal.ui.views.variables.details.IDetailPaneContainer#paneChanged(java.lang.String)
327          */

328         public void paneChanged(String JavaDoc newPaneID) {
329             if (newPaneID.equals(DefaultDetailPane.ID)){
330                 applyBackgroundColor(getShell().getDisplay().getSystemColor(SWT.COLOR_INFO_BACKGROUND), fDetailPane.getCurrentControl());
331             }
332         }
333     
334     }
335     
336     private class ViewerListener implements IViewerUpdateListener {
337
338         private Object JavaDoc rootElement;
339         
340         ViewerListener(Object JavaDoc root) {
341             rootElement = root;
342         }
343         
344         /* (non-Javadoc)
345          * @see org.eclipse.debug.internal.ui.viewers.model.provisional.IViewerUpdateListener#updateComplete(org.eclipse.debug.internal.ui.viewers.model.provisional.IViewerUpdate)
346          */

347         public void updateComplete(IViewerUpdate update) {
348             if (update instanceof IHasChildrenUpdate) {
349                 IHasChildrenUpdate hcu = (IHasChildrenUpdate) update;
350                 if (hcu.getElement().equals(rootElement)) {
351                     fViewer.removeViewerUpdateListener(this);
352                     fViewer.expandToLevel(update.getElementPath(), 1);
353                 }
354             }
355         }
356
357         /* (non-Javadoc)
358          * @see org.eclipse.debug.internal.ui.viewers.model.provisional.IViewerUpdateListener#updateStarted(org.eclipse.debug.internal.ui.viewers.model.provisional.IViewerUpdate)
359          */

360         public void updateStarted(IViewerUpdate update) {
361         }
362
363         /* (non-Javadoc)
364          * @see org.eclipse.debug.internal.ui.viewers.model.provisional.IViewerUpdateListener#viewerUpdatesBegin()
365          */

366         public void viewerUpdatesBegin() {
367         }
368
369         /* (non-Javadoc)
370          * @see org.eclipse.debug.internal.ui.viewers.model.provisional.IViewerUpdateListener#viewerUpdatesComplete()
371          */

372         public void viewerUpdatesComplete() {
373         }
374
375         
376     }
377     
378 }
379
Popular Tags