1 11 package org.eclipse.ui.internal.ide.dialogs; 12 13 import com.ibm.icu.text.MessageFormat; 14 15 import org.eclipse.core.resources.IWorkspaceDescription; 16 import org.eclipse.core.resources.ResourcesPlugin; 17 import org.eclipse.core.runtime.CoreException; 18 import org.eclipse.core.runtime.Preferences; 19 import org.eclipse.jface.dialogs.ErrorDialog; 20 import org.eclipse.jface.preference.PreferencePage; 21 import org.eclipse.osgi.util.NLS; 22 import org.eclipse.swt.SWT; 23 import org.eclipse.swt.layout.GridData; 24 import org.eclipse.swt.layout.GridLayout; 25 import org.eclipse.swt.widgets.Composite; 26 import org.eclipse.swt.widgets.Control; 27 import org.eclipse.swt.widgets.Event; 28 import org.eclipse.swt.widgets.Label; 29 import org.eclipse.swt.widgets.Listener; 30 import org.eclipse.swt.widgets.Text; 31 import org.eclipse.ui.IWorkbenchPreferencePage; 32 import org.eclipse.ui.PlatformUI; 33 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; 34 import org.eclipse.ui.internal.ide.IIDEHelpContextIds; 35 36 39 public class FileStatesPage extends PreferencePage implements 40 IWorkbenchPreferencePage, Listener { 41 42 private static final int FAILED_VALUE = -1; 43 44 private static final long DAY_LENGTH = 86400000; 46 47 private static final long MEGABYTES = 1024 * 1024; 48 49 private Text longevityText; 50 51 private Text maxStatesText; 52 53 private Text maxStateSizeText; 54 55 private int FILE_STATES_MAXIMUM = 10000; 57 58 private long STATE_SIZE_MAXIMUM = 100; 59 60 68 private Text addLabelAndText(String labelString, String textValue, 69 Composite parent) { 70 Label label = new Label(parent, SWT.LEFT); 71 label.setText(labelString); 72 73 Text text = new Text(parent, SWT.LEFT | SWT.BORDER); 74 GridData data = new GridData(); 75 text.addListener(SWT.Modify, this); 76 data.horizontalAlignment = GridData.FILL; 77 data.grabExcessHorizontalSpace = true; 78 data.verticalAlignment = GridData.CENTER; 79 data.grabExcessVerticalSpace = false; 80 text.setLayoutData(data); 81 text.setText(textValue); 82 return text; 83 } 84 85 89 private void checkState() { 90 if (longevityText == null || maxStatesText == null 92 || maxStateSizeText == null) { 93 setValid(false); 94 return; 95 } 96 97 if (validateLongTextEntry(longevityText) == FAILED_VALUE) { 98 setValid(false); 99 return; 100 } 101 102 if (validateMaxFileStates() == FAILED_VALUE) { 103 setValid(false); 104 return; 105 } 106 107 if (validateMaxFileStateSize() == FAILED_VALUE) { 108 setValid(false); 109 return; 110 } 111 112 setValid(true); 113 setErrorMessage(null); 114 } 115 116 121 protected Control createContents(Composite parent) { 122 123 PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, 124 IIDEHelpContextIds.FILE_STATES_PREFERENCE_PAGE); 125 126 Composite composite = new Composite(parent, SWT.NONE); 128 GridLayout layout = new GridLayout(); 129 layout.numColumns = 2; 130 composite.setLayout(layout); 131 132 IWorkspaceDescription description = getWorkspaceDescription(); 133 134 long days = description.getFileStateLongevity() / DAY_LENGTH; 136 if (days < 1) { 137 days = 1; 138 } 139 140 long megabytes = description.getMaxFileStateSize() / MEGABYTES; 141 if (megabytes < 1) { 142 megabytes = 1; 143 } 144 145 this.longevityText = addLabelAndText(IDEWorkbenchMessages.FileHistory_longevity, String 146 .valueOf(days), composite); 147 this.maxStatesText = addLabelAndText(IDEWorkbenchMessages.FileHistory_entries, String 148 .valueOf(description.getMaxFileStates()), composite); 149 this.maxStateSizeText = addLabelAndText(IDEWorkbenchMessages.FileHistory_diskSpace, 150 String.valueOf(megabytes), composite); 151 152 checkState(); 153 154 Label spacer = new Label(composite, SWT.NONE); 156 GridData spacerData = new GridData(); 157 spacerData.horizontalSpan = 2; 158 spacer.setLayoutData(spacerData); 159 160 Composite noteComposite = createNoteComposite(parent.getFont(), 161 composite, IDEWorkbenchMessages.Preference_note, IDEWorkbenchMessages.FileHistory_restartNote); 162 GridData noteData = new GridData(); 163 noteData.horizontalSpan = 2; 164 noteComposite.setLayoutData(noteData); 165 166 applyDialogFont(composite); 167 168 return composite; 169 } 170 171 175 private IWorkspaceDescription getWorkspaceDescription() { 176 return ResourcesPlugin.getWorkspace().getDescription(); 177 } 178 179 184 public void handleEvent(Event event) { 185 checkState(); 186 } 187 188 197 public void init(org.eclipse.ui.IWorkbench workbench) { 198 } 199 200 204 protected void performDefaults() { 205 super.performDefaults(); 206 207 Preferences prefs = ResourcesPlugin.getPlugin().getPluginPreferences(); 208 209 long days = prefs 210 .getDefaultLong(ResourcesPlugin.PREF_FILE_STATE_LONGEVITY) 211 / DAY_LENGTH; 212 long megabytes = prefs 213 .getDefaultLong(ResourcesPlugin.PREF_MAX_FILE_STATE_SIZE) 214 / MEGABYTES; 215 this.longevityText.setText(String.valueOf(days)); 216 this.maxStatesText.setText(prefs 217 .getDefaultString(ResourcesPlugin.PREF_MAX_FILE_STATES)); 218 this.maxStateSizeText.setText(String.valueOf(megabytes)); 219 checkState(); 220 } 221 222 225 public boolean performOk() { 226 227 long longevityValue = validateLongTextEntry(longevityText); 228 int maxFileStates = validateMaxFileStates(); 229 long maxStateSize = validateMaxFileStateSize(); 230 if (longevityValue == FAILED_VALUE || maxFileStates == FAILED_VALUE 231 || maxStateSize == FAILED_VALUE) { 232 return false; 233 } 234 235 IWorkspaceDescription description = getWorkspaceDescription(); 236 description.setFileStateLongevity(longevityValue * DAY_LENGTH); 237 description.setMaxFileStates(maxFileStates); 238 description.setMaxFileStateSize(maxStateSize * MEGABYTES); 239 240 try { 241 ResourcesPlugin.getWorkspace().setDescription(description); 243 } catch (CoreException exception) { 244 ErrorDialog.openError(getShell(), IDEWorkbenchMessages.FileHistory_exceptionSaving, exception 245 .getMessage(), exception.getStatus()); 246 return false; 247 } 248 249 return true; 250 251 } 252 253 258 private int validateIntegerTextEntry(Text text) { 259 260 int value; 261 262 try { 263 value = Integer.parseInt(text.getText()); 264 265 } catch (NumberFormatException exception) { 266 setErrorMessage(MessageFormat.format(IDEWorkbenchMessages.FileHistory_invalid, 267 new Object [] { exception.getLocalizedMessage() })); 268 return FAILED_VALUE; 269 } 270 271 if (value <= 0) { 273 setErrorMessage(IDEWorkbenchMessages.FileHistory_mustBePositive); 274 return FAILED_VALUE; 275 } 276 277 return value; 278 } 279 280 285 private long validateLongTextEntry(Text text) { 286 287 long value; 288 289 try { 290 value = Long.parseLong(text.getText()); 291 292 } catch (NumberFormatException exception) { 293 setErrorMessage(MessageFormat.format(IDEWorkbenchMessages.FileHistory_invalid, 294 new Object [] { exception.getLocalizedMessage() })); 295 return FAILED_VALUE; 296 } 297 298 if (value <= 0) { 300 setErrorMessage(IDEWorkbenchMessages.FileHistory_mustBePositive); 301 return FAILED_VALUE; 302 } 303 304 return value; 305 } 306 307 314 private int validateMaxFileStates() { 315 int maxFileStates = validateIntegerTextEntry(this.maxStatesText); 316 if (maxFileStates == FAILED_VALUE) { 317 return maxFileStates; 318 } 319 320 if (maxFileStates > FILE_STATES_MAXIMUM) { 321 setErrorMessage(NLS.bind(IDEWorkbenchMessages.FileHistory_aboveMaxEntries, String.valueOf(FILE_STATES_MAXIMUM))); 322 return FAILED_VALUE; 323 } 324 325 return maxFileStates; 326 } 327 328 335 private long validateMaxFileStateSize() { 336 long maxFileStateSize = validateLongTextEntry(this.maxStateSizeText); 337 if (maxFileStateSize == FAILED_VALUE) { 338 return maxFileStateSize; 339 } 340 341 if (maxFileStateSize > STATE_SIZE_MAXIMUM) { 342 setErrorMessage(NLS.bind(IDEWorkbenchMessages.FileHistory_aboveMaxFileSize, String.valueOf(STATE_SIZE_MAXIMUM))); 343 return FAILED_VALUE; 344 } 345 346 return maxFileStateSize; 347 } 348 349 } 350 | Popular Tags |