KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > dialogs > ShowViewDialog


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.ui.internal.dialogs;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15
16 import org.eclipse.jface.dialogs.Dialog;
17 import org.eclipse.jface.dialogs.IDialogConstants;
18 import org.eclipse.jface.dialogs.IDialogSettings;
19 import org.eclipse.jface.viewers.DoubleClickEvent;
20 import org.eclipse.jface.viewers.IDoubleClickListener;
21 import org.eclipse.jface.viewers.ISelectionChangedListener;
22 import org.eclipse.jface.viewers.IStructuredSelection;
23 import org.eclipse.jface.viewers.ITreeContentProvider;
24 import org.eclipse.jface.viewers.SelectionChangedEvent;
25 import org.eclipse.jface.viewers.StructuredSelection;
26 import org.eclipse.jface.viewers.TreeViewer;
27 import org.eclipse.swt.SWT;
28 import org.eclipse.swt.events.DisposeEvent;
29 import org.eclipse.swt.events.DisposeListener;
30 import org.eclipse.swt.graphics.Color;
31 import org.eclipse.swt.graphics.RGB;
32 import org.eclipse.swt.layout.GridData;
33 import org.eclipse.swt.widgets.Button;
34 import org.eclipse.swt.widgets.Composite;
35 import org.eclipse.swt.widgets.Control;
36 import org.eclipse.swt.widgets.Shell;
37 import org.eclipse.swt.widgets.Text;
38 import org.eclipse.ui.IWorkbenchWindow;
39 import org.eclipse.ui.PlatformUI;
40 import org.eclipse.ui.dialogs.FilteredTree;
41 import org.eclipse.ui.dialogs.PatternFilter;
42 import org.eclipse.ui.internal.IWorkbenchHelpContextIds;
43 import org.eclipse.ui.internal.WorkbenchMessages;
44 import org.eclipse.ui.internal.WorkbenchPlugin;
45 import org.eclipse.ui.internal.registry.ViewRegistry;
46 import org.eclipse.ui.views.IViewCategory;
47 import org.eclipse.ui.views.IViewDescriptor;
48 import org.eclipse.ui.views.IViewRegistry;
49
50 /**
51  * The Show View dialog.
52  */

