KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ide > dialogs > IDEWorkspacePreferencePage


1  /****************************************************************************
2  * Copyright (c) 2000, 2006 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.ide.dialogs;
12
13 import java.util.Collections JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.resources.IWorkspaceDescription;
17 import org.eclipse.core.resources.ResourcesPlugin;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.Preferences;
20 import org.eclipse.jface.preference.FieldEditor;
21 import org.eclipse.jface.preference.IPreferenceStore;
22 import org.eclipse.jface.preference.IntegerFieldEditor;
23 import org.eclipse.jface.preference.PreferencePage;
24 import org.eclipse.jface.preference.RadioGroupFieldEditor;
25 import org.eclipse.jface.preference.StringFieldEditor;
26 import org.eclipse.jface.util.IPropertyChangeListener;
27 import org.eclipse.jface.util.PropertyChangeEvent;
28 import org.eclipse.osgi.util.NLS;
29 import org.eclipse.swt.SWT;
30 import org.eclipse.swt.layout.GridData;
31 import org.eclipse.swt.layout.GridLayout;
32 import org.eclipse.swt.widgets.Button;
33 import org.eclipse.swt.widgets.Composite;
34 import org.eclipse.swt.widgets.Control;
35 import org.eclipse.swt.widgets.Label;
36 import org.eclipse.ui.IWorkbenchPreferencePage;
37 import org.eclipse.ui.PlatformUI;
38 import org.eclipse.ui.WorkbenchEncoding;
39 import org.eclipse.ui.dialogs.PreferenceLinkArea;
40 import org.eclipse.ui.ide.IDEEncoding;
41 import org.eclipse.ui.ide.dialogs.ResourceEncodingFieldEditor;
42 import org.eclipse.ui.internal.ide.IDEInternalPreferences;
43 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
44 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
45 import org.eclipse.ui.internal.ide.IIDEHelpContextIds;
46 import org.eclipse.ui.internal.ide.LineDelimiterEditor;
47 import org.eclipse.ui.preferences.IWorkbenchPreferenceContainer;
48
49 /**
50  * The IDEWorkspacePreferencePage is the page used to set IDE-specific preferences settings
51  * related to workspace.
52
53  *Note:This class extends from PreferencePage,and there's no WorkspacePreferencePage class.
54  *Hence when the IDE settings doesn't appear in this preference page, this page will be empty.
55  */

56 public class IDEWorkspacePreferencePage extends PreferencePage
57         implements IWorkbenchPreferencePage{
58
59     private Button autoBuildButton;
60
61     private Button autoSaveAllButton;
62
63     private IntegerFieldEditor saveInterval;
64
65     private Button autoRefreshButton;
66     
67     private ResourceEncodingFieldEditor encodingEditor;
68
69     private LineDelimiterEditor lineSeparatorEditor;
70     
71     //A boolean to indicate if the user settings were cleared.
72
private boolean clearUserSettings = false;
73
74     private RadioGroupFieldEditor openReferencesEditor;
75
76     /*
77      * (non-Javadoc)
78      *
79      * @see org.eclipse.jface.preference.PreferencePage
80      */

81     protected Control createContents(Composite parent) {
82
83         PlatformUI.getWorkbench().getHelpSystem().setHelp(parent,
84                 IIDEHelpContextIds.WORKSPACE_PREFERENCE_PAGE);
85
86         Composite composite = createComposite(parent);
87
88         PreferenceLinkArea area = new PreferenceLinkArea(composite, SWT.NONE,
89                 "org.eclipse.ui.preferencePages.Startup", IDEWorkbenchMessages.IDEWorkspacePreference_relatedLink,//$NON-NLS-1$
90
(IWorkbenchPreferenceContainer) getContainer(),null);
91
92         GridData data = new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL);
93         area.getControl().setLayoutData(data);
94         
95         Label space = new Label(composite,SWT.NONE);
96         space.setLayoutData(new GridData());
97         
98         createAutoBuildPref(composite);
99         createAutoRefreshControls(composite);
100         createSaveAllBeforeBuildPref(composite);
101         
102         createSpace(composite);
103         createSaveIntervalGroup(composite);
104         createSpace(composite);
105         
106         createOpenPrefControls(composite);
107         
108         Composite lower = new Composite(composite,SWT.NONE);
109         GridLayout lowerLayout = new GridLayout();
110         lowerLayout.numColumns = 2;
111         lowerLayout.makeColumnsEqualWidth = true;
112         lower.setLayout(lowerLayout);
113         
114         lower.setLayoutData(new GridData(
115                 GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL));
116         
117         createEncodingEditorControls(lower);
118         createLineSeparatorEditorControls(lower);
119         applyDialogFont(composite);
120
121         return composite;
122     }
123
124     /**
125      * Creates controls for the preference to open required projects when
126      * opening a project.
127      * @param parent The parent control
128      */

