]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/validators/RangeValidator.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.utils.ui / src / org / simantics / utils / ui / validators / RangeValidator.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in 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 /*\r
13  * 28.6.2006\r
14  */\r
15 package org.simantics.utils.ui.validators;\r
16 \r
17 import org.eclipse.jface.dialogs.IInputValidator;\r
18 \r
19 /**\r
20  * RangeValidator is a validator that validates integer numbers\r
21  * in a given range.\r
22  * @author Toni Kalajainen\r
23  */\r
24 public class RangeValidator implements IInputValidator {\r
25 \r
26     final long minValue;\r
27     final long maxValue;\r
28 \r
29     public RangeValidator(long minValue, long maxValue)\r
30     {\r
31         this.minValue = minValue;\r
32         this.maxValue = maxValue;\r
33     }\r
34 \r
35     @Override\r
36     public String isValid(String newText) {\r
37         try {\r
38             long value = new Long(newText);\r
39             if (value<minValue)\r
40                 return "Value must be at least or over "+minValue;\r
41             if (value>maxValue)\r
42                 return "Value must be at most "+maxValue;\r
43             return null;\r
44         } catch (NumberFormatException e) {\r
45             return "\""+newText+"\" is not an integer value";//;e.getMessage();\r
46         }\r
47     };\r
48 \r
49 }\r