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.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        m_model = model;
050        m_changeHandler = changeHandler;
051
052    }
053
054    /**
055     * @see org.opencms.acacia.client.widgets.serialdate.I_CmsSerialDatePatternController#getView()
056     */
057    abstract public I_CmsSerialDatePatternView getView();
058
059    /**
060     * @param cmd see change handler
061     * @param showDialog see change handler
062     * @see I_ChangeHandler#conditionallyRemoveExceptionsOnChange(Command, boolean)
063     */
064    protected void conditionallyRemoveExceptionsOnChange(Command cmd, boolean showDialog) {
065
066        m_changeHandler.conditionallyRemoveExceptionsOnChange(cmd, showDialog);
067    }
068
069    /**
070     * Call when the value has changed.
071     */
072    protected void onValueChange() {
073
074        m_changeHandler.valueChanged();
075    }
076
077    /**
078     * @param cmd see change handler
079     * @see I_ChangeHandler#removeExceptionsOnChange(Command)
080     */
081    protected void removeExceptionsOnChange(Command cmd) {
082
083        m_changeHandler.removeExceptionsOnChange(cmd);
084    }
085
086    /**
087     * Returns the default values for patterns.
088     * @return the default values for patterns.
089     */
090    PatternDefaultValues getPatternDefaultValues() {
091
092        return m_changeHandler.getPatternDefaultValues();
093    }
094
095    /**
096     * Sets the day of the month.
097     * @param day the day to set.
098     */
099    void setDayOfMonth(String day) {
100
101        final int i = CmsSerialDateUtil.toIntWithDefault(day, -1);
102        if (m_model.getDayOfMonth() != i) {
103            removeExceptionsOnChange(new Command() {
104
105                public void execute() {
106
107                    m_model.setDayOfMonth(i);
108                    onValueChange();
109                }
110            });
111        }
112    }
113
114    /**
115     * Sets the interval.
116     * @param interval the interval to set.
117     */
118    void setInterval(String interval) {
119
120        final int i = CmsSerialDateUtil.toIntWithDefault(interval, -1);
121        if (m_model.getInterval() != i) {
122            removeExceptionsOnChange(new Command() {
123
124                public void execute() {
125
126                    m_model.setInterval(i);
127                    onValueChange();
128                }
129            });
130        }
131
132    }
133}