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.apache.commons.lang3.StringUtils;
20 import org.htmlunit.SgmlPage;
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 public double getMinNumeric() {
67 final String min = getAttributeDirect("min");
68 if (min == ATTRIBUTE_NOT_DEFINED) {
69 return 0;
70 }
71 try {
72 return Double.parseDouble(min);
73 }
74 catch (final NumberFormatException e) {
75 return 0;
76 }
77 }
78
79
80
81
82 public double getMaxNumeric() {
83 final String max = getAttributeDirect("max");
84 if (max == ATTRIBUTE_NOT_DEFINED) {
85 return 100;
86 }
87 try {
88 return Double.parseDouble(max);
89 }
90 catch (final NumberFormatException e) {
91 return 100;
92 }
93 }
94
95
96
97
98 public double getStepNumeric() {
99 final String step = getAttributeDirect("step");
100 if (step == ATTRIBUTE_NOT_DEFINED) {
101 return 1;
102 }
103 try {
104 return Double.parseDouble(step);
105 }
106 catch (final NumberFormatException e) {
107 return 1;
108 }
109 }
110
111
112
113
114 @Override
115 public void setDefaultChecked(final boolean defaultChecked) {
116
117 }
118
119
120
121
122 @Override
123 public void setValue(final String newValue) {
124 try {
125 if (StringUtils.isNotEmpty(newValue)) {
126 setValue(Double.parseDouble(newValue));
127 }
128 else {
129 final double min = getMinNumeric();
130 final double max = getMaxNumeric();
131
132
133 setValue(min + ((max - min) / 2));
134 }
135 }
136 catch (final NumberFormatException ignored) {
137
138 }
139 }
140
141 private void setValue(final double newValue) {
142 double value = newValue;
143
144 final double min = getMinNumeric();
145 final double max = getMaxNumeric();
146
147 if (value > max) {
148 value = max;
149 }
150 else {
151 if (value < min) {
152 value = min;
153 }
154 }
155
156 final double step = getStepNumeric();
157 value = value - min;
158 int fact = (int) (value / step);
159 final double rest = value % step;
160 if (rest >= step / 2) {
161 fact++;
162 }
163 value = min + step * fact;
164
165 if (!Double.isInfinite(value) && (value == Math.floor(value))) {
166 super.setValue(Integer.toString((int) value));
167 }
168 else {
169 super.setValue(Double.toString(value));
170 }
171 }
172
173 @Override
174 protected void valueAttributeChanged(final String attributeValue, final boolean isValueDirty) {
175 if (isValueDirty) {
176 return;
177 }
178
179 try {
180 if (StringUtils.isNotEmpty(attributeValue)) {
181 setRawValue(Double.parseDouble(attributeValue));
182 }
183 else {
184 final double min = getMinNumeric();
185 final double max = getMaxNumeric();
186
187
188 setRawValue(min + ((max - min) / 2));
189 }
190 }
191 catch (final NumberFormatException ignored) {
192
193 }
194 }
195
196 private void setRawValue(final double newValue) {
197 double value = newValue;
198
199 final double min = getMinNumeric();
200 final double max = getMaxNumeric();
201
202 if (value > max) {
203 value = max;
204 }
205 else {
206 if (value < min) {
207 value = min;
208 }
209 }
210
211 final double step = getStepNumeric();
212 value = value - min;
213 int fact = (int) (value / step);
214 final double rest = value % step;
215 if (rest >= step / 2) {
216 fact++;
217 }
218 value = min + step * fact;
219
220 if (!Double.isInfinite(value) && value == Math.floor(value)) {
221 setRawValue(Integer.toString((int) value));
222 }
223 else {
224 setRawValue(Double.toString(value));
225 }
226 }
227
228
229
230
231 @Override
232 protected boolean isRequiredSupported() {
233 return false;
234 }
235
236
237
238
239 @Override
240 public boolean isValid() {
241 return super.isValid() && isMaxValid() && isMinValid();
242 }
243
244
245
246
247
248
249
250
251 private boolean isMinValid() {
252 if (!getRawValue().isEmpty() && !getMin().isEmpty()) {
253 try {
254 final double value = Double.parseDouble(getRawValue());
255 return getMinNumeric() <= value;
256 }
257 catch (final NumberFormatException ignored) {
258
259 }
260 }
261 return true;
262 }
263
264
265
266
267
268
269
270
271 private boolean isMaxValid() {
272 if (!getRawValue().isEmpty() && !getMax().isEmpty()) {
273 try {
274 final int value = Integer.parseInt(getRawValue());
275 return getMaxNumeric() >= value;
276 }
277 catch (final NumberFormatException ignored) {
278
279 }
280 }
281 return true;
282 }
283 }