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 import org.htmlunit.html.impl.Color;
22
23
24
25
26
27
28
29
30 public class HtmlColorInput extends HtmlInput implements LabelableElement {
31
32
33
34
35
36
37
38
39 HtmlColorInput(final String qualifiedName, final SgmlPage page,
40 final Map<String, DomAttr> attributes) {
41 super(qualifiedName, page, attributes);
42 if (getValueAttribute() == ATTRIBUTE_NOT_DEFINED) {
43 setValue("#000000");
44 }
45 }
46
47
48
49
50 @Override
51 public void setDefaultChecked(final boolean defaultChecked) {
52
53 }
54
55
56
57
58 @Override
59 public void setValue(final String newValue) {
60 if (StringUtils.isEmpty(newValue)) {
61 super.setValue("#000000");
62 return;
63 }
64
65 if (isValid(newValue)) {
66 super.setValue(newValue);
67 }
68 }
69
70 @Override
71 protected void valueAttributeChanged(final String attributeValue, final boolean isValueDirty) {
72 if (isValueDirty) {
73 return;
74 }
75
76 if (StringUtils.isEmpty(attributeValue)) {
77 setRawValue("#000000");
78 return;
79 }
80
81 if (isValid(attributeValue)) {
82 setRawValue(attributeValue);
83 }
84 }
85
86 private static boolean isValid(final String value) {
87 boolean valid = false;
88 if (value.length() == 7 && value.charAt(0) == '#') {
89 try {
90 new Color(
91 Integer.valueOf(value.substring(1, 3), 16),
92 Integer.valueOf(value.substring(3, 5), 16),
93 Integer.valueOf(value.substring(5, 7), 16));
94 valid = true;
95 }
96 catch (final IllegalArgumentException ignored) {
97
98 }
99 }
100 return valid;
101 }
102
103
104
105
106 @Override
107 protected boolean isRequiredSupported() {
108 return false;
109 }
110 }