KickJava   Java API By Example, From Geeks To Geeks.

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


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
12 package org.eclipse.ui.internal.dialogs;
13
14 import com.ibm.icu.text.Collator;
15 import java.util.ArrayList JavaDoc;
16 import java.util.Collections JavaDoc;
17 import java.util.Comparator JavaDoc;
18
19 import org.eclipse.jface.dialogs.IDialogConstants;
20 import org.eclipse.jface.dialogs.MessageDialog;
21 import org.eclipse.jface.preference.IPreferenceStore;
22 import org.eclipse.jface.preference.PreferencePage;
23 import org.eclipse.jface.resource.ImageDescriptor;
24 import org.eclipse.osgi.util.NLS;
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.graphics.Font;
29 import org.eclipse.swt.layout.GridData;
30 import org.eclipse.swt.layout.GridLayout;
31 import org.eclipse.swt.widgets.Button;
32 import org.eclipse.swt.widgets.Composite;
33 import org.eclipse.swt.widgets.Control;
34 import org.eclipse.swt.widgets.Group;
35 import org.eclipse.swt.widgets.Label;
36 import org.eclipse.swt.widgets.Shell;
37 import org.eclipse.swt.widgets.Table;
38 import org.eclipse.swt.widgets.TableItem;
39 import org.eclipse.swt.widgets.Widget;
40 import org.eclipse.ui.IPerspectiveDescriptor;
41 import org.eclipse.ui.IWorkbench;
42 import org.eclipse.ui.IWorkbenchPage;
43 import org.eclipse.ui.IWorkbenchPreferenceConstants;
44 import org.eclipse.ui.IWorkbenchPreferencePage;
45 import org.eclipse.ui.IWorkbenchWindow;
46 import org.eclipse.ui.PlatformUI;
47 import org.eclipse.ui.internal.IPreferenceConstants;
48 import org.eclipse.ui.internal.IWorkbenchHelpContextIds;
49 import org.eclipse.ui.internal.WorkbenchMessages;
50 import org.eclipse.ui.internal.WorkbenchPage;
51 import org.eclipse.ui.internal.WorkbenchPlugin;
52 import org.eclipse.ui.internal.registry.PerspectiveDescriptor;
53 import org.eclipse.ui.internal.registry.PerspectiveRegistry;
54 import org.eclipse.ui.internal.util.Descriptors;
55 import org.eclipse.ui.internal.util.PrefUtil;
56 import org.eclipse.ui.internal.util.Util;
57
58 /**
59  * The Workbench / Perspectives preference page.
60  */

