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.datepicker.client.ui;
017
018import com.github.gwtbootstrap.client.ui.AppendButton;
019import com.github.gwtbootstrap.client.ui.base.AddOn;
020import com.github.gwtbootstrap.client.ui.base.HasAlternateSize;
021import com.github.gwtbootstrap.client.ui.base.HasIcon;
022import com.github.gwtbootstrap.client.ui.base.HasVisibility;
023import com.github.gwtbootstrap.client.ui.constants.AlternateSize;
024import com.github.gwtbootstrap.client.ui.constants.BaseIconType;
025import com.github.gwtbootstrap.client.ui.constants.IconPosition;
026import com.github.gwtbootstrap.client.ui.constants.IconSize;
027import com.github.gwtbootstrap.client.ui.constants.IconType;
028import com.github.gwtbootstrap.client.ui.event.HasVisibleHandlers;
029import com.github.gwtbootstrap.client.ui.event.HiddenHandler;
030import com.github.gwtbootstrap.client.ui.event.HideHandler;
031import com.github.gwtbootstrap.client.ui.event.ShowHandler;
032import com.github.gwtbootstrap.client.ui.event.ShownHandler;
033import com.github.gwtbootstrap.datepicker.client.ui.base.DateBoxBase;
034import com.github.gwtbootstrap.datepicker.client.ui.base.HasAllDatePickerHandlers;
035import com.github.gwtbootstrap.datepicker.client.ui.base.HasDateFormat;
036import com.google.gwt.editor.client.IsEditor;
037import com.google.gwt.editor.client.adapters.TakesValueEditor;
038import com.google.gwt.event.dom.client.ChangeHandler;
039import com.google.gwt.event.dom.client.HasChangeHandlers;
040import com.google.gwt.event.logical.shared.HasValueChangeHandlers;
041import com.google.gwt.event.logical.shared.ValueChangeHandler;
042import com.google.gwt.event.shared.HandlerRegistration;
043import com.google.gwt.user.client.ui.HasValue;
044import com.google.gwt.user.client.ui.ValueBoxBase.TextAlignment;
045
046import java.util.Date;
047
048/**
049 * DateBoxAppended component.
050 *
051 * @since 2.0.4.0
052 * @author Carlos Alexandro Becker
053 */
054public class DateBoxAppended extends AppendButton implements HasValue<Date>,
055        HasDateFormat, HasIcon, HasValueChangeHandlers<Date>, HasVisibility,
056        HasChangeHandlers, HasVisibleHandlers, HasAllDatePickerHandlers,
057        HasAlternateSize, IsEditor<TakesValueEditor<Date>> {
058
059    /**
060     * An 'adapter' to change some aspects of the behavior of datepickerbase.
061     */
062    private class DateBoxAppendedAdapter extends DateBoxBase {
063        @Override
064        protected void configure() {
065            DateBoxAppended.this.addStyleName("date");
066            super.configure(this);
067        }
068    }
069
070    private final DateBoxAppendedAdapter box;
071    private AddOn icon;
072    {
073        this.box = new DateBoxAppendedAdapter();
074        icon = new AddOn();
075        icon.setIcon(IconType.TH);
076        add(box);
077        add(icon);
078    }
079
080
081    /**
082     * {@inheritDoc}
083     */
084    @Override
085    public Date getValue() {
086        return box.getValue();
087    }
088
089    /**
090     * {@inheritDoc}
091     */
092    @Override
093    public void setValue(Date value) {
094        box.setValue(value);
095    }
096
097    /**
098     * {@inheritDoc}
099     */
100    @Override
101    public void setValue(Date value, boolean fireEvents) {
102        box.setValue(value, fireEvents);
103    }
104
105     /**
106     * {@inheritDoc}
107     */
108    @Override
109    public void setAlternateSize(AlternateSize size) {
110        box.setAlternateSize(size);
111    }
112
113    public void setAlignment(TextAlignment align) {
114        box.setAlignment(align);
115    }
116
117    /**
118     * {@inheritDoc}
119     */
120    @Override
121    public HandlerRegistration addValueChangeHandler(
122            ValueChangeHandler<Date> dateValueChangeHandler) {
123        return box.addValueChangeHandler(dateValueChangeHandler);
124    }
125
126    /**
127     * {@inheritDoc}
128     */
129    @Override
130    public void setFormat(String format) {
131        box.setFormat(format);
132    }
133
134    /**
135     * {@inheritDoc}
136     */
137    @Override
138    public void setIcon(IconType type) {
139        setBaseIcon(type);
140    }
141
142    /**
143     * {@inheritDoc}
144     */
145    @Override
146    public void setBaseIcon(BaseIconType type) {
147        icon.setBaseIcon(type);
148    }
149
150    /**
151     * {@inheritDoc}
152     */
153    @Override
154    public void setIconSize(IconSize size) {
155        icon.setIconSize(size);
156    }
157
158    /**
159     * {@inheritDoc}
160     */
161    @Override
162    public void setCustomIconStyle(String customIconStyle) {
163        icon.setCustomIconStyle(customIconStyle);
164    }
165
166    /**
167     * {@inheritDoc}
168     *
169     * @deprecated
170     */
171    @Override
172    public void setIconPosition(IconPosition position) {
173        icon.setIconPosition(position);
174    }
175
176    /**
177     * {@inheritDoc}
178     */
179    @Override
180    public void setAutoClose(boolean autoclose) {
181        box.setAutoClose(autoclose);
182    }
183
184    /**
185     * {@inheritDoc}
186     */
187    @Override
188    public void setEndDate(String date) {
189        box.setEndDate(date);
190    }
191
192    /**
193     * {@inheritDoc}
194     */
195    @Override
196    public void setEndDate_(Date date) {
197        box.setEndDate_(date);
198    }
199
200    /**
201     * {@inheritDoc}
202     */
203    @Override
204    public void setLanguage(String language) {
205        box.setLanguage(language);
206    }
207
208    /**
209     * {@inheritDoc}
210     */
211    @Override
212    public void setStartDate(String date) {
213        box.setStartDate(date);
214    }
215
216    /**
217     * {@inheritDoc}
218     */
219    @Override
220    public void setStartDate_(Date date) {
221        box.setStartDate_(date);
222    }
223
224    /**
225     * {@inheritDoc}
226     */
227    @Override
228    public void setStartView(ViewMode mode) {
229        box.setStartView(mode);
230    }
231
232    /**
233     * {@inheritDoc}
234     */
235    @Override
236    public void setStartView(String mode) {
237        box.setStartView(mode);
238    }
239
240    /**
241     * {@inheritDoc}
242     */
243    @Override
244    public void setWeekStart(int start) {
245        box.setWeekStart(start);
246    }
247
248    /**
249     * {@inheritDoc}
250     */
251    @Override
252    public TakesValueEditor<Date> asEditor() {
253        return box.asEditor();
254    }
255
256    /**
257     * {@inheritDoc}
258     */
259    @Override
260    public HandlerRegistration addHideHandler(HideHandler handler) {
261        return box.addHideHandler(handler);
262    }
263
264    /**
265     * {@inheritDoc}
266     */
267    @Override
268    public HandlerRegistration addHiddenHandler(HiddenHandler handler) {
269        return box.addHiddenHandler(handler);
270    }
271
272    /**
273     * {@inheritDoc}
274     */
275    @Override
276    public HandlerRegistration addShowHandler(ShowHandler handler) {
277        return box.addShowHandler(handler);
278    }
279
280    /**
281     * {@inheritDoc}
282     */
283    @Override
284    public HandlerRegistration addShownHandler(ShownHandler handler) {
285        return box.addShownHandler(handler);
286    }
287
288    /**
289     * {@inheritDoc}
290     */
291    @Override
292    public HandlerRegistration addChangeHandler(ChangeHandler handler) {
293        return box.addChangeHandler(handler);
294    }
295
296    /**
297     * {@inheritDoc}
298     */
299    @Override
300    public void show() {
301        box.show();
302    }
303
304    /**
305     * {@inheritDoc}
306     */
307    @Override
308    public void hide() {
309        box.hide();
310    }
311
312    /**
313     * {@inheritDoc}
314     */
315    @Override
316    public void toggle() {
317        box.toggle();
318    }
319
320    /**
321    * @see com.google.gwt.user.client.ui.ValueBoxBase#isReadOnly()
322    */
323    public boolean isReadOnly() {
324        return box.isReadOnly();
325    }
326
327    /**
328     * @see com.google.gwt.user.client.ui.ValueBoxBase#setReadOnly(boolean) 
329     */
330    public void setReadOnly(boolean readonly) {
331        box.setReadOnly(readonly);
332    }
333}