KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 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
12 package org.eclipse.ui.internal.dialogs;
13
14 import java.util.Arrays JavaDoc;
15 import java.util.Comparator JavaDoc;
16 import java.util.Locale JavaDoc;
17
18 import org.eclipse.core.runtime.IConfigurationElement;
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.core.runtime.Platform;
22 import org.eclipse.core.runtime.Status;
23 import org.eclipse.jface.dialogs.IDialogConstants;
24 import org.eclipse.jface.dialogs.MessageDialog;
25 import org.eclipse.jface.preference.IPreferenceStore;
26 import org.eclipse.jface.preference.PreferencePage;
27 import org.eclipse.jface.util.IPropertyChangeListener;
28 import org.eclipse.jface.util.PropertyChangeEvent;
29 import org.eclipse.jface.window.Window;
30 import org.eclipse.osgi.util.NLS;
31 import org.eclipse.swt.SWT;
32 import org.eclipse.swt.accessibility.AccessibleAdapter;
33 import org.eclipse.swt.accessibility.AccessibleEvent;
34 import org.eclipse.swt.events.SelectionAdapter;
35 import org.eclipse.swt.events.SelectionEvent;
36 import org.eclipse.swt.events.SelectionListener;
37 import org.eclipse.swt.layout.FormAttachment;
38 import org.eclipse.swt.layout.FormData;
39 import org.eclipse.swt.layout.FormLayout;
40 import org.eclipse.swt.layout.GridData;
41 import org.eclipse.swt.layout.GridLayout;
42 import org.eclipse.swt.widgets.Button;
43 import org.eclipse.swt.widgets.Combo;
44 import org.eclipse.swt.widgets.Composite;
45 import org.eclipse.swt.widgets.Control;
46 import org.eclipse.swt.widgets.Group;
47 import org.eclipse.swt.widgets.Label;
48 import org.eclipse.swt.widgets.Text;
49 import org.eclipse.ui.IWorkbench;
50 import org.eclipse.ui.IWorkbenchPreferenceConstants;
51 import org.eclipse.ui.IWorkbenchPreferencePage;
52 import org.eclipse.ui.PlatformUI;
53 import org.eclipse.ui.internal.IPreferenceConstants;
54 import org.eclipse.ui.internal.IWorkbenchConstants;
55 import org.eclipse.ui.internal.IWorkbenchHelpContextIds;
56 import org.eclipse.ui.internal.Workbench;
57 import org.eclipse.ui.internal.WorkbenchMessages;
58 import org.eclipse.ui.internal.WorkbenchPlugin;
59 import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants;
60 import org.eclipse.ui.internal.themes.IThemeDescriptor;
61 import org.eclipse.ui.internal.util.PrefUtil;
62 import org.eclipse.ui.preferences.IWorkbenchPreferenceContainer;
63 import org.eclipse.ui.progress.UIJob;
64 import org.eclipse.ui.themes.ITheme;
65 import org.eclipse.ui.themes.IThemeManager;
66
67 import com.ibm.icu.text.Collator;
68
69 /**
70  * The ViewsPreferencePage is the page used to set preferences for the
71  * appearance of the workbench. Originally this applied only to views but now
72  * applies to the overall appearance, hence the name.
73  */

