KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.dialogs;
12
13 import org.eclipse.jface.dialogs.IDialogConstants;
14 import org.eclipse.jface.preference.IPreferenceStore;
15 import org.eclipse.jface.preference.PreferencePage;
16 import org.eclipse.jface.util.OpenStrategy;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.events.SelectionAdapter;
19 import org.eclipse.swt.events.SelectionEvent;
20 import org.eclipse.swt.graphics.Font;
21 import org.eclipse.swt.layout.GridData;
22 import org.eclipse.swt.layout.GridLayout;
23 import org.eclipse.swt.widgets.Button;
24 import org.eclipse.swt.widgets.Combo;
25 import org.eclipse.swt.widgets.Composite;
26 import org.eclipse.swt.widgets.Control;
27 import org.eclipse.swt.widgets.Group;
28 import org.eclipse.swt.widgets.Label;
29 import org.eclipse.ui.IWorkbench;
30 import org.eclipse.ui.IWorkbenchPreferenceConstants;
31 import org.eclipse.ui.IWorkbenchPreferencePage;
32 import org.eclipse.ui.IWorkbenchWindow;
33 import org.eclipse.ui.PlatformUI;
34 import org.eclipse.ui.internal.IPreferenceConstants;
35 import org.eclipse.ui.internal.IWorkbenchHelpContextIds;
36 import org.eclipse.ui.internal.WorkbenchMessages;
37 import org.eclipse.ui.internal.WorkbenchPlugin;
38 import org.eclipse.ui.internal.WorkbenchWindow;
39 import org.eclipse.ui.internal.util.PrefUtil;
40
41 /**
42  * Generic workbench main preference page.
43  */

