KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ide > dialogs > ProjectPerspectiveChoiceDialog


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 package org.eclipse.ui.internal.ide.dialogs;
14
15 import com.ibm.icu.text.Collator;
16 import java.util.ArrayList JavaDoc;
17 import java.util.Collections JavaDoc;
18 import java.util.Comparator JavaDoc;
19
20 import org.eclipse.core.resources.ResourcesPlugin;
21 import org.eclipse.jface.dialogs.Dialog;
22 import org.eclipse.jface.dialogs.ErrorDialog;
23 import org.eclipse.jface.dialogs.IDialogConstants;
24 import org.eclipse.jface.viewers.ISelectionChangedListener;
25 import org.eclipse.jface.viewers.IStructuredContentProvider;
26 import org.eclipse.jface.viewers.IStructuredSelection;
27 import org.eclipse.jface.viewers.SelectionChangedEvent;
28 import org.eclipse.jface.viewers.StructuredSelection;
29 import org.eclipse.jface.viewers.TableViewer;
30 import org.eclipse.jface.viewers.Viewer;
31 import org.eclipse.swt.SWT;
32 import org.eclipse.swt.custom.BusyIndicator;
33 import org.eclipse.swt.events.SelectionAdapter;
34 import org.eclipse.swt.events.SelectionEvent;
35 import org.eclipse.swt.graphics.Font;
36 import org.eclipse.swt.layout.GridData;
37 import org.eclipse.swt.layout.GridLayout;
38 import org.eclipse.swt.widgets.Button;
39 import org.eclipse.swt.widgets.Composite;
40 import org.eclipse.swt.widgets.Control;
41 import org.eclipse.swt.widgets.Group;
42 import org.eclipse.swt.widgets.Label;
43 import org.eclipse.swt.widgets.Shell;
44 import org.eclipse.ui.IPerspectiveDescriptor;
45 import org.eclipse.ui.IPerspectiveRegistry;
46 import org.eclipse.ui.IWorkbenchPage;
47 import org.eclipse.ui.IWorkbenchWindow;
48 import org.eclipse.ui.PlatformUI;
49 import org.eclipse.ui.WorkbenchException;
50 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
51 import org.eclipse.ui.internal.ide.IIDEHelpContextIds;
52 import org.eclipse.ui.model.PerspectiveLabelProvider;
53
54 public class ProjectPerspectiveChoiceDialog extends Dialog {
55     private static final int MIN_DIALOG_WIDTH = 200;
56
57     private static final int MIN_DIALOG_HEIGHT = 250;
58
59     private IWorkbenchWindow window;
60
61     private ArrayList JavaDoc persps;
62
63     private IPerspectiveDescriptor chosenPersp;
64
65     private boolean sameWindow = true;
66
67     private TableViewer list;
68
69     private Comparator JavaDoc comparator = new Comparator JavaDoc() {
70         private Collator collator = Collator.getInstance();
71
72         public int compare(Object JavaDoc ob1, Object JavaDoc ob2) {
73             IPerspectiveDescriptor d1 = (IPerspectiveDescriptor) ob1;
74             IPerspectiveDescriptor d2 = (IPerspectiveDescriptor) ob2;
75             return collator.compare(d1.getLabel(), d2.getLabel());
76         }
77     };
78
79     /**
80      * Create a ProjectPerspectiveChoiceDialog
81      * @param window the workbench window launching this dialog
82      * @param perspIds the list of ids the user can choose from
83      */

84     public ProjectPerspectiveChoiceDialog(IWorkbenchWindow window,
85             String JavaDoc[] perspIds) {
86         super(window.getShell());
87         this.window = window;
88
89         IWorkbenchPage page = window.getActivePage();
90         if (page != null) {
91             chosenPersp = page.getPerspective();
92         }
93
94         IPerspectiveRegistry reg = window.getWorkbench()
95                 .getPerspectiveRegistry();
96         persps = new ArrayList JavaDoc(perspIds.length);
97         for (int i = 0; i < perspIds.length; i++) {
98             IPerspectiveDescriptor desc;
99             desc = reg.findPerspectiveWithId(perspIds[i]);
100             if (desc != null && desc != chosenPersp) {
101                 persps.add(desc);
102             }
103         }
104         Collections.sort(persps, comparator);
105
106         if (chosenPersp != null) {
107             persps.add(0, chosenPersp);
108         }
109     }
110
111     /* (non-Javadoc)
112      * Method declared in Window.
113      */

114     protected void configureShell(Shell shell) {
115         super.configureShell(shell);
116         shell.setText(IDEWorkbenchMessages.ProjectPerspectiveChoiceDialog_title);
117         PlatformUI.getWorkbench().getHelpSystem().setHelp(shell,
118                 IIDEHelpContextIds.SHOW_PROJECT_PERSPECTIVE_DIALOG);
119     }
120
121     /* (non-Javadoc)
122      * Method declared on Dialog.
123      */

124     protected void createButtonsForButtonBar(Composite parent) {
125         createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,
126                 true);
127     }
128
129     /* (non-Javadoc)
130      * Method declared on Dialog.
131      */

