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