001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 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: *******************************************************************************/package org.eclipse.ui.internal.ide.dialogs;
011:
012: import com.ibm.icu.text.MessageFormat;
013:
014: import org.eclipse.core.resources.IWorkspaceDescription;
015: import org.eclipse.core.resources.ResourcesPlugin;
016: import org.eclipse.core.runtime.CoreException;
017: import org.eclipse.core.runtime.Preferences;
018: import org.eclipse.jface.dialogs.ErrorDialog;
019: import org.eclipse.jface.preference.PreferencePage;
020: import org.eclipse.osgi.util.NLS;
021: import org.eclipse.swt.SWT;
022: import org.eclipse.swt.layout.GridData;
023: import org.eclipse.swt.layout.GridLayout;
024: import org.eclipse.swt.widgets.Composite;
025: import org.eclipse.swt.widgets.Control;
026: import org.eclipse.swt.widgets.Event;
027: import org.eclipse.swt.widgets.Label;
028: import org.eclipse.swt.widgets.Listener;
029: import org.eclipse.swt.widgets.Text;
030: import org.eclipse.ui.IWorkbenchPreferencePage;
031: import org.eclipse.ui.PlatformUI;
032: import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
033: import org.eclipse.ui.internal.ide.IIDEHelpContextIds;
034:
035: /**
036: * The FileStatesPage is the page used to set the file states sizes for the workbench.
037: */
038: public class FileStatesPage extends PreferencePage implements
039: IWorkbenchPreferencePage, Listener {
040:
041: private static final int FAILED_VALUE = -1;
042:
043: //Set the length of the day as we have to convert back and forth
044: private static final long DAY_LENGTH = 86400000;
045:
046: private static final long MEGABYTES = 1024 * 1024;
047:
048: private Text longevityText;
049:
050: private Text maxStatesText;
051:
052: private Text maxStateSizeText;
053:
054: //Choose a maximum to prevent OutOfMemoryErrors
055: private int FILE_STATES_MAXIMUM = 10000;
056:
057: private long STATE_SIZE_MAXIMUM = 100;
058:
059: /**
060: * This method takes the string for the title of a text field and the value for the
061: * text of the field.
062: * @return org.eclipse.swt.widgets.Text
063: * @param labelString java.lang.String
064: * @param textValue java.lang.String
065: * @param parent Composite
066: */
067: private Text addLabelAndText(String labelString, String textValue,
068: Composite parent) {
069: Label label = new Label(parent, SWT.LEFT);
070: label.setText(labelString);
071:
072: Text text = new Text(parent, SWT.LEFT | SWT.BORDER);
073: GridData data = new GridData();
074: text.addListener(SWT.Modify, this );
075: data.horizontalAlignment = GridData.FILL;
076: data.grabExcessHorizontalSpace = true;
077: data.verticalAlignment = GridData.CENTER;
078: data.grabExcessVerticalSpace = false;
079: text.setLayoutData(data);
080: text.setText(textValue);
081: return text;
082: }
083:
084: /**
085: * Recomputes the page's error state by validating all
086: * the fields.
087: */
088: private void checkState() {
089: // Assume invalid if the controls not created yet
090: if (longevityText == null || maxStatesText == null
091: || maxStateSizeText == null) {
092: setValid(false);
093: return;
094: }
095:
096: if (validateLongTextEntry(longevityText) == FAILED_VALUE) {
097: setValid(false);
098: return;
099: }
100:
101: if (validateMaxFileStates() == FAILED_VALUE) {
102: setValid(false);
103: return;
104: }
105:
106: if (validateMaxFileStateSize() == FAILED_VALUE) {
107: setValid(false);
108: return;
109: }
110:
111: setValid(true);
112: setErrorMessage(null);
113: }
114:
115: /*
116: * Create the contents control for the workspace file states.
117: * @returns Control
118: * @param parent Composite
119: */
120: protected Control createContents(Composite parent) {
121:
122: PlatformUI.getWorkbench().getHelpSystem().setHelp(parent,
123: IIDEHelpContextIds.FILE_STATES_PREFERENCE_PAGE);
124:
125: // button group
126: Composite composite = new Composite(parent, SWT.NONE);
127: GridLayout layout = new GridLayout();
128: layout.numColumns = 2;
129: composite.setLayout(layout);
130:
131: IWorkspaceDescription description = getWorkspaceDescription();
132:
133: //Get the current value and make sure we get at least one day out of it.
134: long days = description.getFileStateLongevity() / DAY_LENGTH;
135: if (days < 1) {
136: days = 1;
137: }
138:
139: long megabytes = description.getMaxFileStateSize() / MEGABYTES;
140: if (megabytes < 1) {
141: megabytes = 1;
142: }
143:
144: this .longevityText = addLabelAndText(
145: IDEWorkbenchMessages.FileHistory_longevity, String
146: .valueOf(days), composite);
147: this .maxStatesText = addLabelAndText(
148: IDEWorkbenchMessages.FileHistory_entries, String
149: .valueOf(description.getMaxFileStates()),
150: composite);
151: this .maxStateSizeText = addLabelAndText(
152: IDEWorkbenchMessages.FileHistory_diskSpace, String
153: .valueOf(megabytes), composite);
154:
155: checkState();
156:
157: //Create a spacing label to breakup the note from the fields
158: Label spacer = new Label(composite, SWT.NONE);
159: GridData spacerData = new GridData();
160: spacerData.horizontalSpan = 2;
161: spacer.setLayoutData(spacerData);
162:
163: Composite noteComposite = createNoteComposite(parent.getFont(),
164: composite, IDEWorkbenchMessages.Preference_note,
165: IDEWorkbenchMessages.FileHistory_restartNote);
166: GridData noteData = new GridData();
167: noteData.horizontalSpan = 2;
168: noteComposite.setLayoutData(noteData);
169:
170: applyDialogFont(composite);
171:
172: return composite;
173: }
174:
175: /**
176: * Get the Workspace Description this page is operating on.
177: * @return org.eclipse.core.resources.IWorkspaceDescription
178: */
179: private IWorkspaceDescription getWorkspaceDescription() {
180: return ResourcesPlugin.getWorkspace().getDescription();
181: }
182:
183: /**
184: * Sent when an event that the receiver has registered for occurs.
185: *
186: * @param event the event which occurred
187: */
188: public void handleEvent(Event event) {
189: checkState();
190: }
191:
192: /**
193: * Initializes this preference page for the given workbench.
194: * <p>
195: * This method is called automatically as the preference page is being created
196: * and initialized. Clients must not call this method.
197: * </p>
198: *
199: * @param workbench the workbench
200: */
201: public void init(org.eclipse.ui.IWorkbench workbench) {
202: }
203:
204: /**
205: * Performs special processing when this page's Defaults button has been pressed.
206: * Reset the entries to thier default values.
207: */
208: protected void performDefaults() {
209: super .performDefaults();
210:
211: Preferences prefs = ResourcesPlugin.getPlugin()
212: .getPluginPreferences();
213:
214: long days = prefs
215: .getDefaultLong(ResourcesPlugin.PREF_FILE_STATE_LONGEVITY)
216: / DAY_LENGTH;
217: long megabytes = prefs
218: .getDefaultLong(ResourcesPlugin.PREF_MAX_FILE_STATE_SIZE)
219: / MEGABYTES;
220: this .longevityText.setText(String.valueOf(days));
221: this .maxStatesText
222: .setText(prefs
223: .getDefaultString(ResourcesPlugin.PREF_MAX_FILE_STATES));
224: this .maxStateSizeText.setText(String.valueOf(megabytes));
225: checkState();
226: }
227:
228: /**
229: * Perform the result of the OK from the receiver.
230: */
231: public boolean performOk() {
232:
233: long longevityValue = validateLongTextEntry(longevityText);
234: int maxFileStates = validateMaxFileStates();
235: long maxStateSize = validateMaxFileStateSize();
236: if (longevityValue == FAILED_VALUE
237: || maxFileStates == FAILED_VALUE
238: || maxStateSize == FAILED_VALUE) {
239: return false;
240: }
241:
242: IWorkspaceDescription description = getWorkspaceDescription();
243: description.setFileStateLongevity(longevityValue * DAY_LENGTH);
244: description.setMaxFileStates(maxFileStates);
245: description.setMaxFileStateSize(maxStateSize * MEGABYTES);
246:
247: try {
248: //As it is only a copy save it back in
249: ResourcesPlugin.getWorkspace().setDescription(description);
250: } catch (CoreException exception) {
251: ErrorDialog.openError(getShell(),
252: IDEWorkbenchMessages.FileHistory_exceptionSaving,
253: exception.getMessage(), exception.getStatus());
254: return false;
255: }
256:
257: return true;
258:
259: }
260:
261: /**
262: * Validate a text entry for an integer field. Return the result if there are
263: * no errors, otherwise return -1 and set the entry field error.
264: * @return int
265: */
266: private int validateIntegerTextEntry(Text text) {
267:
268: int value;
269:
270: try {
271: value = Integer.parseInt(text.getText());
272:
273: } catch (NumberFormatException exception) {
274: setErrorMessage(MessageFormat.format(
275: IDEWorkbenchMessages.FileHistory_invalid,
276: new Object[] { exception.getLocalizedMessage() }));
277: return FAILED_VALUE;
278: }
279:
280: //Be sure all values are non zero and positive
281: if (value <= 0) {
282: setErrorMessage(IDEWorkbenchMessages.FileHistory_mustBePositive);
283: return FAILED_VALUE;
284: }
285:
286: return value;
287: }
288:
289: /**
290: * Validate a text entry for a long field. Return the result if there are
291: * no errors, otherwise return -1 and set the entry field error.
292: * @return long
293: */
294: private long validateLongTextEntry(Text text) {
295:
296: long value;
297:
298: try {
299: value = Long.parseLong(text.getText());
300:
301: } catch (NumberFormatException exception) {
302: setErrorMessage(MessageFormat.format(
303: IDEWorkbenchMessages.FileHistory_invalid,
304: new Object[] { exception.getLocalizedMessage() }));
305: return FAILED_VALUE;
306: }
307:
308: //Be sure all values are non zero and positive
309: if (value <= 0) {
310: setErrorMessage(IDEWorkbenchMessages.FileHistory_mustBePositive);
311: return FAILED_VALUE;
312: }
313:
314: return value;
315: }
316:
317: /**
318: * Validate the maximum file states.
319: * Return the value if successful, otherwise
320: * return FAILED_VALUE.
321: * Set the error message if it fails.
322: * @return int
323: */
324: private int validateMaxFileStates() {
325: int maxFileStates = validateIntegerTextEntry(this .maxStatesText);
326: if (maxFileStates == FAILED_VALUE) {
327: return maxFileStates;
328: }
329:
330: if (maxFileStates > FILE_STATES_MAXIMUM) {
331: setErrorMessage(NLS.bind(
332: IDEWorkbenchMessages.FileHistory_aboveMaxEntries,
333: String.valueOf(FILE_STATES_MAXIMUM)));
334: return FAILED_VALUE;
335: }
336:
337: return maxFileStates;
338: }
339:
340: /**
341: * Validate the maximum file state size.
342: * Return the value if successful, otherwise
343: * return FAILED_VALUE.
344: * Set the error message if it fails.
345: * @return long
346: */
347: private long validateMaxFileStateSize() {
348: long maxFileStateSize = validateLongTextEntry(this.maxStateSizeText);
349: if (maxFileStateSize == FAILED_VALUE) {
350: return maxFileStateSize;
351: }
352:
353: if (maxFileStateSize > STATE_SIZE_MAXIMUM) {
354: setErrorMessage(NLS.bind(
355: IDEWorkbenchMessages.FileHistory_aboveMaxFileSize,
356: String.valueOf(STATE_SIZE_MAXIMUM)));
357: return FAILED_VALUE;
358: }
359:
360: return maxFileStateSize;
361: }
362:
363: }
|