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