61 public class PerspectivesPreferencePage extends PreferencePage implements
62         IWorkbenchPreferencePage {
63     private IWorkbench workbench;
64
65     private PerspectiveRegistry perspectiveRegistry;
66
67     private ArrayList JavaDoc perspectives;
68
69     private String JavaDoc defaultPerspectiveId;
70
71     private ArrayList JavaDoc perspToDelete = new ArrayList JavaDoc();
72
73     private ArrayList JavaDoc perspToRevert = new ArrayList JavaDoc();
74
75     private Table perspectivesTable;
76     
77     private Button revertButton;
78
79     private Button deleteButton;
80
81     private Button setDefaultButton;
82
83     // widgets for open perspective mode;
84
private Button openSameWindowButton;
85
86     private Button openNewWindowButton;
87
88     private int openPerspMode;
89
90     // widgets for open view mode
91
private int openViewMode;
92
93     private Button openEmbedButton;
94
95     private Button openFastButton;
96     
97     // labels
98
private final String JavaDoc OVM_TITLE = WorkbenchMessages.OpenViewMode_title;
99
100     private final String JavaDoc OVM_EMBED = WorkbenchMessages.OpenViewMode_embed;
101
102     private final String JavaDoc OVM_FAST = WorkbenchMessages.OpenViewMode_fast;
103
104     private final String JavaDoc OPM_TITLE = WorkbenchMessages.OpenPerspectiveMode_optionsTitle;
105
106     private final String JavaDoc OPM_SAME_WINDOW = WorkbenchMessages.OpenPerspectiveMode_sameWindow;
107
108     private final String JavaDoc OPM_NEW_WINDOW = WorkbenchMessages.OpenPerspectiveMode_newWindow;
109
110     /**
111      * <code>Comparator</code> to compare two perspective descriptors
112      */

113     private Comparator JavaDoc comparator = new Comparator JavaDoc() {
114         private Collator collator = Collator.getInstance();
115
116         public int compare(Object JavaDoc ob1, Object JavaDoc ob2) {
117             IPerspectiveDescriptor d1 = (IPerspectiveDescriptor) ob1;
118             IPerspectiveDescriptor d2 = (IPerspectiveDescriptor) ob2;
119             return collator.compare(d1.getLabel(), d2.getLabel());
120         }
121     };
122
123     /**
124      * Creates the page's UI content.
125      */

126     protected Control createContents(Composite parent) {
127         // @issue if the product subclasses this page, then it should provide
128
// the help content
129
PlatformUI.getWorkbench().getHelpSystem().setHelp(parent,
130                 IWorkbenchHelpContextIds.PERSPECTIVES_PREFERENCE_PAGE);
131
132         Composite composite = createComposite(parent);
133
134         createOpenPerspButtonGroup(composite);
135         createOpenViewButtonGroup(composite);
136         createCustomizePerspective(composite);
137
138         return composite;
139     }
140
141     /**
142      * Creates the composite which will contain all the preference controls for
143      * this page.
144      *
145      * @param parent
146      * the parent composite
147      * @return the composite for this page
148      */

149     protected Composite createComposite(Composite parent) {
150         Composite composite = new Composite(parent, SWT.NONE);
151         GridData data = new GridData(GridData.FILL_BOTH);
152         composite.setLayoutData(data);
153         composite.setFont(parent.getFont());
154         GridLayout layout = new GridLayout();
155         layout.marginWidth = 0;
156         layout.marginHeight = 0;
157         layout.verticalSpacing = 10;
158         composite.setLayout(layout);
159         return composite;
160     }
161
162     /**
163      * Create a composite that contains buttons for selecting the open
164      * perspective mode.
165      *
166      * @param composite
167      * the parent composite
168      */

169     protected void createOpenPerspButtonGroup(Composite composite) {
170
171         Font font = composite.getFont();
172
173         Group buttonComposite = new Group(composite, SWT.LEFT);
174         buttonComposite.setText(OPM_TITLE);
175         buttonComposite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
176         buttonComposite.setFont(composite.getFont());
177         GridLayout layout = new GridLayout();
178         layout.numColumns = 2;
179         buttonComposite.setLayout(layout);
180
181         openSameWindowButton = new Button(buttonComposite, SWT.RADIO);
182         openSameWindowButton.setText(OPM_SAME_WINDOW);
183         openSameWindowButton
184                 .setSelection(IPreferenceConstants.OPM_ACTIVE_PAGE == openPerspMode);
185         openSameWindowButton.setFont(font);
186         openSameWindowButton.addSelectionListener(new SelectionAdapter() {
187             public void widgetSelected(SelectionEvent e) {
188                 openPerspMode = IPreferenceConstants.OPM_ACTIVE_PAGE;
189             }
190         });
191
192         openNewWindowButton = new Button(buttonComposite, SWT.RADIO);
193         openNewWindowButton.setText(OPM_NEW_WINDOW);
194         openNewWindowButton
195                 .setSelection(IPreferenceConstants.OPM_NEW_WINDOW == openPerspMode);
196         openNewWindowButton.setFont(font);
197         openNewWindowButton.addSelectionListener(new SelectionAdapter() {
198             public void widgetSelected(SelectionEvent e) {
199                 openPerspMode = IPreferenceConstants.OPM_NEW_WINDOW;
200             }
201         });
202
203     }
204
205     /**
206      * Creates a composite that contains buttons for selecting open view mode.
207      *
208      * @param composite
209      * the parent composite
210      */

211     protected void createOpenViewButtonGroup(Composite composite) {
212
213         Font font = composite.getFont();
214
215         Group buttonComposite = new Group(composite, SWT.LEFT);
216         buttonComposite.setText(OVM_TITLE);
217         buttonComposite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
218         buttonComposite.setFont(composite.getFont());
219         GridLayout layout = new GridLayout();
220         layout.numColumns = 2;
221         buttonComposite.setLayout(layout);
222
223         openEmbedButton = new Button(buttonComposite, SWT.RADIO);
224         openEmbedButton.setText(OVM_EMBED);
225         openEmbedButton
226                 .setSelection(openViewMode == IPreferenceConstants.OVM_EMBED);
227         openEmbedButton.addSelectionListener(new SelectionAdapter() {
228             public void widgetSelected(SelectionEvent e) {
229                 openViewMode = IPreferenceConstants.OVM_EMBED;
230             }
231         });
232         openEmbedButton.setFont(font);
233
234         // Open view as float no longer supported
235
if (openViewMode == IPreferenceConstants.OVM_FLOAT) {
236             openViewMode = IPreferenceConstants.OVM_FAST;
237         }
238
239         openFastButton = new Button(buttonComposite, SWT.RADIO);
240         openFastButton.setText(OVM_FAST);
241         openFastButton
242                 .setSelection(openViewMode == IPreferenceConstants.OVM_FAST);
243         openFastButton.addSelectionListener(new SelectionAdapter() {
244             public void widgetSelected(SelectionEvent e) {
245                 openViewMode = IPreferenceConstants.OVM_FAST;
246             }
247         });
248         openFastButton.setFont(font);
249
250     }
251
252     /**
253      * Create a table of 3 buttons to enable the user to manage customized
254      * perspectives.
255      *
256      * @param parent
257      * the parent for the button parent
258      * @return Composite that the buttons are created in.
259      */

260     protected Composite createCustomizePerspective(Composite parent) {
261
262         Font font = parent.getFont();
263
264         // define container & its gridding
265
Composite perspectivesComponent = new Composite(parent, SWT.NONE);
266         perspectivesComponent.setLayoutData(new GridData(GridData.FILL_BOTH));
267         perspectivesComponent.setFont(parent.getFont());
268
269         GridLayout layout = new GridLayout();
270         layout.numColumns = 2;
271         layout.marginWidth = 0;
272         layout.marginHeight = 0;
273         perspectivesComponent.setLayout(layout);
274
275         // Add the label
276
Label label = new Label(perspectivesComponent, SWT.LEFT);
277         label.setText(WorkbenchMessages.PerspectivesPreference_available);
278         GridData data = new GridData();
279         data.horizontalSpan = 2;
280         label.setLayoutData(data);
281         label.setFont(font);
282
283         // Add perspectivesTable.
284
perspectivesTable = new Table(perspectivesComponent, SWT.H_SCROLL | SWT.V_SCROLL
285                 | SWT.BORDER);
286         perspectivesTable.addSelectionListener(new SelectionAdapter() {
287             public void widgetSelected(SelectionEvent e) {
288                 updateButtons();
289             }
290         });
291         perspectivesTable.setFont(font);
292
293         data = new GridData(GridData.FILL_BOTH);
294         data.grabExcessHorizontalSpace = true;
295         data.grabExcessVerticalSpace = true;
296         perspectivesTable.setLayoutData(data);
297
298         // Populate the perspectivesTable
299
IPerspectiveDescriptor[] persps = perspectiveRegistry.getPerspectives();
300         perspectives = new ArrayList JavaDoc(persps.length);
301         for (int i = 0; i < persps.length; i++) {
302             perspectives.add(i, persps[i]);
303         }
304         Collections.sort(perspectives, comparator);
305         defaultPerspectiveId = perspectiveRegistry.getDefaultPerspective();
306         updatePerspectivesTable();
307         
308         // Create vertical button bar.
309
Composite buttonBar = (Composite) createVerticalButtonBar(perspectivesComponent);
310         data = new GridData(GridData.FILL_VERTICAL);
311         buttonBar.setLayoutData(data);
312         
313         //Add note label
314
String JavaDoc NOTE_LABEL = WorkbenchMessages.Preference_note;
315         String JavaDoc REVERT_NOTE = WorkbenchMessages.RevertPerspective_note;
316         Composite noteComposite = createNoteComposite(font, parent,
317                 NOTE_LABEL, REVERT_NOTE);
318         GridData noteData = new GridData();
319         noteData.horizontalSpan = 2;
320         noteComposite.setLayoutData(noteData);
321         return perspectivesComponent;
322     }
323
324     /**
325      * Creates a new vertical button with the given id.
326      * <p>
327      * The default implementation of this framework method creates a standard
328      * push button, registers for selection events including button presses and
329      * help requests, and registers default buttons with its shell. The button
330      * id is stored as the buttons client data.
331      * </p>
332      *
333      * @param parent
334      * the parent composite
335      * @param label
336      * the label from the button
337      * @param defaultButton
338      * <code>true</code> if the button is to be the default button,
339      * and <code>false</code> otherwise
340      * @return Button The created button.
341      */

342     protected Button createVerticalButton(Composite parent, String JavaDoc label,
343             boolean defaultButton) {
344         Button button = new Button(parent, SWT.PUSH);
345
346         button.setText(label);
347
348         GridData data = setButtonLayoutData(button);
349         data.horizontalAlignment = GridData.FILL;
350
351         button.addSelectionListener(new SelectionAdapter() {
352             public void widgetSelected(SelectionEvent event) {
353                 verticalButtonPressed(event.widget);
354             }
355         });
356         button.setToolTipText(label);
357         if (defaultButton) {
358             Shell shell = parent.getShell();
359             if (shell != null) {
360                 shell.setDefaultButton(button);
361             }
362         }
363         button.setFont(parent.getFont());
364         return button;
365     }
366
367     /**
368      * Creates and returns the vertical button bar.
369      *
370      * @param parent
371      * the parent composite to contain the button bar
372      * @return the button bar control
373      */

374     protected Control createVerticalButtonBar(Composite parent) {
375         // Create composite.
376
Composite composite = new Composite(parent, SWT.NULL);
377
378         // create a layout with spacing and margins appropriate for the font
379
// size.
380
GridLayout layout = new GridLayout();
381         layout.numColumns = 1;
382         layout.marginWidth = 5;
383         layout.marginHeight = 0;
384         layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
385         layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
386         composite.setLayout(layout);
387         composite.setFont(parent.getFont());
388
389         // Add the buttons to the button bar.
390
setDefaultButton = createVerticalButton(composite, WorkbenchMessages.PerspectivesPreference_MakeDefault, false);
391         setDefaultButton.setToolTipText(WorkbenchMessages.PerspectivesPreference_MakeDefaultTip);
392
393         revertButton = createVerticalButton(composite, WorkbenchMessages.PerspectivesPreference_Reset, false);
394         revertButton.setToolTipText(WorkbenchMessages.PerspectivesPreference_ResetTip);
395
396         deleteButton = createVerticalButton(composite, WorkbenchMessages.PerspectivesPreference_Delete, false);
397         deleteButton.setToolTipText(WorkbenchMessages.PerspectivesPreference_DeleteTip);
398         updateButtons();
399
400         return composite;
401     }
402
403     /**
404      * @see IWorkbenchPreferencePage
405      */

406     public void init(IWorkbench aWorkbench) {
407         this.workbench = aWorkbench;
408         this.perspectiveRegistry = (PerspectiveRegistry) workbench
409                 .getPerspectiveRegistry();
410         IPreferenceStore store = WorkbenchPlugin.getDefault()
411                 .getPreferenceStore();
412         setPreferenceStore(store);
413
414         openViewMode = store.getInt(IPreferenceConstants.OPEN_VIEW_MODE);
415         openPerspMode = store.getInt(IPreferenceConstants.OPEN_PERSP_MODE);
416     }
417
418     /**
419      * The default button has been pressed.
420      */

421     protected void performDefaults() {
422         //Project perspective preferences
423
IPreferenceStore store = WorkbenchPlugin.getDefault()
424                 .getPreferenceStore();
425
426         openViewMode = store.getDefaultInt(IPreferenceConstants.OPEN_VIEW_MODE);
427         // Open view as float no longer supported
428
if (openViewMode == IPreferenceConstants.OVM_FLOAT) {
429             openViewMode = IPreferenceConstants.OVM_FAST;
430         }
431         openEmbedButton
432                 .setSelection(openViewMode == IPreferenceConstants.OVM_EMBED);
433         openFastButton
434                 .setSelection(openViewMode == IPreferenceConstants.OVM_FAST);
435
436         openPerspMode = store
437                 .getDefaultInt(IPreferenceConstants.OPEN_PERSP_MODE);
438         openSameWindowButton
439                 .setSelection(IPreferenceConstants.OPM_ACTIVE_PAGE == openPerspMode);
440         openNewWindowButton
441                 .setSelection(IPreferenceConstants.OPM_NEW_WINDOW == openPerspMode);
442
443         String JavaDoc currentDefault = perspectiveRegistry.getDefaultPerspective();
444         
445         int index = indexOf(currentDefault);
446         if (index >= 0){
447             defaultPerspectiveId = currentDefault;
448             updatePerspectivesTable();
449             perspectivesTable.setSelection(index);
450         }
451         
452         String JavaDoc newDefault = PrefUtil.getAPIPreferenceStore().getDefaultString(
453                 IWorkbenchPreferenceConstants.DEFAULT_PERSPECTIVE_ID);
454         
455         IPerspectiveDescriptor desc = null;
456         if (newDefault != null) {
457             desc = workbench.getPerspectiveRegistry().findPerspectiveWithId(newDefault);
458         }
459         if (desc == null) {
460             newDefault = workbench.getPerspectiveRegistry().getDefaultPerspective();
461         }
462         
463         defaultPerspectiveId = newDefault;
464         updatePerspectivesTable();
465
466     }
467
468     /**
469      * Look up the index of the perpective with the given if.
470      * @param perspectiveId or <code>null</code>
471      * @return int -1 if it cannot be found
472      */

473     private int indexOf(String JavaDoc perspectiveId) {
474         if (perspectiveId == null) {
475             return -1;
476         }
477         PerspectiveDescriptor[] descriptors =
478             new PerspectiveDescriptor[perspectives.size()];
479         perspectives.toArray(descriptors);
480         for (int i = 0; i < descriptors.length; i++) {
481             PerspectiveDescriptor descriptor = descriptors[i];
482             if(descriptor.getId().equals(perspectiveId)) {
483                 return i;
484             }
485         }
486         return -1;
487     }
488
489     /**
490      * Deletes the perspectives selected by the user if there is no opened
491      * instance of that perspective.
492      *
493      * @return boolean <code>true</code> if all of the perspectives could be
494      * deleted.
495      */

496     private boolean findOpenInstance(IPerspectiveDescriptor desc) {
497         IWorkbenchWindow windows[] = workbench.getWorkbenchWindows();
498         
499         //find all active perspectives currently
500
for (int i = 0; i < windows.length; i++) {
501             IWorkbenchPage pages[] = windows[i].getPages();
502             for (int j = 0; j < pages.length; j++) {
503                 WorkbenchPage page = (WorkbenchPage) pages[j];
504                 if (page.findPerspective(desc) != null) {
505                         MessageDialog
506                                 .openInformation(
507                                         getShell(),
508                                         WorkbenchMessages.PerspectivesPreference_cannotdelete_title,
509                                         NLS.bind(WorkbenchMessages.PerspectivesPreference_cannotdelete_message,desc.getLabel()));
510                         return true;
511                 }
512             }
513         }
514
515         return false;
516     }
517
518     /**
519      * Apply the user's changes if any
520      */

521     public boolean performOk() {
522         // Set the default perspective
523
if (!Util.equals(defaultPerspectiveId, perspectiveRegistry.getDefaultPerspective())) {
524             perspectiveRegistry.setDefaultPerspective(defaultPerspectiveId);
525         }
526         
527         //Delete the perspective
528
if(perspectives.size()<perspectiveRegistry.getPerspectives().length) {
529             perspectiveRegistry.deletePerspectives(perspToDelete);
530         }
531                 
532         // Revert the perspectives
533
perspectiveRegistry.revertPerspectives(perspToRevert);
534
535         IPreferenceStore store = getPreferenceStore();
536
537         // store the open view mode setting
538
store.setValue(IPreferenceConstants.OPEN_VIEW_MODE, openViewMode);
539
540         // store the open perspective mode setting
541
store.setValue(IPreferenceConstants.OPEN_PERSP_MODE, openPerspMode);
542
543         // save both the API prefs and the internal prefs
544
// the API prefs are modified by
545
// PerspectiveRegistry.setDefaultPerspective
546
PrefUtil.savePrefs();
547
548         return true;
549     }
550
551     /**
552      * Update the button enablement state.
553      */

554     protected void updateButtons() {
555         // Get selection.
556
int index = perspectivesTable.getSelectionIndex();
557
558         // Map it to the perspective descriptor
559
PerspectiveDescriptor desc = null;
560         if (index > -1) {
561             desc = (PerspectiveDescriptor) perspectives.get(index);
562         }
563
564         // Do enable.
565
if (desc != null) {
566             revertButton.setEnabled(desc.isPredefined()
567                     && desc.hasCustomDefinition()
568                     && !perspToRevert.contains(desc));
569             deleteButton.setEnabled(!desc.isPredefined());
570             setDefaultButton.setEnabled(true);
571         } else {
572             revertButton.setEnabled(false);
573             deleteButton.setEnabled(false);
574             setDefaultButton.setEnabled(false);
575         }
576     }
577
578     /**
579      * Update the perspectivesTable.
580      */

581     protected void updatePerspectivesTable() {
582         // Populate the table with the items
583
perspectivesTable.removeAll();
584         for (int i = 0; i < perspectives.size(); i++) {
585             PerspectiveDescriptor persp = (PerspectiveDescriptor) perspectives.get(i);
586             newPerspectivesTableItem(persp, i, false);
587         }
588     }
589     
590     /**
591      * Create a new tableItem using given perspective, and set image for the new item.
592      */

593     protected TableItem newPerspectivesTableItem(IPerspectiveDescriptor persp,
594             int index, boolean selected) {
595         
596         ImageDescriptor image = persp.getImageDescriptor();
597         
598         TableItem item = new TableItem(perspectivesTable, SWT.NULL, index);
599         if (image != null) {
600             Descriptors.setImage(item, image);
601         }
602         String JavaDoc label=persp.getLabel();
603         if (persp.getId().equals(defaultPerspectiveId)){
604             label = NLS.bind(WorkbenchMessages.PerspectivesPreference_defaultLabel, label );
605             
606         }
607         item.setText(label);
608         item.setData(persp);
609         if (selected) {
610             perspectivesTable.setSelection(index);
611         }
612
613         return item;
614     }
615
616     /**
617      * Notifies that this page's button with the given id has been pressed.
618      *
619      * @param button
620      * the button that was pressed
621      */

622     protected void verticalButtonPressed(Widget button) {
623         // Get selection.
624
int index = perspectivesTable.getSelectionIndex();
625
626         // Map it to the perspective descriptor
627
PerspectiveDescriptor desc = null;
628         if (index > -1) {
629             desc = (PerspectiveDescriptor) perspectives.get(index);
630         } else {
631             return;
632         }
633
634         // Take action.
635
if (button == revertButton) {
636             if (desc.isPredefined() && !perspToRevert.contains(desc)) {
637                 perspToRevert.add(desc);
638             }
639         } else if (button == deleteButton) {
640             if (!desc.isPredefined() && !perspToDelete.contains(desc)) {
641                 if(!findOpenInstance(desc)){
642                     perspToDelete.add(desc);
643                     perspToRevert.remove(desc);
644                     perspectives.remove(desc);
645                     updatePerspectivesTable();
646                 }
647                     
648             }
649         } else if (button == setDefaultButton) {
650             defaultPerspectiveId = desc.getId();
651             updatePerspectivesTable();
652             perspectivesTable.setSelection(index);
653         }
654
655         updateButtons();
656     }
657     
658     public void dispose() {
659          super.dispose();
660     }
661 }
662
Popular Tags