001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH (https://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: https://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: https://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
096        m_controller = controller;
097        m_model = model;
098        m_model.registerValueChangeObserver(this);
099
100        m_dateList = new CmsCheckableDatePanel(m_dateFormat);
101        m_dateList.setWidth("100%");
102        initWidget(uiBinder.createAndBindUi(this));
103        m_addButton.setText(Messages.get().key(Messages.GUI_SERIALDATE_BUTTON_ADD_INDIVIDUAL_0));
104        m_removeSelectedButton.setText(Messages.get().key(Messages.GUI_SERIALDATE_BUTTON_REMOVE_INDIVIDUAL_0));
105
106    }
107
108    /**
109     * @see org.opencms.acacia.client.widgets.serialdate.I_CmsSerialDateValueChangeObserver#onValueChange()
110     */
111    public void onValueChange() {
112
113        m_dateList.setDates(m_model.getIndividualDates());
114
115    }
116
117    /**
118     * Handle click on "Add" button.
119     * @param e the click event.
120     */
121    @UiHandler("m_addButton")
122    void addButtonClick(ClickEvent e) {
123
124        if (null != m_newDate.getValue()) {
125            m_dateList.addDate(m_newDate.getValue());
126            m_newDate.setValue(null);
127            if (handleChange()) {
128                m_controller.setDates(m_dateList.getDates());
129            }
130        }
131    }
132
133    /**
134     * Handle value change event on the individual dates list.
135     * @param event the change event.
136     */
137    @UiHandler("m_dateList")
138    void dateListValueChange(ValueChangeEvent<SortedSet<Date>> event) {
139
140        if (handleChange()) {
141            m_controller.setDates(event.getValue());
142        }
143    }
144
145    /**
146     * Returns a flag, indicating if change actions should be triggered.
147     * @return a flag, indicating if change actions should be triggered.
148     */
149    boolean handleChange() {
150
151        return m_triggerChangeActions;
152    }
153
154    /**
155     * Handle click on "Remove selected" button.
156     * @param e the click event.
157     */
158    @UiHandler("m_removeSelectedButton")
159    void removeSelectedButtonClick(ClickEvent e) {
160
161        m_dateList.setDates(m_dateList.getUncheckedDates());
162    }
163
164}