/
BaseCheckBox.java
591 lines (531 loc) · 18.6 KB
/
BaseCheckBox.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
/*
* #%L
* GwtMaterial
* %%
* Copyright (C) 2015 - 2017 GwtMaterialDesign
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
/*
* Copyright 2009 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package gwt.material.design.client.base;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.InputElement;
import com.google.gwt.dom.client.LabelElement;
import com.google.gwt.dom.client.Style.WhiteSpace;
import com.google.gwt.editor.client.IsEditor;
import com.google.gwt.editor.client.LeafValueEditor;
import com.google.gwt.editor.client.adapters.TakesValueEditor;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.i18n.client.HasDirection.Direction;
import com.google.gwt.i18n.shared.DirectionEstimator;
import com.google.gwt.i18n.shared.HasDirectionEstimator;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.*;
import gwt.material.design.client.constants.CssName;
/**
* A standard check box widget.
* <p>
* This class also serves as a base class for
* {@link RadioButton}.
* <p>
* <p>
* <img class='gallery' src='doc-files/CheckBox.png'/>
* </p>
* <p>
* <p>
* <h3>Built-in Bidi Text Support</h3>
* This widget is capable of automatically adjusting its direction according to
* its content. This feature is controlled by {@link #setDirectionEstimator} or
* passing a DirectionEstimator parameter to the constructor, and is off by
* default.
* </p>
* <p>
* <h3>CSS Style Rules</h3>
* <dl>
* <dt>.gwt-CheckBox</dt>
* <dd>the outer element</dd>
* <dt>.gwt-CheckBox-disabled</dt>
* <dd>applied when Checkbox is disabled</dd>
* </dl>
* <p>
* <p>
* <h3>Example</h3>
* {@example com.google.gwt.examples.CheckBoxExample}
* </p>
*/
public class BaseCheckBox extends ButtonBase implements HasName, HasValue<Boolean>,
HasWordWrap, HasDirectionalSafeHtml, HasDirectionEstimator, IsEditor<LeafValueEditor<Boolean>> {
public static final DirectionEstimator DEFAULT_DIRECTION_ESTIMATOR =
DirectionalTextHelper.DEFAULT_DIRECTION_ESTIMATOR;
final DirectionalTextHelper directionalTextHelper;
InputElement inputElem;
LabelElement labelElem;
private LeafValueEditor<Boolean> editor;
private boolean valueChangeHandlerInitialized;
/**
* Creates a check box with no label.
*/
public BaseCheckBox() {
this(DOM.createSpan());
setStyleName(CssName.GWT_CHECKBOX);
}
/**
* Creates a check box with the specified text label.
*
* @param label the check box's label
*/
public BaseCheckBox(SafeHtml label) {
this(label.asString(), true);
}
/**
* Creates a check box with the specified text label.
*
* @param label the check box's label
* @param dir the text's direction. Note that {@code DEFAULT} means direction
* should be inherited from the widget's parent element.
*/
public BaseCheckBox(SafeHtml label, Direction dir) {
this();
setHTML(label, dir);
}
/**
* Creates a check box with the specified text label.
*
* @param label the check box's label
* @param directionEstimator A DirectionEstimator object used for automatic
* direction adjustment. For convenience,
* {@link #DEFAULT_DIRECTION_ESTIMATOR} can be used.
*/
public BaseCheckBox(SafeHtml label, DirectionEstimator directionEstimator) {
this();
setDirectionEstimator(directionEstimator);
setHTML(label.asString());
}
/**
* Creates a check box with the specified text label.
*
* @param label the check box's label
*/
public BaseCheckBox(String label) {
this();
setText(label);
}
/**
* Creates a check box with the specified text label.
*
* @param label the check box's label
* @param dir the text's direction. Note that {@code DEFAULT} means direction
* should be inherited from the widget's parent element.
*/
public BaseCheckBox(String label, Direction dir) {
this();
setText(label, dir);
}
/**
* Creates a label with the specified text and a default direction estimator.
*
* @param label the check box's label
* @param directionEstimator A DirectionEstimator object used for automatic
* direction adjustment. For convenience,
* {@link #DEFAULT_DIRECTION_ESTIMATOR} can be used.
*/
public BaseCheckBox(String label, DirectionEstimator directionEstimator) {
this();
setDirectionEstimator(directionEstimator);
setText(label);
}
/**
* Creates a check box with the specified text label.
*
* @param label the check box's label
* @param asHTML <code>true</code> to treat the specified label as html
*/
public BaseCheckBox(String label, boolean asHTML) {
this();
if (asHTML) {
setHTML(label);
} else {
setText(label);
}
}
protected BaseCheckBox(Element elem) {
super(elem);
inputElem = InputElement.as(DOM.createInputCheck());
labelElem = Document.get().createLabelElement();
getElement().appendChild(inputElem);
getElement().appendChild(labelElem);
String uid = DOM.createUniqueId();
inputElem.setPropertyString("id", uid);
labelElem.setHtmlFor(uid);
directionalTextHelper = new DirectionalTextHelper(labelElem, true);
// Accessibility: setting tab index to be 0 by default, ensuring element
// appears in tab sequence. FocusWidget's setElement method already
// calls setTabIndex, which is overridden below. However, at the time
// that this call is made, inputElem has not been created. So, we have
// to call setTabIndex again, once inputElem has been created.
setTabIndex(0);
}
@Override
public HandlerRegistration addValueChangeHandler(
final ValueChangeHandler<Boolean> handler) {
// Is this the first value change handler? If so, time to add handlers
if (!valueChangeHandlerInitialized) {
ensureDomEventHandlers();
valueChangeHandlerInitialized = true;
}
return addHandler(handler, ValueChangeEvent.getType());
}
@Override
public LeafValueEditor<Boolean> asEditor() {
if (editor == null) {
editor = TakesValueEditor.of(this);
}
return editor;
}
@Override
public DirectionEstimator getDirectionEstimator() {
return directionalTextHelper.getDirectionEstimator();
}
/**
* Returns the value property of the input element that backs this widget.
* This is the value that will be associated with the CheckBox name and
* submitted to the server if a {@link FormPanel} that holds it is submitted
* and the box is checked.
* <p>
* Don't confuse this with {@link #getValue}, which returns true or false if
* the widget is checked.
*/
public String getFormValue() {
return inputElem.getValue();
}
@Override
public String getHTML() {
return directionalTextHelper.getTextOrHtml(true);
}
@Override
public String getName() {
return inputElem.getName();
}
@Override
public int getTabIndex() {
return inputElem.getTabIndex();
}
@Override
public String getText() {
return directionalTextHelper.getTextOrHtml(false);
}
@Override
public Direction getTextDirection() {
return directionalTextHelper.getTextDirection();
}
/**
* Determines whether this check box is currently checked.
* <p>
* Note that this <em>does not</em> return the value property of the checkbox
* input element wrapped by this widget. For access to that property, see
* {@link #getFormValue()}
*
* @return <code>true</code> if the check box is checked, false otherwise.
* Will not return null
*/
@Override
public Boolean getValue() {
if (isAttached()) {
return inputElem.isChecked();
} else {
return inputElem.isDefaultChecked();
}
}
@Override
public boolean getWordWrap() {
return !WhiteSpace.NOWRAP.getCssName().equals(getElement().getStyle().getWhiteSpace());
}
/**
* Determines whether this check box is currently checked.
*
* @return <code>true</code> if the check box is checked
* @deprecated Use {@link #getValue} instead
*/
@Deprecated
public boolean isChecked() {
// Funny comparison b/c getValue could in theory return null
return getValue() == true;
}
@Override
public boolean isEnabled() {
return !inputElem.isDisabled();
}
@Override
public void setAccessKey(char key) {
inputElem.setAccessKey("" + key);
}
/**
* Checks or unchecks this check box. Does not fire {@link ValueChangeEvent}.
* (If you want the event to fire, use {@link #setValue(Boolean, boolean)})
*
* @param checked <code>true</code> to check the check box.
* @deprecated Use {@link #setValue(Boolean)} instead
*/
@Deprecated
public void setChecked(boolean checked) {
setValue(checked);
}
/**
* {@inheritDoc}
* <p>
* See note at {@link #setDirectionEstimator(DirectionEstimator)}.
*/
@Override
public void setDirectionEstimator(boolean enabled) {
directionalTextHelper.setDirectionEstimator(enabled);
}
/**
* {@inheritDoc}
* <p>
* Note: DirectionEstimator should be set before the label has any content;
* it's highly recommended to set it using a constructor. Reason: if the
* label already has non-empty content, this will update its direction
* according to the new estimator's result. This may cause flicker, and thus
* should be avoided.
*/
@Override
public void setDirectionEstimator(DirectionEstimator directionEstimator) {
directionalTextHelper.setDirectionEstimator(directionEstimator);
}
@Override
public void setEnabled(boolean enabled) {
inputElem.setDisabled(!enabled);
if (enabled) {
removeStyleDependentName(CssName.DISABLED);
} else {
addStyleDependentName(CssName.DISABLED);
}
}
@Override
public void setFocus(boolean focused) {
if (focused) {
inputElem.focus();
} else {
inputElem.blur();
}
}
/**
* Set the value property on the input element that backs this widget. This is
* the value that will be associated with the CheckBox's name and submitted to
* the server if a {@link FormPanel} that holds it is submitted and the box is
* checked.
* <p>
* Don't confuse this with {@link #setValue}, which actually checks and
* unchecks the box.
*
* @param value
*/
public void setFormValue(String value) {
inputElem.setAttribute("value", value);
}
@Override
public void setHTML(SafeHtml html, Direction dir) {
directionalTextHelper.setTextOrHtml(html.asString(), dir, true);
}
@Override
public void setHTML(String html) {
directionalTextHelper.setTextOrHtml(html, true);
}
@Override
public void setName(String name) {
inputElem.setName(name);
}
@Override
public void setTabIndex(int index) {
// Need to guard against call to setTabIndex before inputElem is
// initialized. This happens because FocusWidget's (a superclass of
// CheckBox) setElement method calls setTabIndex before inputElem is
// initialized. See CheckBox's protected constructor for more information.
if (inputElem != null) {
inputElem.setTabIndex(index);
}
}
@Override
public void setText(String text) {
directionalTextHelper.setTextOrHtml(text, false);
}
@Override
public void setText(String text, Direction dir) {
directionalTextHelper.setTextOrHtml(text, dir, false);
}
/**
* Checks or unchecks the check box.
* <p>
* Note that this <em>does not</em> set the value property of the checkbox
* input element wrapped by this widget. For access to that property, see
* {@link #setFormValue(String)}
*
* @param value true to check, false to uncheck; null value implies false
*/
@Override
public void setValue(Boolean value) {
setValue(value, false);
}
/**
* Checks or unchecks the check box, firing {@link ValueChangeEvent} if
* appropriate.
* <p>
* Note that this <em>does not</em> set the value property of the checkbox
* input element wrapped by this widget. For access to that property, see
* {@link #setFormValue(String)}
*
* @param value true to check, false to uncheck; null value implies false
* @param fireEvents If true, and value has changed, fire a
* {@link ValueChangeEvent}
*/
@Override
public void setValue(Boolean value, boolean fireEvents) {
if (value == null) {
value = Boolean.FALSE;
}
Boolean oldValue = getValue();
inputElem.setChecked(value);
inputElem.setDefaultChecked(value);
if (value.equals(oldValue)) {
return;
}
if (fireEvents) {
ValueChangeEvent.fire(this, value);
}
}
@Override
public void setWordWrap(boolean wrap) {
getElement().getStyle().setWhiteSpace(wrap ? WhiteSpace.NORMAL : WhiteSpace.NOWRAP);
}
// Unlike other widgets the CheckBox sinks on its inputElement, not
// its wrapper
@Override
public void sinkEvents(int eventBitsToAdd) {
if (isOrWasAttached()) {
Event.sinkEvents(inputElem, eventBitsToAdd
| Event.getEventsSunk(inputElem));
} else {
super.sinkEvents(eventBitsToAdd);
}
}
protected void ensureDomEventHandlers() {
addClickHandler(event -> {
// Checkboxes always toggle their value, no need to compare
// with old value. Radio buttons are not so lucky, see
// overrides in RadioButton
ValueChangeEvent.fire(BaseCheckBox.this, getValue());
});
}
/**
* <b>Affected Elements:</b>
* <ul>
* <li>-label = label next to checkbox.</li>
* </ul>
*
* @see UIObject#onEnsureDebugId(String)
*/
@Override
protected void onEnsureDebugId(String baseID) {
super.onEnsureDebugId(baseID);
ensureDebugId(labelElem, baseID, "label");
ensureDebugId(inputElem, baseID, "input");
labelElem.setHtmlFor(inputElem.getId());
}
/**
* This method is called when a widget is attached to the browser's document.
* onAttach needs special handling for the CheckBox case. Must still call
* {@link Widget#onAttach()} to preserve the <code>onAttach</code> contract.
*/
@Override
protected void onLoad() {
DOM.setEventListener(inputElem, this);
}
/**
* This method is called when a widget is detached from the browser's
* document. Overridden because of IE bug that throws away checked state and
* in order to clear the event listener off of the <code>inputElem</code>.
*/
@Override
protected void onUnload() {
// Clear out the inputElem's event listener (breaking the circular
// reference between it and the widget).
DOM.setEventListener(inputElem, null);
setValue(getValue());
}
/**
* Replace the current input element with a new one. Preserves all state
* except for the name property, for nasty reasons related to radio button
* grouping. (See implementation of {@link RadioButton#setName}.)
*
* @param elem the new input element
*/
protected void replaceInputElement(Element elem) {
replaceInputElement(DOM.asOld(elem));
}
/**
* @deprecated Call and use {@link #replaceInputElement(Element)} instead.
*/
@Deprecated
protected void replaceInputElement(com.google.gwt.user.client.Element elem) {
InputElement newInputElem = InputElement.as(elem);
// Collect information we need to set
int tabIndex = getTabIndex();
boolean checked = getValue();
boolean enabled = isEnabled();
String formValue = getFormValue();
String uid = inputElem.getId();
String accessKey = inputElem.getAccessKey();
int sunkEvents = Event.getEventsSunk(inputElem);
// Clear out the old input element
DOM.setEventListener(inputElem, null);
getElement().replaceChild(newInputElem, inputElem);
// Sink events on the new element
Event.sinkEvents(elem, Event.getEventsSunk(inputElem));
Event.sinkEvents(inputElem, 0);
inputElem = newInputElem;
// Setup the new element
Event.sinkEvents(inputElem, sunkEvents);
inputElem.setId(uid);
if (!"".equals(accessKey)) {
inputElem.setAccessKey(accessKey);
}
setTabIndex(tabIndex);
setValue(checked);
setEnabled(enabled);
setFormValue(formValue);
// Set the event listener
if (isAttached()) {
DOM.setEventListener(inputElem, this);
}
}
}