|
21 | 21 | */ |
22 | 22 |
|
23 | 23 | 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; |
24 | 30 | import com.google.gwt.uibinder.client.UiBinder; |
25 | 31 | import com.google.gwt.user.client.ui.Composite; |
26 | 32 | import com.google.gwt.user.client.ui.Widget; |
27 | 33 |
|
28 | | -public class MaterialRange extends Composite { |
| 34 | +/** |
| 35 | + * Slider for numeric values |
| 36 | + */ |
| 37 | +public class MaterialRange extends Composite implements HasChangeHandlers{ |
29 | 38 |
|
30 | | - private static MaterialRangeUiBinder uiBinder = GWT |
31 | | - .create(MaterialRangeUiBinder.class); |
| 39 | + private static MaterialRangeUiBinder uiBinder = GWT.create(MaterialRangeUiBinder.class); |
32 | 40 |
|
33 | 41 | interface MaterialRangeUiBinder extends UiBinder<Widget, MaterialRange> { |
34 | 42 | } |
35 | 43 |
|
| 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 | + |
36 | 52 | public MaterialRange() { |
37 | 53 | initWidget(uiBinder.createAndBindUi(this)); |
38 | 54 | } |
39 | 55 |
|
| 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 | + |
40 | 161 | } |
0 commit comments