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;
031import org.opencms.acacia.shared.I_CmsSerialDateValue.PatternType;
032import org.opencms.acacia.shared.I_CmsSerialDateValue.WeekDay;
033import org.opencms.acacia.shared.I_CmsSerialDateValue.WeekOfMonth;
034import org.opencms.ade.contenteditor.client.Messages;
035import org.opencms.gwt.client.ui.input.CmsCheckBox;
036import org.opencms.gwt.client.ui.input.CmsRadioButton;
037import org.opencms.gwt.client.ui.input.CmsRadioButtonGroup;
038import org.opencms.gwt.client.ui.input.CmsSelectBox;
039
040import java.util.ArrayList;
041import java.util.Collection;
042import java.util.List;
043
044import com.google.gwt.core.shared.GWT;
045import com.google.gwt.dom.client.Element;
046import com.google.gwt.event.dom.client.FocusEvent;
047import com.google.gwt.event.logical.shared.ValueChangeEvent;
048import com.google.gwt.event.logical.shared.ValueChangeHandler;
049import com.google.gwt.uibinder.client.UiBinder;
050import com.google.gwt.uibinder.client.UiField;
051import com.google.gwt.uibinder.client.UiHandler;
052import com.google.gwt.user.client.ui.Composite;
053import com.google.gwt.user.client.ui.FlowPanel;
054import com.google.gwt.user.client.ui.HTMLPanel;
055
056/**
057 * The monthly pattern panel.<p>
058 * */
059public class CmsPatternPanelMonthlyView extends Composite implements I_CmsSerialDatePatternView {
060
061    /** The UI binder interface. */
062    interface I_CmsPatternPanelMonthlyUiBinder extends UiBinder<HTMLPanel, CmsPatternPanelMonthlyView> {
063        // nothing to do
064    }
065
066    /** Name of the "every x. day in month" radio button. */
067    private static final String DAYS_RADIOBUTTON = "everyday";
068
069    /** Name of the "at weeks" radio button. */
070    private static final String WEEKS_RADIOBUTTON = "workingday";
071
072    /** The UI binder instance. */
073    private static I_CmsPatternPanelMonthlyUiBinder uiBinder = GWT.create(I_CmsPatternPanelMonthlyUiBinder.class);
074
075    /** Checkboxes for the week days. */
076    List<CmsCheckBox> m_checkboxes = new ArrayList<CmsCheckBox>(I_CmsSerialDateValue.NUM_OF_WEEKDAYS);
077
078    /* UI elements for "every day". */
079
080    /** The day month radio button. */
081    @UiField(provided = true)
082    CmsRadioButton m_dayMonthRadioButton;
083    /** The text box for the date input. */
084    @UiField
085    CmsFocusAwareTextBox m_everyDay;
086    /** The select box for the month selection. */
087    @UiField
088    CmsFocusAwareTextBox m_everyMonth;
089
090    /** The days label. */
091    @UiField
092    Element m_labelDays;
093
094    /** The every label. */
095    @UiField
096    Element m_labelEvery;
097
098    /** The months label. */
099    @UiField
100    Element m_everyLabelMonth;
101
102    /* UI elements for "at week day". */
103
104    /** The week day month radio button. */
105    @UiField(provided = true)
106    CmsRadioButton m_weekDayMonthRadioButton;
107
108    /** The select box for the numeric selection. */
109    @UiField
110    FlowPanel m_weekPanel;
111
112    /** The select box for the day selection. */
113    @UiField
114    CmsSelectBox m_atDay;
115
116    /** The select box for the month selection. */
117    @UiField
118    CmsFocusAwareTextBox m_atMonth;
119
120    /** The month label. */
121    @UiField
122    Element m_atLabelMonth;
123
124    /** Group off all radio buttons. */
125    CmsRadioButtonGroup m_group = new CmsRadioButtonGroup();
126
127    /** The model to read the data from. */
128    private final I_CmsObservableSerialDateValue m_model;
129    /** The controller to handle changes. */
130    final CmsPatternPanelMonthlyController m_controller;
131
132    /** Flag, indicating if change actions should not be triggered. */
133    private boolean m_triggerChangeActions = true;
134
135    /**
136     * Default constructor to create the panel.<p>
137     * @param controller the controller that handles value changes.
138     * @param model the model that provides the values.
139     */
140    public CmsPatternPanelMonthlyView(
141        CmsPatternPanelMonthlyController controller,
142        I_CmsObservableSerialDateValue model) {
143
144        m_controller = controller;
145        m_model = model;
146        m_model.registerValueChangeObserver(this);
147
148        m_dayMonthRadioButton = new CmsRadioButton(
149            DAYS_RADIOBUTTON,
150            Messages.get().key(Messages.GUI_SERIALDATE_MONTHLY_MONTHDAY_AT_0));
151        m_dayMonthRadioButton.setGroup(m_group);
152        m_dayMonthRadioButton.setChecked(true);
153        m_weekDayMonthRadioButton = new CmsRadioButton(
154            WEEKS_RADIOBUTTON,
155            Messages.get().key(Messages.GUI_SERIALDATE_MONTHLY_WEEKDAY_AT_0));
156        m_weekDayMonthRadioButton.setGroup(m_group);
157        m_group.addValueChangeHandler(new ValueChangeHandler<String>() {
158
159            public void onValueChange(ValueChangeEvent<String> event) {
160
161                if (handleChange() && (event.getValue() != null)) {
162                    m_controller.setPatternScheme(event.getValue().equals(m_weekDayMonthRadioButton.getName()), true);
163                }
164            }
165        });
166        initWidget(uiBinder.createAndBindUi(this));
167
168        fillWeekPanel();
169        m_everyDay.setFormValueAsString("1");
170        m_everyDay.setTriggerChangeOnKeyPress(true);
171        m_labelDays.setInnerText(Messages.get().key(Messages.GUI_SERIALDATE_MONTHLY_MONTHDAY_DAY_EVERY_0));
172        m_everyMonth.setFormValueAsString("1");
173        m_everyMonth.setTriggerChangeOnKeyPress(true);
174        m_everyLabelMonth.setInnerText(Messages.get().key(Messages.GUI_SERIALDATE_MONTHLY_MONTH_0));
175
176        m_labelEvery.setInnerText(Messages.get().key(Messages.GUI_SERIALDATE_MONTHLY_WEEKDAY_EVERY_0));
177        m_atLabelMonth.setInnerText(Messages.get().key(Messages.GUI_SERIALDATE_MONTHLY_MONTH_0));
178        m_atMonth.setFormValueAsString("1");
179        m_atMonth.setTriggerChangeOnKeyPress(true);
180        initSelectBoxes();
181    }
182
183    /**
184     * @see org.opencms.acacia.client.widgets.serialdate.I_CmsSerialDateValueChangeObserver#onValueChange()
185     */
186    public void onValueChange() {
187
188        if (m_model.getPatternType().equals(PatternType.MONTHLY)) {
189            m_triggerChangeActions = false;
190            if (null == m_model.getWeekDay()) {
191                m_group.selectButton(m_dayMonthRadioButton);
192                if (!m_everyDay.isFocused()) {
193                    m_everyDay.setFormValueAsString(String.valueOf(m_model.getDayOfMonth()));
194                }
195                if (!m_everyMonth.isFocused()) {
196                    m_everyMonth.setFormValueAsString(String.valueOf(m_model.getInterval()));
197                }
198                m_atMonth.setFormValueAsString(Integer.toString(m_controller.getPatternDefaultValues().getInterval()));
199                m_atDay.selectValue(String.valueOf(m_controller.getPatternDefaultValues().getWeekDay()));
200                checkExactlyTheWeeksCheckBoxes(m_controller.getPatternDefaultValues().getWeeksOfMonth());
201            } else {
202                m_group.selectButton(m_weekDayMonthRadioButton);
203                if (!m_atMonth.isFocused()) {
204                    m_atMonth.setFormValueAsString(String.valueOf(m_model.getInterval()));
205                }
206                m_atDay.selectValue(m_model.getWeekDay().toString());
207                checkExactlyTheWeeksCheckBoxes(m_model.getWeeksOfMonth());
208                m_everyDay.setFormValueAsString(
209                    Integer.toString(m_controller.getPatternDefaultValues().getDayOfMonth()));
210                m_everyMonth.setFormValueAsString(
211                    Integer.toString(m_controller.getPatternDefaultValues().getInterval()));
212            }
213            m_triggerChangeActions = true;
214        }
215
216    }
217
218    /**
219     * Returns a flag, indicating if change actions should be triggered.
220     * @return a flag, indicating if change actions should be triggered.
221     */
222    boolean handleChange() {
223
224        return m_triggerChangeActions;
225    }
226
227    /**
228     * Handles "at" input field focus.
229     * @param event the focus event.
230     */
231    @UiHandler({"m_atDay", "m_atMonth"})
232    void onAtFocus(FocusEvent event) {
233
234        if (handleChange()) {
235            m_group.selectButton(m_weekDayMonthRadioButton);
236        }
237    }
238
239    /**
240     * Handles the every day change event.<p>
241     *
242     * @param event the change event
243     */
244    @UiHandler("m_everyDay")
245    void onEveryDayValueChange(ValueChangeEvent<String> event) {
246
247        if (handleChange()) {
248            m_controller.setDayOfMonth(event.getValue());
249        }
250
251    }
252
253    /**
254     * Handles "every" input field focus.
255     * @param event the focus event.
256     */
257    @UiHandler({"m_everyDay", "m_everyMonth"})
258    void onEveryFocus(FocusEvent event) {
259
260        if (handleChange()) {
261            m_group.selectButton(m_dayMonthRadioButton);
262        }
263    }
264
265    /**
266     * Handles interval changes.<p>
267     *
268     * @param event the value change event
269     */
270    @UiHandler({"m_atMonth", "m_everyMonth"})
271    void onIntervalValueChange(ValueChangeEvent<String> event) {
272
273        if (handleChange()) {
274            m_controller.setInterval(event.getValue());
275        }
276
277    }
278
279    /**
280     * Handles week day changes.
281     * @param event the change event.
282     */
283    @UiHandler("m_atDay")
284    void onWeekDayChange(ValueChangeEvent<String> event) {
285
286        if (handleChange()) {
287            m_controller.setWeekDay(event.getValue());
288        }
289    }
290
291    /**
292     * Creates a check box and adds it to the week panel and the checkboxes.
293     * @param internalValue the internal value of the checkbox
294     * @param labelMessageKey key for the label of the checkbox
295     */
296    private void addCheckBox(final String internalValue, String labelMessageKey) {
297
298        CmsCheckBox box = new CmsCheckBox(Messages.get().key(labelMessageKey));
299        box.setInternalValue(internalValue);
300        box.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
301
302            public void onValueChange(ValueChangeEvent<Boolean> event) {
303
304                if (handleChange()) {
305                    m_controller.weeksChange(internalValue, event.getValue());
306                }
307            }
308        });
309        m_weekPanel.add(box);
310        m_checkboxes.add(box);
311
312    }
313
314    /**
315     * Check exactly the week check-boxes representing the given weeks.
316     * @param weeksToCheck the weeks selected.
317     */
318    private void checkExactlyTheWeeksCheckBoxes(Collection<WeekOfMonth> weeksToCheck) {
319
320        for (CmsCheckBox cb : m_checkboxes) {
321            cb.setChecked(weeksToCheck.contains(WeekOfMonth.valueOf(cb.getInternalValue())));
322        }
323    }
324
325    /**
326     * Fills the week panel with checkboxes.
327     */
328    private void fillWeekPanel() {
329
330        addCheckBox(WeekOfMonth.FIRST.toString(), Messages.GUI_SERIALDATE_WEEKDAYNUMBER_1_0);
331        addCheckBox(WeekOfMonth.SECOND.toString(), Messages.GUI_SERIALDATE_WEEKDAYNUMBER_2_0);
332        addCheckBox(WeekOfMonth.THIRD.toString(), Messages.GUI_SERIALDATE_WEEKDAYNUMBER_3_0);
333        addCheckBox(WeekOfMonth.FOURTH.toString(), Messages.GUI_SERIALDATE_WEEKDAYNUMBER_4_0);
334        addCheckBox(WeekOfMonth.LAST.toString(), Messages.GUI_SERIALDATE_WEEKDAYNUMBER_5_0);
335    }
336
337    /**
338     * Creates the 'at' selection view.<p>
339     * */
340    private void initSelectBoxes() {
341
342        m_atDay.getOpener().setStyleName(
343            org.opencms.acacia.client.css.I_CmsWidgetsLayoutBundle.INSTANCE.widgetCss().selectBoxSelected());
344        m_atDay.getSelectorPopup().addStyleName(I_CmsLayoutBundle.INSTANCE.globalWidgetCss().selectBoxPopup());
345        m_atDay.addOption(WeekDay.SUNDAY.toString(), Messages.get().key(Messages.GUI_SERIALDATE_DAY_SUNDAY_0));
346        m_atDay.addOption(WeekDay.MONDAY.toString(), Messages.get().key(Messages.GUI_SERIALDATE_DAY_MONDAY_0));
347        m_atDay.addOption(WeekDay.TUESDAY.toString(), Messages.get().key(Messages.GUI_SERIALDATE_DAY_TUESDAY_0));
348        m_atDay.addOption(WeekDay.WEDNESDAY.toString(), Messages.get().key(Messages.GUI_SERIALDATE_DAY_WEDNESDAY_0));
349        m_atDay.addOption(WeekDay.THURSDAY.toString(), Messages.get().key(Messages.GUI_SERIALDATE_DAY_THURSDAY_0));
350        m_atDay.addOption(WeekDay.FRIDAY.toString(), Messages.get().key(Messages.GUI_SERIALDATE_DAY_FRIDAY_0));
351        m_atDay.addOption(WeekDay.SATURDAY.toString(), Messages.get().key(Messages.GUI_SERIALDATE_DAY_SATURDAY_0));
352        m_atDay.setWidth("100px");
353    }
354
355}