]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/chart/property/TimeInputValidator.java
Externalize strings
[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.eclipse.osgi.util.NLS;
20 import org.simantics.utils.format.TimeFormat;
21
22 /**
23  * @author Toni Kalajainen
24  */
25 public class TimeInputValidator implements IInputValidator {
26         
27         Format decimalFormat = new DecimalFormat("0"); //$NON-NLS-1$
28
29         Format timeFormat = new TimeFormat(100, 0);
30         
31         double minValue;
32         
33         public TimeInputValidator(double minValue) {
34                 this.minValue = minValue;
35         }       
36         
37         public Double parse(String text) {
38                 if (text==null) return null;
39                 text = text.trim();
40                 if (text.equals("")) return null; //$NON-NLS-1$
41
42                 try {
43                         return (Double) timeFormat.parseObject(text);
44                 } catch (ParseException e) {
45                 }
46
47                 try {
48                         Number n = (Number) decimalFormat.parseObject(text);
49                         if (n instanceof Double) return (Double) n;
50                         return n.doubleValue();
51                 } catch (ParseException e) {
52                 }
53                 
54                 return null;
55         }
56
57         @Override
58         public String isValid(String text) {
59                 if (text == null)
60                         return "Null"; //$NON-NLS-1$
61                 text = text.trim();
62                 if (text.equals("")) //$NON-NLS-1$
63                         return null;
64
65                 try {
66                         Double d = (Double) timeFormat.parseObject(text);
67
68                         if (d != null && d.doubleValue() < minValue)
69                                 return NLS.bind(Messages.TimeInputValidator_ValueRange, minValue);
70                         return null;
71                 } catch (ParseException e) {
72                 }
73
74                 try {
75                         Number n = (Number) decimalFormat.parseObject(text);
76                         if (n != null && n.doubleValue() < minValue)
77                                 return NLS.bind(Messages.TimeInputValidator_ValueRange, minValue); 
78                         return null;
79                 } catch (ParseException e) {
80                         return "Unable to parse specified value as a measure of time."; //$NON-NLS-1$
81                 }
82         }
83         
84 }