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 com.google.gwt.uibinder.client.UiConstructor;
023import com.google.gwt.user.client.DOM;
024import com.google.gwt.user.client.ui.HasText;
025import gwt.material.design.client.base.AbstractValueWidget;
026import gwt.material.design.client.base.HasInputType;
027import gwt.material.design.client.base.MaterialWidget;
028import gwt.material.design.client.base.mixin.TextMixin;
029import gwt.material.design.client.constants.InputType;
030
031public class MaterialInput extends AbstractValueWidget<String> implements HasInputType, HasText {
032
033    private static final String MIN = "min";
034    private static final String MAX = "max";
035
036    public MaterialInput() {
037        super(DOM.createElement("input"));
038    }
039
040    @UiConstructor
041    public MaterialInput(final InputType type) {
042        this();
043        setType(type);
044    }
045
046    public void setMin(final String min) {
047        getElement().setAttribute(MIN, min);
048    }
049
050    public void setMax(final String max) {
051        getElement().setAttribute(MAX, max);
052    }
053
054    @Override
055    public void setType(final InputType inputType) {
056        getElement().setAttribute(TYPE, inputType.getType());
057    }
058
059    @Override
060    public InputType getType() {
061        if (getElement().getAttribute(TYPE) == null || getElement().getAttribute(TYPE).isEmpty()) {
062            return null;
063        }
064        return InputType.valueOf(getElement().getAttribute(TYPE));
065    }
066
067    public void setRequired(boolean required) {
068        getElement().removeAttribute("required");
069        if (required) {
070            getElement().setAttribute("required", "");
071        }
072    }
073
074    public boolean isRequired() {
075        return getElement().hasAttribute("required");
076    }
077
078    @Override
079    public void setValue(String value, boolean fireEvents) {
080        getElement().setPropertyString("value", value != null ? value : "");
081        super.setValue(value, fireEvents);
082    }
083
084    @Override
085    public String getValue() {
086        return getElement().getPropertyString("value");
087    }
088
089    @Override
090    public String getText() {
091        return getValue();
092    }
093
094    @Override
095    public void setText(String text) {
096        setValue(text, true);
097    }
098}