129     private void createOpenPrefControls(Composite parent) {
130         String JavaDoc name = IDEInternalPreferences.OPEN_REQUIRED_PROJECTS;
131         String JavaDoc label = IDEWorkbenchMessages.IDEWorkspacePreference_openReferencedProjects;
132         String JavaDoc[][] namesAndValues = {
133                 { IDEWorkbenchMessages.Always, IDEInternalPreferences.PSPM_ALWAYS },
134                 { IDEWorkbenchMessages.Never, IDEInternalPreferences.PSPM_NEVER },
135                 { IDEWorkbenchMessages.Prompt, IDEInternalPreferences.PSPM_PROMPT } };
136         openReferencesEditor = new RadioGroupFieldEditor(name, label, 3, namesAndValues, parent, true);
137         openReferencesEditor.setPreferenceStore(getIDEPreferenceStore());
138         openReferencesEditor.setPage(this);
139         openReferencesEditor.load();
140     }
141
142     protected void createSaveAllBeforeBuildPref(Composite composite) {
143         autoSaveAllButton = new Button(composite, SWT.CHECK);
144         autoSaveAllButton.setText(IDEWorkbenchMessages.IDEWorkspacePreference_savePriorToBuilding);
145         autoSaveAllButton.setToolTipText(IDEWorkbenchMessages.IDEWorkspacePreference_savePriorToBuildingToolTip);
146         autoSaveAllButton.setSelection(getIDEPreferenceStore().getBoolean(
147                 IDEInternalPreferences.SAVE_ALL_BEFORE_BUILD));
148     }
149
150     protected void createAutoBuildPref(Composite composite) {
151         autoBuildButton = new Button(composite, SWT.CHECK);
152         autoBuildButton.setText(IDEWorkbenchMessages.IDEWorkspacePreference_autobuild);
153         autoBuildButton.setToolTipText(IDEWorkbenchMessages.IDEWorkspacePreference_autobuildToolTip);
154         autoBuildButton.setSelection(ResourcesPlugin.getWorkspace()
155                 .isAutoBuilding());
156     }
157
158     /**
159      * Create a composite that contains entry fields specifying save interval
160      * preference.
161      *
162      * @param composite the Composite the group is created in.
163      */

164     private void createSaveIntervalGroup(Composite composite) {
165         Composite groupComposite = new Composite(composite, SWT.LEFT);
166         GridLayout layout = new GridLayout();
167         layout.numColumns = 2;
168         groupComposite.setLayout(layout);
169         GridData gd = new GridData();
170         gd.horizontalAlignment = GridData.FILL;
171         gd.grabExcessHorizontalSpace = true;
172         groupComposite.setLayoutData(gd);
173
174         saveInterval = new IntegerFieldEditor(
175                 IDEInternalPreferences.SAVE_INTERVAL, IDEWorkbenchMessages.WorkbenchPreference_saveInterval,
176                 groupComposite);
177
178         // @issue we should drop our preference constant and let clients use
179
// core's pref. ours is not up-to-date anyway if someone changes this
180
// interval directly thru core api.
181
saveInterval.setPreferenceStore(getIDEPreferenceStore());
182         saveInterval.setPage(this);
183         saveInterval.setTextLimit(Integer.toString(
184                 IDEInternalPreferences.MAX_SAVE_INTERVAL).length());
185         saveInterval.setErrorMessage(NLS.bind(IDEWorkbenchMessages.WorkbenchPreference_saveIntervalError, new Integer JavaDoc(IDEInternalPreferences.MAX_SAVE_INTERVAL)));
186         saveInterval
187                 .setValidateStrategy(StringFieldEditor.VALIDATE_ON_KEY_STROKE);
188         saveInterval.setValidRange(1, IDEInternalPreferences.MAX_SAVE_INTERVAL);
189
190         IWorkspaceDescription description = ResourcesPlugin.getWorkspace()
191                 .getDescription();
192         long interval = description.getSnapshotInterval() / 60000;
193         saveInterval.setStringValue(Long.toString(interval));
194
195         saveInterval.setPropertyChangeListener(new IPropertyChangeListener() {
196
197             public void propertyChange(PropertyChangeEvent event) {
198                 if (event.getProperty().equals(FieldEditor.IS_VALID)) {
199                     setValid(saveInterval.isValid());
200                 }
201             }
202         });
203
204     }
205     
206     /**
207      * Create the Refresh controls
208      *
209      * @param parent
210      */

