KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Sebastian Davids <sdavids@gmx.de> - Fix for bug 19346 - Dialog font should be
11  * activated and used by other components.
12  *******************************************************************************/

13
14 package org.eclipse.ui.internal.dialogs;
15
16 import org.eclipse.jface.dialogs.Dialog;
17 import org.eclipse.jface.dialogs.IDialogConstants;
18 import org.eclipse.jface.viewers.DoubleClickEvent;
19 import org.eclipse.jface.viewers.IDoubleClickListener;
20 import org.eclipse.jface.viewers.ISelectionChangedListener;
21 import org.eclipse.jface.viewers.IStructuredSelection;
22 import org.eclipse.jface.viewers.SelectionChangedEvent;
23 import org.eclipse.jface.viewers.TableViewer;
24 import org.eclipse.jface.viewers.ViewerComparator;
25 import org.eclipse.swt.SWT;
26 import org.eclipse.swt.events.SelectionAdapter;
27 import org.eclipse.swt.events.SelectionEvent;
28 import org.eclipse.swt.layout.GridData;
29 import org.eclipse.swt.widgets.Button;
30 import org.eclipse.swt.widgets.Composite;
31 import org.eclipse.swt.widgets.Control;
32 import org.eclipse.swt.widgets.Shell;
33 import org.eclipse.ui.IPerspectiveDescriptor;
34 import org.eclipse.ui.IPerspectiveRegistry;
35 import org.eclipse.ui.PlatformUI;
36 import org.eclipse.ui.activities.ITriggerPoint;
37 import org.eclipse.ui.activities.WorkbenchActivityHelper;
38 import org.eclipse.ui.internal.IWorkbenchHelpContextIds;
39 import org.eclipse.ui.internal.WorkbenchMessages;
40 import org.eclipse.ui.internal.activities.ws.ActivityMessages;
41 import org.eclipse.ui.internal.activities.ws.ActivityViewerFilter;
42 import org.eclipse.ui.internal.activities.ws.WorkbenchTriggerPoints;
43 import org.eclipse.ui.model.PerspectiveLabelProvider;
44
45 /**
46  * A dialog for perspective creation
47  */

