001package gwt.material.design.client.ui;
002
003/*
004 * #%L
005 * GwtMaterial
006 * %%
007 * Copyright (C) 2015 GwtMaterialDesign
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 * 
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 * 
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import com.google.gwt.dom.client.Document;
024import com.google.gwt.text.shared.Parser;
025import com.google.gwt.text.shared.Renderer;
026import com.google.gwt.text.shared.testing.PassthroughParser;
027import com.google.gwt.text.shared.testing.PassthroughRenderer;
028import com.google.gwt.uibinder.client.UiConstructor;
029import gwt.material.design.client.constants.InputType;
030import gwt.material.design.client.base.HasInputType;
031import gwt.material.design.client.base.ValueBoxBase;
032
033public class  MaterialInput extends ValueBoxBase<String> implements HasInputType {
034
035    private static final String MIN = "min";
036    private static final String MAX = "max";
037
038    public MaterialInput() {
039        this(PassthroughRenderer.instance(), PassthroughParser.instance());
040    }
041
042    public MaterialInput(Renderer<String> renderer, Parser<String> parser) {
043        super(Document.get().createElement("input"), renderer, parser);
044    }
045
046    @UiConstructor
047    public MaterialInput(final InputType type) {
048        this();
049        setType(type);
050    }
051
052    public void setMin(final String min) {
053        getElement().setAttribute(MIN, min);
054    }
055
056    public void setMax(final String max) {
057        getElement().setAttribute(MAX, max);
058    }
059
060    @Override
061    public void setType(final InputType inputType) {
062        getElement().setAttribute(TYPE, inputType.getType());
063    }
064
065    @Override
066    public InputType getType() {
067        if (getElement().getAttribute(TYPE) == null || getElement().getAttribute(TYPE).isEmpty()) { return null; }
068        return InputType.valueOf(getElement().getAttribute(TYPE));
069    }
070
071    public void setRequired(boolean required) {
072        getElement().removeAttribute("required");
073        if(required) {
074            getElement().setAttribute("required", "");
075        }
076    }
077
078    public boolean isRequired() {
079        return getElement().hasAttribute("required");
080    }
081}