74 public class ViewsPreferencePage extends PreferencePage implements
75         IWorkbenchPreferencePage {
76
77     private Button showTextOnPerspectiveBar;
78
79     /*
80      * change the tab style of the workbench
81      */

82     private Button showTraditionalStyleTabs;
83
84     private Button enableAnimations;
85
86     private Button editorTopButton;
87
88     private Button editorBottomButton;
89
90     private Button viewTopButton;
91
92     private Button viewBottomButton;
93
94     private Button perspLeftButton;
95
96     private Button perspTopLeftButton;
97
98     private Button perspTopRightButton;
99
100     static final String JavaDoc EDITORS_TITLE = WorkbenchMessages.ViewsPreference_editors;
101
102     private static final String JavaDoc EDITORS_TOP_TITLE = WorkbenchMessages.ViewsPreference_editors_top;
103
104     private static final String JavaDoc EDITORS_BOTTOM_TITLE = WorkbenchMessages.ViewsPreference_editors_bottom;
105
106     private static final String JavaDoc VIEWS_TITLE = WorkbenchMessages.ViewsPreference_views;
107
108     private static final String JavaDoc VIEWS_TOP_TITLE = WorkbenchMessages.ViewsPreference_views_top;
109
110     private static final String JavaDoc VIEWS_BOTTOM_TITLE = WorkbenchMessages.ViewsPreference_views_bottom;
111
112     private static final String JavaDoc PERSP_TITLE = WorkbenchMessages.ViewsPreference_perspectiveBar;
113
114     private static final String JavaDoc PERSP_LEFT_TITLE = WorkbenchMessages.ViewsPreference_perspectiveBar_left;
115
116     private static final String JavaDoc PERSP_TOP_LEFT_TITLE = WorkbenchMessages.ViewsPreference_perspectiveBar_topLeft;
117
118     private static final String JavaDoc PERSP_TOP_RIGHT_TITLE = WorkbenchMessages.ViewsPreference_perspectiveBar_topRight;
119
120     // These constants aren't my favourite idea, but to get this preference done
121
// for M9... A better solution might be to have the presentation factory set
122
// its dependant preference defaults on startup. I've filed bug 63346 to do
123
// something about this area.
124
private static final String JavaDoc R21PRESENTATION_ID = "org.eclipse.ui.internal.r21presentationFactory"; //$NON-NLS-1$
125
private static final String JavaDoc DEFAULT_PRESENTATION_ID = IWorkbenchConstants.DEFAULT_PRESENTATION_ID;
126     private static final String JavaDoc R30_PRESENTATION_ID = "org.eclipse.ui.presentations.r30"; //$NON-NLS-1$
127

128     private static final String JavaDoc INITIAL_VAL = new String JavaDoc();
129
130     private static final int INITIAL_LOC_INT = -1;
131
132     // remembers whether the hidden fastview bar pref needs to be changed on
133
// OK/Apply
134
private String JavaDoc fastViewLoc = INITIAL_VAL;
135
136     private String JavaDoc showTextOnPerspBar = INITIAL_VAL;
137
138     private int editorAlignment;
139
140     private boolean editorAlignmentChanged = false;
141
142     private int viewAlignment;
143
144     private boolean viewAlignmentChanged = false;
145
146     private String JavaDoc perspBarLocation;
147
148     private Combo themeCombo;
149
150     private Combo presentationCombo;
151
152     private IConfigurationElement[] presentationFactories;
153
154     private String JavaDoc currentPresentationFactoryId;
155
156     private Button overridePresButton;
157
158     private IPropertyChangeListener overrideListener;
159
160     private boolean restartPosted = false;
161     
162     private Group editorTabGroup;
163     
164     private Group viewTabGroup;
165     
166     private Group perspBarTabGroup;
167     
168     private Text themeDescriptionText;
169
170     /**
171      * Create a composite that for creating the tab toggle buttons.
172      *
173      * @param composite Composite
174      * @param title String
175      */

176     private Group createButtonGroup(Composite composite, String JavaDoc title) {
177         Group buttonComposite = new Group(composite, SWT.NONE);
178         buttonComposite.setText(title);
179         FormLayout layout = new FormLayout();
180         layout.marginWidth = 5; // same as GridData default
181
layout.marginHeight = 5; // same as GridData default
182
buttonComposite.setLayout(layout);
183         buttonComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
184         return buttonComposite;
185     }
186
187     /**
188      * Creates and returns the SWT control for the customized body of this
189      * preference page under the given parent composite.
190      * <p>
191      * This framework method must be implemented by concrete subclasses.
192      * </p>
193      *
194      * @param parent the parent composite
195      * @return Control the new control
196      */

197     protected Control createContents(Composite parent) {
198         initializeDialogUnits(parent);
199         
200         PlatformUI.getWorkbench().getHelpSystem().setHelp(parent,
201                 IWorkbenchHelpContextIds.VIEWS_PREFERENCE_PAGE);
202
203         IPreferenceStore internalStore = PrefUtil.getInternalPreferenceStore();
204         IPreferenceStore apiStore = PrefUtil.getAPIPreferenceStore();
205
206         editorAlignment = internalStore
207                 .getInt(IPreferenceConstants.EDITOR_TAB_POSITION);
208         viewAlignment = internalStore
209                 .getInt(IPreferenceConstants.VIEW_TAB_POSITION);
210         perspBarLocation = apiStore
211                 .getString(IWorkbenchPreferenceConstants.DOCK_PERSPECTIVE_BAR);
212
213         Composite composite = new Composite(parent, SWT.NONE);
214         composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
215
216         GridLayout layout = new GridLayout();
217         layout.marginWidth = 0;
218         layout.marginHeight = 0;
219         layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
220         composite.setLayout(layout);
221
222         createPresentationCombo(composite);
223         createPresentationOverride(composite);
224         createEditorTabButtonGroup(composite);
225         createViewTabButtonGroup(composite);
226         createPerspBarTabButtonGroup(composite);
227         createShowTextOnPerspectiveBarPref(composite);
228         createThemeCombo(composite);
229         createThemeDescriptionText(composite);
230         createShowTraditionalStyleTabsPref(composite);
231         createEnableAnimationsPref(composite);
232
233         updateOverride();
234         hookOverrideListener();
235         
236         applyDialogFont(composite);
237         
238         return composite;
239     }
240
241     private void createThemeCombo(Composite composite) {
242         new Label(composite, SWT.NONE).setText(WorkbenchMessages.ViewsPreference_currentTheme);
243         themeCombo = new Combo(composite, SWT.READ_ONLY);
244         themeCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
245         themeCombo.addSelectionListener(new SelectionAdapter() {
246             public void widgetSelected(SelectionEvent e) {
247                 refreshThemeDescriptionText();
248             }
249         });
250         refreshThemeCombo(PlatformUI.getWorkbench().getThemeManager().getCurrentTheme().getId());
251     }
252
253      
254     /**
255      * Create the text box that will contain the current theme description
256      * text (if any).
257      *
258      * @param parent
259      * the parent <code>Composite</code>.
260      */

261     private void createThemeDescriptionText(Composite parent) {
262         new Label(parent, SWT.NONE)
263                 .setText(WorkbenchMessages.ViewsPreference_currentThemeDescription);
264
265         themeDescriptionText = new Text(parent, SWT.H_SCROLL | SWT.V_SCROLL
266                 | SWT.READ_ONLY | SWT.BORDER | SWT.WRAP);
267         themeDescriptionText.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
268                 true, true));
269
270         refreshThemeDescriptionText();
271     }
272     
273     private void refreshThemeDescriptionText() {
274         String JavaDoc description = null;
275         int idx = themeCombo.getSelectionIndex();
276         // idx == 0 is "Default" which has no description
277
if (idx > 0) {
278             IThemeDescriptor theme = WorkbenchPlugin.getDefault()
279                     .getThemeRegistry().getThemes()[idx - 1];
280             description = theme.getDescription();
281         }
282         if (description == null) {
283             description = ""; //$NON-NLS-1$
284
}
285         themeDescriptionText.setText(description);
286     }
287
288     private Button createCheckButton(Composite composite, String JavaDoc text, boolean selection) {
289         Button button = new Button(composite, SWT.CHECK);
290         button.setText(text);
291         button.setSelection(selection);
292         return button;
293     }
294     
295     private void createPresentationOverride(Composite parent) {
296         IPreferenceStore store = getPreferenceStore();
297         boolean override = store.getBoolean(IPreferenceConstants.OVERRIDE_PRESENTATION);
298         
299         // workaround to catch the case where the show text value was changed outside of this page
300
// turn off text on persp bar
301
boolean showText = PrefUtil.getAPIPreferenceStore().getBoolean(IWorkbenchPreferenceConstants.SHOW_TEXT_ON_PERSPECTIVE_BAR);
302         if (showText && isR21(currentPresentationFactoryId) || !showText && isR30(currentPresentationFactoryId)) {
303             if (!override) {
304                 store.setValue(IPreferenceConstants.OVERRIDE_PRESENTATION, true);
305                 override = true;
306             }
307         }
308         // workaround to catch the case where the perspective switcher location was changed outside of this page
309
// turn off text on persp bar
310
String JavaDoc barLocation = PrefUtil.getAPIPreferenceStore().getString(IWorkbenchPreferenceConstants.DOCK_PERSPECTIVE_BAR);
311         if (!barLocation.equals(IWorkbenchPreferenceConstants.LEFT) && isR21(currentPresentationFactoryId) || !barLocation.equals(IWorkbenchPreferenceConstants.TOP_RIGHT) && isR30(currentPresentationFactoryId)) {
312             if (!override) {
313                 store.setValue(IPreferenceConstants.OVERRIDE_PRESENTATION, true);
314                 override = true;
315             }
316         }
317             
318         overridePresButton = createCheckButton(parent, WorkbenchMessages.ViewsPreference_override, override);
319         overridePresButton.addSelectionListener(new SelectionListener() {
320             public void widgetSelected(SelectionEvent e) {
321                 updateOverrideState(overridePresButton.getSelection());
322             }
323
324             public void widgetDefaultSelected(SelectionEvent e) {
325                 updateOverrideState(overridePresButton.getSelection());
326             }
327         });
328     }
329
330     private void updateOverrideState(boolean override) {
331         IPreferenceStore store = getPreferenceStore();
332         if (store.getBoolean(IPreferenceConstants.OVERRIDE_PRESENTATION) != override) {
333             store
334                     .setValue(IPreferenceConstants.OVERRIDE_PRESENTATION,
335                             override);
336         }
337         // as we are no longer overriding the prefs should match the selected
338
// presentation
339
if (!override)
340             setPresentationPrefs(getSelectedPresentationID());
341     }
342
343     private void createPresentationCombo(Composite parent) {
344         new Label(parent, SWT.NONE).setText(WorkbenchMessages.ViewsPreference_currentPresentation);
345
346         presentationCombo = new Combo(parent, SWT.READ_ONLY);
347         presentationCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
348         presentationCombo.addSelectionListener(new SelectionListener() {
349
350             public void widgetSelected(SelectionEvent e) {
351                 updateSettings();
352             }
353
354             public void widgetDefaultSelected(SelectionEvent e) {
355                 updateSettings();
356             }
357
358             private void updateSettings() {
359                 if (!overridePresButton.getSelection()) {
360                     setPresentationPrefs(getSelectedPresentationID());
361                 }
362             }
363         });
364
365         refreshPresentationCombo();
366         setPresentationSelection();
367     }
368
369     /**
370      * Set the two supplied controls to be beside each other.
371      */

