|
| 1 | +package com.akjava.gwt.html5.client.input; |
| 2 | + |
| 3 | +/* |
| 4 | + * based on Hidden,Checkbox,ListBox |
| 5 | + * |
| 6 | + * Copyright 2008 Google Inc. |
| 7 | + * |
| 8 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 9 | + * use this file except in compliance with the License. You may obtain a copy of |
| 10 | + * the License at |
| 11 | + * |
| 12 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | + * |
| 14 | + * Unless required by applicable law or agreed to in writing, software |
| 15 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 16 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 17 | + * License for the specific language governing permissions and limitations under |
| 18 | + * the License. |
| 19 | + */ |
| 20 | + |
| 21 | + |
| 22 | +import com.google.gwt.dom.client.Document; |
| 23 | +import com.google.gwt.dom.client.Element; |
| 24 | +import com.google.gwt.dom.client.InputElement; |
| 25 | +import com.google.gwt.editor.client.IsEditor; |
| 26 | +import com.google.gwt.editor.client.LeafValueEditor; |
| 27 | +import com.google.gwt.editor.client.adapters.TakesValueEditor; |
| 28 | +import com.google.gwt.event.dom.client.ChangeEvent; |
| 29 | +import com.google.gwt.event.dom.client.ChangeHandler; |
| 30 | +import com.google.gwt.event.logical.shared.ValueChangeEvent; |
| 31 | +import com.google.gwt.event.logical.shared.ValueChangeHandler; |
| 32 | +import com.google.gwt.event.shared.HandlerRegistration; |
| 33 | +import com.google.gwt.user.client.ui.HasName; |
| 34 | +import com.google.gwt.user.client.ui.HasValue; |
| 35 | +import com.google.gwt.user.client.ui.RootPanel; |
| 36 | +import com.google.gwt.user.client.ui.Widget; |
| 37 | + |
| 38 | +/** |
| 39 | + * |
| 40 | + * @author aki |
| 41 | + * |
| 42 | + */ |
| 43 | +public class Range extends Widget implements HasName,HasValue<Number>, IsEditor<LeafValueEditor<Number>> { |
| 44 | + |
| 45 | + /** |
| 46 | + * Creates a range widget that wraps an existing <input type='range'> |
| 47 | + * element. |
| 48 | + * |
| 49 | + * This element must already be attached to the document. If the element is |
| 50 | + * removed from the document, you must call |
| 51 | + * {@link RootPanel#detachNow(Widget)}. |
| 52 | + * |
| 53 | + * @param element the element to be wrapped |
| 54 | + */ |
| 55 | + public static Range wrap(Element element) { |
| 56 | + // Assert that the element is attached. |
| 57 | + assert Document.get().getBody().isOrHasChild(element); |
| 58 | + |
| 59 | + Range range = new Range(element); |
| 60 | + |
| 61 | + // Mark it attached and remember it for cleanup. |
| 62 | + range.onAttach(); |
| 63 | + RootPanel.detachOnWindowClose(range); |
| 64 | + |
| 65 | + return range; |
| 66 | + } |
| 67 | + |
| 68 | + protected void ensureDomEventHandlers() { |
| 69 | + addChangeHandler(new ChangeHandler() { |
| 70 | + @Override |
| 71 | + public void onChange(ChangeEvent event) { |
| 72 | + ValueChangeEvent.fire(Range.this, getValue()); |
| 73 | + } |
| 74 | + }); |
| 75 | + |
| 76 | + |
| 77 | + } |
| 78 | + |
| 79 | + protected HandlerRegistration addChangeHandler(ChangeHandler handler) { |
| 80 | + return addDomHandler(handler, ChangeEvent.getType()); |
| 81 | + } |
| 82 | + |
| 83 | + private LeafValueEditor<Number> editor; |
| 84 | + |
| 85 | + /** |
| 86 | + * Constructor for <code>range</code>. |
| 87 | + */ |
| 88 | + public Range() { |
| 89 | + InputElement input=Document.get().createHiddenInputElement(); |
| 90 | + input.setAttribute("type", "range"); |
| 91 | + setElement(input); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Constructor for <code>Hidden</code>. |
| 96 | + * |
| 97 | + * @param name name of the hidden field |
| 98 | + */ |
| 99 | + public Range(String name) { |
| 100 | + this(); |
| 101 | + setName(name); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Constructor for <code>Hidden</code>. |
| 106 | + * |
| 107 | + * @param name name of the hidden field |
| 108 | + * @param value value of the hidden field |
| 109 | + */ |
| 110 | + public Range(String name, Number min,Number max,Number step) { |
| 111 | + this(name); |
| 112 | + //setValue(value); |
| 113 | + getInputElement().setAttribute("min", String.valueOf(min)); |
| 114 | + getInputElement().setAttribute("max", String.valueOf(max)); |
| 115 | + getInputElement().setAttribute("step", String.valueOf(step)); |
| 116 | + } |
| 117 | + |
| 118 | + public void setMin(double value){ |
| 119 | + getInputElement().setAttribute("min", String.valueOf(value)); |
| 120 | + } |
| 121 | + public void setMax(double value){ |
| 122 | + getInputElement().setAttribute("max", String.valueOf(value)); |
| 123 | + } |
| 124 | + public double getMax(){ |
| 125 | + if(getInputElement().getAttribute("max")==null){ |
| 126 | + return 0; |
| 127 | + } |
| 128 | + return Double.parseDouble(getInputElement().getAttribute("max")); |
| 129 | + } |
| 130 | + public double getMin(){ |
| 131 | + if(getInputElement().getAttribute("min")==null){ |
| 132 | + return 0; |
| 133 | + } |
| 134 | + return Double.parseDouble(getInputElement().getAttribute("min")); |
| 135 | + } |
| 136 | + |
| 137 | + public Range(String name, Number min,Number max){ |
| 138 | + this(name,min,max,1); |
| 139 | + } |
| 140 | + |
| 141 | + |
| 142 | + /** |
| 143 | + * This constructor may be used by subclasses to explicitly use an existing |
| 144 | + * element. This element must be an <input> element whose type is |
| 145 | + * 'hidden'. |
| 146 | + * |
| 147 | + * @param element the element to be used |
| 148 | + */ |
| 149 | + protected Range(Element element) { |
| 150 | + assert InputElement.as(element).getType().equalsIgnoreCase("range"); |
| 151 | + setElement(element); |
| 152 | + } |
| 153 | + |
| 154 | + public LeafValueEditor<Number> asEditor() { |
| 155 | + if (editor == null) { |
| 156 | + editor = TakesValueEditor.of(this); |
| 157 | + } |
| 158 | + return editor; |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Gets the default value of the range field. |
| 163 | + * |
| 164 | + * @return the default value |
| 165 | + */ |
| 166 | + public Number getDefaultValue() { |
| 167 | + return Double.valueOf(getInputElement().getDefaultValue()); |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * Gets the id of the range field. |
| 172 | + * |
| 173 | + * @return the id |
| 174 | + */ |
| 175 | + public String getID() { |
| 176 | + return getElement().getId(); |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * Gets the name of the range field. |
| 181 | + * |
| 182 | + * @return the name |
| 183 | + */ |
| 184 | + |
| 185 | + public String getName() { |
| 186 | + return getInputElement().getName(); |
| 187 | + } |
| 188 | + |
| 189 | + /** |
| 190 | + * Gets the value of the range field. |
| 191 | + * must start # & not empty |
| 192 | + * @return the value |
| 193 | + */ |
| 194 | + public Number getValue() { |
| 195 | + String value=getInputElement().getValue(); |
| 196 | + |
| 197 | + return Double.valueOf(value); |
| 198 | + } |
| 199 | + |
| 200 | + /** |
| 201 | + * get raw value |
| 202 | + * @return |
| 203 | + */ |
| 204 | + public Number getInputValue() { |
| 205 | + return Double.valueOf(getInputElement().getValue()); |
| 206 | + } |
| 207 | + |
| 208 | + /** |
| 209 | + * Sets the default value of the range field. |
| 210 | + * |
| 211 | + * @param defaultValue default value to set |
| 212 | + */ |
| 213 | + public void setDefaultValue(Integer defaultValue) { |
| 214 | + getInputElement().setDefaultValue(String.valueOf(defaultValue)); |
| 215 | + } |
| 216 | + |
| 217 | + /** |
| 218 | + * Sets the id of the range field. |
| 219 | + * |
| 220 | + * @param id id to set |
| 221 | + */ |
| 222 | + public void setID(String id) { |
| 223 | + getElement().setId(id); |
| 224 | + } |
| 225 | + |
| 226 | + /** |
| 227 | + * Sets the name of the range field. |
| 228 | + * |
| 229 | + * @param name name of the field |
| 230 | + */ |
| 231 | + public void setName(String name) { |
| 232 | + if (name == null) { |
| 233 | + throw new NullPointerException("Name cannot be null"); |
| 234 | + } else if (name.equals("")) { |
| 235 | + throw new IllegalArgumentException("Name cannot be an empty string."); |
| 236 | + } |
| 237 | + |
| 238 | + getInputElement().setName(name); |
| 239 | + } |
| 240 | + |
| 241 | + /** |
| 242 | + * Sets the value of the range field. |
| 243 | + * |
| 244 | + * @param value value to set |
| 245 | + */ |
| 246 | + public void setValue(Number value) { |
| 247 | + //getInputElement().setValue(String.valueOf(value)); |
| 248 | + setValue(value,false); |
| 249 | + } |
| 250 | + |
| 251 | + public final native void setValue(Element element,Number value) /*-{ |
| 252 | + element.value = value; |
| 253 | +}-*/; |
| 254 | + |
| 255 | + private InputElement getInputElement() { |
| 256 | + return getElement().cast(); |
| 257 | + } |
| 258 | + |
| 259 | + /* |
| 260 | + * i have no idea so far |
| 261 | +@Override |
| 262 | +public HandlerRegistration addValueChangeHandler(ValueChangeHandler<String> handler) { |
| 263 | + // TODO Auto-generated method stub |
| 264 | + return null; |
| 265 | +} |
| 266 | +*/ |
| 267 | + |
| 268 | +@Override |
| 269 | +public void setValue(Number value, boolean fireEvents) { |
| 270 | + if (value == null) { |
| 271 | + value = 0; |
| 272 | + } |
| 273 | + |
| 274 | + Number oldValue = getValue(); |
| 275 | + setValue(getInputElement(),value); |
| 276 | + |
| 277 | + if (value.equals(oldValue)) { |
| 278 | + return; |
| 279 | + } |
| 280 | + |
| 281 | + if (fireEvents) { |
| 282 | + ValueChangeEvent.fire(this, value); |
| 283 | + } |
| 284 | + |
| 285 | + //setValue(value); |
| 286 | +} |
| 287 | + |
| 288 | +private boolean valueChangeHandlerInitialized; |
| 289 | +@Override |
| 290 | +public HandlerRegistration addValueChangeHandler(ValueChangeHandler<Number> handler) { |
| 291 | + // Is this the first value change handler? If so, time to add handlers |
| 292 | + if (!valueChangeHandlerInitialized) { |
| 293 | + ensureDomEventHandlers(); |
| 294 | + valueChangeHandlerInitialized = true; |
| 295 | + } |
| 296 | + return addHandler(handler, ValueChangeEvent.getType()); |
| 297 | +} |
| 298 | + |
| 299 | +/* |
| 300 | + don't wrok |
| 301 | + public HandlerRegistration addValueChangeHandler( |
| 302 | + ValueChangeHandler<String> handler) { |
| 303 | + return addHandler(handler, ValueChangeEvent.getType()); |
| 304 | + } |
| 305 | + */ |
| 306 | + |
| 307 | + |
| 308 | +} |
| 309 | + |
0 commit comments