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