001/*
002 *  Copyright 2012 GWT-Bootstrap
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package com.github.gwtbootstrap.client.ui;
017
018import com.github.gwtbootstrap.client.ui.base.HasType;
019import com.github.gwtbootstrap.client.ui.base.StyleHelper;
020import com.github.gwtbootstrap.client.ui.constants.ButtonType;
021import com.github.gwtbootstrap.client.ui.constants.Constants;
022import com.github.gwtbootstrap.client.ui.resources.ButtonSize;
023import com.google.gwt.dom.client.Document;
024import com.google.gwt.dom.client.Element;
025import com.google.gwt.dom.client.InputElement;
026import com.google.gwt.event.dom.client.ChangeEvent;
027import com.google.gwt.event.dom.client.ChangeHandler;
028import com.google.gwt.event.dom.client.ClickHandler;
029import com.google.gwt.event.dom.client.HasChangeHandlers;
030import com.google.gwt.event.logical.shared.ValueChangeEvent;
031import com.google.gwt.event.logical.shared.ValueChangeHandler;
032import com.google.gwt.event.shared.HandlerRegistration;
033import com.google.gwt.safehtml.shared.SafeHtml;
034import com.google.gwt.user.client.ui.FocusWidget;
035import com.google.gwt.user.client.ui.HasName;
036import com.google.gwt.user.client.ui.HasText;
037import com.google.gwt.user.client.ui.HasValue;
038
039/**
040 * A SubmitButton for Bootstrap form.
041 * 
042 * @since 2.0.4.0
043 * @author ohashi keisuke
044 */
045public class SubmitButton extends FocusWidget implements HasType<ButtonType>, HasValue<String>, HasText, HasName, HasChangeHandlers {
046
047        private boolean valueChangeHandlerInitialized;
048
049        /**
050         * Creates an submit button with no caption.
051         */
052        public SubmitButton() {
053                super(Document.get().createSubmitInputElement());
054                setStyleName(Constants.BTN);
055        }
056
057        private InputElement asInputElement() {
058                return getElement().cast();
059        }
060
061        /**
062         * This constructor may be used by subclasses to explicitly use an existing
063         * element. This element must be a &lt;input type="submit"&gt; element.
064         * 
065         * @param element
066         *            the element to be used
067         */
068        protected SubmitButton(Element element) {
069                super(element);
070                assert "submit".equalsIgnoreCase(element.<InputElement> cast().getType());
071        }
072
073        /**
074         * Creates a button with the given HTML caption and click listener.
075         * 
076         * @param html
077         *            the HTML caption
078         * @param handler
079         *            the click handler
080         */
081        public SubmitButton(SafeHtml html,
082                ClickHandler handler) {
083                this(html.asString(), handler);
084        }
085
086        /**
087         * Creates a button with the given HTML caption.
088         * 
089         * @param html
090         *            the HTML caption
091         */
092        public SubmitButton(SafeHtml html) {
093                this(html.asString());
094        }
095
096        /**
097         * Creates a button with the given Text caption and click listener.
098         * 
099         * @param text
100         *            the Text caption
101         * @param handler
102         *            the click handler
103         */
104        public SubmitButton(String text,
105                ClickHandler handler) {
106                this(text);
107                addClickHandler(handler);
108        }
109
110        /**
111         * Creates a button with the given Text caption.
112         * 
113         * @param text
114         *            the Text caption
115         */
116        public SubmitButton(String text) {
117                this();
118                setValue(text);
119        }
120
121        /**
122         * Programmatic equivalent of the user clicking the button.
123         */
124        public void click() {
125                getElement().<InputElement> cast().click();
126        }
127
128        /**
129         * Sets the type of the Button.
130         * <p>
131         * Different types give the button a different look.
132         * 
133         * @param type
134         *            the type of the Button.
135         */
136        public void setType(ButtonType type) {
137                StyleHelper.changeStyle(this, type, ButtonType.class);
138        }
139
140        /**
141         * Sets the size of the Button.
142         * 
143         * @param size
144         *            the size of the Button.
145         */
146        public void setSize(ButtonSize size) {
147                StyleHelper.changeStyle(this, size, ButtonSize.class);
148        }
149
150        /**
151         * {@inheritDoc}
152         */
153        public HandlerRegistration addChangeHandler(ChangeHandler handler) {
154                return addDomHandler(handler, ChangeEvent.getType());
155        }
156
157        /**
158         * @see com.google.gwt.event.logical.shared.HasValueChangeHandlers#addValueChangeHandler(com.google.gwt.event.logical.shared.ValueChangeHandler)
159         */
160        public HandlerRegistration addValueChangeHandler(ValueChangeHandler<String> handler) {
161                if (!valueChangeHandlerInitialized) {
162                        valueChangeHandlerInitialized = true;
163                        addChangeHandler(new ChangeHandler() {
164
165                                public void onChange(ChangeEvent event) {
166                                        ValueChangeEvent.fire(SubmitButton.this, getValue());
167                                }
168                        });
169                }
170                return addHandler(handler, ValueChangeEvent.getType());
171        }
172
173        /**
174         * {@inheritDoc}
175         */
176        @Override
177        public void setName(String name) {
178                asInputElement().setName(name);
179        }
180
181        /**
182         * {@inheritDoc}
183         */
184        @Override
185        public String getName() {
186                return asInputElement().getName();
187        }
188
189        /**
190         * {@inheritDoc}
191         */
192        @Override
193        public String getText() {
194                return asInputElement().getValue();
195        }
196
197        /**
198         * {@inheritDoc}
199         */
200        @Override
201        public void setText(String text) {
202                setValue(text);
203        }
204
205        /**
206         * {@inheritDoc}
207         */
208        @Override
209        public String getValue() {
210                return asInputElement().getValue();
211        }
212
213        /**
214         * {@inheritDoc}
215         */
216        @Override
217        public void setValue(String value) {
218                this.setValue(value , false);
219        }
220
221        /**
222         * {@inheritDoc}
223         */
224        @Override
225        public void setValue(String value, boolean fireEvents) {
226                String oldValue = getValue();
227                asInputElement().setValue(value);
228                if (fireEvents) {
229                        ValueChangeEvent.fireIfNotEqual(this, oldValue, value);
230                }
231        }
232        
233    /**
234     * Set element as a Block Level Button
235     * @param block true:Block Level false:Default
236     */
237    public void setBlock(boolean block) {
238        setStyleName(Constants.BTN_BLOCK, block);
239    }
240}