1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.html;
16
17 import java.util.Map;
18
19 import org.htmlunit.SgmlPage;
20 import org.htmlunit.util.StringUtils;
21
22
23
24
25
26
27
28
29
30 public class HtmlRangeInput extends HtmlInput implements LabelableElement {
31
32
33
34
35
36
37
38
39 HtmlRangeInput(final String qualifiedName, final SgmlPage page,
40 final Map<String, DomAttr> attributes) {
41 super(qualifiedName, page, attributes);
42
43 final String value = getValueAttribute();
44 if (value == ATTRIBUTE_NOT_DEFINED) {
45 final double min = getMinNumeric();
46 final double max = getMaxNumeric();
47 if (max < min) {
48 setValue(min);
49 unmarkValueDirty();
50 return;
51 }
52
53 final double val = min + ((max - min) / 2);
54 setValue(val);
55 unmarkValueDirty();
56 }
57 else {
58 setValue(value);
59 unmarkValueDirty();
60 }
61 }
62
63
64
65
66
67
68
69 public double getMinNumeric() {
70 final String min = getAttributeDirect("min");
71 if (min == ATTRIBUTE_NOT_DEFINED) {
72 return 0;
73 }
74 try {
75 return Double.parseDouble(min);
76 }
77 catch (final NumberFormatException e) {
78 return 0;
79 }
80 }
81
82
83
84
85
86
87
88 public double getMaxNumeric() {
89 final String max = getAttributeDirect("max");
90 if (max == ATTRIBUTE_NOT_DEFINED) {
91 return 100;
92 }
93 try {
94 return Double.parseDouble(max);
95 }
96 catch (final NumberFormatException e) {
97 return 100;
98 }
99 }
100
101
102
103
104
105
106
107 public double getStepNumeric() {
108 final String step = getAttributeDirect("step");
109 if (step == ATTRIBUTE_NOT_DEFINED) {
110 return 1;
111 }
112 try {
113 return Double.parseDouble(step);
114 }
115 catch (final NumberFormatException e) {
116 return 1;
117 }
118 }
119
120
121
122
123 @Override
124 public void setDefaultChecked(final boolean defaultChecked) {
125
126 }
127
128
129
130
131 @Override
132 public void setValue(final String newValue) {
133 try {
134 if (!StringUtils.isEmptyOrNull(newValue)) {
135 setValue(Double.parseDouble(newValue));
136 }
137 else {
138 final double min = getMinNumeric();
139 final double max = getMaxNumeric();
140
141
142 setValue(min + ((max - min) / 2));
143 }
144 }
145 catch (final NumberFormatException ignored) {
146
147 }
148 }
149
150 private void setValue(final double newValue) {
151 double value = newValue;
152
153 final double min = getMinNumeric();
154 final double max = getMaxNumeric();
155
156 if (value > max) {
157 value = max;
158 }
159 else {
160 if (value < min) {
161 value = min;
162 }
163 }
164
165 final double step = getStepNumeric();
166 value = value - min;
167 int fact = (int) (value / step);
168 final double rest = value % step;
169 if (rest >= step / 2) {
170 fact++;
171 }
172 value = min + step * fact;
173
174 if (!Double.isInfinite(value) && (value == Math.floor(value))) {
175 super.setValue(Integer.toString((int) value));
176 }
177 else {
178 super.setValue(Double.toString(value));
179 }
180 }
181
182 @Override
183 protected void valueAttributeChanged(final String attributeValue, final boolean isValueDirty) {
184 if (isValueDirty) {
185 return;
186 }
187
188 try {
189 if (!org.htmlunit.util.StringUtils.isEmptyOrNull(attributeValue)) {
190 setRawValue(Double.parseDouble(attributeValue));
191 }
192 else {
193 final double min = getMinNumeric();
194 final double max = getMaxNumeric();
195
196
197 setRawValue(min + ((max - min) / 2));
198 }
199 }
200 catch (final NumberFormatException ignored) {
201
202 }
203 }
204
205 private void setRawValue(final double newValue) {
206 double value = newValue;
207
208 final double min = getMinNumeric();
209 final double max = getMaxNumeric();
210
211 if (value > max) {
212 value = max;
213 }
214 else {
215 if (value < min) {
216 value = min;
217 }
218 }
219
220 final double step = getStepNumeric();
221 value = value - min;
222 int fact = (int) (value / step);
223 final double rest = value % step;
224 if (rest >= step / 2) {
225 fact++;
226 }
227 value = min + step * fact;
228
229 if (!Double.isInfinite(value) && value == Math.floor(value)) {
230 setRawValue(Integer.toString((int) value));
231 }
232 else {
233 setRawValue(Double.toString(value));
234 }
235 }
236
237
238
239
240 @Override
241 protected boolean isRequiredSupported() {
242 return false;
243 }
244
245
246
247
248 @Override
249 public boolean isValid() {
250 return super.isValid() && isMaxValid() && isMinValid();
251 }
252
253
254
255
256
257
258
259
260 private boolean isMinValid() {
261 if (!getRawValue().isEmpty() && !getMin().isEmpty()) {
262 try {
263 final double value = Double.parseDouble(getRawValue());
264 return getMinNumeric() <= value;
265 }
266 catch (final NumberFormatException ignored) {
267
268 }
269 }
270 return true;
271 }
272
273
274
275
276
277
278
279
280 private boolean isMaxValid() {
281 if (!getRawValue().isEmpty() && !getMax().isEmpty()) {
282 try {
283 final int value = Integer.parseInt(getRawValue());
284 return getMaxNumeric() >= value;
285 }
286 catch (final NumberFormatException ignored) {
287
288 }
289 }
290 return true;
291 }
292 }