KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.eclipse.jface.preference.FieldEditor;
15 import org.eclipse.jface.preference.IPreferenceStore;
16 import org.eclipse.jface.preference.IntegerFieldEditor;
17 import org.eclipse.jface.preference.PreferencePage;
18 import org.eclipse.jface.preference.StringFieldEditor;
19 import org.eclipse.jface.util.IPropertyChangeListener;
20 import org.eclipse.jface.util.PropertyChangeEvent;
21 import org.eclipse.osgi.util.NLS;
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.events.SelectionAdapter;
24 import org.eclipse.swt.events.SelectionEvent;
25 import org.eclipse.swt.graphics.Font;
26 import org.eclipse.swt.layout.GridData;
27 import org.eclipse.swt.layout.GridLayout;
28 import org.eclipse.swt.widgets.Button;
29 import org.eclipse.swt.widgets.Composite;
30 import org.eclipse.swt.widgets.Control;
31 import org.eclipse.swt.widgets.Group;
32 import org.eclipse.ui.IWorkbench;
33 import org.eclipse.ui.IWorkbenchPreferenceConstants;
34 import org.eclipse.ui.IWorkbenchPreferencePage;
35 import org.eclipse.ui.PlatformUI;
36 import org.eclipse.ui.internal.EditorHistory;
37 import org.eclipse.ui.internal.IPreferenceConstants;
38 import org.eclipse.ui.internal.IWorkbenchHelpContextIds;
39 import org.eclipse.ui.internal.WorkbenchMessages;
40 import org.eclipse.ui.internal.WorkbenchPlugin;
41 import org.eclipse.ui.internal.tweaklets.TabBehaviour;
42 import org.eclipse.ui.internal.tweaklets.Tweaklets;
43 import org.eclipse.ui.internal.util.PrefUtil;
44
45 /**
46  * The Editors preference page of the workbench.
47  */

