1 /*******************************************************************************
2 * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
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.charts.editor;
14 import java.text.DecimalFormat;
15 import java.text.Format;
16 import java.text.ParseException;
18 import org.eclipse.jface.dialogs.IInputValidator;
19 import org.simantics.utils.format.TimeFormat;
22 * @author Toni Kalajainen
24 public class TimeInputValidator implements IInputValidator {
26 public static final TimeInputValidator INSTANCE = new TimeInputValidator(-Double.MAX_VALUE);
28 public Format decimalFormat = new DecimalFormat("0");
30 public Format timeFormat = new TimeFormat(100, 0);
34 public TimeInputValidator(double minValue) {
35 this.minValue = minValue;
38 public Double parse(String text) {
39 if (text==null) return null;
41 if (text.equals("")) return null;
44 return (Double) timeFormat.parseObject(text);
45 } catch (ParseException e) {
49 Number n = (Number) decimalFormat.parseObject(text);
50 if (n instanceof Double) return (Double) n;
51 return n.doubleValue();
52 } catch (ParseException e) {
59 public String isValid(String text) {
60 if (text==null) return "Null";
62 if (text.equals("")) return null;
65 Double d = (Double) timeFormat.parseObject(text);
67 if (d != null && d.doubleValue()<minValue)
68 return "Value must be at least "+minValue+" seconds.";
70 } catch (ParseException e) {
74 Number n = (Number) decimalFormat.parseObject(text);
75 if (n != null && n.doubleValue()<minValue)
76 return "Value must be at least "+minValue+" seconds.";
78 } catch (ParseException e) {