001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (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.acacia.shared.I_CmsSerialDateValue.WeekDay;
031import org.opencms.acacia.shared.I_CmsSerialDateValue.WeekOfMonth;
032
033import com.google.gwt.user.client.Command;
034
035/** Controller for the monthly pattern panel. */
036public class CmsPatternPanelMonthlyController extends A_CmsPatternPanelController {
037
038    /** The controlled view. */
039    private final CmsPatternPanelMonthlyView m_view;
040
041    /**
042     * Constructor for the monthly pattern panel controller
043     * @param model the model to read data from.
044     * @param changeHandler the change handler.
045     */
046    CmsPatternPanelMonthlyController(final CmsSerialDateValue model, final I_ChangeHandler changeHandler) {
047
048        super(model, changeHandler);
049        m_view = new CmsPatternPanelMonthlyView(this, m_model);
050    }
051
052    /**
053     * @see org.opencms.acacia.client.widgets.serialdate.A_CmsPatternPanelController#getView()
054     */
055    @Override
056    public I_CmsSerialDatePatternView getView() {
057
058        return m_view;
059    }
060
061    /**
062     * Set the pattern scheme to either "by weekday" or "by day of month".
063     * @param isByWeekDay flag, indicating if the pattern "by weekday" should be set.
064     * @param fireChange flag, indicating if a value change event should be fired.
065     */
066    public void setPatternScheme(final boolean isByWeekDay, final boolean fireChange) {
067
068        if (isByWeekDay ^ (null != m_model.getWeekDay())) {
069            removeExceptionsOnChange(new Command() {
070
071                public void execute() {
072
073                    if (isByWeekDay) {
074                        m_model.setWeekOfMonth(getPatternDefaultValues().getWeekOfMonth());
075                        m_model.setWeekDay(getPatternDefaultValues().getWeekDay());
076                    } else {
077                        m_model.clearWeekDays();
078                        m_model.clearWeeksOfMonth();
079                        m_model.setDayOfMonth(getPatternDefaultValues().getDayOfMonth());
080                    }
081                    m_model.setInterval(getPatternDefaultValues().getInterval());
082                    if (fireChange) {
083                        onValueChange();
084                    }
085                }
086            });
087        }
088
089    }
090
091    /**
092     * Set the week day the event should take place.
093     * @param dayString the day as string.
094     */
095    public void setWeekDay(String dayString) {
096
097        final WeekDay day = WeekDay.valueOf(dayString);
098        if (m_model.getWeekDay() != day) {
099            removeExceptionsOnChange(new Command() {
100
101                public void execute() {
102
103                    m_model.setWeekDay(day);
104                    onValueChange();
105                }
106            });
107        }
108
109    }
110
111    /**
112     * Handle a change in the weeks of month.
113     * @param week the changed weeks checkbox's internal value.
114     * @param value the new value of the changed checkbox.
115     */
116    public void weeksChange(String week, Boolean value) {
117
118        final WeekOfMonth changedWeek = WeekOfMonth.valueOf(week);
119        boolean newValue = (null != value) && value.booleanValue();
120        boolean currentValue = m_model.getWeeksOfMonth().contains(changedWeek);
121        if (newValue != currentValue) {
122            if (newValue) {
123                setPatternScheme(true, false);
124                m_model.addWeekOfMonth(changedWeek);
125                onValueChange();
126            } else {
127                removeExceptionsOnChange(new Command() {
128
129                    public void execute() {
130
131                        m_model.removeWeekOfMonth(changedWeek);
132                        onValueChange();
133                    }
134                });
135            }
136        }
137    }
138}