KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > views > variables > details > DetailPaneProxy


1 /*******************************************************************************
2  * Copyright (c) 2006, 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.internal.ui.views.variables.details;
12
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.core.runtime.IAdaptable;
15 import org.eclipse.core.runtime.IStatus;
16 import org.eclipse.core.runtime.Status;
17 import org.eclipse.debug.internal.ui.DebugUIPlugin;
18 import org.eclipse.debug.ui.IDetailPane;
19 import org.eclipse.jface.viewers.IStructuredSelection;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.layout.GridData;
22 import org.eclipse.swt.widgets.Control;
23 import org.eclipse.swt.widgets.Label;
24
25 /**
26  * Acts as a proxy between a view and a detail pane. Controls how information is displayed
27  * in the details pane in a view. Currently used by the variables, registers and expression
28  * views as well as the inspect popup dialog. The different types of detail panes use the
29  * detailPaneFactories extension and must implement <code>IDetailPane</code>. This class acts
30  * as a proxy to the current detail pane, each time the detail pane type changes, this class
31  * disposes of the old pane and creates the new one. Uses a <code>DetailPaneManager</code> to
32  * organize and find the panes to display.
33  *
34  * @see IDetailPane
35  * @see DetailPaneManager
36  * @since 3.3
37  */

38 public class DetailPaneProxy {
39     
40     /**
41      * The IDetailPane currently being used to display detailed information.
42      */

43     private IDetailPane fCurrentPane;
44     
45     /**
46      * The UI control that the current detail pane is using to display details.
47      */

48     private Control fCurrentControl;
49     
50     /**
51      * Detail pane container that the detail panes will be added to.
52      */

53     private IDetailPaneContainer fParentContainer;
54     
55     /**
56      * Constructor that sets up the detail pane for a view. Note that no default pane
57      * is created, so a control will not be created until <code>display</code> is called.
58      *
59      * @param parent the detail pane container that is holding this detail pane
60      */

61     public DetailPaneProxy(IDetailPaneContainer parent){
62         fParentContainer = parent;
63     }
64
65     /**
66      * Displays the given selection in the preferred detail pane for that type of selection.
67      * Informs the parent container if the type of detail pane changes.
68      * If a null or empty selection is passed and a current pane exists, that view will be cleared.
69      * If a null or empty selection is passed and no pane exists, the default view is created.
70      *
71      * @param selection The selection to display detailed information for
72      */

73     public void display(IStructuredSelection selection){
74         
75         if ((selection == null || selection.isEmpty()) && fCurrentPane != null){
76             fCurrentPane.display(selection);
77             return;
78         }
79         
80         String JavaDoc preferredPaneID = DetailPaneManager.getDefault().getPreferredPaneFromSelection(selection);
81         
82         // Don't change anything if the preferred pane is the current pane
83
if (fCurrentPane != null && preferredPaneID != null && preferredPaneID.equals(fCurrentPane.getID())){
84             fCurrentPane.display(selection);
85             return;
86         }
87         
88         setupPane(preferredPaneID, selection);
89         
90         // Inform the container that a new detail pane is being used
91
fParentContainer.paneChanged(preferredPaneID);
92
93     }
94     
95     /**
96      * Tells the current detail pane (if one exists) that it is gaining focus.
97      *
98      * @return true if the current pane successfully set focus to a control, false otherwise
99      */

100     public boolean setFocus(){
101         if (fCurrentPane != null){
102             return fCurrentPane.setFocus();
103         }
104         return false;
105     }
106     
107     /**
108      * Disposes of the current pane.
109      */

110     public void dispose(){
111         if (fCurrentPane != null) fCurrentPane.dispose();
112         if (fCurrentControl != null && !fCurrentControl.isDisposed()) fCurrentControl.dispose();
113     }
114     
115     /**
116      * Checks if the current pane supports the <code>IAdaptable</code> framework
117      * and if so, calls its <code>getAdapter()</code> method.
118      * @param required
119      * @return
120      */

121     public Object JavaDoc getAdapter(Class JavaDoc required){
122         if (fCurrentPane != null && fCurrentPane instanceof IAdaptable){
123             return ((IAdaptable)fCurrentPane).getAdapter(required);
124         }
125         else{
126             return null;
127         }
128     }
129     
130     public Control getCurrentControl(){
131         return fCurrentControl;
132     }
133     
134     public String JavaDoc getCurrentPaneID(){
135         if (fCurrentPane != null){
136             return fCurrentPane.getID();
137         }
138         return null;
139     }
140     
141     /**
142      * Finds or creates an initialized detail pane with the given ID. Asks the detail
143      * pane to create the control and display the selection.
144      *
145      * @param paneID the ID of the pane to display in
146      * @param selection the selection to display
147      */

148     private void setupPane(String JavaDoc paneID, IStructuredSelection selection) {
149         if (fCurrentPane != null) fCurrentPane.dispose();
150         if (fCurrentControl != null && !fCurrentControl.isDisposed()) fCurrentControl.dispose();
151         if (paneID != null){
152             fCurrentPane = DetailPaneManager.getDefault().getDetailPaneFromID(paneID);
153             if (fCurrentPane != null){
154                 fCurrentPane.init(fParentContainer.getWorkbenchPartSite());
155                 fCurrentControl = fCurrentPane.createControl(fParentContainer.getParentComposite());
156                 if (fCurrentControl != null){
157                     fParentContainer.getParentComposite().layout(true);
158                     fCurrentPane.display(selection);
159                 } else{
160                     createErrorLabel(DetailMessages.DetailPaneProxy_0);
161                     DebugUIPlugin.log(new CoreException(new Status(IStatus.ERROR, DebugUIPlugin.getUniqueIdentifier(), "The detail pane \""+ fCurrentPane.getID() + "\" did not create and return a control."))); //$NON-NLS-1$ //$NON-NLS-2$
162
}
163             } else {
164                 createErrorLabel(DetailMessages.DetailPaneProxy_0);
165                 DebugUIPlugin.log(new CoreException(new Status(IStatus.ERROR, DebugUIPlugin.getUniqueIdentifier(), "Could not create the detail pane with ID " + paneID))); //$NON-NLS-1$
166
}
167         } else {
168             createErrorLabel(DetailMessages.DetailPaneProxy_1);
169             DebugUIPlugin.log(new CoreException(new Status(IStatus.ERROR, DebugUIPlugin.getUniqueIdentifier(), "No detail panes could be found to display the current selection."))); //$NON-NLS-1$
170
}
171     }
172
173     /**
174      * Creates a label in the detail pane area with the given message.
175      *
176      * @param message The message to display
177      */

178     private void createErrorLabel(String JavaDoc message){
179         if (fCurrentPane != null) fCurrentPane.dispose();
180         if (fCurrentControl != null && !fCurrentControl.isDisposed()) fCurrentControl.dispose();
181         Label errorLabel = new Label(fParentContainer.getParentComposite(),SWT.LEFT);
182         errorLabel.setText(message);
183         errorLabel.setLayoutData(new GridData(GridData.FILL_BOTH));
184         fCurrentControl = errorLabel;
185         fParentContainer.getParentComposite().layout();
186     }
187     
188 }
189
Popular Tags