48 public class EditorsPreferencePage extends PreferencePage implements
49         IWorkbenchPreferencePage {
50     private static final int REUSE_INDENT = 10;
51
52     protected Composite editorReuseGroup;
53
54     private Button reuseEditors;
55
56     protected Button showMultipleEditorTabs;
57
58     protected Button useIPersistableEditor;
59
60     private Composite editorReuseIndentGroup;
61
62     private Composite editorReuseThresholdGroup;
63
64     private IntegerFieldEditor reuseEditorsThreshold;
65
66     private Group dirtyEditorReuseGroup;
67
68     private Button openNewEditor;
69
70     private Button promptToReuseEditor;
71
72     private IntegerFieldEditor recentFilesEditor;
73
74     private IPropertyChangeListener validityChangeListener = new IPropertyChangeListener() {
75         public void propertyChange(PropertyChangeEvent event) {
76             if (event.getProperty().equals(FieldEditor.IS_VALID)) {
77                 updateValidState();
78             }
79         }
80     };
81
82     private Button promptWhenStillOpenEditor;
83
84     protected Control createContents(Composite parent) {
85         Composite composite = createComposite(parent);
86
87         createEditorHistoryGroup(composite);
88
89         createSpace(composite);
90         createShowMultipleEditorTabsPref(composite);
91         createUseIPersistablePref(composite);
92         createPromptWhenStillOpenPref(composite);
93         createEditorReuseGroup(composite);
94         ((TabBehaviour)Tweaklets.get(TabBehaviour.KEY)).setPreferenceVisibility(editorReuseGroup, showMultipleEditorTabs);
95
96         updateValidState();
97
98         setHelpContext(parent);
99
100         return composite;
101     }
102
103     protected void setHelpContext(Composite parent) {
104         // @issue the IDE subclasses this, but should provide its own help
105
PlatformUI.getWorkbench().getHelpSystem().setHelp(parent,
106                 IWorkbenchHelpContextIds.WORKBENCH_EDITOR_PREFERENCE_PAGE);
107     }
108
109     protected void createSpace(Composite parent) {
110         WorkbenchPreferencePage.createSpace(parent);
111     }
112
113     protected void createShowMultipleEditorTabsPref(Composite composite) {
114         showMultipleEditorTabs = new Button(composite, SWT.CHECK);
115         showMultipleEditorTabs.setText(WorkbenchMessages.WorkbenchPreference_showMultipleEditorTabsButton);
116         showMultipleEditorTabs.setFont(composite.getFont());
117         showMultipleEditorTabs.setSelection(getPreferenceStore().getBoolean(
118                 IPreferenceConstants.SHOW_MULTIPLE_EDITOR_TABS));
119         setButtonLayoutData(showMultipleEditorTabs);
120     }
121
122     protected void createUseIPersistablePref(Composite composite) {
123         useIPersistableEditor = new Button(composite, SWT.CHECK);
124         useIPersistableEditor.setText(WorkbenchMessages.WorkbenchPreference_useIPersistableEditorButton);
125         useIPersistableEditor.setFont(composite.getFont());
126         useIPersistableEditor.setSelection(getPreferenceStore().getBoolean(
127                 IPreferenceConstants.USE_IPERSISTABLE_EDITORS));
128         setButtonLayoutData(useIPersistableEditor);
129     }
130     
131     protected void createPromptWhenStillOpenPref(Composite composite) {
132         promptWhenStillOpenEditor = new Button(composite, SWT.CHECK);
133         promptWhenStillOpenEditor.setText(WorkbenchMessages.WorkbenchPreference_promptWhenStillOpenButton);
134         promptWhenStillOpenEditor.setFont(composite.getFont());
135         promptWhenStillOpenEditor.setSelection(getAPIPreferenceStore().getBoolean(
136                 IWorkbenchPreferenceConstants.PROMPT_WHEN_SAVEABLE_STILL_OPEN));
137         setButtonLayoutData(promptWhenStillOpenEditor);
138     }
139     
140     protected Composite createComposite(Composite parent) {
141         Composite composite = new Composite(parent, SWT.NULL);
142         GridLayout layout = new GridLayout();
143         layout.marginWidth = 0;
144         layout.marginHeight = 0;
145         composite.setLayout(layout);
146         composite.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_FILL
147                 | GridData.HORIZONTAL_ALIGN_FILL));
148         composite.setFont(parent.getFont());
149         return composite;
150     }
151
152     public void init(IWorkbench workbench) {
153         // do nothing
154
}
155
156     protected void performDefaults() {
157         IPreferenceStore store = getPreferenceStore();
158         showMultipleEditorTabs
159                 .setSelection(store
160                         .getDefaultBoolean(IPreferenceConstants.SHOW_MULTIPLE_EDITOR_TABS));
161         useIPersistableEditor
162                 .setSelection(store
163                         .getDefaultBoolean(IPreferenceConstants.USE_IPERSISTABLE_EDITORS));
164         promptWhenStillOpenEditor
165                 .setSelection(getAPIPreferenceStore()
166                         .getDefaultBoolean(IWorkbenchPreferenceConstants.PROMPT_WHEN_SAVEABLE_STILL_OPEN));
167         reuseEditors.setSelection(store
168                 .getDefaultBoolean(IPreferenceConstants.REUSE_EDITORS_BOOLEAN));
169         dirtyEditorReuseGroup.setEnabled(reuseEditors.getSelection());
170         openNewEditor.setSelection(!store
171                 .getDefaultBoolean(IPreferenceConstants.REUSE_DIRTY_EDITORS));
172         openNewEditor.setEnabled(reuseEditors.getSelection());
173         promptToReuseEditor.setSelection(store
174                 .getDefaultBoolean(IPreferenceConstants.REUSE_DIRTY_EDITORS));
175         promptToReuseEditor.setEnabled(reuseEditors.getSelection());
176         reuseEditorsThreshold.loadDefault();
177         reuseEditorsThreshold.getLabelControl(editorReuseThresholdGroup)
178                 .setEnabled(reuseEditors.getSelection());
179         reuseEditorsThreshold.getTextControl(editorReuseThresholdGroup)
180                 .setEnabled(reuseEditors.getSelection());
181         recentFilesEditor.loadDefault();
182     }
183
184     public boolean performOk() {
185         IPreferenceStore store = getPreferenceStore();
186         store.setValue(IPreferenceConstants.SHOW_MULTIPLE_EDITOR_TABS,
187                 showMultipleEditorTabs.getSelection());
188         store.setValue(IPreferenceConstants.USE_IPERSISTABLE_EDITORS,
189                 useIPersistableEditor.getSelection());
190         getAPIPreferenceStore().setValue(IWorkbenchPreferenceConstants.PROMPT_WHEN_SAVEABLE_STILL_OPEN,
191                 promptWhenStillOpenEditor.getSelection());
192         
193         // store the reuse editors setting
194
store.setValue(IPreferenceConstants.REUSE_EDITORS_BOOLEAN, reuseEditors
195                 .getSelection());
196         store.setValue(IPreferenceConstants.REUSE_DIRTY_EDITORS,
197                 promptToReuseEditor.getSelection());
198         reuseEditorsThreshold.store();
199
200         // store the recent files setting
201
recentFilesEditor.store();
202         
203         PrefUtil.savePrefs();
204         return super.performOk();
205     }
206
207     /**
208      * Returns preference store that belongs to the our plugin.
209      *
210      * @return the preference store for this plugin
211      */