48 public class SelectPerspectiveDialog extends Dialog implements
49         ISelectionChangedListener {
50
51     final private static int LIST_HEIGHT = 300;
52
53     final private static int LIST_WIDTH = 300;
54
55     private TableViewer list;
56
57     private Button okButton;
58
59     private IPerspectiveDescriptor perspDesc;
60
61     private IPerspectiveRegistry perspReg;
62
63     private ActivityViewerFilter activityViewerFilter = new ActivityViewerFilter();
64
65     private Button showAllButton;
66
67     /**
68      * PerspectiveDialog constructor comment.
69      *
70      * @param parentShell the parent shell
71      * @param perspReg the perspective registry
72      */

73     public SelectPerspectiveDialog(Shell parentShell,
74             IPerspectiveRegistry perspReg) {
75         super(parentShell);
76         this.perspReg = perspReg;
77         setShellStyle(getShellStyle() | SWT.RESIZE);
78     }
79
80     /*
81      * (non-Javadoc)
82      *
83      * @see org.eclipse.jface.dialogs.Dialog#cancelPressed()
84      */

85     protected void cancelPressed() {
86         perspDesc = null;
87         super.cancelPressed();
88     }
89
90     /*
91      * (non-Javadoc)
92      *
93      * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
94      */

95     protected void configureShell(Shell shell) {
96         super.configureShell(shell);
97         shell.setText(WorkbenchMessages.SelectPerspective_shellTitle);
98         PlatformUI.getWorkbench().getHelpSystem().setHelp(shell,
99                 IWorkbenchHelpContextIds.SELECT_PERSPECTIVE_DIALOG);
100     }
101
102     /**
103      * Adds buttons to this dialog's button bar.
104      * <p>
105      * The default implementation of this framework method adds standard ok and
106      * cancel buttons using the <code>createButton</code> framework method.
107      * Subclasses may override.
108      * </p>
109      *
110      * @param parent the button bar composite
111      */

112     protected void createButtonsForButtonBar(Composite parent) {
113         okButton = createButton(parent, IDialogConstants.OK_ID,
114                 IDialogConstants.OK_LABEL, true);
115         createButton(parent, IDialogConstants.CANCEL_ID,
116                 IDialogConstants.CANCEL_LABEL, false);
117         updateButtons();
118     }
119
120     /**
121      * Creates and returns the contents of the upper part of this dialog (above
122      * the button bar).
123      *
124      * @param parent the parent composite to contain the dialog area
125      * @return the dialog area control
126      */

127     protected Control createDialogArea(Composite parent) {
128         // Run super.
129
Composite composite = (Composite) super.createDialogArea(parent);
130         composite.setFont(parent.getFont());
131
132         createViewer(composite);
133         layoutTopControl(list.getControl());
134         if (needsShowAllButton()) {
135             createShowAllButton(composite);
136         }
137         // Return results.
138
return composite;
139     }
140
141     /**
142      * @return whether a show-all button is needed. A show all button is needed only if the list contains filtered items.
143      */

144     private boolean needsShowAllButton() {
145         return activityViewerFilter.getHasEncounteredFilteredItem();
146     }
147
148     /**
149      * Create a show all button in the parent.
150      *
151      * @param parent the parent <code>Composite</code>.
152      */

153     private void createShowAllButton(Composite parent) {
154         showAllButton = new Button(parent, SWT.CHECK);
155         showAllButton
156                 .setText(ActivityMessages.Perspective_showAll);
157         showAllButton.addSelectionListener(new SelectionAdapter() {
158
159             /* (non-Javadoc)
160              * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
161              */

162             public void widgetSelected(SelectionEvent e) {
163                 if (showAllButton.getSelection()) {
164                     list.resetFilters();
165                 } else {
166                     list.addFilter(activityViewerFilter);
167                 }
168             }
169         });
170
171     }
172
173     /**
174      * Create a new viewer in the parent.
175      *
176      * @param parent the parent <code>Composite</code>.
177      */

178     private void createViewer(Composite parent) {
179         // Add perspective list.
180
list = new TableViewer(parent, SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL
181                 | SWT.BORDER);
182         list.getTable().setFont(parent.getFont());
183         list.setLabelProvider(new PerspectiveLabelProvider());
184         list.setContentProvider(new PerspContentProvider());
185         list.addFilter(activityViewerFilter);
186         list.setComparator(new ViewerComparator());
187         list.setInput(perspReg);
188         list.addSelectionChangedListener(this);
189         list.addDoubleClickListener(new IDoubleClickListener() {
190             public void doubleClick(DoubleClickEvent event) {
191                 handleDoubleClickEvent();
192             }
193         });
194     }
195
196     /**
197      * Returns the current selection.
198      *
199      * @return the current selection
200      */

201     public IPerspectiveDescriptor getSelection() {
202         return perspDesc;
203     }
204
205     /**
206      * Handle a double click event on the list
207      */

208     protected void handleDoubleClickEvent() {
209         okPressed();
210     }
211
212     /**
213      * Layout the top control.
214      *
215      * @param control the control.
216      */

217     private void layoutTopControl(Control control) {
218         GridData spec = new GridData(GridData.FILL_BOTH);
219         spec.widthHint = LIST_WIDTH;
220         spec.heightHint = LIST_HEIGHT;
221         control.setLayoutData(spec);
222     }
223
224     /**
225      * Notifies that the selection has changed.
226      *
227      * @param event event object describing the change
228      */

229     public void selectionChanged(SelectionChangedEvent event) {
230         updateSelection(event);
231         updateButtons();
232     }
233
234     /**
235      * Update the button enablement state.
236      */

237     protected void updateButtons() {
238         okButton.setEnabled(getSelection() != null);
239     }
240
241     /**
242      * Update the selection object.
243      */

244     protected void updateSelection(SelectionChangedEvent event) {
245         perspDesc = null;
246         IStructuredSelection sel = (IStructuredSelection) event.getSelection();
247         if (!sel.isEmpty()) {
248             Object JavaDoc obj = sel.getFirstElement();
249             if (obj instanceof IPerspectiveDescriptor) {
250                 perspDesc = (IPerspectiveDescriptor) obj;
251             }
252         }
253     }
254
255     /* (non-Javadoc)
256      * @see org.eclipse.jface.dialogs.Dialog#okPressed()
257      */

258     protected void okPressed() {
259         ITriggerPoint triggerPoint = PlatformUI.getWorkbench()
260                 .getActivitySupport().getTriggerPointManager().getTriggerPoint(
261                         WorkbenchTriggerPoints.OPEN_PERSPECITVE_DIALOG);
262         if (WorkbenchActivityHelper.allowUseOf(triggerPoint, getSelection())) {
263             super.okPressed();
264         }
265     }
266 }
267
Popular Tags