001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.dialogs;
011:
012: import org.eclipse.jface.dialogs.IDialogConstants;
013: import org.eclipse.jface.preference.IPreferenceStore;
014: import org.eclipse.jface.preference.PreferencePage;
015: import org.eclipse.jface.util.OpenStrategy;
016: import org.eclipse.swt.SWT;
017: import org.eclipse.swt.events.SelectionAdapter;
018: import org.eclipse.swt.events.SelectionEvent;
019: import org.eclipse.swt.graphics.Font;
020: import org.eclipse.swt.layout.GridData;
021: import org.eclipse.swt.layout.GridLayout;
022: import org.eclipse.swt.widgets.Button;
023: import org.eclipse.swt.widgets.Combo;
024: import org.eclipse.swt.widgets.Composite;
025: import org.eclipse.swt.widgets.Control;
026: import org.eclipse.swt.widgets.Group;
027: import org.eclipse.swt.widgets.Label;
028: import org.eclipse.ui.IWorkbench;
029: import org.eclipse.ui.IWorkbenchPreferenceConstants;
030: import org.eclipse.ui.IWorkbenchPreferencePage;
031: import org.eclipse.ui.IWorkbenchWindow;
032: import org.eclipse.ui.PlatformUI;
033: import org.eclipse.ui.internal.IPreferenceConstants;
034: import org.eclipse.ui.internal.IWorkbenchHelpContextIds;
035: import org.eclipse.ui.internal.WorkbenchMessages;
036: import org.eclipse.ui.internal.WorkbenchPlugin;
037: import org.eclipse.ui.internal.WorkbenchWindow;
038: import org.eclipse.ui.internal.util.PrefUtil;
039:
040: /**
041: * Generic workbench main preference page.
042: */
043: public class WorkbenchPreferencePage extends PreferencePage implements
044: IWorkbenchPreferencePage {
045: private Button stickyCycleButton;
046:
047: private Button doubleClickButton;
048:
049: private Button singleClickButton;
050:
051: private Button selectOnHoverButton;
052:
053: private Button openAfterDelayButton;
054:
055: private Button showUserDialogButton;
056:
057: private boolean openOnSingleClick;
058:
059: private boolean selectOnHover;
060:
061: private boolean openAfterDelay;
062:
063: private Button showHeapStatusButton;
064:
065: /*
066: * (non-Javadoc)
067: *
068: * @see org.eclipse.jface.preference.PreferencePage
069: */
070: protected Control createContents(Composite parent) {
071:
072: // @issue if the product subclasses this page, then it should provide
073: // the help content
074: PlatformUI.getWorkbench().getHelpSystem().setHelp(parent,
075: IWorkbenchHelpContextIds.WORKBENCH_PREFERENCE_PAGE);
076:
077: Composite composite = createComposite(parent);
078:
079: createButtons(composite);
080:
081: createSpace(composite);
082: createOpenModeGroup(composite);
083:
084: applyDialogFont(composite);
085:
086: return composite;
087: }
088:
089: /**
090: * Create the buttons at the top of the preference page.
091: * @param composite
092: */
093: protected void createButtons(Composite composite) {
094: createShowUserDialogPref(composite);
095: createStickyCyclePref(composite);
096: createHeapStatusPref(composite);
097: }
098:
099: /**
100: * Create the widget for the user dialog preference.
101: *
102: * @param composite
103: */
104: protected void createShowUserDialogPref(Composite composite) {
105: showUserDialogButton = new Button(composite, SWT.CHECK);
106: showUserDialogButton
107: .setText(WorkbenchMessages.WorkbenchPreference_RunInBackgroundButton);
108: showUserDialogButton
109: .setToolTipText(WorkbenchMessages.WorkbenchPreference_RunInBackgroundToolTip);
110: showUserDialogButton.setSelection(WorkbenchPlugin.getDefault()
111: .getPreferenceStore().getBoolean(
112: IPreferenceConstants.RUN_IN_BACKGROUND));
113: }
114:
115: /**
116: * Create the widget for the heap status preference.
117: *
118: * @param composite
119: */
120: protected void createHeapStatusPref(Composite composite) {
121: showHeapStatusButton = new Button(composite, SWT.CHECK);
122: showHeapStatusButton
123: .setText(WorkbenchMessages.WorkbenchPreference_HeapStatusButton);
124: showHeapStatusButton
125: .setToolTipText(WorkbenchMessages.WorkbenchPreference_HeapStatusButtonToolTip);
126:
127: showHeapStatusButton
128: .setSelection(PrefUtil
129: .getAPIPreferenceStore()
130: .getBoolean(
131: IWorkbenchPreferenceConstants.SHOW_MEMORY_MONITOR));
132: }
133:
134: /**
135: * Creates the composite which will contain all the preference controls for
136: * this page.
137: *
138: * @param parent
139: * the parent composite
140: * @return the composite for this page
141: */
142: protected Composite createComposite(Composite parent) {
143: Composite composite = new Composite(parent, SWT.NONE);
144: GridLayout layout = new GridLayout();
145: layout.marginWidth = 0;
146: layout.marginHeight = 0;
147: composite.setLayout(layout);
148: composite.setLayoutData(new GridData(
149: GridData.VERTICAL_ALIGN_FILL
150: | GridData.HORIZONTAL_ALIGN_FILL));
151: return composite;
152: }
153:
154: protected void createStickyCyclePref(Composite composite) {
155: stickyCycleButton = new Button(composite, SWT.CHECK);
156: stickyCycleButton
157: .setText(WorkbenchMessages.WorkbenchPreference_stickyCycleButton);
158: stickyCycleButton.setSelection(getPreferenceStore().getBoolean(
159: IPreferenceConstants.STICKY_CYCLE));
160: }
161:
162: protected void createOpenModeGroup(Composite composite) {
163:
164: Font font = composite.getFont();
165:
166: Group buttonComposite = new Group(composite, SWT.LEFT);
167: GridLayout layout = new GridLayout();
168: buttonComposite.setLayout(layout);
169: GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL
170: | GridData.GRAB_HORIZONTAL);
171: buttonComposite.setLayoutData(data);
172: buttonComposite
173: .setText(WorkbenchMessages.WorkbenchPreference_openMode);
174:
175: String label = WorkbenchMessages.WorkbenchPreference_doubleClick;
176: doubleClickButton = createRadioButton(buttonComposite, label);
177: doubleClickButton.addSelectionListener(new SelectionAdapter() {
178:
179: public void widgetSelected(SelectionEvent e) {
180: selectClickMode(singleClickButton.getSelection());
181: }
182: });
183: doubleClickButton.setSelection(!openOnSingleClick);
184:
185: label = WorkbenchMessages.WorkbenchPreference_singleClick;
186: singleClickButton = createRadioButton(buttonComposite, label);
187: singleClickButton.addSelectionListener(new SelectionAdapter() {
188:
189: public void widgetSelected(SelectionEvent e) {
190: selectClickMode(singleClickButton.getSelection());
191: }
192: });
193: singleClickButton.setSelection(openOnSingleClick);
194:
195: label = WorkbenchMessages.WorkbenchPreference_singleClick_SelectOnHover;
196: selectOnHoverButton = new Button(buttonComposite, SWT.CHECK
197: | SWT.LEFT);
198: selectOnHoverButton.setText(label);
199: selectOnHoverButton.setEnabled(openOnSingleClick);
200: selectOnHoverButton.setSelection(selectOnHover);
201: selectOnHoverButton
202: .addSelectionListener(new SelectionAdapter() {
203:
204: public void widgetSelected(SelectionEvent e) {
205: selectOnHover = selectOnHoverButton
206: .getSelection();
207: }
208: });
209: data = new GridData();
210: data.horizontalIndent = 20;
211: selectOnHoverButton.setLayoutData(data);
212:
213: label = WorkbenchMessages.WorkbenchPreference_singleClick_OpenAfterDelay;
214: openAfterDelayButton = new Button(buttonComposite, SWT.CHECK
215: | SWT.LEFT);
216: openAfterDelayButton.setText(label);
217: openAfterDelayButton.setEnabled(openOnSingleClick);
218: openAfterDelayButton.setSelection(openAfterDelay);
219: openAfterDelayButton
220: .addSelectionListener(new SelectionAdapter() {
221:
222: public void widgetSelected(SelectionEvent e) {
223: openAfterDelay = openAfterDelayButton
224: .getSelection();
225: }
226: });
227: data = new GridData();
228: data.horizontalIndent = 20;
229: openAfterDelayButton.setLayoutData(data);
230:
231: createNoteComposite(
232: font,
233: buttonComposite,
234: WorkbenchMessages.Preference_note,
235: WorkbenchMessages.WorkbenchPreference_noEffectOnAllViews);
236: }
237:
238: private void selectClickMode(boolean singleClick) {
239: openOnSingleClick = singleClick;
240: selectOnHoverButton.setEnabled(openOnSingleClick);
241: openAfterDelayButton.setEnabled(openOnSingleClick);
242: }
243:
244: /**
245: * Utility method that creates a radio button instance and sets the default
246: * layout data.
247: *
248: * @param parent
249: * the parent for the new button
250: * @param label
251: * the label for the new button
252: * @return the newly-created button
253: */
254: protected static Button createRadioButton(Composite parent,
255: String label) {
256: Button button = new Button(parent, SWT.RADIO | SWT.LEFT);
257: button.setText(label);
258: return button;
259: }
260:
261: /**
262: * Utility method that creates a combo box
263: *
264: * @param parent
265: * the parent for the new label
266: * @return the new widget
267: */
268: protected static Combo createCombo(Composite parent) {
269: Combo combo = new Combo(parent, SWT.READ_ONLY);
270: GridData data = new GridData(GridData.FILL_HORIZONTAL);
271: data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
272: combo.setLayoutData(data);
273: return combo;
274: }
275:
276: /**
277: * Utility method that creates a label instance and sets the default layout
278: * data.
279: *
280: * @param parent
281: * the parent for the new label
282: * @param text
283: * the text for the new label
284: * @return the new label
285: */
286: protected static Label createLabel(Composite parent, String text) {
287: Label label = new Label(parent, SWT.LEFT);
288: label.setText(text);
289: GridData data = new GridData();
290: data.horizontalSpan = 1;
291: data.horizontalAlignment = GridData.FILL;
292: label.setLayoutData(data);
293: return label;
294: }
295:
296: /**
297: * Creates a tab of one horizontal spans.
298: *
299: * @param parent
300: * the parent in which the tab should be created
301: */
302: protected static void createSpace(Composite parent) {
303: Label vfiller = new Label(parent, SWT.LEFT);
304: GridData gridData = new GridData();
305: gridData = new GridData();
306: gridData.horizontalAlignment = GridData.BEGINNING;
307: gridData.grabExcessHorizontalSpace = false;
308: gridData.verticalAlignment = GridData.CENTER;
309: gridData.grabExcessVerticalSpace = false;
310: vfiller.setLayoutData(gridData);
311: }
312:
313: /**
314: * Returns preference store that belongs to the our plugin.
315: *
316: * @return the preference store for this plugin
317: */
318: protected IPreferenceStore doGetPreferenceStore() {
319: return WorkbenchPlugin.getDefault().getPreferenceStore();
320: }
321:
322: /**
323: * @see IWorkbenchPreferencePage
324: */
325: public void init(IWorkbench aWorkbench) {
326: IPreferenceStore store = getPreferenceStore();
327: openOnSingleClick = store
328: .getBoolean(IPreferenceConstants.OPEN_ON_SINGLE_CLICK);
329: selectOnHover = store
330: .getBoolean(IPreferenceConstants.SELECT_ON_HOVER);
331: openAfterDelay = store
332: .getBoolean(IPreferenceConstants.OPEN_AFTER_DELAY);
333: }
334:
335: /**
336: * The default button has been pressed.
337: */
338: protected void performDefaults() {
339: IPreferenceStore store = getPreferenceStore();
340: stickyCycleButton.setSelection(store
341: .getBoolean(IPreferenceConstants.STICKY_CYCLE));
342: openOnSingleClick = store
343: .getDefaultBoolean(IPreferenceConstants.OPEN_ON_SINGLE_CLICK);
344: selectOnHover = store
345: .getDefaultBoolean(IPreferenceConstants.SELECT_ON_HOVER);
346: openAfterDelay = store
347: .getDefaultBoolean(IPreferenceConstants.OPEN_AFTER_DELAY);
348: singleClickButton.setSelection(openOnSingleClick);
349: doubleClickButton.setSelection(!openOnSingleClick);
350: selectOnHoverButton.setSelection(selectOnHover);
351: openAfterDelayButton.setSelection(openAfterDelay);
352: selectOnHoverButton.setEnabled(openOnSingleClick);
353: openAfterDelayButton.setEnabled(openOnSingleClick);
354: stickyCycleButton.setSelection(store
355: .getDefaultBoolean(IPreferenceConstants.STICKY_CYCLE));
356: showUserDialogButton
357: .setSelection(store
358: .getDefaultBoolean(IPreferenceConstants.RUN_IN_BACKGROUND));
359: showHeapStatusButton
360: .setSelection(PrefUtil
361: .getAPIPreferenceStore()
362: .getDefaultBoolean(
363: IWorkbenchPreferenceConstants.SHOW_MEMORY_MONITOR));
364:
365: super .performDefaults();
366: }
367:
368: /**
369: * The user has pressed Ok. Store/apply this page's values appropriately.
370: */
371: public boolean performOk() {
372: IPreferenceStore store = getPreferenceStore();
373:
374: // store the keep cycle part dialogs sticky preference
375: store.setValue(IPreferenceConstants.STICKY_CYCLE,
376: stickyCycleButton.getSelection());
377: store.setValue(IPreferenceConstants.OPEN_ON_SINGLE_CLICK,
378: openOnSingleClick);
379: store.setValue(IPreferenceConstants.SELECT_ON_HOVER,
380: selectOnHover);
381: store.setValue(IPreferenceConstants.OPEN_AFTER_DELAY,
382: openAfterDelay);
383: store.setValue(IPreferenceConstants.RUN_IN_BACKGROUND,
384: showUserDialogButton.getSelection());
385: PrefUtil.getAPIPreferenceStore().setValue(
386: IWorkbenchPreferenceConstants.SHOW_MEMORY_MONITOR,
387: showHeapStatusButton.getSelection());
388: updateHeapStatus(showHeapStatusButton.getSelection());
389:
390: int singleClickMethod = openOnSingleClick ? OpenStrategy.SINGLE_CLICK
391: : OpenStrategy.DOUBLE_CLICK;
392: if (openOnSingleClick) {
393: if (selectOnHover) {
394: singleClickMethod |= OpenStrategy.SELECT_ON_HOVER;
395: }
396: if (openAfterDelay) {
397: singleClickMethod |= OpenStrategy.ARROW_KEYS_OPEN;
398: }
399: }
400: OpenStrategy.setOpenMethod(singleClickMethod);
401:
402: PrefUtil.savePrefs();
403: return true;
404: }
405:
406: /**
407: * Show or hide the heap status based on selection.
408: * @param selection
409: */
410: private void updateHeapStatus(boolean selection) {
411: IWorkbenchWindow[] windows = PlatformUI.getWorkbench()
412: .getWorkbenchWindows();
413: for (int i = 0; i < windows.length; i++) {
414: IWorkbenchWindow window = windows[i];
415: if (window instanceof WorkbenchWindow) {
416: ((WorkbenchWindow) window).showHeapStatus(selection);
417: }
418: }
419:
420: }
421: }
|