372     private void attachControls(Control leftControl, Control rightControl) {
373         FormData leftData = new FormData();
374         leftData.left = new FormAttachment(0, 0);
375
376         FormData rightData = new FormData();
377         rightData.left = new FormAttachment(leftControl, convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING));
378
379         leftControl.setLayoutData(leftData);
380         rightControl.setLayoutData(rightData);
381     }
382
383     /**
384      * Create a composite that contains buttons for selecting tab position for
385      * the edit selection.
386      *
387      * @param composite Composite
388      */

389     private void createEditorTabButtonGroup(Composite composite) {
390         editorTabGroup = createButtonGroup(composite, EDITORS_TITLE);
391
392         editorTopButton = new Button(editorTabGroup, SWT.RADIO);
393         editorTopButton.setText(EDITORS_TOP_TITLE);
394         editorTopButton.setSelection(editorAlignment == SWT.TOP);
395         editorTopButton.getAccessible().addAccessibleListener(
396                 new AccessibleAdapter() {
397                     public void getName(AccessibleEvent e) {
398                         e.result = EDITORS_TITLE;
399                     }
400                 });
401
402         editorBottomButton = new Button(editorTabGroup, SWT.RADIO);
403         editorBottomButton.setText(EDITORS_BOTTOM_TITLE);
404         editorBottomButton.setSelection(editorAlignment == SWT.BOTTOM);
405
406         SelectionListener sel = new SelectionAdapter() {
407             public void widgetSelected(SelectionEvent e) {
408                 if (e.widget.equals(editorTopButton)) {
409                     if (editorAlignment != SWT.TOP) {
410                         editorAlignment = SWT.TOP;
411                         editorAlignmentChanged = true;
412                     }
413                 } else if (e.widget.equals(editorBottomButton)) {
414                     if (editorAlignment != SWT.BOTTOM) {
415                         editorAlignment = SWT.BOTTOM;
416                         editorAlignmentChanged = true;
417                     }
418                 }
419             }
420         };
421
422         editorTopButton.addSelectionListener(sel);
423         editorBottomButton.addSelectionListener(sel);
424
425         attachControls(editorTopButton, editorBottomButton);
426     }
427
428     /**
429      * Create a composite that contains buttons for selecting tab position for
430      * the view selection.
431      *
432      * @param composite Composite
433      */

