001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.acacia.client.widgets.serialdate;
029
030import org.opencms.ade.contenteditor.client.Messages;
031import org.opencms.gwt.client.ui.CmsPushButton;
032import org.opencms.gwt.client.ui.input.datebox.CmsDateBox;
033
034import java.util.Date;
035import java.util.SortedSet;
036
037import com.google.gwt.core.shared.GWT;
038import com.google.gwt.event.dom.client.ClickEvent;
039import com.google.gwt.event.logical.shared.ValueChangeEvent;
040import com.google.gwt.uibinder.client.UiBinder;
041import com.google.gwt.uibinder.client.UiField;
042import com.google.gwt.uibinder.client.UiHandler;
043import com.google.gwt.user.client.ui.Composite;
044import com.google.gwt.user.client.ui.HTMLPanel;
045
046/**
047 * The daily pattern panel.<p>
048 * */
049public class CmsPatternPanelIndividualView extends Composite implements I_CmsSerialDatePatternView {
050
051    /** The UI binder interface. */
052    interface I_CmsPatternPanelIndividualUiBinder extends UiBinder<HTMLPanel, CmsPatternPanelIndividualView> {
053        // nothing to do
054    }
055
056    /** The UI binder instance. */
057    private static I_CmsPatternPanelIndividualUiBinder uiBinder = GWT.create(I_CmsPatternPanelIndividualUiBinder.class);
058
059    /** Format with date only. */
060    private String m_dateFormat = Messages.get().keyDefault(Messages.GUI_SERIALDATE_DATE_FORMAT_0, null);
061
062    /** Input field for new dates. */
063    @UiField
064    CmsDateBox m_newDate;
065
066    /** Button to add new dates. */
067    @UiField
068    CmsPushButton m_addButton;
069
070    /** Button to remove selected dates. */
071    @UiField
072    CmsPushButton m_removeSelectedButton;
073
074    /** The ui element for the list with the individual dates */
075    @UiField(provided = true)
076    CmsCheckableDatePanel m_dateList;
077
078    /** The model to read the data from. */
079    private final I_CmsObservableSerialDateValue m_model;
080
081    /** The controller to handle changes. */
082    final CmsPatternPanelIndividualController m_controller;
083
084    /** Flag, indicating if change actions should not be triggered. */
085    private boolean m_triggerChangeActions = true;
086
087    /**
088     * Default constructor to create the panel.<p>
089     * @param controller the controller that handles value changes.
090     * @param model the model that provides the values.
091     */
092    public CmsPatternPanelIndividualView(
093        CmsPatternPanelIndividualController controller,
094        I_CmsObservableSerialDateValue model) {
095        m_controller = controller;
096        m_model = model;
097        m_model.registerValueChangeObserver(this);
098
099        m_dateList = new CmsCheckableDatePanel(m_dateFormat);
100        m_dateList.setWidth("100%");
101        initWidget(uiBinder.createAndBindUi(this));
102        m_addButton.setText(Messages.get().key(Messages.GUI_SERIALDATE_BUTTON_ADD_INDIVIDUAL_0));
103        m_removeSelectedButton.setText(Messages.get().key(Messages.GUI_SERIALDATE_BUTTON_REMOVE_INDIVIDUAL_0));
104
105    }
106
107    /**
108     * @see org.opencms.acacia.client.widgets.serialdate.I_CmsSerialDateValueChangeObserver#onValueChange()
109     */
110    public void onValueChange() {
111
112        m_dateList.setDates(m_model.getIndividualDates());
113
114    }
115
116    /**
117     * Handle click on "Add" button.
118     * @param e the click event.
119     */
120    @UiHandler("m_addButton")
121    void addButtonClick(ClickEvent e) {
122
123        if (null != m_newDate.getValue()) {
124            m_dateList.addDate(m_newDate.getValue());
125            m_newDate.setValue(null);
126            if (handleChange()) {
127                m_controller.setDates(m_dateList.getDates());
128            }
129        }
130    }
131
132    /**
133     * Handle value change event on the individual dates list.
134     * @param event the change event.
135     */
136    @UiHandler("m_dateList")
137    void dateListValueChange(ValueChangeEvent<SortedSet<Date>> event) {
138
139        if (handleChange()) {
140            m_controller.setDates(event.getValue());
141        }
142    }
143
144    /**
145     * Returns a flag, indicating if change actions should be triggered.
146     * @return a flag, indicating if change actions should be triggered.
147     */
148    boolean handleChange() {
149
150        return m_triggerChangeActions;
151    }
152
153    /**
154     * Handle click on "Remove selected" button.
155     * @param e the click event.
156     */
157    @UiHandler("m_removeSelectedButton")
158    void removeSelectedButtonClick(ClickEvent e) {
159
160        m_dateList.setDates(m_dateList.getUncheckedDates());
161    }
162
163}