X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.ui%2Fsrc%2Forg%2Fsimantics%2Fui%2Fworkbench%2Fpreferences%2FNumberFieldEditor.java;fp=bundles%2Forg.simantics.ui%2Fsrc%2Forg%2Fsimantics%2Fui%2Fworkbench%2Fpreferences%2FNumberFieldEditor.java;h=97ad65e182550b251133e24fc69fb6abb30ae237;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.ui/src/org/simantics/ui/workbench/preferences/NumberFieldEditor.java b/bundles/org.simantics.ui/src/org/simantics/ui/workbench/preferences/NumberFieldEditor.java new file mode 100644 index 000000000..97ad65e18 --- /dev/null +++ b/bundles/org.simantics.ui/src/org/simantics/ui/workbench/preferences/NumberFieldEditor.java @@ -0,0 +1,150 @@ +/******************************************************************************* + * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.ui.workbench.preferences; + +import org.eclipse.jface.preference.StringFieldEditor; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Text; +import org.simantics.ui.workbench.WorkbenchResources; + +public class NumberFieldEditor extends StringFieldEditor { + + private double minValidValue = 0; + + private double maxValidValue = Double.MAX_VALUE; + + private static final int DEFAULT_TEXT_LIMIT = 10; + + /** + * Creates a new number field editor + */ + protected NumberFieldEditor() { + } + + /** + * Creates an number field editor. + * + * @param name the name of the preference this field editor works on + * @param labelText the label text of the field editor + * @param parent the parent of the field editor's control + */ + public NumberFieldEditor(String name, String labelText, Composite parent) { + this(name, labelText, parent, DEFAULT_TEXT_LIMIT); + } + + /** + * Creates an number field editor. + * + * @param name the name of the preference this field editor works on + * @param labelText the label text of the field editor + * @param parent the parent of the field editor's control + * @param textLimit the maximum number of characters in the text. + */ + public NumberFieldEditor(String name, String labelText, Composite parent, + int textLimit) { + init(name, labelText); + setTextLimit(textLimit); + setEmptyStringAllowed(false); + setErrorMessage(WorkbenchResources.getString("NumberFieldEditor.errorMessage"));//$NON-NLS-1$ + createControl(parent); + } + + /** + * Sets the range of valid values for this field. + * + * @param min the minimum allowed value (inclusive) + * @param max the maximum allowed value (inclusive) + */ + public void setValidRange(double min, double max) { + minValidValue = min; + maxValidValue = max; + setErrorMessage(WorkbenchResources.format( + "NumberFieldEditor.errorMessageRange", //$NON-NLS-1$ + new Object[] { new Double(min), new Double(max) })); + } + + /* (non-Javadoc) + * Method declared on StringFieldEditor. + * Checks whether the entered String is a valid number or not. + */ + protected boolean checkState() { + + Text text = getTextControl(); + + if (text == null) { + return false; + } + + String numberString = text.getText(); + try { + double number = Double.parseDouble(numberString); + if (number >= minValidValue && number <= maxValidValue) { + clearErrorMessage(); + return true; + } + + showErrorMessage(); + return false; + + } catch (NumberFormatException e1) { + showErrorMessage(); + } + + return false; + } + + /* (non-Javadoc) + * Method declared on FieldEditor. + */ + protected void doLoad() { + Text text = getTextControl(); + if (text != null) { + double value = getPreferenceStore().getDouble(getPreferenceName()); + text.setText("" + value);//$NON-NLS-1$ + oldValue = "" + value; //$NON-NLS-1$ + } + + } + + /* (non-Javadoc) + * Method declared on FieldEditor. + */ + protected void doLoadDefault() { + Text text = getTextControl(); + if (text != null) { + double value = getPreferenceStore().getDefaultDouble(getPreferenceName()); + text.setText("" + value);//$NON-NLS-1$ + } + valueChanged(); + } + + /* (non-Javadoc) + * Method declared on FieldEditor. + */ + protected void doStore() { + Text text = getTextControl(); + if (text != null) { + getPreferenceStore().setValue(getPreferenceName(), Double.parseDouble(text.getText())); + } + } + + /** + * Returns this field editor's current value as a double. + * + * @return the value + * @exception NumberFormatException if the String does not + * contain a valid number + */ + public double getDoubleValue() throws NumberFormatException { + return Double.parseDouble(getStringValue()); + } +} \ No newline at end of file