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.modeling.ui.chart.property;
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.eclipse.osgi.util.NLS;
20 import org.simantics.utils.format.TimeFormat;
23 * @author Toni Kalajainen
25 public class TimeInputValidator implements IInputValidator {
27 Format decimalFormat = new DecimalFormat("0"); //$NON-NLS-1$
29 Format timeFormat = new TimeFormat(100, 0);
33 public TimeInputValidator(double minValue) {
34 this.minValue = minValue;
37 public Double parse(String text) {
38 if (text==null) return null;
40 if (text.equals("")) return null; //$NON-NLS-1$
43 return (Double) timeFormat.parseObject(text);
44 } catch (ParseException e) {
48 Number n = (Number) decimalFormat.parseObject(text);
49 if (n instanceof Double) return (Double) n;
50 return n.doubleValue();
51 } catch (ParseException e) {
58 public String isValid(String text) {
60 return "Null"; //$NON-NLS-1$
62 if (text.equals("")) //$NON-NLS-1$
66 Double d = (Double) timeFormat.parseObject(text);
68 if (d != null && d.doubleValue() < minValue)
69 return NLS.bind(Messages.TimeInputValidator_ValueRange, minValue);
71 } catch (ParseException e) {
75 Number n = (Number) decimalFormat.parseObject(text);
76 if (n != null && n.doubleValue() < minValue)
77 return NLS.bind(Messages.TimeInputValidator_ValueRange, minValue);
79 } catch (ParseException e) {
80 return "Unable to parse specified value as a measure of time."; //$NON-NLS-1$