434     private void createViewTabButtonGroup(Composite composite) {
435         viewTabGroup = createButtonGroup(composite, VIEWS_TITLE);
436
437         viewTopButton = new Button(viewTabGroup, SWT.RADIO);
438         viewTopButton.setText(VIEWS_TOP_TITLE);
439         viewTopButton.setSelection(this.viewAlignment == SWT.TOP);
440
441         viewBottomButton = new Button(viewTabGroup, SWT.RADIO);
442         viewBottomButton.setText(VIEWS_BOTTOM_TITLE);
443         viewBottomButton.setSelection(viewAlignment == SWT.BOTTOM);
444
445         SelectionListener sel = new SelectionAdapter() {
446             public void widgetSelected(SelectionEvent e) {
447                 if (e.widget.equals(viewTopButton)) {
448                     if (viewAlignment != SWT.TOP) {
449                         viewAlignment = SWT.TOP;
450                         viewAlignmentChanged = true;
451                     }
452                 } else if (e.widget.equals(viewBottomButton)) {
453                     if (viewAlignment != SWT.BOTTOM) {
454                         viewAlignment = SWT.BOTTOM;
455                         viewAlignmentChanged = true;
456                     }
457                 }
458             }
459         };
460
461         viewTopButton.addSelectionListener(sel);
462         viewBottomButton.addSelectionListener(sel);
463
464         attachControls(viewTopButton, viewBottomButton);
465     }
466
467     /**
468      * Create a composite that contains buttons for selecting perspective
469      * switcher position.
470      *
471      * @param composite Composite
472      */

473     private void createPerspBarTabButtonGroup(Composite composite) {
474         perspBarTabGroup = createButtonGroup(composite, PERSP_TITLE);
475
476         perspLeftButton = new Button(perspBarTabGroup, SWT.RADIO);
477         perspLeftButton.setText(PERSP_LEFT_TITLE);
478         perspLeftButton.setSelection(IWorkbenchPreferenceConstants.LEFT
479                 .equals(perspBarLocation));
480         perspLeftButton.addSelectionListener(new SelectionAdapter() {
481             public void widgetSelected(SelectionEvent e) {
482                 perspBarLocation = IWorkbenchPreferenceConstants.LEFT;
483             }
484         });
485
486         perspTopLeftButton = new Button(perspBarTabGroup, SWT.RADIO);
487         perspTopLeftButton.setText(PERSP_TOP_LEFT_TITLE);
488         perspTopLeftButton.setSelection(IWorkbenchPreferenceConstants.TOP_LEFT
489                 .equals(perspBarLocation));
490         perspTopLeftButton.addSelectionListener(new SelectionAdapter() {
491             public void widgetSelected(SelectionEvent e) {
492                 perspBarLocation = IWorkbenchPreferenceConstants.TOP_LEFT;
493             }
494         });
495
496         perspTopRightButton = new Button(perspBarTabGroup, SWT.RADIO);
497         perspTopRightButton.setText(PERSP_TOP_RIGHT_TITLE);
498         perspTopRightButton
499                 .setSelection(IWorkbenchPreferenceConstants.TOP_RIGHT
500                         .equals(perspBarLocation));
501         perspTopRightButton.addSelectionListener(new SelectionAdapter() {
502             public void widgetSelected(SelectionEvent e) {
503                 perspBarLocation = IWorkbenchPreferenceConstants.TOP_RIGHT;
504             }
505         });
506
507         int spacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
508         
509         FormData leftData = new FormData();
510         leftData.left = new FormAttachment(0, 0);
511
512         FormData topLeftData = new FormData();
513         topLeftData.left = new FormAttachment(perspLeftButton, spacing);
514
515         FormData topRightData = new FormData();
516         topRightData.left = new FormAttachment(perspTopLeftButton, spacing);
517
518         perspLeftButton.setLayoutData(leftData);
519         perspTopLeftButton.setLayoutData(topLeftData);
520         perspTopRightButton.setLayoutData(topRightData);
521     }
522
523     /**
524      * Hook a listener to update the buttons based on an override preference. If
525      * the preference is false then do not allow editing of these options.
526      *
527      */

528     private void hookOverrideListener() {
529         if (overrideListener != null) {
530             return;
531         }
532         IPreferenceStore store = getPreferenceStore();
533         overrideListener = new IPropertyChangeListener() {
534             public void propertyChange(PropertyChangeEvent event) {
535                 if (event.getProperty().equals(
536                         IPreferenceConstants.OVERRIDE_PRESENTATION)) {
537                     updateOverride();
538                 }
539             }
540         };
541         store.addPropertyChangeListener(overrideListener);
542     }
543
544     /**
545      * Dispose resources created by the receiver.
546      */

