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.constants.IconType;
021import com.github.gwtbootstrap.datepicker.client.ui.base.DateBoxBase;
022import com.github.gwtbootstrap.datepicker.client.ui.base.HasDateFormat;
023import com.google.gwt.event.logical.shared.ValueChangeHandler;
024import com.google.gwt.event.shared.HandlerRegistration;
025import com.google.gwt.user.client.ui.HasValue;
026
027import java.util.Date;
028
029/**
030 * DateBoxAppended component.
031 *
032 * @since 2.0.4.0
033 * @author Carlos Alexandro Becker
034 */
035public class DateBoxAppended extends AppendButton implements HasValue<Date>, HasDateFormat {
036    private final DateBoxAppendedAdapter box;
037
038    {
039        this.box = new DateBoxAppendedAdapter();
040        AddOn icon = new AddOn();
041        icon.setIcon(IconType.TH);
042        add(box);
043        add(icon);
044    }
045
046    /**
047     * {@inheritDoc}
048     */
049    @Override
050    public Date getValue() {
051        return box.getValue();
052    }
053
054    /**
055     * {@inheritDoc}
056     */
057    @Override
058    public void setValue(Date value) {
059        box.setValue(value);
060    }
061
062    /**
063     * {@inheritDoc}
064     */
065    @Override
066    public void setValue(Date value, boolean fireEvents) {
067        box.setValue(value, fireEvents);
068    }
069
070    /**
071     * {@inheritDoc}
072     */
073    @Override
074    public HandlerRegistration addValueChangeHandler(ValueChangeHandler<Date> dateValueChangeHandler) {
075        return box.addValueChangeHandler(dateValueChangeHandler);
076    }
077
078    /**
079     * {@inheritDoc}
080     */
081    @Override
082    public void setFormat(String format) {
083        box.setFormat(format);
084    }
085
086    /**
087     * An 'adapter' to change some aspects of the behavior of datepickerbase. //TODO may need refactor.
088     */
089    private class DateBoxAppendedAdapter extends DateBoxBase {
090        @Override
091        protected void configure() {
092            DateBoxAppended that = DateBoxAppended.this;
093            super.configure(that);
094            getBox().getElement().setAttribute("readonly", null);
095            that.addStyleName("date");
096        }
097    }
098}