1 11 package org.eclipse.ui.internal.ide.dialogs; 12 13 import java.util.Collections ; 14 import java.util.List ; 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 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 private boolean clearUserSettings = false; 73 74 private RadioGroupFieldEditor openReferencesEditor; 75 76 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, (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 129 private void createOpenPrefControls(Composite parent) { 130 String name = IDEInternalPreferences.OPEN_REQUIRED_PROJECTS; 131 String label = IDEWorkbenchMessages.IDEWorkspacePreference_openReferencedProjects; 132 String [][] 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 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 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 (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 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 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 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 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 270 protected IPreferenceStore getIDEPreferenceStore() { 271 return IDEWorkbenchPlugin.getDefault().getPreferenceStore(); 272 } 273 274 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 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 } 313 314 317 protected void performDefaults() { 318 319 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 encodings = WorkbenchEncoding.getDefinedEncodings(); 339 Collections.sort(encodings); 340 encodingEditor.loadDefault(); 341 lineSeparatorEditor.loadDefault(); 342 openReferencesEditor.loadDefault(); 343 344 super.performDefaults(); 345 } 346 347 350 public boolean performOk() { 351 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 .getStatus()); 363 } 364 } 365 366 IPreferenceStore store = getIDEPreferenceStore(); 367 368 store.setValue(IDEInternalPreferences.SAVE_ALL_BEFORE_BUILD, 370 autoSaveAllButton.getSelection()); 371 372 long oldSaveInterval = description.getSnapshotInterval() / 60000; 377 long newSaveInterval = new Long (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 ( 385 (int) oldSaveInterval), new Integer ( 386 (int) newSaveInterval)); 387 } catch (CoreException e) { 388 IDEWorkbenchPlugin.log( 389 "Error changing save interval preference", e .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 |