547     public void dispose() {
548         super.dispose();
549         if (overrideListener != null) {
550             getPreferenceStore().removePropertyChangeListener(overrideListener);
551             overrideListener = null;
552         }
553     }
554
555     private void updateOverride() {
556         boolean override = getPreferenceStore().getBoolean(
557                 IPreferenceConstants.OVERRIDE_PRESENTATION);
558         editorTabGroup.setEnabled(override);
559         editorTopButton.setEnabled(override);
560         editorBottomButton.setEnabled(override);
561         viewTabGroup.setEnabled(override);
562         viewTopButton.setEnabled(override);
563         viewBottomButton.setEnabled(override);
564         perspBarTabGroup.setEnabled(override);
565         perspTopLeftButton.setEnabled(override);
566         perspLeftButton.setEnabled(override);
567         perspTopRightButton.setEnabled(override);
568         showTextOnPerspectiveBar.setEnabled(override);
569     }
570
571     private void refreshPresentationCombo() {
572         // get the active presentation
573
presentationCombo.removeAll();
574         refreshPresentationFactories();
575
576         for (int i = 0; i < presentationFactories.length; ++i) {
577             IConfigurationElement el = presentationFactories[i];
578             String JavaDoc name = el.getAttribute(IWorkbenchConstants.TAG_NAME);
579             if (!currentPresentationFactoryId.equals(el
580                     .getAttribute(IWorkbenchConstants.TAG_ID))) {
581                 presentationCombo.add(name);
582             } else {
583                 presentationCombo
584                     .add(NLS.bind(
585                         WorkbenchMessages.ViewsPreference_currentPresentationFormat,
586                         name));
587             }
588         }
589     }
590
591     private void setPresentationSelection() {
592         for (int i = 0; i < presentationFactories.length; ++i) {
593             if (currentPresentationFactoryId.equals(presentationFactories[i]
594                     .getAttribute(IWorkbenchConstants.TAG_ID))) {
595                 presentationCombo.select(i);
596                 return;
597             }
598         }
599         //presentationCombo.select(0);
600
}
601
602     /**
603      * Update this page's list of presentation factories. This should only be
604      * used when the presentation combo is refreshed, as this array will be used
605      * to set the selection from the combo.
606      */

607     private void refreshPresentationFactories() {
608         // update the current selection (used to look for changes on apply)
609
currentPresentationFactoryId = PrefUtil.getAPIPreferenceStore()
610                 .getString(
611                         IWorkbenchPreferenceConstants.PRESENTATION_FACTORY_ID);
612         // Workbench.getInstance().getPresentationId();
613

614         // update the sorted list of factories
615
presentationFactories = Platform.getExtensionRegistry()
616                 .getConfigurationElementsFor(PlatformUI.PLUGIN_ID,
617                         IWorkbenchRegistryConstants.PL_PRESENTATION_FACTORIES);
618
619         // sort the array by name
620
Arrays.sort(presentationFactories, new Comparator JavaDoc() {
621             Collator collator = Collator.getInstance(Locale.getDefault());
622
623             public int compare(Object JavaDoc a, Object JavaDoc b) {
624                 IConfigurationElement el1 = (IConfigurationElement) a;
625                 IConfigurationElement el2 = (IConfigurationElement) b;
626                 return collator.compare(el1
627                         .getAttribute(IWorkbenchConstants.TAG_NAME), el2
628                         .getAttribute(IWorkbenchConstants.TAG_NAME));
629             }
630         });
631     }
632
633     /**
634      * Update the preferences associated with the argument presentation factory.
635      *
636      * @return boolean
637      * <code>true<\code> of the presentation has changed and <code>false<\code> otherwise
638      */

