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.client.widgets.serialdate.CmsSerialDateController.PatternDefaultValues;
031import org.opencms.acacia.shared.CmsSerialDateUtil;
032
033import com.google.gwt.user.client.Command;
034
035/** Abstract base class for pattern panel controllers. */
036public abstract class A_CmsPatternPanelController implements I_CmsSerialDatePatternController {
037
038    /** The change handler called on {@link #onValueChange()}. */
039    private final I_ChangeHandler m_changeHandler;
040    /** The model to read data from. */
041    protected final CmsSerialDateValue m_model;
042
043    /**
044     * Constructor for the abstract pattern panel controller
045     * @param model the model to read data from.
046     * @param changeHandler the handler for value changes.
047     */
048    public A_CmsPatternPanelController(final CmsSerialDateValue model, final I_ChangeHandler changeHandler) {
049
050        m_model = model;
051        m_changeHandler = changeHandler;
052
053    }
054
055    /**
056     * @see org.opencms.acacia.client.widgets.serialdate.I_CmsSerialDatePatternController#getView()
057     */
058    abstract public I_CmsSerialDatePatternView getView();
059
060    /**
061     * @param cmd see change handler
062     * @param showDialog see change handler
063     * @see I_ChangeHandler#conditionallyRemoveExceptionsOnChange(Command, boolean)
064     */
065    protected void conditionallyRemoveExceptionsOnChange(Command cmd, boolean showDialog) {
066
067        m_changeHandler.conditionallyRemoveExceptionsOnChange(cmd, showDialog);
068    }
069
070    /**
071     * Call when the value has changed.
072     */
073    protected void onValueChange() {
074
075        m_changeHandler.valueChanged();
076    }
077
078    /**
079     * @param cmd see change handler
080     * @see I_ChangeHandler#removeExceptionsOnChange(Command)
081     */
082    protected void removeExceptionsOnChange(Command cmd) {
083
084        m_changeHandler.removeExceptionsOnChange(cmd);
085    }
086
087    /**
088     * Returns the default values for patterns.
089     * @return the default values for patterns.
090     */
091    PatternDefaultValues getPatternDefaultValues() {
092
093        return m_changeHandler.getPatternDefaultValues();
094    }
095
096    /**
097     * Sets the day of the month.
098     * @param day the day to set.
099     */
100    void setDayOfMonth(String day) {
101
102        final int i = CmsSerialDateUtil.toIntWithDefault(day, -1);
103        if (m_model.getDayOfMonth() != i) {
104            removeExceptionsOnChange(new Command() {
105
106                public void execute() {
107
108                    m_model.setDayOfMonth(i);
109                    onValueChange();
110                }
111            });
112        }
113    }
114
115    /**
116     * Sets the interval.
117     * @param interval the interval to set.
118     */
119    void setInterval(String interval) {
120
121        final int i = CmsSerialDateUtil.toIntWithDefault(interval, -1);
122        if (m_model.getInterval() != i) {
123            removeExceptionsOnChange(new Command() {
124
125                public void execute() {
126
127                    m_model.setInterval(i);
128                    onValueChange();
129                }
130            });
131        }
132
133    }
134}