132     protected Control createDialogArea(Composite parent) {
133         // page group
134
Composite composite = (Composite) super.createDialogArea(parent);
135
136         composite.setLayout(new GridLayout());
137         GridData data = new GridData(GridData.FILL_BOTH);
138         data.widthHint = MIN_DIALOG_WIDTH;
139         data.heightHint = MIN_DIALOG_HEIGHT;
140         composite.setLayoutData(data);
141
142         createPerspectiveGroup(composite);
143         createOptionGroup(composite);
144
145         if (chosenPersp != null) {
146             list.setSelection(new StructuredSelection(chosenPersp));
147         }
148
149         return composite;
150     }
151
152     /**
153      * Creates the perspective choice controls.
154      *
155      * @param parent the parent composite
156      */

157     private void createPerspectiveGroup(Composite parent) {
158         Font font = parent.getFont();
159         // Label for choosing perspective
160
Label label = new Label(parent, SWT.NONE);
161         label.setFont(font);
162         label.setText(IDEWorkbenchMessages.ProjectPerspectiveChoiceDialog_choosePerspective);
163
164         // Add perspective list.
165
list = new TableViewer(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
166         list.setLabelProvider(new PerspectiveLabelProvider(false));
167         list.setContentProvider(new PerspectiveContentProvider());
168         list.setInput(persps);
169         list.addSelectionChangedListener(new SelectionListener());
170         list.getTable().setLayoutData(new GridData(GridData.FILL_BOTH));
171         list.getTable().setFont(font);
172     }
173
174     /**
175      * Creates the option controls.
176      *
177      * @param parent the parent composite
178      */

179     private void createOptionGroup(Composite parent) {
180         Font font = parent.getFont();
181         // Create the option group
182
Group optionGroup = new Group(parent, SWT.LEFT);
183         GridLayout layout = new GridLayout();
184         optionGroup.setLayout(layout);
185         GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL
186                 | GridData.GRAB_HORIZONTAL);
187         optionGroup.setLayoutData(data);
188         optionGroup.setFont(font);
189         optionGroup.setText(IDEWorkbenchMessages.ProjectPerspectiveChoiceDialog_options);
190
191         // Same window option
192
Button button = new Button(optionGroup, SWT.RADIO | SWT.LEFT);
193         button.setText(IDEWorkbenchMessages.ProjectPerspectiveChoiceDialog_sameWindow);
194         data = new GridData();
195         button.setLayoutData(data);
196         button.setFont(font);
197         button.setSelection(sameWindow);
198         button.addSelectionListener(new SelectionAdapter() {
199             public void widgetSelected(SelectionEvent e) {
200                 sameWindow = true;
201             }
202         });
203
204         // New window option
205
button = new Button(optionGroup, SWT.RADIO | SWT.LEFT);
206         button.setText(IDEWorkbenchMessages.ProjectPerspectiveChoiceDialog_newWindow);
207         data = new GridData();
208         button.setLayoutData(data);
209         button.setFont(font);
210         button.setSelection(!sameWindow);
211         button.addSelectionListener(new SelectionAdapter() {
212             public void widgetSelected(SelectionEvent e) {
213                 sameWindow = false;
214             }
215         });
216     }
217
218     /**
219      * Shows the choosen perspective in the same or
220      * new window depending on the option checked.
221      * Returns the workbench window the perspective
222      * was shown in.
223      */

224     public IWorkbenchWindow showChosenPerspective() {
225         if (chosenPersp == null) {
226             return window;
227         }
228
229         final IWorkbenchWindow[] results = new IWorkbenchWindow[1];
230         final WorkbenchException[] errors = new WorkbenchException[1];
231         BusyIndicator.showWhile(window.getShell().getDisplay(), new Runnable JavaDoc() {
232             public void run() {
233                 if (sameWindow) {
234                     results[0] = window;
235                     IWorkbenchPage page = window.getActivePage();
236                     if (page != null) {
237                         page.setPerspective(chosenPersp);
238                     }
239                 } else {
240                     try {
241                         results[0] = window.getWorkbench().openWorkbenchWindow(
242                                 chosenPersp.getId(),
243                                 ResourcesPlugin.getWorkspace().getRoot());
244                     } catch (WorkbenchException e) {
245                         errors[0] = e;
246                     }
247                 }
248             }
249         });
250
251         IWorkbenchWindow result = results[0];
252         results[0] = null;
253
254         if (errors[0] != null) {
255             ErrorDialog
256                     .openError(
257                             window.getShell(),
258                             IDEWorkbenchMessages.ProjectPerspectiveChoiceDialog_errorTitle,
259                             IDEWorkbenchMessages.ProjectPerspectiveChoiceDialog_errorMessage,
260                             errors[0].getStatus());
261             errors[0] = null;
262             return window;
263         }
264
265         return result;
266     }
267
268     class PerspectiveContentProvider implements IStructuredContentProvider {
269         public PerspectiveContentProvider() {
270             super();
271         }
272
273         public void dispose() {
274         }
275
276         public Object JavaDoc[] getElements(Object JavaDoc element) {
277             if (element instanceof ArrayList JavaDoc) {
278                 return ((ArrayList JavaDoc) element).toArray();
279             }
280             return null;
281         }
282
283         public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
284         }
285
286         public boolean isDeleted(Object JavaDoc element) {
287             return false;
288         }
289     }
290
291     class SelectionListener implements ISelectionChangedListener {
292         public SelectionListener() {
293             super();
294         }
295
296         public void selectionChanged(SelectionChangedEvent event) {
297             IStructuredSelection selection = (IStructuredSelection) event
298                     .getSelection();
299             if (selection != null) {
300                 chosenPersp = (IPerspectiveDescriptor) selection
301                         .getFirstElement();
302             }
303         }
304     }
305 }
306
Popular Tags