44 public class WorkbenchPreferencePage extends PreferencePage implements
45         IWorkbenchPreferencePage {
46     private Button stickyCycleButton;
47
48     private Button doubleClickButton;
49
50     private Button singleClickButton;
51
52     private Button selectOnHoverButton;
53
54     private Button openAfterDelayButton;
55
56     private Button showUserDialogButton;
57
58     private boolean openOnSingleClick;
59
60     private boolean selectOnHover;
61
62     private boolean openAfterDelay;
63
64     private Button showHeapStatusButton;
65
66     /*
67      * (non-Javadoc)
68      *
69      * @see org.eclipse.jface.preference.PreferencePage
70      */

71     protected Control createContents(Composite parent) {
72
73         // @issue if the product subclasses this page, then it should provide
74
// the help content
75
PlatformUI.getWorkbench().getHelpSystem().setHelp(parent,
76                 IWorkbenchHelpContextIds.WORKBENCH_PREFERENCE_PAGE);
77
78         Composite composite = createComposite(parent);
79
80         createButtons(composite);
81
82         createSpace(composite);
83         createOpenModeGroup(composite);
84
85         applyDialogFont(composite);
86
87         return composite;
88     }
89
90     /**
91      * Create the buttons at the top of the preference page.
92      * @param composite
93      */

94     protected void createButtons(Composite composite) {
95         createShowUserDialogPref(composite);
96         createStickyCyclePref(composite);
97         createHeapStatusPref(composite);
98     }
99
100     /**
101      * Create the widget for the user dialog preference.
102      *
103      * @param composite
104      */

105     protected void createShowUserDialogPref(Composite composite) {
106         showUserDialogButton = new Button(composite, SWT.CHECK);
107         showUserDialogButton.setText(WorkbenchMessages.WorkbenchPreference_RunInBackgroundButton);
108         showUserDialogButton.setToolTipText(WorkbenchMessages.WorkbenchPreference_RunInBackgroundToolTip);
109         showUserDialogButton.setSelection(WorkbenchPlugin.getDefault()
110                 .getPreferenceStore().getBoolean(
111                         IPreferenceConstants.RUN_IN_BACKGROUND));
112     }
113     
114     /**
115      * Create the widget for the heap status preference.
116      *
117      * @param composite
118      */

119     protected void createHeapStatusPref(Composite composite) {
120         showHeapStatusButton = new Button(composite, SWT.CHECK);
121         showHeapStatusButton.setText(WorkbenchMessages.WorkbenchPreference_HeapStatusButton);
122         showHeapStatusButton.setToolTipText(WorkbenchMessages.WorkbenchPreference_HeapStatusButtonToolTip);
123         
124         showHeapStatusButton.setSelection(PrefUtil.getAPIPreferenceStore().getBoolean(
125                         IWorkbenchPreferenceConstants.SHOW_MEMORY_MONITOR));
126     }
127
128     /**
129      * Creates the composite which will contain all the preference controls for
130      * this page.
131      *
132      * @param parent
133      * the parent composite
134      * @return the composite for this page
135      */

136     protected Composite createComposite(Composite parent) {
137         Composite composite = new Composite(parent, SWT.NONE);
138         GridLayout layout = new GridLayout();
139         layout.marginWidth = 0;
140         layout.marginHeight = 0;
141         composite.setLayout(layout);
142         composite.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_FILL
143                 | GridData.HORIZONTAL_ALIGN_FILL));
144         return composite;
145     }
146
147     protected void createStickyCyclePref(Composite composite) {
148         stickyCycleButton = new Button(composite, SWT.CHECK);
149         stickyCycleButton.setText(WorkbenchMessages.WorkbenchPreference_stickyCycleButton);
150         stickyCycleButton.setSelection(getPreferenceStore().getBoolean(
151                 IPreferenceConstants.STICKY_CYCLE));
152     }
153
154     protected void createOpenModeGroup(Composite composite) {
155
156         Font font = composite.getFont();
157
158         Group buttonComposite = new Group(composite, SWT.LEFT);
159         GridLayout layout = new GridLayout();
160         buttonComposite.setLayout(layout);
161         GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL
162                 | GridData.GRAB_HORIZONTAL);
163         buttonComposite.setLayoutData(data);
164         buttonComposite.setText(WorkbenchMessages.WorkbenchPreference_openMode);
165
166         String JavaDoc label = WorkbenchMessages.WorkbenchPreference_doubleClick;
167         doubleClickButton = createRadioButton(buttonComposite, label);
168         doubleClickButton.addSelectionListener(new SelectionAdapter() {
169
170             public void widgetSelected(SelectionEvent e) {
171                 selectClickMode(singleClickButton.getSelection());
172             }
173         });
174         doubleClickButton.setSelection(!openOnSingleClick);
175
176         label = WorkbenchMessages.WorkbenchPreference_singleClick;
177         singleClickButton = createRadioButton(buttonComposite, label);
178         singleClickButton.addSelectionListener(new SelectionAdapter() {
179
180             public void widgetSelected(SelectionEvent e) {
181                 selectClickMode(singleClickButton.getSelection());
182             }
183         });
184         singleClickButton.setSelection(openOnSingleClick);
185
186         label = WorkbenchMessages.WorkbenchPreference_singleClick_SelectOnHover;
187         selectOnHoverButton = new Button(buttonComposite, SWT.CHECK | SWT.LEFT);
188         selectOnHoverButton.setText(label);
189         selectOnHoverButton.setEnabled(openOnSingleClick);
190         selectOnHoverButton.setSelection(selectOnHover);
191         selectOnHoverButton.addSelectionListener(new SelectionAdapter() {
192
193             public void widgetSelected(SelectionEvent e) {
194                 selectOnHover = selectOnHoverButton.getSelection();
195             }
196         });
197         data = new GridData();
198         data.horizontalIndent = 20;
199         selectOnHoverButton.setLayoutData(data);
200
201         label = WorkbenchMessages.WorkbenchPreference_singleClick_OpenAfterDelay;
202         openAfterDelayButton = new Button(buttonComposite, SWT.CHECK | SWT.LEFT);
203         openAfterDelayButton.setText(label);
204         openAfterDelayButton.setEnabled(openOnSingleClick);
205         openAfterDelayButton.setSelection(openAfterDelay);
206         openAfterDelayButton.addSelectionListener(new SelectionAdapter() {
207
208             public void widgetSelected(SelectionEvent e) {
209                 openAfterDelay = openAfterDelayButton.getSelection();
210             }
211         });
212         data = new GridData();
213         data.horizontalIndent = 20;
214         openAfterDelayButton.setLayoutData(data);
215
216         createNoteComposite(font, buttonComposite, WorkbenchMessages.Preference_note,
217                 WorkbenchMessages.WorkbenchPreference_noEffectOnAllViews);
218     }
219
220     private void selectClickMode(boolean singleClick) {
221         openOnSingleClick = singleClick;
222         selectOnHoverButton.setEnabled(openOnSingleClick);
223         openAfterDelayButton.setEnabled(openOnSingleClick);
224     }
225
226     /**
227      * Utility method that creates a radio button instance and sets the default
228      * layout data.
229      *
230      * @param parent
231      * the parent for the new button
232      * @param label
233      * the label for the new button
234      * @return the newly-created button
235      */

236     protected static Button createRadioButton(Composite parent, String JavaDoc label) {
237         Button button = new Button(parent, SWT.RADIO | SWT.LEFT);
238         button.setText(label);
239         return button;
240     }
241
242     /**
243      * Utility method that creates a combo box
244      *
245      * @param parent
246      * the parent for the new label
247      * @return the new widget
248      */

249     protected static Combo createCombo(Composite parent) {
250         Combo combo = new Combo(parent, SWT.READ_ONLY);
251         GridData data = new GridData(GridData.FILL_HORIZONTAL);
252         data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
253         combo.setLayoutData(data);
254         return combo;
255     }
256
257     /**
258      * Utility method that creates a label instance and sets the default layout
259      * data.
260      *
261      * @param parent
262      * the parent for the new label
263      * @param text
264      * the text for the new label
265      * @return the new label
266      */

267     protected static Label createLabel(Composite parent, String JavaDoc text) {
268         Label label = new Label(parent, SWT.LEFT);
269         label.setText(text);
270         GridData data = new GridData();
271         data.horizontalSpan = 1;
272         data.horizontalAlignment = GridData.FILL;
273         label.setLayoutData(data);
274         return label;
275     }
276
277     /**
278      * Creates a tab of one horizontal spans.
279      *
280      * @param parent
281      * the parent in which the tab should be created
282      */

