1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.html;
16
17 import static org.htmlunit.util.StringUtils.toRootLowerCase;
18
19 import java.io.IOException;
20 import java.util.Map;
21
22 import org.htmlunit.Page;
23 import org.htmlunit.SgmlPage;
24 import org.htmlunit.javascript.host.event.KeyboardEvent;
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40 public class HtmlCheckBoxInput extends HtmlInput implements LabelableElement {
41
42
43
44
45 private static final String DEFAULT_VALUE = "on";
46
47 private boolean defaultCheckedState_;
48 private boolean checkedState_;
49
50
51
52
53
54
55
56
57
58
59
60 HtmlCheckBoxInput(final String qualifiedName, final SgmlPage page,
61 final Map<String, DomAttr> attributes) {
62 super(qualifiedName, page, attributes);
63
64 if (getAttributeDirect(VALUE_ATTRIBUTE) == ATTRIBUTE_NOT_DEFINED) {
65 setRawValue(DEFAULT_VALUE);
66 }
67
68 defaultCheckedState_ = hasAttribute(ATTRIBUTE_CHECKED);
69 checkedState_ = defaultCheckedState_;
70 }
71
72
73
74
75
76 @Override
77 public boolean isChecked() {
78 return checkedState_;
79 }
80
81
82
83
84
85 @Override
86 public void reset() {
87 setChecked(defaultCheckedState_);
88 }
89
90
91
92
93 @Override
94 public Page setChecked(final boolean isChecked) {
95 checkedState_ = isChecked;
96 return executeOnChangeHandlerIfAppropriate(this);
97 }
98
99
100
101
102 @Override
103 protected void doType(final int keyCode, final boolean lastType) throws IOException {
104 if (keyCode == KeyboardEvent.DOM_VK_SPACE) {
105 click();
106 return;
107 }
108
109 super.doType(keyCode, lastType);
110 }
111
112
113
114
115 @Override
116 protected boolean doClickStateUpdate(final boolean shiftKey, final boolean ctrlKey) throws IOException {
117 checkedState_ = !isChecked();
118 super.doClickStateUpdate(shiftKey, ctrlKey);
119 return true;
120 }
121
122
123
124
125 @Override
126 protected void doClickFireChangeEvent() {
127 executeOnChangeHandlerIfAppropriate(this);
128 }
129
130
131
132
133
134 @Override
135 protected boolean isStateUpdateFirst() {
136 return true;
137 }
138
139
140
141
142 @Override
143 protected void preventDefault() {
144 checkedState_ = !checkedState_;
145 }
146
147
148
149
150
151
152 @Override
153 public void setDefaultValue(final String defaultValue) {
154 super.setDefaultValue(defaultValue);
155 setValue(defaultValue);
156 }
157
158
159
160
161
162 @Override
163 public void setValue(final String newValue) {
164 super.setValue(newValue);
165 super.setDefaultValue(newValue);
166 }
167
168
169
170
171
172 @Override
173 public void setDefaultChecked(final boolean defaultChecked) {
174 defaultCheckedState_ = defaultChecked;
175 setChecked(isDefaultChecked());
176 }
177
178
179
180
181
182 @Override
183 public boolean isDefaultChecked() {
184 return defaultCheckedState_;
185 }
186
187 @Override
188 protected Object getInternalValue() {
189 return isChecked();
190 }
191
192 @Override
193 void handleFocusLostValueChanged() {
194
195 }
196
197
198
199
200 @Override
201 protected void setAttributeNS(final String namespaceURI, final String qualifiedName, final String attributeValue,
202 final boolean notifyAttributeChangeListeners, final boolean notifyMutationObservers) {
203 final String qualifiedNameLC = toRootLowerCase(qualifiedName);
204
205 if (VALUE_ATTRIBUTE.equals(qualifiedNameLC)) {
206 super.setAttributeNS(namespaceURI, qualifiedNameLC, attributeValue, notifyAttributeChangeListeners,
207 notifyMutationObservers);
208 setRawValue(attributeValue);
209 return;
210 }
211
212 if (ATTRIBUTE_CHECKED.equals(qualifiedNameLC)) {
213 checkedState_ = true;
214 }
215 super.setAttributeNS(namespaceURI, qualifiedNameLC, attributeValue, notifyAttributeChangeListeners,
216 notifyMutationObservers);
217 }
218
219
220
221
222 @Override
223 protected boolean propagateClickStateUpdateToParent() {
224 return false;
225 }
226
227 @Override
228 public boolean isValueMissingValidityState() {
229 return hasAttribute(ATTRIBUTE_REQUIRED) && !isChecked();
230 }
231 }