X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.charts%2Fsrc%2Forg%2Fsimantics%2Fcharts%2Feditor%2FTimeInputValidator.java;fp=bundles%2Forg.simantics.charts%2Fsrc%2Forg%2Fsimantics%2Fcharts%2Feditor%2FTimeInputValidator.java;h=5752fe7327e7298a6a81f52cb185f0dcd7027b6f;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.charts/src/org/simantics/charts/editor/TimeInputValidator.java b/bundles/org.simantics.charts/src/org/simantics/charts/editor/TimeInputValidator.java new file mode 100644 index 000000000..5752fe732 --- /dev/null +++ b/bundles/org.simantics.charts/src/org/simantics/charts/editor/TimeInputValidator.java @@ -0,0 +1,83 @@ +/******************************************************************************* + * Copyright (c) 2007, 2011 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.charts.editor; + +import java.text.DecimalFormat; +import java.text.Format; +import java.text.ParseException; + +import org.eclipse.jface.dialogs.IInputValidator; +import org.simantics.utils.format.TimeFormat; + +/** + * @author Toni Kalajainen + */ +public class TimeInputValidator implements IInputValidator { + + public static final TimeInputValidator INSTANCE = new TimeInputValidator(-Double.MAX_VALUE); + + public Format decimalFormat = new DecimalFormat("0"); + + public Format timeFormat = new TimeFormat(100, 0); + + double minValue; + + public TimeInputValidator(double minValue) { + this.minValue = minValue; + } + + public Double parse(String text) { + if (text==null) return null; + text = text.trim(); + if (text.equals("")) return null; + + try { + return (Double) timeFormat.parseObject(text); + } catch (ParseException e) { + } + + try { + Number n = (Number) decimalFormat.parseObject(text); + if (n instanceof Double) return (Double) n; + return n.doubleValue(); + } catch (ParseException e) { + } + + return null; + } + + @Override + public String isValid(String text) { + if (text==null) return "Null"; + text = text.trim(); + if (text.equals("")) return null; + + try { + Double d = (Double) timeFormat.parseObject(text); + + if (d != null && d.doubleValue()