639     private boolean updatePresentationPreferences() {
640         // There are some preference values associated with the R2.1
641
// presentation that cannot be captured in the presentation
642
// factory. Perhaps the extension point should contain these
643
// (a list of attributes?), but for now it is done manually.
644

645         if (presentationCombo == null) {
646             return false;
647         }
648
649         String JavaDoc id = getSelectedPresentationID();
650
651         // if it hasn't changed then there's nothing to do
652
if (id.equals(currentPresentationFactoryId)) {
653             return false;
654         }
655
656         currentPresentationFactoryId = id;
657         // apply 2.1 prefs if needed
658
setPresentationPrefs(id);
659         // set the new presentation factory id
660
PrefUtil.getAPIPreferenceStore().putValue(
661                 IWorkbenchPreferenceConstants.PRESENTATION_FACTORY_ID, id);
662         // a restart is required to update the presentation
663
return true;
664     }
665
666     private void setPresentationPrefs(String JavaDoc id) {
667         if (isR21(id)) {
668             setR21Preferences();
669         } else if (isR30(id)) {
670             setR30Preferences();
671         } else if (isR33(id)) {
672             setR33Preferences();
673         }
674     }
675
676     private boolean isR33(String JavaDoc id) {
677         return DEFAULT_PRESENTATION_ID.equals(id);
678     }
679
680     private boolean isR30(String JavaDoc id) {
681         return R30_PRESENTATION_ID.equals(id);
682     }
683
684     private boolean isR21(String JavaDoc id) {
685         return R21PRESENTATION_ID.equals(id);
686     }
687
688     private String JavaDoc getSelectedPresentationID() {
689         int selection = presentationCombo.getSelectionIndex();
690         IConfigurationElement element = presentationFactories[selection];
691         String JavaDoc id = element.getAttribute(IWorkbenchConstants.TAG_ID);
692         return id;
693     }
694
695     private void setR33Preferences() {
696         setR30Preferences();
697         
698         // Turn -on- the new Min/Max behaviour
699
IPreferenceStore apiStore = PrefUtil.getAPIPreferenceStore();
700         apiStore.setValue(IWorkbenchPreferenceConstants.ENABLE_NEW_MIN_MAX, true);
701     }
702
703     private void setR30Preferences() {
704         IPreferenceStore internalStore = PrefUtil.getInternalPreferenceStore();
705         IPreferenceStore apiStore = PrefUtil.getAPIPreferenceStore();
706
707         // Turn -off- the new min/max behaviour
708
apiStore.setValue(IWorkbenchPreferenceConstants.ENABLE_NEW_MIN_MAX, false);
709
710         setEditorAlignDefault(internalStore);
711         setViewAlignDefault(internalStore);
712
713         // perspective switcher on the left
714
perspBarLocation = apiStore
715                 .getDefaultString(IWorkbenchPreferenceConstants.DOCK_PERSPECTIVE_BAR);
716         perspLeftButton.setSelection(perspBarLocation
717                 .equals(IWorkbenchPreferenceConstants.LEFT));
718         perspTopLeftButton.setSelection(perspBarLocation
719                 .equals(IWorkbenchPreferenceConstants.TOP_LEFT));
720         perspTopRightButton.setSelection(perspBarLocation
721                 .equals(IWorkbenchPreferenceConstants.TOP_RIGHT));
722
723         // perspective bar should be set to default on OK or Apply
724
perspBarLocation = INITIAL_VAL;
725
726         // turn off text on persp bar
727
showTextOnPerspectiveBar
728                 .setSelection(apiStore
729                         .getDefaultBoolean(IWorkbenchPreferenceConstants.SHOW_TEXT_ON_PERSPECTIVE_BAR));
730         // text on perspective bar should be set to default on OK or Apply
731
showTextOnPerspBar = INITIAL_VAL;
732
733         // fast view bar should be set to default on OK or Apply
734
fastViewLoc = INITIAL_VAL;
735     }
736
737     private void setViewAlignDefault(IPreferenceStore internalStore) {
738         int oldVal;
739         // reset the preferences for 3.0 presentation
740
oldVal = viewAlignment;
741         viewAlignment = internalStore
742                 .getDefaultInt(IPreferenceConstants.VIEW_TAB_POSITION);
743         viewTopButton.setSelection(viewAlignment == SWT.TOP);
744         viewBottomButton.setSelection(viewAlignment == SWT.BOTTOM);
745
746         // view tabs should be set to default on OK or Apply
747
if (oldVal != viewAlignment) {
748             viewAlignmentChanged = true;
749         }
750         viewAlignment = INITIAL_LOC_INT;
751     }
752
753     private void setEditorAlignDefault(IPreferenceStore internalStore) {
754         // editor tabs on the bottom, really should associate this with the
755
// presentation
756
int oldVal = editorAlignment;
757         editorAlignment = internalStore
758                 .getDefaultInt(IPreferenceConstants.EDITOR_TAB_POSITION);
759         editorTopButton.setSelection(editorAlignment == SWT.TOP);
760         editorBottomButton.setSelection(editorAlignment == SWT.BOTTOM);
761
762         // editor tabs should be set to default on OK or Apply
763
if (oldVal != editorAlignment) {
764             editorAlignmentChanged = true;
765         }
766         editorAlignment = INITIAL_LOC_INT;
767     }
768
769     private void setR21Preferences() {
770         // editor tabs on the bottom, really should associate this with the
771
// presentation
772
int oldVal = editorAlignment;
773         editorAlignment = SWT.TOP;
774         editorTopButton.setSelection(editorAlignment == SWT.TOP);
775         editorBottomButton.setSelection(editorAlignment == SWT.BOTTOM);
776         if (oldVal != editorAlignment) {
777             editorAlignmentChanged = true;
778         }
779
780         // view tabs on the bottom, really should associate this with the
781
// presentation
782
oldVal = viewAlignment;
783         viewAlignment = SWT.BOTTOM;
784         viewTopButton.setSelection(viewAlignment == SWT.TOP);
785         viewBottomButton.setSelection(viewAlignment == SWT.BOTTOM);
786         if (oldVal != viewAlignment) {
787             viewAlignmentChanged = true;
788         }
789
790         // perspective switcher on the left, really should associate this with
791
// the presentation
792
perspBarLocation = IWorkbenchPreferenceConstants.LEFT;
793         perspLeftButton.setSelection(perspBarLocation
794                 .equals(IWorkbenchPreferenceConstants.LEFT));
795         perspTopLeftButton.setSelection(perspBarLocation
796                 .equals(IWorkbenchPreferenceConstants.TOP_LEFT));
797         perspTopRightButton.setSelection(perspBarLocation
798                 .equals(IWorkbenchPreferenceConstants.TOP_RIGHT));
799
800         // turn off text on persp bar, really should associate this with the
801
// presentation
802
showTextOnPerspectiveBar.setSelection(false);
803         showTextOnPerspBar = String.valueOf(false);
804
805         // fast view bar on the left (hidden pref, set it directly)
806
fastViewLoc = IWorkbenchPreferenceConstants.LEFT;
807
808         // Turn -off- the new min/max behaviour
809
IPreferenceStore apiStore = PrefUtil.getAPIPreferenceStore();
810         apiStore.setValue(IWorkbenchPreferenceConstants.ENABLE_NEW_MIN_MAX, false);
811     }
812
813     /**
814      * @param themeToSelect the id of the theme to be selected
815      */

816     private void refreshThemeCombo(String JavaDoc themeToSelect) {
817         themeCombo.removeAll();
818         ITheme currentTheme = PlatformUI.getWorkbench().getThemeManager()
819                 .getCurrentTheme();
820
821         IThemeDescriptor[] descs = WorkbenchPlugin.getDefault()
822                 .getThemeRegistry().getThemes();
823         String JavaDoc defaultThemeString = PlatformUI.getWorkbench().getThemeManager()
824                 .getTheme(IThemeManager.DEFAULT_THEME).getLabel();
825         if (currentTheme.getId().equals(IThemeManager.DEFAULT_THEME)) {
826             defaultThemeString = NLS.bind(
827                     WorkbenchMessages.ViewsPreference_currentThemeFormat,
828                     new Object JavaDoc[] { defaultThemeString });
829         }
830         themeCombo.add(defaultThemeString);
831         
832         String JavaDoc themeString;
833         int selection = 0;
834         for (int i = 0; i < descs.length; i++) {
835             themeString = descs[i].getName();
836             if (descs[i].getId().equals(currentTheme.getId())) {
837                 themeString = NLS.bind(
838                         WorkbenchMessages.ViewsPreference_currentThemeFormat,
839                         new Object JavaDoc[] { themeString });
840             }
841             if (themeToSelect.equals(descs[i].getId())) {
842                 selection = i + 1;
843             }
844             themeCombo.add(themeString);
845         }
846         themeCombo.select(selection);
847     }
848
849     /**
850      * Create the button and text that support setting the preference for
851      * showing text labels on the perspective switching bar.
852      */

