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.acacia.shared.I_CmsSerialDateValue.WeekDay;
031import org.opencms.ade.contenteditor.client.Messages;
032import org.opencms.gwt.client.ui.input.CmsCheckBox;
033
034import java.util.ArrayList;
035import java.util.List;
036import java.util.SortedSet;
037import java.util.TreeSet;
038
039import com.google.gwt.core.shared.GWT;
040import com.google.gwt.dom.client.Element;
041import com.google.gwt.event.logical.shared.ValueChangeEvent;
042import com.google.gwt.event.logical.shared.ValueChangeHandler;
043import com.google.gwt.uibinder.client.UiBinder;
044import com.google.gwt.uibinder.client.UiField;
045import com.google.gwt.uibinder.client.UiHandler;
046import com.google.gwt.user.client.ui.Composite;
047import com.google.gwt.user.client.ui.FlowPanel;
048import com.google.gwt.user.client.ui.HTMLPanel;
049
050/**
051 * The weekly pattern panel.<p>
052 * */
053public class CmsPatternPanelWeeklyView extends Composite implements I_CmsSerialDatePatternView {
054
055    /** The UI binder interface. */
056    interface I_CmsPatternPanelWeekly extends UiBinder<HTMLPanel, CmsPatternPanelWeeklyView> {
057        // nothing to do
058    }
059
060    /** The UI binder instance. */
061    private static I_CmsPatternPanelWeekly uiBinder = GWT.create(I_CmsPatternPanelWeekly.class);
062
063    /** The array of all checkboxes. */
064    List<CmsCheckBox> m_checkboxes = new ArrayList<CmsCheckBox>();
065
066    /** The panel for all values of the day selection. */
067    @UiField
068    FlowPanel m_dayPanel = new FlowPanel();
069
070    /** The text box for the date input. */
071    @UiField
072    CmsFocusAwareTextBox m_everyDay;
073
074    /** The every label. */
075    @UiField
076    Element m_labelEvery;
077
078    /** The weeks label. */
079    @UiField
080    Element m_labelWeeks;
081
082    /** The handler for check box value changes. */
083    private ValueChangeHandler<Boolean> m_checkBoxValueChangeHandler;
084
085    /** The model to read the data from. */
086    private final I_CmsObservableSerialDateValue m_model;
087
088    /** The controller to handle changes. */
089    final CmsPatternPanelWeeklyController m_controller;
090
091    /** Flag, indicating if change actions should not be triggered. */
092    private boolean m_triggerChangeActions = true;
093
094    /**
095     * Default constructor to create the panel.<p>
096     * @param model the model to read data from.
097     * @param controller the controller to communicate with.
098     */
099    public CmsPatternPanelWeeklyView(CmsPatternPanelWeeklyController controller, I_CmsObservableSerialDateValue model) {
100
101        m_controller = controller;
102        m_model = model;
103        m_model.registerValueChangeObserver(this);
104        m_checkBoxValueChangeHandler = new ValueChangeHandler<Boolean>() {
105
106            public void onValueChange(ValueChangeEvent<Boolean> event) {
107
108                if (handleChange()) {
109                    m_controller.setWeekDays(getWeekDays());
110                }
111            }
112        };
113
114        initWidget(uiBinder.createAndBindUi(this));
115
116        m_labelEvery.setInnerText(Messages.get().key(Messages.GUI_SERIALDATE_WEEKLY_EVERY_0));
117        m_everyDay.setTriggerChangeOnKeyPress(true);
118        m_labelWeeks.setInnerText(Messages.get().key(Messages.GUI_SERIALDATE_WEEKLY_WEEK_AT_0));
119        createDayPanel();
120    }
121
122    /**
123     * @see org.opencms.acacia.client.widgets.serialdate.I_CmsSerialDateValueChangeObserver#onValueChange()
124     */
125    public void onValueChange() {
126
127        m_triggerChangeActions = false;
128        if (!m_everyDay.isFocused()) {
129            m_everyDay.setFormValueAsString("" + m_model.getInterval());
130        }
131        setWeekDays(m_model.getWeekDays());
132        m_triggerChangeActions = true;
133
134    }
135
136    /**
137     * Returns all selected days.<p>
138     * @return all selected days
139     * */
140    protected SortedSet<WeekDay> getWeekDays() {
141
142        SortedSet<WeekDay> result = new TreeSet<>();
143        for (CmsCheckBox box : m_checkboxes) {
144            if (box.isChecked()) {
145                result.add(WeekDay.valueOf(box.getInternalValue()));
146            }
147        }
148        return result;
149    }
150
151    /**
152     * Returns a flag, indicating if change actions should be triggered.
153     * @return a flag, indicating if change actions should be triggered.
154     */
155    boolean handleChange() {
156
157        return m_triggerChangeActions;
158    }
159
160    /**
161     * Handles the days key press event.<p>
162     *
163     * @param event the key press event
164     */
165    @UiHandler("m_everyDay")
166    void onDaysValueChange(ValueChangeEvent<String> event) {
167
168        if (handleChange()) {
169            m_controller.setInterval(m_everyDay.getText());
170        }
171    }
172
173    /**
174     * Creates the day selection view.<p>
175     * */
176    private void createDayPanel() {
177
178        CmsCheckBox box = new CmsCheckBox(Messages.get().key(Messages.GUI_SERIALDATE_DAY_MONDAY_0));
179        box.setInternalValue(WeekDay.MONDAY.toString());
180        box.addValueChangeHandler(m_checkBoxValueChangeHandler);
181        m_checkboxes.add(box);
182        m_dayPanel.add(box);
183        box = new CmsCheckBox(Messages.get().key(Messages.GUI_SERIALDATE_DAY_TUESDAY_0));
184        box.setInternalValue(WeekDay.TUESDAY.toString());
185        box.addValueChangeHandler(m_checkBoxValueChangeHandler);
186        m_checkboxes.add(box);
187        m_dayPanel.add(box);
188        box = new CmsCheckBox(Messages.get().key(Messages.GUI_SERIALDATE_DAY_WEDNESDAY_0));
189        box.setInternalValue(WeekDay.WEDNESDAY.toString());
190        box.addValueChangeHandler(m_checkBoxValueChangeHandler);
191        m_checkboxes.add(box);
192        m_dayPanel.add(box);
193        box = new CmsCheckBox(Messages.get().key(Messages.GUI_SERIALDATE_DAY_THURSDAY_0));
194        box.setInternalValue(WeekDay.THURSDAY.toString());
195        box.addValueChangeHandler(m_checkBoxValueChangeHandler);
196        m_checkboxes.add(box);
197        m_dayPanel.add(box);
198        box = new CmsCheckBox(Messages.get().key(Messages.GUI_SERIALDATE_DAY_FRIDAY_0));
199        box.setInternalValue(WeekDay.FRIDAY.toString());
200        box.addValueChangeHandler(m_checkBoxValueChangeHandler);
201        m_checkboxes.add(box);
202        m_dayPanel.add(box);
203        box = new CmsCheckBox(Messages.get().key(Messages.GUI_SERIALDATE_DAY_SATURDAY_0));
204        box.setInternalValue(WeekDay.SATURDAY.toString());
205        box.addValueChangeHandler(m_checkBoxValueChangeHandler);
206        m_checkboxes.add(box);
207        m_dayPanel.add(box);
208        box = new CmsCheckBox(Messages.get().key(Messages.GUI_SERIALDATE_DAY_SUNDAY_0));
209        box.setInternalValue(WeekDay.SUNDAY.toString());
210        box.addValueChangeHandler(m_checkBoxValueChangeHandler);
211        box.addValueChangeHandler(m_checkBoxValueChangeHandler);
212        m_checkboxes.add(box);
213        m_dayPanel.add(box);
214    }
215
216    /**
217     * Selects all days.<p>
218     * @param weekDays List of selected days
219     * */
220    private void setWeekDays(SortedSet<WeekDay> weekDays) {
221
222        List<CmsCheckBox> checked = new ArrayList<CmsCheckBox>();
223
224        for (WeekDay day : weekDays) {
225            for (CmsCheckBox box : m_checkboxes) {
226                if (box.getInternalValue().equals(day.toString())) {
227                    checked.add(box);
228                }
229            }
230        }
231        for (CmsCheckBox box : m_checkboxes) {
232            if (checked.contains(box)) {
233                box.setChecked(true);
234            } else {
235                box.setChecked(false);
236            }
237        }
238
239    }
240}