211     private void createAutoRefreshControls(Composite parent) {
212
213         this.autoRefreshButton = new Button(parent, SWT.CHECK);
214         this.autoRefreshButton.setText(IDEWorkbenchMessages.IDEWorkspacePreference_RefreshButtonText);
215         this.autoRefreshButton.setToolTipText(IDEWorkbenchMessages.IDEWorkspacePreference_RefreshButtonToolTip);
216
217         boolean autoRefresh = ResourcesPlugin.getPlugin()
218                 .getPluginPreferences().getBoolean(
219                         ResourcesPlugin.PREF_AUTO_REFRESH);
220         this.autoRefreshButton.setSelection(autoRefresh);
221     }
222
223     /**
224      * Create a composite that contains the encoding controls
225      *
226      * @param parent
227      */

228     private void createEncodingEditorControls(Composite parent){
229         Composite encodingComposite = new Composite(parent,SWT.NONE);
230         encodingComposite.setLayout(new GridLayout());
231         encodingComposite.setLayoutData(new GridData(
232                 GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL));
233         
234         encodingEditor = new ResourceEncodingFieldEditor(IDEWorkbenchMessages.WorkbenchPreference_encoding, encodingComposite, ResourcesPlugin
235                 .getWorkspace().getRoot());
236
237         encodingEditor.setPage(this);
238         encodingEditor.load();
239         encodingEditor.setPropertyChangeListener(new IPropertyChangeListener() {
240             /* (non-Javadoc)
241              * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
242              */

243             public void propertyChange(PropertyChangeEvent event) {
244                 if (event.getProperty().equals(FieldEditor.IS_VALID)) {
245                     setValid(encodingEditor.isValid());
246                 }
247
248             }
249         });
250     }
251     
252     /**
253      * Create a composite that contains the line delimiter controls
254      *
255      * @param parent
256      */

257     private void createLineSeparatorEditorControls(Composite parent){
258         Composite lineComposite = new Composite(parent,SWT.NONE);
259         lineComposite.setLayout(new GridLayout());
260         lineComposite.setLayoutData(new GridData(
261                 GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL));
262         
263         lineSeparatorEditor = new LineDelimiterEditor(lineComposite);
264         lineSeparatorEditor.doLoad();
265     }
266     /**
267      * Returns the IDE preference store.
268      * @return the preference store.
269      */

270     protected IPreferenceStore getIDEPreferenceStore() {
271         return IDEWorkbenchPlugin.getDefault().getPreferenceStore();
272     }
273     
274     /**
275      * Creates a tab of one horizontal spans.
276      *
277      * @param parent
278      * the parent in which the tab should be created
279      */

280     protected static void createSpace(Composite parent) {
281         Label vfiller = new Label(parent, SWT.LEFT);
282         GridData gridData = new GridData();
283         gridData = new GridData();
284         gridData.horizontalAlignment = GridData.BEGINNING;
285         gridData.grabExcessHorizontalSpace = false;
286         gridData.verticalAlignment = GridData.CENTER;
287         gridData.grabExcessVerticalSpace = false;
288         vfiller.setLayoutData(gridData);
289     }
290
291     /**
292      * Creates the composite which will contain all the preference controls for
293      * this page.
294      *
295      * @param parent
296      * the parent composite
297      * @return the composite for this page
298      */

