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
022//@formatter:off
023
024/**
025 * Material Integer Box is an input field that accepts any Integer based string
026 * from user. <h3>UiBinder Usage:</h3>
027 * <p>
028 * <pre> {@code <m:MaterialIntegerBox ui:field="txtIntegerBox" label="Integer value"/>
029 *
030 * // Setting value in Java
031 * txtIntegerBox.setValue(10);
032 * }
033 * </pre>
034 * <p>
035 * The parsing and formatting of the number are done natively by the browser,
036 * using the i18n settings from the user.
037 *
038 * @author paulux84
039 * @see <a href="http://gwtmaterialdesign.github.io/gwt-material-demo/#textfields">Material
040 * IntegerBox</a>
041 * @see <a href="https://material.io/guidelines/components/text-fields.html#">Material Design Specification</a>
042 */
043// @formatter:on
044public class MaterialIntegerBox extends MaterialNumberBox<Integer> {
045
046    public MaterialIntegerBox() {
047        setStep("1");
048    }
049
050    public MaterialIntegerBox(String placeholder) {
051        this();
052        setPlaceholder(placeholder);
053    }
054
055    public MaterialIntegerBox(String placeholder, int value) {
056        this(placeholder);
057        setValue(value);
058    }
059
060    @Override
061    protected Integer parseNumber(double number) {
062        if (Double.isNaN(number)) {
063            return null;
064        }
065        return (int) Math.rint(number);
066    }
067}