1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.ui.workbench.preferences;
14 import org.eclipse.jface.preference.StringFieldEditor;
15 import org.eclipse.swt.widgets.Composite;
16 import org.eclipse.swt.widgets.Text;
17 import org.simantics.ui.workbench.WorkbenchResources;
19 public class NumberFieldEditor extends StringFieldEditor {
21 private double minValidValue = 0;
23 private double maxValidValue = Double.MAX_VALUE;
25 private static final int DEFAULT_TEXT_LIMIT = 10;
28 * Creates a new number field editor
30 protected NumberFieldEditor() {
34 * Creates an number field editor.
36 * @param name the name of the preference this field editor works on
37 * @param labelText the label text of the field editor
38 * @param parent the parent of the field editor's control
40 public NumberFieldEditor(String name, String labelText, Composite parent) {
41 this(name, labelText, parent, DEFAULT_TEXT_LIMIT);
45 * Creates an number field editor.
47 * @param name the name of the preference this field editor works on
48 * @param labelText the label text of the field editor
49 * @param parent the parent of the field editor's control
50 * @param textLimit the maximum number of characters in the text.
52 public NumberFieldEditor(String name, String labelText, Composite parent,
54 init(name, labelText);
55 setTextLimit(textLimit);
56 setEmptyStringAllowed(false);
57 setErrorMessage(WorkbenchResources.getString("NumberFieldEditor.errorMessage"));//$NON-NLS-1$
58 createControl(parent);
62 * Sets the range of valid values for this field.
64 * @param min the minimum allowed value (inclusive)
65 * @param max the maximum allowed value (inclusive)
67 public void setValidRange(double min, double max) {
70 setErrorMessage(WorkbenchResources.format(
71 "NumberFieldEditor.errorMessageRange", //$NON-NLS-1$
72 new Object[] { new Double(min), new Double(max) }));
76 * Method declared on StringFieldEditor.
77 * Checks whether the entered String is a valid number or not.
79 protected boolean checkState() {
81 Text text = getTextControl();
87 String numberString = text.getText();
89 double number = Double.parseDouble(numberString);
90 if (number >= minValidValue && number <= maxValidValue) {
98 } catch (NumberFormatException e1) {
106 * Method declared on FieldEditor.
108 protected void doLoad() {
109 Text text = getTextControl();
111 double value = getPreferenceStore().getDouble(getPreferenceName());
112 text.setText("" + value);//$NON-NLS-1$
113 oldValue = "" + value; //$NON-NLS-1$
119 * Method declared on FieldEditor.
121 protected void doLoadDefault() {
122 Text text = getTextControl();
124 double value = getPreferenceStore().getDefaultDouble(getPreferenceName());
125 text.setText("" + value);//$NON-NLS-1$
131 * Method declared on FieldEditor.
133 protected void doStore() {
134 Text text = getTextControl();
136 getPreferenceStore().setValue(getPreferenceName(), Double.parseDouble(text.getText()));
141 * Returns this field editor's current value as a double.
144 * @exception NumberFormatException if the <code>String</code> does not
145 * contain a valid number
147 public double getDoubleValue() throws NumberFormatException {
148 return Double.parseDouble(getStringValue());