KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > preference > ScaleFieldEditor


1 /*******************************************************************************
2  * Copyright (c) 2003, 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.jface.preference;
12
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.events.DisposeEvent;
15 import org.eclipse.swt.events.DisposeListener;
16 import org.eclipse.swt.events.SelectionAdapter;
17 import org.eclipse.swt.events.SelectionEvent;
18 import org.eclipse.swt.layout.GridData;
19 import org.eclipse.swt.widgets.Composite;
20 import org.eclipse.swt.widgets.Control;
21 import org.eclipse.swt.widgets.Scale;
22
23 /**
24  * A field editor for an integer type preference. This class may be used as is,
25  * or subclassed as required.
26  *
27  * @since 3.0
28  */

29 public class ScaleFieldEditor extends FieldEditor {
30
31     /**
32      * Value that will feed Scale.setIncrement(int).
33      */

34     private int incrementValue;
35
36     /**
37      * Value that will feed Scale.setMaximum(int).
38      */

39     private int maxValue;
40
41     /**
42      * Value that will feed Scale.setMinimum(int).
43      */

44     private int minValue;
45
46     /**
47      * Old integer value.
48      */

49     private int oldValue;
50
51     /**
52      * Value that will feed Scale.setPageIncrement(int).
53      */

54     private int pageIncrementValue;
55
56     /**
57      * The scale, or <code>null</code> if none.
58      */

59     protected Scale scale;
60
61     /**
62      * Creates a scale field editor.
63      *
64      * @param name
65      * the name of the preference this field editor works on
66      * @param labelText
67      * the label text of the field editor
68      * @param parent
69      * the parent of the field editor's control
70      */

71     public ScaleFieldEditor(String JavaDoc name, String JavaDoc labelText, Composite parent) {
72         super(name, labelText, parent);
73         setDefaultValues();
74     }
75
76     /**
77      * Creates a scale field editor with particular scale values.
78      *
79      * @param name
80      * the name of the preference this field editor works on
81      * @param labelText
82      * the label text of the field editor
83      * @param parent
84      * the parent of the field editor's control
85      * @param min
86      * the value used for Scale.setMinimum(int).
87      * @param max
88      * the value used for Scale.setMaximum(int).
89      * @param increment
90      * the value used for Scale.setIncrement(int).
91      * @param pageIncrement
92      * the value used for Scale.setPageIncrement(int).
93      */

94     public ScaleFieldEditor(String JavaDoc name, String JavaDoc labelText, Composite parent,
95             int min, int max, int increment, int pageIncrement) {
96         super(name, labelText, parent);
97         setValues(min, max, increment, pageIncrement);
98     }
99
100     /*
101      * (non-Javadoc)
102      *
103      * @see org.eclipse.jface.preference.FieldEditor#adjustForNumColumns(int)
104      */

105     protected void adjustForNumColumns(int numColumns) {
106         ((GridData) scale.getLayoutData()).horizontalSpan = numColumns - 1;
107     }
108
109     /*
110      * (non-Javadoc)
111      *
112      * @see org.eclipse.jface.preference.FieldEditor#doFillIntoGrid(org.eclipse.swt.widgets.Composite,
113      * int)
114      */

115     protected void doFillIntoGrid(Composite parent, int numColumns) {
116         Control control = getLabelControl(parent);
117         GridData gd = new GridData();
118         control.setLayoutData(gd);
119
120         scale = getScaleControl(parent);
121         gd = new GridData(GridData.FILL_HORIZONTAL);
122         gd.verticalAlignment = GridData.FILL;
123         gd.horizontalSpan = numColumns - 1;
124         gd.grabExcessHorizontalSpace = true;
125         scale.setLayoutData(gd);
126         updateScale();
127     }
128
129     /*
130      * (non-Javadoc)
131      *
132      * @see org.eclipse.jface.preference.FieldEditor#doLoad()
133      */

134     protected void doLoad() {
135         if (scale != null) {
136             int value = getPreferenceStore().getInt(getPreferenceName());
137             scale.setSelection(value);
138             oldValue = value;
139         }
140     }
141
142     /*
143      * (non-Javadoc)
144      *
145      * @see org.eclipse.jface.preference.FieldEditor#doLoadDefault()
146      */

147     protected void doLoadDefault() {
148         if (scale != null) {
149             int value = getPreferenceStore().getDefaultInt(getPreferenceName());
150             scale.setSelection(value);
151         }
152         valueChanged();
153     }
154
155     /*
156      * (non-Javadoc)
157      *
158      * @see org.eclipse.jface.preference.FieldEditor#doStore()
159      */

160     protected void doStore() {
161         getPreferenceStore()
162                 .setValue(getPreferenceName(), scale.getSelection());
163     }
164
165     /**
166      * Returns the value that will be used for Scale.setIncrement(int).
167      *
168      * @return the value.
169      * @see org.eclipse.swt.widgets.Scale#setIncrement(int)
170      */

171     public int getIncrement() {
172         return incrementValue;
173     }
174
175     /**
176      * Returns the value that will be used for Scale.setMaximum(int).
177      *
178      * @return the value.
179      * @see org.eclipse.swt.widgets.Scale#setMaximum(int)
180      */

181     public int getMaximum() {
182         return maxValue;
183     }
184
185     /**
186      * Returns the value that will be used for Scale.setMinimum(int).
187      *
188      * @return the value.
189      * @see org.eclipse.swt.widgets.Scale#setMinimum(int)
190      */

191     public int getMinimum() {
192         return minValue;
193     }
194
195     /*
196      * (non-Javadoc)
197      *
198      * @see org.eclipse.jface.preference.FieldEditor#getNumberOfControls()
199      */

200     public int getNumberOfControls() {
201         return 2;
202     }
203
204     /**
205      * Returns the value that will be used for Scale.setPageIncrement(int).
206      *
207      * @return the value.
208      * @see org.eclipse.swt.widgets.Scale#setPageIncrement(int)
209      */

210     public int getPageIncrement() {
211         return pageIncrementValue;
212     }
213
214     /**
215      * Returns this field editor's scale control.
216      *
217      * @return the scale control, or <code>null</code> if no scale field is
218      * created yet
219      */

220     public Scale getScaleControl() {
221         return scale;
222     }
223
224     /**
225      * Returns this field editor's scale control. The control is created if it
226      * does not yet exist.
227      *
228      * @param parent
229      * the parent
230      * @return the scale control
231      */

232     private Scale getScaleControl(Composite parent) {
233         if (scale == null) {
234             scale = new Scale(parent, SWT.HORIZONTAL);
235             scale.setFont(parent.getFont());
236             scale.addSelectionListener(new SelectionAdapter() {
237                 public void widgetSelected(SelectionEvent event) {
238                     valueChanged();
239                 }
240             });
241             scale.addDisposeListener(new DisposeListener() {
242                 public void widgetDisposed(DisposeEvent event) {
243                     scale = null;
244                 }
245             });
246         } else {
247             checkParent(scale, parent);
248         }
249         return scale;
250     }
251
252     /**
253      * Set default values for the various scale fields. These defaults are:<br>
254      * <ul>
255      * <li>Minimum = 0
256      * <li>Maximim = 10
257      * <li>Increment = 1
258      * <li>Page Increment = 1
259      * </ul>
260      */

261     private void setDefaultValues() {
262         setValues(0, 10, 1, 1);
263     }
264
265     /*
266      * (non-Javadoc)
267      *
268      * @see org.eclipse.jface.preference.FieldEditor#setFocus()
269      */

270     public void setFocus() {
271         if (scale != null && !scale.isDisposed()) {
272             scale.setFocus();
273         }
274     }
275
276     /**
277      * Set the value to be used for Scale.setIncrement(int) and update the
278      * scale.
279      *
280      * @param increment
281      * a value greater than 0.
282      * @see org.eclipse.swt.widgets.Scale#setIncrement(int)
283      */

284     public void setIncrement(int increment) {
285         this.incrementValue = increment;
286         updateScale();
287     }
288
289     /**
290      * Set the value to be used for Scale.setMaximum(int) and update the
291      * scale.
292      *
293      * @param max
294      * a value greater than 0.
295      * @see org.eclipse.swt.widgets.Scale#setMaximum(int)
296      */

297     public void setMaximum(int max) {
298         this.maxValue = max;
299         updateScale();
300     }
301
302     /**
303      * Set the value to be used for Scale.setMinumum(int) and update the
304      * scale.
305      *
306      * @param min
307      * a value greater than 0.
308      * @see org.eclipse.swt.widgets.Scale#setMinimum(int)
309      */

310     public void setMinimum(int min) {
311         this.minValue = min;
312         updateScale();
313     }
314
315     /**
316      * Set the value to be used for Scale.setPageIncrement(int) and update the
317      * scale.
318      *
319      * @param pageIncrement
320      * a value greater than 0.
321      * @see org.eclipse.swt.widgets.Scale#setPageIncrement(int)
322      */

323     public void setPageIncrement(int pageIncrement) {
324         this.pageIncrementValue = pageIncrement;
325         updateScale();
326     }
327
328     /**
329      * Set all Scale values.
330      *
331      * @param min
332      * the value used for Scale.setMinimum(int).
333      * @param max
334      * the value used for Scale.setMaximum(int).
335      * @param increment
336      * the value used for Scale.setIncrement(int).
337      * @param pageIncrement
338      * the value used for Scale.setPageIncrement(int).
339      */

340     private void setValues(int min, int max, int increment, int pageIncrement) {
341         this.incrementValue = increment;
342         this.maxValue = max;
343         this.minValue = min;
344         this.pageIncrementValue = pageIncrement;
345         updateScale();
346     }
347
348     /**
349      * Update the scale particulars with set values.
350      */

351     private void updateScale() {
352         if (scale != null && !scale.isDisposed()) {
353             scale.setMinimum(getMinimum());
354             scale.setMaximum(getMaximum());
355             scale.setIncrement(getIncrement());
356             scale.setPageIncrement(getPageIncrement());
357         }
358     }
359
360     /**
361      * Informs this field editor's listener, if it has one, about a change to
362      * the value (<code>VALUE</code> property) provided that the old and new
363      * values are different.
364      * <p>
365      * This hook is <em>not</em> called when the scale is initialized (or
366      * reset to the default value) from the preference store.
367      * </p>
368      */

369     protected void valueChanged() {
370         setPresentsDefaultValue(false);
371
372         int newValue = scale.getSelection();
373         if (newValue != oldValue) {
374             fireStateChanged(IS_VALID, false, true);
375             fireValueChanged(VALUE, new Integer JavaDoc(oldValue),
376                     new Integer JavaDoc(newValue));
377             oldValue = newValue;
378         }
379     }
380 }
381
Popular Tags