KickJava   Java API By Example, From Geeks To Geeks.

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


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 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 /**
37  * The FileStatesPage is the page used to set the file states sizes for the workbench.
38  */

39 public class FileStatesPage extends PreferencePage implements
40         IWorkbenchPreferencePage, Listener {
41
42     private static final int FAILED_VALUE = -1;
43
44     //Set the length of the day as we have to convert back and forth
45
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     //Choose a maximum to prevent OutOfMemoryErrors
56
private int FILE_STATES_MAXIMUM = 10000;
57
58     private long STATE_SIZE_MAXIMUM = 100;
59
60     /**
61      * This method takes the string for the title of a text field and the value for the
62      * text of the field.
63      * @return org.eclipse.swt.widgets.Text
64      * @param labelString java.lang.String
65      * @param textValue java.lang.String
66      * @param parent Composite
67      */

68     private Text addLabelAndText(String JavaDoc labelString, String JavaDoc 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     /**
86      * Recomputes the page's error state by validating all
87      * the fields.
88      */

89     private void checkState() {
90         // Assume invalid if the controls not created yet
91
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     /*
117      * Create the contents control for the workspace file states.
118      * @returns Control
119      * @param parent Composite
120      */

121     protected Control createContents(Composite parent) {
122
123         PlatformUI.getWorkbench().getHelpSystem().setHelp(parent,
124                 IIDEHelpContextIds.FILE_STATES_PREFERENCE_PAGE);
125
126         // button group
127
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         //Get the current value and make sure we get at least one day out of it.
135
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         //Create a spacing label to breakup the note from the fields
155
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     /**
172      * Get the Workspace Description this page is operating on.
173      * @return org.eclipse.core.resources.IWorkspaceDescription
174      */

175     private IWorkspaceDescription getWorkspaceDescription() {
176         return ResourcesPlugin.getWorkspace().getDescription();
177     }
178
179     /**
180      * Sent when an event that the receiver has registered for occurs.
181      *
182      * @param event the event which occurred
183      */

184     public void handleEvent(Event event) {
185         checkState();
186     }
187
188     /**
189      * Initializes this preference page for the given workbench.
190      * <p>
191      * This method is called automatically as the preference page is being created
192      * and initialized. Clients must not call this method.
193      * </p>
194      *
195      * @param workbench the workbench
196      */

197     public void init(org.eclipse.ui.IWorkbench workbench) {
198     }
199
200     /**
201      * Performs special processing when this page's Defaults button has been pressed.
202      * Reset the entries to thier default values.
203      */

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     /**
223      * Perform the result of the OK from the receiver.
224      */

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             //As it is only a copy save it back in
242
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     /**
254      * Validate a text entry for an integer field. Return the result if there are
255      * no errors, otherwise return -1 and set the entry field error.
256      * @return int
257      */

258     private int validateIntegerTextEntry(Text text) {
259
260         int value;
261
262         try {
263             value = Integer.parseInt(text.getText());
264
265         } catch (NumberFormatException JavaDoc exception) {
266             setErrorMessage(MessageFormat.format(IDEWorkbenchMessages.FileHistory_invalid,
267                     new Object JavaDoc[] { exception.getLocalizedMessage() }));
268             return FAILED_VALUE;
269         }
270
271         //Be sure all values are non zero and positive
272
if (value <= 0) {
273             setErrorMessage(IDEWorkbenchMessages.FileHistory_mustBePositive);
274             return FAILED_VALUE;
275         }
276
277         return value;
278     }
279
280     /**
281      * Validate a text entry for a long field. Return the result if there are
282      * no errors, otherwise return -1 and set the entry field error.
283      * @return long
284      */

285     private long validateLongTextEntry(Text text) {
286
287         long value;
288
289         try {
290             value = Long.parseLong(text.getText());
291
292         } catch (NumberFormatException JavaDoc exception) {
293             setErrorMessage(MessageFormat.format(IDEWorkbenchMessages.FileHistory_invalid,
294                     new Object JavaDoc[] { exception.getLocalizedMessage() }));
295             return FAILED_VALUE;
296         }
297
298         //Be sure all values are non zero and positive
299
if (value <= 0) {
300             setErrorMessage(IDEWorkbenchMessages.FileHistory_mustBePositive);
301             return FAILED_VALUE;
302         }
303
304         return value;
305     }
306
307     /**
308      * Validate the maximum file states.
309      * Return the value if successful, otherwise
310      * return FAILED_VALUE.
311      * Set the error message if it fails.
312      * @return int
313      */

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     /**
329      * Validate the maximum file state size.
330      * Return the value if successful, otherwise
331      * return FAILED_VALUE.
332      * Set the error message if it fails.
333      * @return long
334      */

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