212     protected IPreferenceStore doGetPreferenceStore() {
213         return WorkbenchPlugin.getDefault().getPreferenceStore();
214     }
215
216     protected IPreferenceStore getAPIPreferenceStore() {
217         return PrefUtil.getAPIPreferenceStore();
218     }
219     
220     protected void updateValidState() {
221         if (!recentFilesEditor.isValid()) {
222             setErrorMessage(recentFilesEditor.getErrorMessage());
223             setValid(false);
224         } else if (!reuseEditorsThreshold.isValid()) {
225             setErrorMessage(reuseEditorsThreshold.getErrorMessage());
226             setValid(false);
227         } else {
228             setErrorMessage(null);
229             setValid(true);
230         }
231     }
232
233     /**
234      * Create a composite that contains entry fields specifying editor reuse preferences.
235      */

236     protected void createEditorReuseGroup(Composite composite) {
237
238         Font font = composite.getFont();
239
240         editorReuseGroup = new Composite(composite, SWT.LEFT);
241         GridLayout layout = new GridLayout();
242         // Line up with other entries in preference page
243
layout.marginWidth = 0;
244         layout.marginHeight = 0;
245         editorReuseGroup.setLayout(layout);
246         editorReuseGroup.setLayoutData(new GridData(
247                 GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL));
248         editorReuseGroup.setFont(font);
249
250         reuseEditors = new Button(editorReuseGroup, SWT.CHECK);
251         reuseEditors.setText(WorkbenchMessages.WorkbenchPreference_reuseEditors);
252         reuseEditors.setLayoutData(new GridData());
253         reuseEditors.setFont(font);
254
255         IPreferenceStore store = WorkbenchPlugin.getDefault()
256                 .getPreferenceStore();
257         reuseEditors.setSelection(store
258                 .getBoolean(IPreferenceConstants.REUSE_EDITORS_BOOLEAN));
259         reuseEditors.addSelectionListener(new SelectionAdapter() {
260             public void widgetSelected(SelectionEvent e) {
261                 reuseEditorsThreshold
262                         .getLabelControl(editorReuseThresholdGroup).setEnabled(
263                                 reuseEditors.getSelection());
264                 reuseEditorsThreshold.getTextControl(editorReuseThresholdGroup)
265                         .setEnabled(reuseEditors.getSelection());
266                 dirtyEditorReuseGroup.setEnabled(reuseEditors.getSelection());
267                 openNewEditor.setEnabled(reuseEditors.getSelection());
268                 promptToReuseEditor.setEnabled(reuseEditors.getSelection());
269             }
270         });
271
272         editorReuseIndentGroup = new Composite(editorReuseGroup, SWT.LEFT);
273         GridLayout indentLayout = new GridLayout();
274         indentLayout.marginWidth = REUSE_INDENT;
275         editorReuseIndentGroup.setLayout(indentLayout);
276         editorReuseIndentGroup.setLayoutData(new GridData(
277                 GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL));
278
279         editorReuseThresholdGroup = new Composite(editorReuseIndentGroup,
280                 SWT.LEFT);
281         editorReuseThresholdGroup.setLayout(new GridLayout());
282         editorReuseThresholdGroup.setLayoutData(new GridData(
283                 GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL));
284         editorReuseThresholdGroup.setFont(font);
285
286         reuseEditorsThreshold = new IntegerFieldEditor(
287                 IPreferenceConstants.REUSE_EDITORS,
288                 WorkbenchMessages.WorkbenchPreference_reuseEditorsThreshold, editorReuseThresholdGroup);
289
290         reuseEditorsThreshold.setPreferenceStore(WorkbenchPlugin.getDefault()
291                 .getPreferenceStore());
292         reuseEditorsThreshold.setPage(this);
293         reuseEditorsThreshold.setTextLimit(2);
294         reuseEditorsThreshold.setErrorMessage(WorkbenchMessages.WorkbenchPreference_reuseEditorsThresholdError);
295         reuseEditorsThreshold
296                 .setValidateStrategy(StringFieldEditor.VALIDATE_ON_KEY_STROKE);
297         reuseEditorsThreshold.setValidRange(1, 99);
298         reuseEditorsThreshold.load();
299         reuseEditorsThreshold.getLabelControl(editorReuseThresholdGroup)
300                 .setEnabled(reuseEditors.getSelection());
301         reuseEditorsThreshold.getTextControl(editorReuseThresholdGroup)
302                 .setEnabled(reuseEditors.getSelection());
303         reuseEditorsThreshold.setPropertyChangeListener(validityChangeListener);
304
305         dirtyEditorReuseGroup = new Group(editorReuseIndentGroup, SWT.NONE);
306         dirtyEditorReuseGroup.setLayout(new GridLayout());
307         dirtyEditorReuseGroup.setLayoutData(new GridData(
308                 GridData.FILL_HORIZONTAL));
309         dirtyEditorReuseGroup.setText(WorkbenchMessages.WorkbenchPreference_reuseDirtyEditorGroupTitle);
310         dirtyEditorReuseGroup.setFont(font);
311         dirtyEditorReuseGroup.setEnabled(reuseEditors.getSelection());
312
313         promptToReuseEditor = new Button(dirtyEditorReuseGroup, SWT.RADIO);
314         promptToReuseEditor.setText(WorkbenchMessages.WorkbenchPreference_promptToReuseEditor);
315         promptToReuseEditor.setFont(font);
316         promptToReuseEditor.setSelection(store
317                 .getBoolean(IPreferenceConstants.REUSE_DIRTY_EDITORS));
318         promptToReuseEditor.setEnabled(reuseEditors.getSelection());
319
320         openNewEditor = new Button(dirtyEditorReuseGroup, SWT.RADIO);
321         openNewEditor.setText(WorkbenchMessages.WorkbenchPreference_openNewEditor);
322         openNewEditor.setFont(font);
323         openNewEditor.setSelection(!store
324                 .getBoolean(IPreferenceConstants.REUSE_DIRTY_EDITORS));
325         openNewEditor.setEnabled(reuseEditors.getSelection());
326
327     }
328
329     /**
330      * Create a composite that contains entry fields specifying editor history preferences.
331      */

