001/* 002 * #%L 003 * GwtMaterial 004 * %% 005 * Copyright (C) 2015 - 2017 GwtMaterialDesign 006 * %% 007 * Licensed under the Apache License, Version 2.0 (the "License"); 008 * you may not use this file except in compliance with the License. 009 * You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 * #L% 019 */ 020package gwt.material.design.client.ui; 021 022import gwt.material.design.client.base.NumberBox; 023import gwt.material.design.client.base.NumberBox.NumberHandler; 024import gwt.material.design.client.constants.InputType; 025 026import static gwt.material.design.jquery.client.api.JQuery.$; 027 028//@formatter:off 029 030/** 031 * Material Number Box is the base class for other numeric input boxes, such as {@link MaterialIntegerBox} and 032 * {@link MaterialDoubleBox}. 033 * 034 * @author paulux84 035 * @see <a href="http://gwtmaterialdesign.github.io/gwt-material-demo/#textfields">Material MaterialNumberBox</a> 036 * @see <a href="https://material.io/guidelines/components/text-fields.html#">Material Design Specification</a> 037 */ 038//@formatter:on 039public abstract class MaterialNumberBox<T> extends MaterialValueBox<T> { 040 041 protected MaterialNumberBox() { 042 setup(new NumberBox<>(new NumberHandler<>(this))); 043 setType(InputType.NUMBER); 044 } 045 046 /** 047 * Set step attribute to input element. 048 * 049 * @param step "any" or number like for example 1 or 2.5 or 100, etc... 050 */ 051 public void setStep(String step) { 052 valueBoxBase.getElement().setAttribute("step", step); 053 } 054 055 public String getStep() { 056 return valueBoxBase.getElement().getAttribute("step"); 057 } 058 059 public void setMin(String min) { 060 valueBoxBase.getElement().setAttribute("min", min); 061 } 062 063 public String getMin() { 064 return valueBoxBase.getElement().getAttribute("min"); 065 } 066 067 public void setMax(String max) { 068 valueBoxBase.getElement().setAttribute("max", max); 069 } 070 071 public String getMax() { 072 return valueBoxBase.getElement().getAttribute("max"); 073 } 074 075 @Override 076 public T getValue() { 077 if (getValueAsNumber() != null) { 078 return parseNumber(getValueAsNumber()); 079 } 080 return null; 081 } 082 083 protected abstract T parseNumber(double number); 084 085 /** 086 * Returns the value parsed natively by the browser. 087 * 088 * @return the value set on the component, or NaN if none is set 089 */ 090 public Double getValueAsNumber() { 091 String value = (String) $(valueBoxBase.getElement()).val(); 092 if (value != null && !value.isEmpty()) { 093 return Double.parseDouble(value); 094 } else { 095 return null; 096 } 097 } 098}