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.gwt.client.validation;
029
030import java.util.LinkedList;
031
032/**
033 * This is a helper class for scheduling form validations.<p>
034 *
035 * Since validations can be asynchronous, it would be possible for a validation to start while another one
036 * is still waiting for a response from the server if it were ran directly. This might result in an inconsistent state
037 * of the form fields being validated. To prevent this, validation controllers use this class to schedule validations,
038 * and call this class again after they're finished to execute the next validation.<p>
039 *
040 * The result of this is that a validation will only start after all unfinished validations which have been scheduled
041 * before it have finished running.<p>
042 *
043 * @since 8.0.0
044 */
045public class CmsValidationScheduler {
046
047    /** The singleton instance of this class. */
048    private static CmsValidationScheduler instance = new CmsValidationScheduler();
049
050    /** The queue of validations which still need to be run. */
051    private LinkedList<CmsValidationController> m_actions = new LinkedList<CmsValidationController>();
052
053    /** A flag which indicates whether there is no validation currently running. */
054    private boolean m_idle = true;
055
056    /**
057     * Hidden default constructor.<p>
058     */
059    protected CmsValidationScheduler() {
060
061        // do nothing
062    }
063
064    /**
065     * Returns the singleton instance of the validation scheduler.<p>
066     *
067     * @return the validation scheduler
068     */
069    public static CmsValidationScheduler get() {
070
071        return instance;
072    }
073
074    /**
075     * This method should be called by the validation controller when it has finished running.<p>
076     *
077     * It will execute the next validation if there is one.<p>
078     */
079    public void executeNext() {
080
081        if (!m_idle) {
082            if (m_actions.isEmpty()) {
083                m_idle = true;
084            } else {
085                CmsValidationController action = m_actions.removeFirst();
086                action.internalStartValidation();
087            }
088        } else {
089            assert false; // this shouldn't happen
090        }
091    }
092
093    /**
094     * This schedules a new validation to be run after all currently scheduled or running validations have finished.<p>
095     *
096     * If there are no validations running, the validation will be started immediately.<p>
097     *
098     * @param action the validation to be scheduled
099     */
100    public void schedule(CmsValidationController action) {
101
102        if (m_idle) {
103            m_idle = false;
104            action.internalStartValidation();
105        } else {
106            m_actions.add(action);
107        }
108    }
109}