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