299     protected Composite createComposite(Composite parent) {
300         Composite composite = new Composite(parent, SWT.NONE);
301         GridLayout layout = new GridLayout();
302         layout.marginWidth = 0;
303         layout.marginHeight = 0;
304         composite.setLayout(layout);
305         composite.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_FILL
306                 | GridData.HORIZONTAL_ALIGN_FILL));
307         return composite;
308     }
309     
310     public void init(org.eclipse.ui.IWorkbench workbench) {
311         //no-op
312
}
313     
314     /**
315      * The default button has been pressed.
316      */

317     protected void performDefaults() {
318
319         // core holds onto this preference.
320
boolean autoBuild = ResourcesPlugin.getPlugin().getPluginPreferences()
321                 .getDefaultBoolean(ResourcesPlugin.PREF_AUTO_BUILDING);
322         autoBuildButton.setSelection(autoBuild);
323
324         IPreferenceStore store = getIDEPreferenceStore();
325         autoSaveAllButton
326                 .setSelection(store
327                         .getDefaultBoolean(IDEInternalPreferences.SAVE_ALL_BEFORE_BUILD));
328         saveInterval.loadDefault();
329
330         
331         boolean autoRefresh = ResourcesPlugin.getPlugin()
332                 .getPluginPreferences().getDefaultBoolean(
333                         ResourcesPlugin.PREF_AUTO_REFRESH);
334         autoRefreshButton.setSelection(autoRefresh);
335         
336         clearUserSettings = true;
337
338         List JavaDoc encodings = WorkbenchEncoding.getDefinedEncodings();
339         Collections.sort(encodings);
340         encodingEditor.loadDefault();
341         lineSeparatorEditor.loadDefault();
342         openReferencesEditor.loadDefault();
343
344         super.performDefaults();
345     }
346
347     /**
348      * The user has pressed Ok. Store/apply this page's values appropriately.
349      */

350     public boolean performOk() {
351         // set the workspace auto-build flag
352
IWorkspaceDescription description = ResourcesPlugin.getWorkspace()
353                 .getDescription();
354         if (autoBuildButton.getSelection() != ResourcesPlugin.getWorkspace()
355                 .isAutoBuilding()) {
356             try {
357                 description.setAutoBuilding(autoBuildButton.getSelection());
358                 ResourcesPlugin.getWorkspace().setDescription(description);
359             } catch (CoreException e) {
360                 IDEWorkbenchPlugin.log(
361                         "Error changing auto build workspace setting.", e//$NON-NLS-1$
362
.getStatus());
363             }
364         }
365
366         IPreferenceStore store = getIDEPreferenceStore();
367
368         // store the save all prior to build setting
369
store.setValue(IDEInternalPreferences.SAVE_ALL_BEFORE_BUILD,
370                 autoSaveAllButton.getSelection());
371
372         // store the workspace save interval
373
// @issue we should drop our preference constant and let clients use
374
// core's pref. ours is not up-to-date anyway if someone changes this
375
// interval directly thru core api.
376
long oldSaveInterval = description.getSnapshotInterval() / 60000;
377         long newSaveInterval = new Long JavaDoc(saveInterval.getStringValue())
378                 .longValue();
379         if (oldSaveInterval != newSaveInterval) {
380             try {
381                 description.setSnapshotInterval(newSaveInterval * 60000);
382                 ResourcesPlugin.getWorkspace().setDescription(description);
383                 store.firePropertyChangeEvent(
384                         IDEInternalPreferences.SAVE_INTERVAL, new Integer JavaDoc(
385                                 (int) oldSaveInterval), new Integer JavaDoc(
386                                 (int) newSaveInterval));
387             } catch (CoreException e) {
388                 IDEWorkbenchPlugin.log(
389                         "Error changing save interval preference", e //$NON-NLS-1$
390
.getStatus());
391             }
392         }
393         
394         Preferences preferences = ResourcesPlugin.getPlugin()
395                 .getPluginPreferences();
396
397         boolean autoRefresh = autoRefreshButton.getSelection();
398         preferences.setValue(ResourcesPlugin.PREF_AUTO_REFRESH, autoRefresh);
399         
400         if (clearUserSettings) {
401             IDEEncoding.clearUserEncodings();
402         }
403         encodingEditor.store();
404         lineSeparatorEditor.store();
405         openReferencesEditor.store();
406         return super.performOk();
407     }
408
409 }
410
Popular Tags