]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.charts/src/org/simantics/charts/editor/TimeInputValidator.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.charts / src / org / simantics / charts / editor / 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.charts.editor;\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         public static final TimeInputValidator INSTANCE = new TimeInputValidator(-Double.MAX_VALUE);\r
27 \r
28         public Format decimalFormat = new DecimalFormat("0");\r
29 \r
30         public Format timeFormat = new TimeFormat(100, 0);\r
31 \r
32         double minValue;\r
33 \r
34         public TimeInputValidator(double minValue) {\r
35                 this.minValue = minValue;\r
36         }\r
37 \r
38         public Double parse(String text) {\r
39                 if (text==null) return null;\r
40                 text = text.trim();\r
41                 if (text.equals("")) return null;\r
42 \r
43                 try {\r
44                         return (Double) timeFormat.parseObject(text);\r
45                 } catch (ParseException e) {\r
46                 }\r
47 \r
48                 try {\r
49                         Number n = (Number) decimalFormat.parseObject(text);\r
50                         if (n instanceof Double) return (Double) n;\r
51                         return n.doubleValue();\r
52                 } catch (ParseException e) {\r
53                 }\r
54                 \r
55                 return null;\r
56         }\r
57 \r
58         @Override\r
59         public String isValid(String text) {\r
60                 if (text==null) return "Null";\r
61                 text = text.trim();\r
62                 if (text.equals("")) return null;\r
63 \r
64                 try {\r
65                         Double d = (Double) timeFormat.parseObject(text);\r
66                         \r
67                         if (d != null && d.doubleValue()<minValue)\r
68                                 return "Value must be at least "+minValue+" seconds.";\r
69                         return null;\r
70                 } catch (ParseException e) {\r
71                 }\r
72                 \r
73                 try {\r
74                         Number n = (Number) decimalFormat.parseObject(text);\r
75                         if (n != null && n.doubleValue()<minValue)\r
76                                 return "Value must be at least "+minValue+" seconds.";\r
77                         return null;\r
78                 } catch (ParseException e) {\r
79                         return e.toString();\r
80                 }\r
81         }\r
82                 \r
83 }\r