53 public class ShowViewDialog extends Dialog implements
54         ISelectionChangedListener, IDoubleClickListener {
55
56     private static final String JavaDoc DIALOG_SETTING_SECTION_NAME = "ShowViewDialog"; //$NON-NLS-1$
57

58     private static final int LIST_HEIGHT = 300;
59
60     private static final int LIST_WIDTH = 250;
61
62     private static final String JavaDoc STORE_EXPANDED_CATEGORIES_ID = DIALOG_SETTING_SECTION_NAME
63             + ".STORE_EXPANDED_CATEGORIES_ID"; //$NON-NLS-1$
64

65     private static final String JavaDoc STORE_SELECTED_VIEW_ID = DIALOG_SETTING_SECTION_NAME
66             + ".STORE_SELECTED_VIEW_ID"; //$NON-NLS-1$
67

68     private FilteredTree filteredTree;
69
70     private Button okButton;
71
72     private IViewDescriptor[] viewDescs = new IViewDescriptor[0];
73
74     private IViewRegistry viewReg;
75
76     private IWorkbenchWindow window;
77
78     private Color dimmedForeground;
79
80     /**
81      * Constructs a new ShowViewDialog.
82      *
83      * @param window the workbench window
84      * @param viewReg the view registry
85      */

86     public ShowViewDialog(IWorkbenchWindow window, IViewRegistry viewReg) {
87         super(window.getShell());
88         this.window = window;
89         this.viewReg = viewReg;
90         setShellStyle(getShellStyle() | SWT.RESIZE);
91     }
92
93     /**
94      * This method is called if a button has been pressed.
95      */

96     protected void buttonPressed(int buttonId) {
97         if (buttonId == IDialogConstants.OK_ID) {
98             saveWidgetValues();
99         }
100         super.buttonPressed(buttonId);
101     }
102
103     /**
104      * Notifies that the cancel button of this dialog has been pressed.
105      */

106     protected void cancelPressed() {
107         viewDescs = new IViewDescriptor[0];
108         super.cancelPressed();
109     }
110
111     /*
112      * (non-Javadoc)
113      *
114      * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
115      */

116     protected void configureShell(Shell shell) {
117         super.configureShell(shell);
118         shell.setText(WorkbenchMessages.ShowView_shellTitle);
119         PlatformUI.getWorkbench().getHelpSystem().setHelp(shell,
120                 IWorkbenchHelpContextIds.SHOW_VIEW_DIALOG);
121     }
122
123     /**
124      * Adds buttons to this dialog's button bar.
125      * <p>
126      * The default implementation of this framework method adds standard ok and
127      * cancel buttons using the <code>createButton</code> framework method.
128      * Subclasses may override.
129      * </p>
130      *
131      * @param parent the button bar composite
132      */

133     protected void createButtonsForButtonBar(Composite parent) {
134         okButton = createButton(parent, IDialogConstants.OK_ID,
135                 IDialogConstants.OK_LABEL, true);
136         createButton(parent, IDialogConstants.CANCEL_ID,
137                 IDialogConstants.CANCEL_LABEL, false);
138         updateButtons();
139     }
140
141     /**
142      * Creates and returns the contents of the upper part of this dialog (above
143      * the button bar).
144      *
145      * @param parent the parent composite to contain the dialog area
146      * @return the dialog area control
147      */

148     protected Control createDialogArea(Composite parent) {
149         // Run super.
150
Composite composite = (Composite) super.createDialogArea(parent);
151         composite.setFont(parent.getFont());
152
153         createFilteredTreeViewer(composite);
154
155         layoutTopControl(filteredTree);
156
157         // Restore the last state
158
restoreWidgetValues();
159
160         // Return results.
161
return composite;
162     }
163
164     /**
165      * Blends c1 and c2 based in the provided ratio.
166      *
167      * @param c1
168      * first color
169      * @param c2
170      * second color
171      * @param ratio
172      * percentage of the first color in the blend (0-100)
173      * @return the RGB value of the blended color
174      *
175      * copied from FormColors.java
176      */

177     private static RGB blend(RGB c1, RGB c2, int ratio) {
178         int r = blend(c1.red, c2.red, ratio);
179         int g = blend(c1.green, c2.green, ratio);
180         int b = blend(c1.blue, c2.blue, ratio);
181         return new RGB(r, g, b);
182     }
183
184     private static int blend(int v1, int v2, int ratio) {
185         int b = (ratio * v1 + (100 - ratio) * v2) / 100;
186         return Math.min(255, b);
187     }
188
189     /**
190      * Create a new filtered tree viewer in the parent.
191      *
192      * @param parent the parent <code>Composite</code>.
193      */

194     private void createFilteredTreeViewer(Composite parent) {
195         PatternFilter filter = new ViewPatternFilter();
196         int styleBits = SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER;
197         filteredTree = new FilteredTree(parent, styleBits, filter);
198         filteredTree.setBackground(parent.getDisplay().getSystemColor(
199                 SWT.COLOR_WIDGET_BACKGROUND));
200         
201         TreeViewer treeViewer = filteredTree.getViewer();
202         Control treeControl = treeViewer.getControl();
203         RGB dimmedRGB = blend(treeControl.getForeground().getRGB(), treeControl.getBackground().getRGB(), 60);
204         dimmedForeground = new Color(treeControl.getDisplay(), dimmedRGB);
205         treeControl.addDisposeListener(new DisposeListener() {
206             public void widgetDisposed(DisposeEvent e) {
207                 dimmedForeground.dispose();
208             }
209         });
210         
211         treeViewer.setLabelProvider(new ViewLabelProvider(window, dimmedForeground));
212         treeViewer.setContentProvider(new ViewContentProvider());
213         treeViewer.setComparator(new ViewComparator((ViewRegistry) viewReg));
214         treeViewer.setInput(viewReg);
215         treeViewer.addSelectionChangedListener(this);
216         treeViewer.addDoubleClickListener(this);
217         treeViewer.addFilter(new CapabilityFilter());
218
219         // if the tree has only one or zero views, disable the filter text control
220
if (hasAtMostOneView(filteredTree.getViewer())) {
221             Text filterText = filteredTree.getFilterControl();
222             if (filterText != null) {
223                 filterText.setEnabled(false);
224             }
225         }
226
227         applyDialogFont(filteredTree);
228     }
229
230     /**
231      * Return whether or not there are less than two views in the list.
232      *
233      * @param tree
234      * @return <code>true</code> if there are less than two views in the list.
235      */

236     private boolean hasAtMostOneView(TreeViewer tree) {
237         ITreeContentProvider contentProvider = (ITreeContentProvider) tree
238                 .getContentProvider();
239         Object JavaDoc[] children = contentProvider.getElements(tree.getInput());
240
241         if (children.length <= 1) {
242             if (children.length == 0) {
243                 return true;
244             }
245             return !contentProvider.hasChildren(children[0]);
246         }
247         return false;
248     }
249
250     /*
251      * (non-Javadoc)
252      *
253      * @see org.eclipse.jface.viewers.IDoubleClickListener#doubleClick(org.eclipse.jface.viewers.DoubleClickEvent)
254      */

255     public void doubleClick(DoubleClickEvent event) {
256         IStructuredSelection s = (IStructuredSelection) event.getSelection();
257         Object JavaDoc element = s.getFirstElement();
258         if (filteredTree.getViewer().isExpandable(element)) {
259             filteredTree.getViewer().setExpandedState(element, !filteredTree.getViewer().getExpandedState(element));
260         } else if (viewDescs.length > 0) {
261             saveWidgetValues();
262             setReturnCode(OK);
263             close();
264         }
265     }
266
267     /**
268      * Return the dialog store to cache values into
269      */

270     protected IDialogSettings getDialogSettings() {
271         IDialogSettings workbenchSettings = WorkbenchPlugin.getDefault()
272                 .getDialogSettings();
273         IDialogSettings section = workbenchSettings
274                 .getSection(DIALOG_SETTING_SECTION_NAME);
275         if (section == null) {
276             section = workbenchSettings
277                     .addNewSection(DIALOG_SETTING_SECTION_NAME);
278         }
279         return section;
280     }
281
282     /**
283      * Returns the descriptors for the selected views.
284      *
285      * @return the descriptors for the selected views
286      */

287     public IViewDescriptor[] getSelection() {
288         return viewDescs;
289     }
290
291     /**
292      * Layout the top control.
293      *
294      * @param control the control.
295      */

296     private void layoutTopControl(Control control) {
297         GridData spec = new GridData(GridData.FILL_BOTH);
298         spec.widthHint = LIST_WIDTH;
299         spec.heightHint = LIST_HEIGHT;
300         control.setLayoutData(spec);
301     }
302
303     /**
304      * Use the dialog store to restore widget values to the values that they
305      * held last time this dialog was used to completion.
306      */

307     protected void restoreWidgetValues() {
308         IDialogSettings settings = getDialogSettings();
309
310         String JavaDoc[] expandedCategoryIds = settings
311                 .getArray(STORE_EXPANDED_CATEGORIES_ID);
312         if (expandedCategoryIds == null) {
313             return;
314         }
315
316         ViewRegistry reg = (ViewRegistry) viewReg;
317         ArrayList JavaDoc categoriesToExpand = new ArrayList JavaDoc(expandedCategoryIds.length);
318         for (int i = 0; i < expandedCategoryIds.length; i++) {
319             IViewCategory category = reg.findCategory(expandedCategoryIds[i]);
320             if (category != null) {
321                 categoriesToExpand.add(category);
322             }
323         }
324
325         if (!categoriesToExpand.isEmpty()) {
326             filteredTree.getViewer().setExpandedElements(categoriesToExpand.toArray());
327         }
328         
329         String JavaDoc selectedViewId = settings.get(STORE_SELECTED_VIEW_ID);
330         if (selectedViewId != null) {
331             IViewDescriptor viewDesc = reg.find(selectedViewId);
332             if (viewDesc != null) {
333                 filteredTree.getViewer().setSelection(new StructuredSelection(viewDesc), true);
334             }
335         }
336     }
337
338     /**
339      * Since OK was pressed, write widget values to the dialog store so that
340      * they will persist into the next invocation of this dialog
341      */

342     protected void saveWidgetValues() {
343         IDialogSettings settings = getDialogSettings();
344
345         // Collect the ids of the all expanded categories
346
Object JavaDoc[] expandedElements = filteredTree.getViewer().getExpandedElements();
347         String JavaDoc[] expandedCategoryIds = new String JavaDoc[expandedElements.length];
348         for (int i = 0; i < expandedElements.length; ++i) {
349             expandedCategoryIds[i] = ((IViewCategory) expandedElements[i]).getId();
350         }
351
352         // Save them for next time.
353
settings.put(STORE_EXPANDED_CATEGORIES_ID, expandedCategoryIds);
354         
355         String JavaDoc selectedViewId = ""; //$NON-NLS-1$
356
if (viewDescs.length > 0) {
357             // in the case of a multi-selection, it's probably less confusing
358
// to store just the first rather than the whole multi-selection
359
selectedViewId = viewDescs[0].getId();
360         }
361         settings.put(STORE_SELECTED_VIEW_ID, selectedViewId);
362     }
363
364     /**
365      * Notifies that the selection has changed.
366      *
367      * @param event event object describing the change
368      */

369     public void selectionChanged(SelectionChangedEvent event) {
370         updateSelection(event);
371         updateButtons();
372     }
373
374     /**
375      * Update the button enablement state.
376      */

377     protected void updateButtons() {
378         if (okButton != null) {
379             okButton.setEnabled(getSelection().length > 0);
380         }
381     }
382
383     /**
384      * Update the selection object.
385      */

386     protected void updateSelection(SelectionChangedEvent event) {
387         ArrayList JavaDoc descs = new ArrayList JavaDoc();
388         IStructuredSelection sel = (IStructuredSelection) event.getSelection();
389         for (Iterator JavaDoc i = sel.iterator(); i.hasNext();) {
390             Object JavaDoc o = i.next();
391             if (o instanceof IViewDescriptor) {
392                 descs.add(o);
393             }
394         }
395         viewDescs = new IViewDescriptor[descs.size()];
396         descs.toArray(viewDescs);
397     }
398 }
399
Popular Tags