1 /*
2 Copyright (C) 2000 - 2007 Grid Systems, S.A.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2, as
6 published by the Free Software Foundation.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 */
17
18 /*
19 * Project: KernelConfigurator
20 * Created on 25-feb-2004
21 *
22 * Copyright (c)2003 Grid Systems
23 */
24 package com.gridsystems.config.tools;
25
26 /**
27 * Ensures a value is a valid integer between two limits.
28 *
29 * @author <a href="mailto:rruiz@gridsystems.com">Rodrigo Ruiz Aguayo</a>
30 * @version 1.0
31 */
32 public class NumberVerifier extends TextVerifier {
33 /**
34 * Error detected by this verifier.
35 */
36 public static final String ID_EMPTY = "EmptyField";
37
38 /**
39 * Error detected by this verifier.
40 */
41 public static final String ID_OUTOFRANGE = "OutOfRange";
42
43 /**
44 * Error detected by this verifier.
45 */
46 public static final String ID_INVALID = "InvalidNumber";
47
48 /**
49 * Minimum allowed value.
50 */
51 private int min;
52
53 /**
54 * Maximum allowed value.
55 */
56 private int max;
57
58 /**
59 * Empty string allowed flag.
60 */
61 private boolean emptyAllowed;
62
63 /**
64 * Error identifier.
65 */
66 protected String id;
67
68 /**
69 * Creates a new instance with no bounds, and not empty values allowed.
70 *
71 * @param support The ErrorSupport instance
72 * @param id The error identifier
73 */
74 public NumberVerifier(ErrorSupport support, String id) {
75 this(support, id, Integer.MIN_VALUE, Integer.MAX_VALUE, false);
76 }
77
78 /**
79 * Creates a new instance with no bounds.
80 *
81 * @param support The ErrorSupport instance
82 * @param id The error identifier
83 * @param emptyAllowed Whether to allow empty values or not
84 */
85 public NumberVerifier(ErrorSupport support, String id, boolean emptyAllowed) {
86 this(support, id, Integer.MIN_VALUE, Integer.MAX_VALUE, emptyAllowed);
87 }
88
89 /**
90 * Creates a new instance.
91 *
92 * @param support The ErrorSupport instance
93 * @param id The error identifier
94 * @param min Minimum valid value
95 * @param max Maximum valid value
96 * @param emptyAllowed Whether to allow empty values or not
97 */
98 public NumberVerifier(ErrorSupport support, String id, int min, int max,
99 boolean emptyAllowed) {
100 super(support);
101 this.id = id;
102 this.min = min;
103 this.max = max;
104 this.emptyAllowed = emptyAllowed;
105 }
106
107 /**
108 * Performs the validation of the specified text.
109 *
110 * @param text The text to verify
111 * @return true if the text is valid, false otherwise
112 */
113 public boolean verify(String text) {
114 int value = 0;
115
116 boolean v1 = emptyAllowed || (text != null && !text.equals(""));
117 boolean v2 = true;
118 try {
119 value = Integer.parseInt(text.trim());
120 } catch (Exception e) {
121 v2 = false;
122 }
123
124 boolean v3 = (value >= min && value <= max);
125 boolean valid = v1 && v2 && v3;
126
127 if (id == null) {
128 updateError(ID_OUTOFRANGE, !v3);
129 updateError(ID_INVALID, !v2);
130 updateError(ID_EMPTY, !v1);
131 } else {
132 updateError(id, !valid);
133 }
134
135 return valid;
136 }
137
138 /**
139 * @see TextVerifier#cleanErrors()
140 */
141 public void cleanErrors() {
142 if (id == null) {
143 updateError(ID_OUTOFRANGE, false);
144 updateError(ID_INVALID, false);
145 updateError(ID_EMPTY, false);
146 } else {
147 updateError(id, false);
148 }
149 }
150 }