283     protected static void createSpace(Composite parent) {
284         Label vfiller = new Label(parent, SWT.LEFT);
285         GridData gridData = new GridData();
286         gridData = new GridData();
287         gridData.horizontalAlignment = GridData.BEGINNING;
288         gridData.grabExcessHorizontalSpace = false;
289         gridData.verticalAlignment = GridData.CENTER;
290         gridData.grabExcessVerticalSpace = false;
291         vfiller.setLayoutData(gridData);
292     }
293
294     /**
295      * Returns preference store that belongs to the our plugin.
296      *
297      * @return the preference store for this plugin
298      */

299     protected IPreferenceStore doGetPreferenceStore() {
300         return WorkbenchPlugin.getDefault().getPreferenceStore();
301     }
302
303     /**
304      * @see IWorkbenchPreferencePage
305      */

306     public void init(IWorkbench aWorkbench) {
307         IPreferenceStore store = getPreferenceStore();
308         openOnSingleClick = store
309                 .getBoolean(IPreferenceConstants.OPEN_ON_SINGLE_CLICK);
310         selectOnHover = store.getBoolean(IPreferenceConstants.SELECT_ON_HOVER);
311         openAfterDelay = store
312                 .getBoolean(IPreferenceConstants.OPEN_AFTER_DELAY);
313     }
314
315     /**
316      * The default button has been pressed.
317      */

318     protected void performDefaults() {
319         IPreferenceStore store = getPreferenceStore();
320         stickyCycleButton.setSelection(store
321                 .getBoolean(IPreferenceConstants.STICKY_CYCLE));
322         openOnSingleClick = store
323                 .getDefaultBoolean(IPreferenceConstants.OPEN_ON_SINGLE_CLICK);
324         selectOnHover = store
325                 .getDefaultBoolean(IPreferenceConstants.SELECT_ON_HOVER);
326         openAfterDelay = store
327                 .getDefaultBoolean(IPreferenceConstants.OPEN_AFTER_DELAY);
328         singleClickButton.setSelection(openOnSingleClick);
329         doubleClickButton.setSelection(!openOnSingleClick);
330         selectOnHoverButton.setSelection(selectOnHover);
331         openAfterDelayButton.setSelection(openAfterDelay);
332         selectOnHoverButton.setEnabled(openOnSingleClick);
333         openAfterDelayButton.setEnabled(openOnSingleClick);
334         stickyCycleButton.setSelection(store
335                 .getDefaultBoolean(IPreferenceConstants.STICKY_CYCLE));
336         showUserDialogButton.setSelection(store.getDefaultBoolean(
337                 IPreferenceConstants.RUN_IN_BACKGROUND));
338         showHeapStatusButton.setSelection(PrefUtil.getAPIPreferenceStore().getDefaultBoolean(
339                 IWorkbenchPreferenceConstants.SHOW_MEMORY_MONITOR));
340         
341         super.performDefaults();
342     }
343
344     /**
345      * The user has pressed Ok. Store/apply this page's values appropriately.
346      */

347     public boolean performOk() {
348         IPreferenceStore store = getPreferenceStore();
349
350         // store the keep cycle part dialogs sticky preference
351
store.setValue(IPreferenceConstants.STICKY_CYCLE, stickyCycleButton
352                 .getSelection());
353         store.setValue(IPreferenceConstants.OPEN_ON_SINGLE_CLICK,
354                 openOnSingleClick);
355         store.setValue(IPreferenceConstants.SELECT_ON_HOVER, selectOnHover);
356         store.setValue(IPreferenceConstants.OPEN_AFTER_DELAY, openAfterDelay);
357         store.setValue(IPreferenceConstants.RUN_IN_BACKGROUND,
358                 showUserDialogButton.getSelection());
359         PrefUtil.getAPIPreferenceStore().setValue(IWorkbenchPreferenceConstants.SHOW_MEMORY_MONITOR, showHeapStatusButton.getSelection());
360         updateHeapStatus(showHeapStatusButton.getSelection());
361         
362         int singleClickMethod = openOnSingleClick ? OpenStrategy.SINGLE_CLICK
363                 : OpenStrategy.DOUBLE_CLICK;
364         if (openOnSingleClick) {
365             if (selectOnHover) {
366                 singleClickMethod |= OpenStrategy.SELECT_ON_HOVER;
367             }
368             if (openAfterDelay) {
369                 singleClickMethod |= OpenStrategy.ARROW_KEYS_OPEN;
370             }
371         }
372         OpenStrategy.setOpenMethod(singleClickMethod);
373
374         PrefUtil.savePrefs();
375         return true;
376     }
377
378     /**
379      * Show or hide the heap status based on selection.
380      * @param selection
381      */

382     private void updateHeapStatus(boolean selection) {
383         IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows();
384         for (int i = 0; i < windows.length; i++) {
385             IWorkbenchWindow window = windows[i];
386             if(window instanceof WorkbenchWindow){
387                 ((WorkbenchWindow) window).showHeapStatus(selection);
388             }
389         }
390         
391     }
392 }
393
Popular Tags