Skip to content

Commit df33f90

Browse files
committed
MaterialRange extension with min,max,value and ChangeHandler
1 parent 7f3a9c4 commit df33f90

File tree

2 files changed

+125
-4
lines changed

2 files changed

+125
-4
lines changed

src/main/java/gwt/material/design/client/ui/MaterialRange.java

Lines changed: 124 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,141 @@
2121
*/
2222

2323
import com.google.gwt.core.client.GWT;
24+
import com.google.gwt.dom.client.Element;
25+
import com.google.gwt.dom.client.NodeList;
26+
import com.google.gwt.event.dom.client.ChangeEvent;
27+
import com.google.gwt.event.dom.client.ChangeHandler;
28+
import com.google.gwt.event.dom.client.HasChangeHandlers;
29+
import com.google.gwt.event.shared.HandlerRegistration;
2430
import com.google.gwt.uibinder.client.UiBinder;
2531
import com.google.gwt.user.client.ui.Composite;
2632
import com.google.gwt.user.client.ui.Widget;
2733

28-
public class MaterialRange extends Composite {
34+
/**
35+
* Slider for numeric values
36+
*/
37+
public class MaterialRange extends Composite implements HasChangeHandlers{
2938

30-
private static MaterialRangeUiBinder uiBinder = GWT
31-
.create(MaterialRangeUiBinder.class);
39+
private static MaterialRangeUiBinder uiBinder = GWT.create(MaterialRangeUiBinder.class);
3240

3341
interface MaterialRangeUiBinder extends UiBinder<Widget, MaterialRange> {
3442
}
3543

44+
private static String VALUE = "value";
45+
private static String MAX = "max";
46+
private static String MIN = "min";
47+
private static String INPUT = "INPUT";
48+
49+
// cache the embedded range input element
50+
private Element rangeElement = null;
51+
3652
public MaterialRange() {
3753
initWidget(uiBinder.createAndBindUi(this));
3854
}
3955

56+
/**
57+
* Try to identify the embedded range elements input field (see ui xml)
58+
* @return The found element or null if none found.
59+
*/
60+
private Element getRangeElement() {
61+
if (rangeElement == null) {
62+
NodeList<Element> elements = this.getElement().getElementsByTagName(INPUT);
63+
if (elements != null && elements.getLength() > 0) {
64+
rangeElement = elements.getItem(0);
65+
}
66+
}
67+
return rangeElement;
68+
}
69+
70+
/**
71+
* Retrieve the Integer value from the given Attribute of the range element
72+
* @param attribute The name of the attribute on the range element
73+
* @return The Integer vaulue read from the given attribute or null
74+
*/
75+
private Integer getIntFromRangeElement(String attribute){
76+
Element ele = getRangeElement();
77+
if(ele!=null){
78+
return ele.getPropertyInt(attribute);
79+
}
80+
return null;
81+
}
82+
83+
/**
84+
* Set the given Integer value to the attribute of the range element
85+
* @param attribute
86+
* @param val
87+
*/
88+
private void setIntToRangeElement(String attribute,Integer val)
89+
{
90+
Element ele = getRangeElement();
91+
if(ele!=null){
92+
ele.setPropertyInt(attribute,val);
93+
}
94+
}
95+
96+
/**
97+
* Read the current value
98+
* @return The Integer value or null
99+
*/
100+
public Integer getValue() {
101+
return getIntFromRangeElement(VALUE);
102+
}
103+
104+
/**
105+
* Write the current value
106+
* @param value value must be >= min and <= max
107+
*/
108+
public void setValue(Integer value) {
109+
if (value==null)return;
110+
if (value<getMin())return;
111+
if (value>getMax())return;
112+
setIntToRangeElement(VALUE,value);
113+
}
114+
115+
/**
116+
* Read the min value
117+
* @return The Integer or null
118+
*/
119+
public Integer getMin() {
120+
return getIntFromRangeElement(MIN);
121+
}
122+
123+
/**
124+
* Write the current min value
125+
* @param min value must be < max
126+
*/
127+
public void setMin(Integer min) {
128+
if (min==null)return;
129+
if (min>=getMax())return;
130+
setIntToRangeElement(MIN,min);
131+
}
132+
133+
/**
134+
* Read the max value
135+
* @return The Integer or null
136+
*/
137+
public Integer getMax() {
138+
return getIntFromRangeElement(MAX);
139+
}
140+
141+
/**
142+
* Write the current max value
143+
* @param max value must be > min
144+
*/
145+
public void setMax(Integer max) {
146+
if (max==null)return;
147+
if (max<=getMin())return;
148+
setIntToRangeElement(MAX,max);
149+
}
150+
151+
/**
152+
* Register the ChangeHandler to become notified if the user changes the slider.
153+
* The Handler is called when the user releases the mouse only at the end of the slide
154+
* operation.
155+
*/
156+
@Override
157+
public HandlerRegistration addChangeHandler(ChangeHandler handler) {
158+
return addDomHandler(handler, ChangeEvent.getType());
159+
}
160+
40161
}

src/main/resources/gwt/material/design/client/ui/MaterialRange.ui.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<g:HTMLPanel>
2727
<form action="#">
2828
<p class="range-field">
29-
<input type="range" id="test5" min="0" max="100" />
29+
<input type="range" min="0" max="100" />
3030
</p>
3131
</form>
3232
</g:HTMLPanel>

0 commit comments

Comments
 (0)