]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/chart/property/TimeInputValidator.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / chart / property / TimeInputValidator.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
3  * Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.modeling.ui.chart.property;
13
14 import java.text.DecimalFormat;
15 import java.text.Format;
16 import java.text.ParseException;
17
18 import org.eclipse.jface.dialogs.IInputValidator;
19 import org.simantics.utils.format.TimeFormat;
20
21 /**
22  * @author Toni Kalajainen
23  */
24 public class TimeInputValidator implements IInputValidator {
25         
26         Format decimalFormat = new DecimalFormat("0");
27
28         Format timeFormat = new TimeFormat(100, 0);
29         
30         double minValue;
31         
32         public TimeInputValidator(double minValue) {
33                 this.minValue = minValue;
34         }       
35         
36         public Double parse(String text) {
37                 if (text==null) return null;
38                 text = text.trim();
39                 if (text.equals("")) return null;
40
41                 try {
42                         return (Double) timeFormat.parseObject(text);
43                 } catch (ParseException e) {
44                 }
45
46                 try {
47                         Number n = (Number) decimalFormat.parseObject(text);
48                         if (n instanceof Double) return (Double) n;
49                         return n.doubleValue();
50                 } catch (ParseException e) {
51                 }
52                 
53                 return null;
54         }
55
56         @Override
57         public String isValid(String text) {
58                 if (text==null) return "Null";
59                 text = text.trim();
60                 if (text.equals("")) return null;
61
62                 try {
63                         Double d = (Double) timeFormat.parseObject(text);
64                         
65                         if (d != null && d.doubleValue()<minValue)
66                                 return "The value must be at least "+minValue+" seconds.";
67                         return null;
68                 } catch (ParseException e) {
69                 }
70                 
71                 try {
72                         Number n = (Number) decimalFormat.parseObject(text);
73                         if (n != null && n.doubleValue()<minValue)
74                                 return "The value must be at least "+minValue+" seconds.";
75                         return null;
76                 } catch (ParseException e) {
77                         return "Unable to parse specified value as a measure of time.";
78                 }
79         }
80         
81 }