package org.gwtbootstrap3.client.ui;

/*
 * #%L
 * GwtBootstrap3
 * %%
 * Copyright (C) 2013 GwtBootstrap3
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.InputElement;
import com.google.gwt.event.dom.client.*;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.uibinder.client.UiConstructor;
import com.google.gwt.user.client.ui.HasEnabled;
import com.google.gwt.user.client.ui.HasName;
import org.gwtbootstrap3.client.ui.base.ComplexWidget;
import org.gwtbootstrap3.client.ui.constants.ElementTags;
import org.gwtbootstrap3.client.ui.constants.InputType;
import org.gwtbootstrap3.client.ui.constants.Styles;

/**
 * @author Joshua Godi
 */
public class Input extends ComplexWidget implements HasEnabled, HasInputType, HasPlaceholder, HasFormValue, HasName, HasAllKeyHandlers {
    private static final String MIN = "min";
    private static final String MAX = "max";
    private static final String DISABLED = "disabled";

    public Input() {
        setElement(Document.get().createElement(ElementTags.INPUT));
        addStyleName(Styles.FORM_CONTROL);
    }

    @UiConstructor
    public Input(final InputType type) {
        this();
        setType(type);
    }

    public void setMin(final String min) {
        getElement().setAttribute(MIN, min);
    }

    public void setMax(final String max) {
        getElement().setAttribute(MAX, max);
    }

    @Override
    public void setEnabled(final boolean enabled) {
        getElement().setPropertyBoolean(DISABLED, !enabled);
    }

    @Override
    public boolean isEnabled() {
        return !getElement().getPropertyBoolean(DISABLED);
    }

    @Override
    public void setType(final InputType inputType) {
        getElement().setAttribute(TYPE, inputType.getType());
    }

    @Override
    public InputType getType() {
        if (getElement().getAttribute(TYPE) == null || getElement().getAttribute(TYPE).isEmpty()) {
            return null;
        }
        return InputType.valueOf(getElement().getAttribute(TYPE));
    }

    @Override
    public void setPlaceholder(final String placeHolder) {
        getElement().setAttribute(PLACEHOLDER, placeHolder != null ? placeHolder : "");
    }

    @Override
    public String getPlaceholder() {
        return getElement().getAttribute(PLACEHOLDER);
    }

    @Override
    public String getFormValue() {
        return InputElement.as(getElement()).getValue();
    }

    @Override
    public void setFormValue(final String value) {
        InputElement.as(getElement()).setValue(value);
    }

    @Override
    public String getName() {
        return InputElement.as(getElement()).getName();
    }

    @Override
    public void setName(final String name) {
        InputElement.as(getElement()).setName(name);
    }

    @Override
    public HandlerRegistration addKeyDownHandler(KeyDownHandler handler) {
        return addDomHandler(handler, KeyDownEvent.getType());
    }

    @Override
    public HandlerRegistration addKeyPressHandler(KeyPressHandler handler) {
        return addDomHandler(handler, KeyPressEvent.getType());
    }

    @Override
    public HandlerRegistration addKeyUpHandler(KeyUpHandler handler) {
        return addDomHandler(handler, KeyUpEvent.getType());
    }
}

@@ -745,73 +745,45 @@
 gwt.
-dom.client.InputElement;%0Aimport com.google.gwt.event.dom.client.*
+text.shared.testing.PassthroughParser
 ;%0Aim
@@ -806,12 +806,11 @@
 gwt.
-even
+tex
 t.sh
@@ -814,35 +814,43 @@
 .shared.
-Ha
+testing.PassthroughRe
 nd
-l
 er
-Registration
+er
 ;%0Aimport
@@ -900,102 +900,8 @@
 or;%0A
-import com.google.gwt.user.client.ui.HasEnabled;%0Aimport com.google.gwt.user.client.ui.HasName;
 %0Aimp
@@ -937,29 +937,28 @@
 ui.base.
-ComplexWidget
+ValueBoxBase
 ;%0Aimport
@@ -1181,118 +1181,56 @@
 nds 
-ComplexWidget implements HasEnabled, HasInputType, HasPlaceholder, HasFormValue, HasName, HasAllKeyHandlers
+ValueBoxBase%3CString%3E implements HasInputType
  %7B%0A
+%0A
     
@@ -1318,63 +1318,8 @@
 ax%22;
-%0A    private static final String DISABLED = %22disabled%22;
 %0A%0A  
@@ -1345,22 +1345,14 @@
     
-    setElement
+%09super
 (Doc
@@ -1395,16 +1395,78 @@
 s.INPUT)
+, PassthroughRenderer.instance(), PassthroughParser.instance()
 );%0A     
@@ -1810,255 +1810,8 @@
  %7D%0A%0A
-    @Override%0A    public void setEnabled(final boolean enabled) %7B%0A        getElement().setPropertyBoolean(DISABLED, !enabled);%0A    %7D%0A%0A    @Override%0A    public boolean isEnabled() %7B%0A        return !getElement().getPropertyBoolean(DISABLED);%0A    %7D%0A%0A
     
@@ -2483,945 +2483,6 @@
  %7D%0A%0A
-    @Override%0A    public String getFormValue() %7B%0A        return InputElement.as(getElement()).getValue();%0A    %7D%0A%0A    @Override%0A    public void setFormValue(final String value) %7B%0A        InputElement.as(getElement()).setValue(value);%0A    %7D%0A%0A    @Override%0A    public String getName() %7B%0A        return InputElement.as(getElement()).getName();%0A    %7D%0A%0A    @Override%0A    public void setName(final String name) %7B%0A        InputElement.as(getElement()).setName(name);%0A    %7D%0A%0A    @Override%0A    public HandlerRegistration addKeyDownHandler(KeyDownHandler handler) %7B%0A        return addDomHandler(handler, KeyDownEvent.getType());%0A    %7D%0A%0A    @Override%0A    public HandlerRegistration addKeyPressHandler(KeyPressHandler handler) %7B%0A        return addDomHandler(handler, KeyPressEvent.getType());%0A    %7D%0A%0A    @Override%0A    public HandlerRegistration addKeyUpHandler(KeyUpHandler handler) %7B%0A        return addDomHandler(handler, KeyUpEvent.getType());%0A    %7D%0A
 %7D%0A

Make Input extend ValueBoxBase and remove redundant code