001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 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: * Sebastian Davids <sdavids@gmx.de> - bug 97667 [Preferences] Pref Page General/Editors - problems
011: *******************************************************************************/package org.eclipse.ui.internal.dialogs;
012:
013: import org.eclipse.jface.preference.FieldEditor;
014: import org.eclipse.jface.preference.IPreferenceStore;
015: import org.eclipse.jface.preference.IntegerFieldEditor;
016: import org.eclipse.jface.preference.PreferencePage;
017: import org.eclipse.jface.preference.StringFieldEditor;
018: import org.eclipse.jface.util.IPropertyChangeListener;
019: import org.eclipse.jface.util.PropertyChangeEvent;
020: import org.eclipse.osgi.util.NLS;
021: import org.eclipse.swt.SWT;
022: import org.eclipse.swt.events.SelectionAdapter;
023: import org.eclipse.swt.events.SelectionEvent;
024: import org.eclipse.swt.layout.GridData;
025: import org.eclipse.swt.layout.GridLayout;
026: import org.eclipse.swt.widgets.Button;
027: import org.eclipse.swt.widgets.Composite;
028: import org.eclipse.swt.widgets.Control;
029: import org.eclipse.swt.widgets.Group;
030: import org.eclipse.ui.IWorkbench;
031: import org.eclipse.ui.IWorkbenchPreferenceConstants;
032: import org.eclipse.ui.IWorkbenchPreferencePage;
033: import org.eclipse.ui.PlatformUI;
034: import org.eclipse.ui.internal.EditorHistory;
035: import org.eclipse.ui.internal.IPreferenceConstants;
036: import org.eclipse.ui.internal.IWorkbenchHelpContextIds;
037: import org.eclipse.ui.internal.WorkbenchMessages;
038: import org.eclipse.ui.internal.WorkbenchPlugin;
039: import org.eclipse.ui.internal.tweaklets.TabBehaviour;
040: import org.eclipse.ui.internal.tweaklets.Tweaklets;
041: import org.eclipse.ui.internal.util.PrefUtil;
042:
043: /**
044: * The Editors preference page of the workbench.
045: */
046: public class EditorsPreferencePage extends PreferencePage implements
047: IWorkbenchPreferencePage {
048: private static final int REUSE_INDENT = 10;
049:
050: protected Composite editorReuseGroup;
051:
052: private Button reuseEditors;
053:
054: protected Button showMultipleEditorTabs;
055:
056: protected Button useIPersistableEditor;
057:
058: private Composite editorReuseIndentGroup;
059:
060: private Composite editorReuseThresholdGroup;
061:
062: private IntegerFieldEditor reuseEditorsThreshold;
063:
064: private Group dirtyEditorReuseGroup;
065:
066: private Button openNewEditor;
067:
068: private Button promptToReuseEditor;
069:
070: private IntegerFieldEditor recentFilesEditor;
071:
072: private IPropertyChangeListener validityChangeListener = new IPropertyChangeListener() {
073: public void propertyChange(PropertyChangeEvent event) {
074: if (event.getProperty().equals(FieldEditor.IS_VALID)) {
075: updateValidState();
076: }
077: }
078: };
079:
080: private Button promptWhenStillOpenEditor;
081:
082: protected Control createContents(Composite parent) {
083: Composite composite = createComposite(parent);
084:
085: createEditorHistoryGroup(composite);
086:
087: createSpace(composite);
088: createShowMultipleEditorTabsPref(composite);
089: createUseIPersistablePref(composite);
090: createPromptWhenStillOpenPref(composite);
091: createEditorReuseGroup(composite);
092: ((TabBehaviour) Tweaklets.get(TabBehaviour.KEY))
093: .setPreferenceVisibility(editorReuseGroup,
094: showMultipleEditorTabs);
095:
096: updateValidState();
097:
098: applyDialogFont(composite);
099:
100: setHelpContext(parent);
101:
102: return composite;
103: }
104:
105: protected void setHelpContext(Composite parent) {
106: // @issue the IDE subclasses this, but should provide its own help
107: PlatformUI
108: .getWorkbench()
109: .getHelpSystem()
110: .setHelp(
111: parent,
112: IWorkbenchHelpContextIds.WORKBENCH_EDITOR_PREFERENCE_PAGE);
113: }
114:
115: protected void createSpace(Composite parent) {
116: WorkbenchPreferencePage.createSpace(parent);
117: }
118:
119: protected void createShowMultipleEditorTabsPref(Composite composite) {
120: showMultipleEditorTabs = new Button(composite, SWT.CHECK);
121: showMultipleEditorTabs
122: .setText(WorkbenchMessages.WorkbenchPreference_showMultipleEditorTabsButton);
123: showMultipleEditorTabs
124: .setSelection(getPreferenceStore().getBoolean(
125: IPreferenceConstants.SHOW_MULTIPLE_EDITOR_TABS));
126: setButtonLayoutData(showMultipleEditorTabs);
127: }
128:
129: protected void createUseIPersistablePref(Composite composite) {
130: useIPersistableEditor = new Button(composite, SWT.CHECK);
131: useIPersistableEditor
132: .setText(WorkbenchMessages.WorkbenchPreference_useIPersistableEditorButton);
133: useIPersistableEditor.setSelection(getPreferenceStore()
134: .getBoolean(
135: IPreferenceConstants.USE_IPERSISTABLE_EDITORS));
136: setButtonLayoutData(useIPersistableEditor);
137: }
138:
139: protected void createPromptWhenStillOpenPref(Composite composite) {
140: promptWhenStillOpenEditor = new Button(composite, SWT.CHECK);
141: promptWhenStillOpenEditor
142: .setText(WorkbenchMessages.WorkbenchPreference_promptWhenStillOpenButton);
143: promptWhenStillOpenEditor
144: .setSelection(getAPIPreferenceStore()
145: .getBoolean(
146: IWorkbenchPreferenceConstants.PROMPT_WHEN_SAVEABLE_STILL_OPEN));
147: setButtonLayoutData(promptWhenStillOpenEditor);
148: }
149:
150: protected Composite createComposite(Composite parent) {
151: Composite composite = new Composite(parent, SWT.NULL);
152: GridLayout layout = new GridLayout();
153: layout.marginWidth = 0;
154: layout.marginHeight = 0;
155: composite.setLayout(layout);
156: composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false,
157: false));
158: return composite;
159: }
160:
161: public void init(IWorkbench workbench) {
162: // do nothing
163: }
164:
165: protected void performDefaults() {
166: IPreferenceStore store = getPreferenceStore();
167: showMultipleEditorTabs
168: .setSelection(store
169: .getDefaultBoolean(IPreferenceConstants.SHOW_MULTIPLE_EDITOR_TABS));
170: useIPersistableEditor
171: .setSelection(store
172: .getDefaultBoolean(IPreferenceConstants.USE_IPERSISTABLE_EDITORS));
173: promptWhenStillOpenEditor
174: .setSelection(getAPIPreferenceStore()
175: .getDefaultBoolean(
176: IWorkbenchPreferenceConstants.PROMPT_WHEN_SAVEABLE_STILL_OPEN));
177: reuseEditors
178: .setSelection(store
179: .getDefaultBoolean(IPreferenceConstants.REUSE_EDITORS_BOOLEAN));
180: dirtyEditorReuseGroup.setEnabled(reuseEditors.getSelection());
181: openNewEditor
182: .setSelection(!store
183: .getDefaultBoolean(IPreferenceConstants.REUSE_DIRTY_EDITORS));
184: openNewEditor.setEnabled(reuseEditors.getSelection());
185: promptToReuseEditor
186: .setSelection(store
187: .getDefaultBoolean(IPreferenceConstants.REUSE_DIRTY_EDITORS));
188: promptToReuseEditor.setEnabled(reuseEditors.getSelection());
189: reuseEditorsThreshold.loadDefault();
190: reuseEditorsThreshold
191: .getLabelControl(editorReuseThresholdGroup).setEnabled(
192: reuseEditors.getSelection());
193: reuseEditorsThreshold.getTextControl(editorReuseThresholdGroup)
194: .setEnabled(reuseEditors.getSelection());
195: recentFilesEditor.loadDefault();
196: }
197:
198: public boolean performOk() {
199: IPreferenceStore store = getPreferenceStore();
200: store.setValue(IPreferenceConstants.SHOW_MULTIPLE_EDITOR_TABS,
201: showMultipleEditorTabs.getSelection());
202: store.setValue(IPreferenceConstants.USE_IPERSISTABLE_EDITORS,
203: useIPersistableEditor.getSelection());
204: getAPIPreferenceStore()
205: .setValue(
206: IWorkbenchPreferenceConstants.PROMPT_WHEN_SAVEABLE_STILL_OPEN,
207: promptWhenStillOpenEditor.getSelection());
208:
209: // store the reuse editors setting
210: store.setValue(IPreferenceConstants.REUSE_EDITORS_BOOLEAN,
211: reuseEditors.getSelection());
212: store.setValue(IPreferenceConstants.REUSE_DIRTY_EDITORS,
213: promptToReuseEditor.getSelection());
214: reuseEditorsThreshold.store();
215:
216: // store the recent files setting
217: recentFilesEditor.store();
218:
219: PrefUtil.savePrefs();
220: return super .performOk();
221: }
222:
223: /**
224: * Returns preference store that belongs to the our plugin.
225: *
226: * @return the preference store for this plugin
227: */
228: protected IPreferenceStore doGetPreferenceStore() {
229: return WorkbenchPlugin.getDefault().getPreferenceStore();
230: }
231:
232: protected IPreferenceStore getAPIPreferenceStore() {
233: return PrefUtil.getAPIPreferenceStore();
234: }
235:
236: protected void updateValidState() {
237: if (!recentFilesEditor.isValid()) {
238: setErrorMessage(recentFilesEditor.getErrorMessage());
239: setValid(false);
240: } else if (!reuseEditorsThreshold.isValid()) {
241: setErrorMessage(reuseEditorsThreshold.getErrorMessage());
242: setValid(false);
243: } else {
244: setErrorMessage(null);
245: setValid(true);
246: }
247: }
248:
249: /**
250: * Create a composite that contains entry fields specifying editor reuse preferences.
251: */
252: protected void createEditorReuseGroup(Composite composite) {
253: editorReuseGroup = new Composite(composite, SWT.LEFT);
254: GridLayout layout = new GridLayout();
255: // Line up with other entries in preference page
256: layout.marginWidth = 0;
257: layout.marginHeight = 0;
258: editorReuseGroup.setLayout(layout);
259: editorReuseGroup.setLayoutData(new GridData(
260: GridData.HORIZONTAL_ALIGN_FILL
261: | GridData.GRAB_HORIZONTAL));
262:
263: reuseEditors = new Button(editorReuseGroup, SWT.CHECK);
264: reuseEditors
265: .setText(WorkbenchMessages.WorkbenchPreference_reuseEditors);
266: reuseEditors.setLayoutData(new GridData());
267:
268: IPreferenceStore store = WorkbenchPlugin.getDefault()
269: .getPreferenceStore();
270: reuseEditors
271: .setSelection(store
272: .getBoolean(IPreferenceConstants.REUSE_EDITORS_BOOLEAN));
273: reuseEditors.addSelectionListener(new SelectionAdapter() {
274: public void widgetSelected(SelectionEvent e) {
275: reuseEditorsThreshold.getLabelControl(
276: editorReuseThresholdGroup).setEnabled(
277: reuseEditors.getSelection());
278: reuseEditorsThreshold.getTextControl(
279: editorReuseThresholdGroup).setEnabled(
280: reuseEditors.getSelection());
281: dirtyEditorReuseGroup.setEnabled(reuseEditors
282: .getSelection());
283: openNewEditor.setEnabled(reuseEditors.getSelection());
284: promptToReuseEditor.setEnabled(reuseEditors
285: .getSelection());
286: }
287: });
288:
289: editorReuseIndentGroup = new Composite(editorReuseGroup,
290: SWT.LEFT);
291: GridLayout indentLayout = new GridLayout();
292: indentLayout.marginLeft = REUSE_INDENT;
293: indentLayout.marginRight = 0;
294: editorReuseIndentGroup.setLayout(indentLayout);
295: editorReuseIndentGroup.setLayoutData(new GridData(SWT.FILL,
296: SWT.CENTER, true, false));
297:
298: editorReuseThresholdGroup = new Composite(
299: editorReuseIndentGroup, SWT.LEFT);
300: layout = new GridLayout();
301: layout.marginWidth = 0;
302: editorReuseThresholdGroup.setLayout(layout);
303: editorReuseThresholdGroup.setLayoutData(new GridData(SWT.FILL,
304: SWT.CENTER, true, false));
305:
306: reuseEditorsThreshold = new IntegerFieldEditor(
307: IPreferenceConstants.REUSE_EDITORS,
308: WorkbenchMessages.WorkbenchPreference_reuseEditorsThreshold,
309: editorReuseThresholdGroup);
310:
311: reuseEditorsThreshold.setPreferenceStore(WorkbenchPlugin
312: .getDefault().getPreferenceStore());
313: reuseEditorsThreshold.setPage(this );
314: reuseEditorsThreshold.setTextLimit(2);
315: reuseEditorsThreshold
316: .setErrorMessage(WorkbenchMessages.WorkbenchPreference_reuseEditorsThresholdError);
317: reuseEditorsThreshold
318: .setValidateStrategy(StringFieldEditor.VALIDATE_ON_KEY_STROKE);
319: reuseEditorsThreshold.setValidRange(1, 99);
320: reuseEditorsThreshold.load();
321: reuseEditorsThreshold
322: .getLabelControl(editorReuseThresholdGroup).setEnabled(
323: reuseEditors.getSelection());
324: reuseEditorsThreshold.getTextControl(editorReuseThresholdGroup)
325: .setEnabled(reuseEditors.getSelection());
326: reuseEditorsThreshold
327: .setPropertyChangeListener(validityChangeListener);
328:
329: dirtyEditorReuseGroup = new Group(editorReuseIndentGroup,
330: SWT.NONE);
331: layout = new GridLayout();
332: layout.marginWidth = 0;
333: dirtyEditorReuseGroup.setLayout(layout);
334: dirtyEditorReuseGroup.setLayoutData(new GridData(SWT.FILL,
335: SWT.CENTER, true, false));
336: dirtyEditorReuseGroup
337: .setText(WorkbenchMessages.WorkbenchPreference_reuseDirtyEditorGroupTitle);
338: dirtyEditorReuseGroup.setEnabled(reuseEditors.getSelection());
339:
340: promptToReuseEditor = new Button(dirtyEditorReuseGroup,
341: SWT.RADIO);
342: promptToReuseEditor
343: .setText(WorkbenchMessages.WorkbenchPreference_promptToReuseEditor);
344: promptToReuseEditor.setSelection(store
345: .getBoolean(IPreferenceConstants.REUSE_DIRTY_EDITORS));
346: promptToReuseEditor.setEnabled(reuseEditors.getSelection());
347:
348: openNewEditor = new Button(dirtyEditorReuseGroup, SWT.RADIO);
349: openNewEditor
350: .setText(WorkbenchMessages.WorkbenchPreference_openNewEditor);
351: openNewEditor.setSelection(!store
352: .getBoolean(IPreferenceConstants.REUSE_DIRTY_EDITORS));
353: openNewEditor.setEnabled(reuseEditors.getSelection());
354: }
355:
356: /**
357: * Create a composite that contains entry fields specifying editor history preferences.
358: */
359: protected void createEditorHistoryGroup(Composite composite) {
360: Composite groupComposite = new Composite(composite, SWT.LEFT);
361: GridLayout layout = new GridLayout();
362: layout.numColumns = 2;
363: groupComposite.setLayout(layout);
364: GridData gd = new GridData();
365: gd.horizontalAlignment = GridData.FILL;
366: gd.grabExcessHorizontalSpace = true;
367: groupComposite.setLayoutData(gd);
368:
369: recentFilesEditor = new IntegerFieldEditor(
370: IPreferenceConstants.RECENT_FILES,
371: WorkbenchMessages.WorkbenchPreference_recentFiles,
372: groupComposite);
373:
374: recentFilesEditor.setPreferenceStore(WorkbenchPlugin
375: .getDefault().getPreferenceStore());
376: recentFilesEditor.setPage(this );
377: recentFilesEditor.setTextLimit(Integer.toString(
378: EditorHistory.MAX_SIZE).length());
379: recentFilesEditor.setErrorMessage(NLS.bind(
380: WorkbenchMessages.WorkbenchPreference_recentFilesError,
381: new Integer(EditorHistory.MAX_SIZE)));
382: recentFilesEditor
383: .setValidateStrategy(StringFieldEditor.VALIDATE_ON_KEY_STROKE);
384: recentFilesEditor.setValidRange(0, EditorHistory.MAX_SIZE);
385: recentFilesEditor.load();
386: recentFilesEditor
387: .setPropertyChangeListener(validityChangeListener);
388: }
389: }
|