332     protected void createEditorHistoryGroup(Composite composite) {
333         Composite groupComposite = new Composite(composite, SWT.LEFT);
334         GridLayout layout = new GridLayout();
335         layout.numColumns = 2;
336         groupComposite.setLayout(layout);
337         GridData gd = new GridData();
338         gd.horizontalAlignment = GridData.FILL;
339         gd.grabExcessHorizontalSpace = true;
340         groupComposite.setLayoutData(gd);
341         groupComposite.setFont(composite.getFont());
342
343         recentFilesEditor = new IntegerFieldEditor(
344                 IPreferenceConstants.RECENT_FILES,
345                 WorkbenchMessages.WorkbenchPreference_recentFiles, groupComposite);
346
347         recentFilesEditor.setPreferenceStore(WorkbenchPlugin.getDefault()
348                 .getPreferenceStore());
349         recentFilesEditor.setPage(this);
350         recentFilesEditor.setTextLimit(Integer.toString(EditorHistory.MAX_SIZE)
351                 .length());
352         recentFilesEditor
353                 .setErrorMessage(NLS.bind(WorkbenchMessages.WorkbenchPreference_recentFilesError, new Integer JavaDoc(EditorHistory.MAX_SIZE) ));
354         recentFilesEditor
355                 .setValidateStrategy(StringFieldEditor.VALIDATE_ON_KEY_STROKE);
356         recentFilesEditor.setValidRange(0, EditorHistory.MAX_SIZE);
357         recentFilesEditor.load();
358         recentFilesEditor.setPropertyChangeListener(validityChangeListener);
359
360     }
361 }
362
363
Popular Tags