root / src / gr / ebs / gss / client / components / TristateCheckBox.java @ 623:66f69a7348ed
History | View | Annotate | Download (3.7 kB)
1 |
package gr.ebs.gss.client.components; |
---|---|
2 |
|
3 |
import com.google.gwt.event.dom.client.ClickEvent; |
4 |
import com.google.gwt.event.dom.client.ClickHandler; |
5 |
import com.google.gwt.event.logical.shared.ValueChangeEvent; |
6 |
import com.google.gwt.event.logical.shared.ValueChangeHandler; |
7 |
import com.google.gwt.event.shared.HandlerRegistration; |
8 |
import com.google.gwt.user.client.DOM; |
9 |
import com.google.gwt.user.client.Element; |
10 |
import com.google.gwt.user.client.ui.FocusWidget; |
11 |
import com.google.gwt.user.client.ui.HasValue; |
12 |
|
13 |
/**
|
14 |
* Checkbox with three states (checked, unchecked and undefined).
|
15 |
*
|
16 |
*/
|
17 |
public class TristateCheckBox extends FocusWidget implements HasValue<Boolean> { |
18 |
|
19 |
private static final String UNCHECKED_IMG = "images/tristate_unchecked.gif"; |
20 |
|
21 |
private static final String UNKNOWN_IMG = "images/tristate_intermediate.gif"; |
22 |
|
23 |
private static final String CHECKED_IMG = "images/tristate_checked.gif"; |
24 |
|
25 |
private final Element buttonElement = DOM.createElement("input"); |
26 |
|
27 |
private boolean valueChangeHandlerInitialized; |
28 |
|
29 |
private Boolean value; |
30 |
|
31 |
private Boolean initialValue; |
32 |
|
33 |
public TristateCheckBox(final Boolean state) { |
34 |
DOM.setElementProperty(buttonElement, "type", "image"); |
35 |
setElement(buttonElement); |
36 |
setStyleName("tristateCheckbox");
|
37 |
DOM.setElementAttribute(buttonElement, "src", UNCHECKED_IMG);
|
38 |
|
39 |
addClickHandler(new ClickHandler() {
|
40 |
|
41 |
@Override
|
42 |
public void onClick(ClickEvent event) { |
43 |
final String img = DOM.getElementAttribute(buttonElement, "src"); |
44 |
String newImg;
|
45 |
if (img.endsWith(UNCHECKED_IMG))
|
46 |
newImg = CHECKED_IMG; |
47 |
else if (img.endsWith(UNKNOWN_IMG)) |
48 |
newImg = UNCHECKED_IMG; |
49 |
else if (img.endsWith(CHECKED_IMG)) |
50 |
if (initialValue==null) // Only show unknown choice if there is a reason for it |
51 |
newImg = UNKNOWN_IMG; |
52 |
else
|
53 |
newImg = UNCHECKED_IMG; |
54 |
else
|
55 |
throw new IllegalArgumentException("unknown checkbox state"); |
56 |
|
57 |
DOM.setElementAttribute(buttonElement, "src", newImg);
|
58 |
} |
59 |
|
60 |
}); |
61 |
|
62 |
setState(state); |
63 |
initialValue = state; |
64 |
} |
65 |
|
66 |
public void setState(final Boolean state) { |
67 |
DOM.setElementAttribute(buttonElement, "src", state == null ? |
68 |
UNKNOWN_IMG : state.booleanValue() ? CHECKED_IMG : UNCHECKED_IMG); |
69 |
} |
70 |
|
71 |
public Boolean getState() { |
72 |
final String img = DOM.getElementAttribute(buttonElement, "src"); |
73 |
if (img.endsWith(UNCHECKED_IMG))
|
74 |
return Boolean.FALSE; |
75 |
else if (img.endsWith(UNKNOWN_IMG)) |
76 |
return null; |
77 |
else if (img.endsWith(CHECKED_IMG)) |
78 |
return Boolean.TRUE; |
79 |
else
|
80 |
throw new IllegalArgumentException("unknown checkbox state"); |
81 |
} |
82 |
|
83 |
@Override
|
84 |
public Boolean getValue() { |
85 |
return value;
|
86 |
} |
87 |
|
88 |
@Override
|
89 |
public void setValue(final Boolean _value) { |
90 |
value = _value; |
91 |
} |
92 |
|
93 |
@Override
|
94 |
public HandlerRegistration addValueChangeHandler(
|
95 |
ValueChangeHandler<Boolean> handler) {
|
96 |
// Is this the first value change handler? If so, time to add handlers
|
97 |
if (!valueChangeHandlerInitialized) {
|
98 |
ensureDomEventHandlers(); |
99 |
valueChangeHandlerInitialized = true;
|
100 |
} |
101 |
return addHandler(handler, ValueChangeEvent.getType());
|
102 |
} |
103 |
|
104 |
protected void ensureDomEventHandlers() { |
105 |
addClickHandler(new ClickHandler() {
|
106 |
@Override
|
107 |
public void onClick(ClickEvent event) { |
108 |
ValueChangeEvent.fire(TristateCheckBox.this, getValue()); |
109 |
} |
110 |
}); |
111 |
} |
112 |
|
113 |
@Override
|
114 |
public void setValue(Boolean _value, boolean fireEvents) { |
115 |
Boolean oldValue = getValue();
|
116 |
setValue(_value); |
117 |
if (_value.equals(oldValue))
|
118 |
return;
|
119 |
if (fireEvents)
|
120 |
ValueChangeEvent.fire(this, _value);
|
121 |
} |
122 |
|
123 |
} |