853     protected void createShowTextOnPerspectiveBarPref(Composite composite) {
854         IPreferenceStore apiStore = PrefUtil.getAPIPreferenceStore();
855
856         showTextOnPerspectiveBar = createCheckButton(
857                 composite,
858                 WorkbenchMessages.WorkbenchPreference_showTextOnPerspectiveBar,
859                 apiStore
860                         .getBoolean(IWorkbenchPreferenceConstants.SHOW_TEXT_ON_PERSPECTIVE_BAR));
861     }
862
863     /**
864      * Create the button and text that support setting the preference for
865      * showing text labels on the perspective switching bar
866      */

867     protected void createShowTraditionalStyleTabsPref(Composite composite) {
868         IPreferenceStore apiStore = PrefUtil.getAPIPreferenceStore();
869
870         showTraditionalStyleTabs = createCheckButton(
871                 composite,
872                 WorkbenchMessages.ViewsPreference_traditionalTabs,
873                 apiStore
874                         .getBoolean(IWorkbenchPreferenceConstants.SHOW_TRADITIONAL_STYLE_TABS));
875     }
876
877     protected void createEnableAnimationsPref(Composite composite) {
878         IPreferenceStore apiStore = PrefUtil.getAPIPreferenceStore();
879
880         enableAnimations = createCheckButton(
881                 composite,
882                 WorkbenchMessages.ViewsPreference_enableAnimations,
883                 apiStore
884                         .getBoolean(IWorkbenchPreferenceConstants.ENABLE_ANIMATIONS));
885     }
886
887     /**
888      * Returns preference store that belongs to the our plugin.
889      *
890      * @return IPreferenceStore the preference store for this plugin
891      */

892     protected IPreferenceStore doGetPreferenceStore() {
893         return WorkbenchPlugin.getDefault().getPreferenceStore();
894     }
895
896     /**
897      * Initializes this preference page for the given workbench.
898      * <p>
899      * This method is called automatically as the preference page is being
900      * created and initialized. Clients must not call this method.
901      * </p>
902      *
903      * @param workbench the workbench
904      */

905     public void init(IWorkbench workbench) {
906         currentPresentationFactoryId = PrefUtil.getAPIPreferenceStore()
907                 .getString(
908                         IWorkbenchPreferenceConstants.PRESENTATION_FACTORY_ID);
909     }
910
911     /**
912      * The default button has been pressed.
913      */

914     protected void performDefaults() {
915         IPreferenceStore store = getPreferenceStore();
916         IPreferenceStore apiStore = PrefUtil.getAPIPreferenceStore();
917
918         showTextOnPerspectiveBar
919                 .setSelection(apiStore
920                         .getDefaultBoolean(IWorkbenchPreferenceConstants.SHOW_TEXT_ON_PERSPECTIVE_BAR));
921         showTraditionalStyleTabs
922                 .setSelection(apiStore
923                         .getDefaultBoolean(IWorkbenchPreferenceConstants.SHOW_TRADITIONAL_STYLE_TABS));
924         enableAnimations
925                 .setSelection(apiStore
926                         .getDefaultBoolean(IWorkbenchPreferenceConstants.ENABLE_ANIMATIONS));
927
928         String JavaDoc presID = apiStore
929                 .getDefaultString(IWorkbenchPreferenceConstants.PRESENTATION_FACTORY_ID);
930         currentPresentationFactoryId = presID;
931         setPresentationSelection();
932
933         boolean overridePrefs = store
934                 .getDefaultBoolean(IPreferenceConstants.OVERRIDE_PRESENTATION);
935         overridePresButton.setSelection(overridePrefs);
936
937         updateOverrideState(overridePrefs);
938         
939         setEditorAlignDefault(store);
940         setViewAlignDefault(store);
941
942         perspBarLocation = apiStore
943                 .getDefaultString(IWorkbenchPreferenceConstants.DOCK_PERSPECTIVE_BAR);
944         perspLeftButton.setSelection(IWorkbenchPreferenceConstants.LEFT
945                 .equals(perspBarLocation));
946         perspTopLeftButton.setSelection(IWorkbenchPreferenceConstants.TOP_LEFT
947                 .equals(perspBarLocation));
948         perspTopRightButton
949                 .setSelection(IWorkbenchPreferenceConstants.TOP_RIGHT
950                         .equals(perspBarLocation));
951
952         refreshThemeCombo(PlatformUI.getWorkbench().getThemeManager()
953                 .getTheme(IThemeManager.DEFAULT_THEME).getId());
954         refreshThemeDescriptionText();
955         
956         WorkbenchPlugin.getDefault().savePluginPreferences();
957         
958         super.performDefaults();
959     }
960
961     /**
962      * The user has pressed Ok. Store/apply this page's values appropriately.
963      */

