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.ugc.client;
029
030import org.opencms.gwt.client.ui.CmsNotification;
031import org.opencms.gwt.client.ui.CmsNotification.Type;
032import org.opencms.gwt.client.ui.CmsNotificationMessage;
033import org.opencms.ugc.client.export.I_CmsBooleanCallback;
034
035/**
036 * Keeps track of currently running requests for the purpose of enabling or disabling a waiting indicator.<p>
037 */
038public class CmsRequestCounter {
039
040    /** Number of active requests. */
041    private int m_counter;
042
043    /** The callback used to enable or disable the wait indicator. */
044    private I_CmsBooleanCallback m_callback = new I_CmsBooleanCallback() {
045
046        private CmsNotificationMessage m_message;
047
048        /**
049         * @see org.opencms.ugc.client.export.I_CmsBooleanCallback#call(boolean)
050         */
051        public void call(boolean b) {
052
053            if (b) {
054                // check if the message is already showing
055                if (m_message == null) {
056                    m_message = CmsNotification.get().sendBusy(Type.NORMAL, "Loading");
057                }
058            } else if (m_message != null) {
059                CmsNotification.get().removeMessage(m_message);
060                m_message = null;
061            }
062        }
063    };
064
065    /**
066     * Decrements the request counter.<p>
067     */
068    public void decrement() {
069
070        m_counter -= 1;
071        if (m_counter < 0) {
072            m_counter = 0;
073        }
074        if (m_counter == 0) {
075            m_callback.call(false);
076        }
077    }
078
079    /**
080     * Increments the request counter.<p>
081     */
082    public void increment() {
083
084        m_counter += 1;
085        m_callback.call(true);
086    }
087
088    /**
089     * Sets the callback.<p>
090     *
091     * @param callback the callback
092     */
093    public void setCallback(I_CmsBooleanCallback callback) {
094
095        m_callback = callback;
096    }
097
098}