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