964     public boolean performOk() {
965         IPreferenceStore store = getPreferenceStore();
966         IPreferenceStore apiStore = PrefUtil.getAPIPreferenceStore();
967         boolean override = store
968                 .getBoolean(IPreferenceConstants.OVERRIDE_PRESENTATION);
969
970         // apply the presentation selection first since it might change some of
971
// the other values
972
boolean restart = updatePresentationPreferences();
973
974         if (showTextOnPerspBar.equals(INITIAL_VAL) && !override) {
975             apiStore
976                     .setToDefault(IWorkbenchPreferenceConstants.SHOW_TEXT_ON_PERSPECTIVE_BAR);
977         } else {
978             apiStore.setValue(
979                     IWorkbenchPreferenceConstants.SHOW_TEXT_ON_PERSPECTIVE_BAR,
980                     showTextOnPerspectiveBar.getSelection());
981         }
982
983         if (editorAlignmentChanged) {
984             if (editorAlignment == INITIAL_LOC_INT) {
985                 store.setToDefault(IPreferenceConstants.EDITOR_TAB_POSITION);
986             } else if (!override) {
987                 // store the editor tab value to setting
988
store.setValue(IPreferenceConstants.EDITOR_TAB_POSITION,
989                         editorAlignment);
990             } else {
991                 // store the editor tab value to setting
992
store.setValue(IPreferenceConstants.EDITOR_TAB_POSITION,
993                         editorAlignment);
994             }
995             restart = true;
996         }
997
998         if (viewAlignmentChanged) {
999             if (viewAlignment == INITIAL_LOC_INT) {
1000                store.setToDefault(IPreferenceConstants.VIEW_TAB_POSITION);
1001            } else if (!override) {
1002                // store the view tab value to setting
1003
store.setValue(IPreferenceConstants.VIEW_TAB_POSITION,
1004                        viewAlignment);
1005            } else {
1006                // store the view tab value to setting
1007
store.setValue(IPreferenceConstants.VIEW_TAB_POSITION,
1008                        viewAlignment);
1009            }
1010            restart = true;
1011        }
1012
1013        if (perspBarLocation.equals(INITIAL_VAL)) {
1014            apiStore
1015                    .setToDefault(IWorkbenchPreferenceConstants.DOCK_PERSPECTIVE_BAR);
1016        } else if (!override) {
1017            // store the perspective bar text enabled setting
1018
apiStore.setValue(
1019                    IWorkbenchPreferenceConstants.DOCK_PERSPECTIVE_BAR,
1020                    perspBarLocation);
1021        } else {
1022            // store the perspective bar text enabled setting
1023
apiStore.setValue(
1024                    IWorkbenchPreferenceConstants.DOCK_PERSPECTIVE_BAR,
1025                    perspBarLocation);
1026        }
1027
1028        if (fastViewLoc.equals(INITIAL_VAL)) {
1029            apiStore
1030                    .setToDefault(IWorkbenchPreferenceConstants.INITIAL_FAST_VIEW_BAR_LOCATION);
1031        } else {
1032            apiStore
1033                    .setValue(
1034                            IWorkbenchPreferenceConstants.INITIAL_FAST_VIEW_BAR_LOCATION,
1035                            fastViewLoc);
1036        }
1037
1038        int idx = themeCombo.getSelectionIndex();
1039        if (idx <= 0) {
1040            Workbench.getInstance().getThemeManager().setCurrentTheme(
1041                    IThemeManager.DEFAULT_THEME);
1042            refreshThemeCombo(IThemeManager.DEFAULT_THEME);
1043        } else {
1044            IThemeDescriptor applyTheme = WorkbenchPlugin.getDefault().getThemeRegistry().getThemes()[idx - 1];
1045            Workbench.getInstance().getThemeManager()
1046                    .setCurrentTheme(applyTheme.getId());
1047            refreshThemeCombo(applyTheme.getId());
1048        }
1049        refreshThemeDescriptionText();
1050        
1051        apiStore.setValue(
1052                IWorkbenchPreferenceConstants.SHOW_TRADITIONAL_STYLE_TABS,
1053                showTraditionalStyleTabs.getSelection());
1054        apiStore.setValue(IWorkbenchPreferenceConstants.ENABLE_ANIMATIONS,
1055                enableAnimations.getSelection());
1056
1057        PrefUtil.savePrefs();
1058
1059        // we can get here through performApply, in that case only post one
1060
// restart
1061
if (restart && !restartPosted) {
1062            if (getContainer() instanceof IWorkbenchPreferenceContainer) {
1063                IWorkbenchPreferenceContainer container = (IWorkbenchPreferenceContainer) getContainer();
1064                UIJob job = new UIJob(WorkbenchMessages.ViewsPreference_restartRequestJobName) {
1065                    public IStatus runInUIThread(IProgressMonitor monitor) {
1066                        // make sure they really want to do this
1067
int really = new MessageDialog(
1068                                null,
1069                                WorkbenchMessages.ViewsPreference_presentationConfirm_title,
1070                                null,
1071                                WorkbenchMessages.ViewsPreference_presentationConfirm_message,
1072                                MessageDialog.QUESTION,
1073                                new String JavaDoc[] {
1074                                        WorkbenchMessages.ViewsPreference_presentationConfirm_yes,
1075                                        WorkbenchMessages.ViewsPreference_presentationConfirm_no },
1076                                1).open();
1077                        if (really == Window.OK) {
1078                            PlatformUI.getWorkbench().restart();
1079                        }
1080                        return Status.OK_STATUS;
1081                    }
1082                };
1083                job.setSystem(true);
1084                container.registerUpdateJob(job);
1085                restartPosted = true;
1086            }
1087        }
1088        return true;
1089    }
1090}
1091
Popular Tags