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 GmbH & Co. KG, 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.util;
029
030import java.util.HashMap;
031import java.util.Map;
032
033import com.google.gwt.user.client.Command;
034import com.google.gwt.user.client.Timer;
035
036/**
037 * Takes care of the burst of the same event, by skipping the first ones and executing only the last one.<p>
038 *
039 * Usage example:
040 *
041 * <pre>
042 *   Window.addResizeHandler(new ResizeHandler() {
043 *       public void onResize(ResizeEvent event) {
044 *           CmsBurstEventManager.get().schedule("resize-window", new Command() {
045 *               public void execute() {
046 *                   // resize
047 *               }
048 *           }, 200);
049 *       }
050 *   });
051 * </pre>
052 *
053 * @since 8.0.0
054 *
055 * @see <a href="http://ui-programming.blogspot.com/2010/02/gwt-how-to-implement-delayedtask-in.html">Original implementation</a>
056 */
057public final class CmsBurstEventManager {
058
059    /**
060     * The class is model of one 'burst' event that is added to the manager.<p>
061     */
062    private static class BurstEvent {
063
064        /** The timer. */
065        private final Timer m_timer;
066
067        /**
068         * Constructor of the 'burst' event.<p>
069         *
070         * @param name the unique name, which identifies the event
071         * @param command command to execute when the timer expires
072         */
073        public BurstEvent(final String name, final Command command) {
074
075            m_timer = new Timer() {
076
077                /**
078                 * @see com.google.gwt.user.client.Timer#run()
079                 */
080                @Override
081                public void run() {
082
083                    if (command != null) {
084                        command.execute();
085                    }
086                    CmsBurstEventManager.get().cancel(name);
087                }
088            };
089        }
090
091        /**
092         * Returns the timer.<p>
093         *
094         * @return the timer
095         */
096        public Timer getTimer() {
097
098            return m_timer;
099        }
100    }
101
102    /** The singleton instance. */
103    private static CmsBurstEventManager INSTANCE;
104
105    /** The internal memory. */
106    private Map<String, BurstEvent> m_memory = new HashMap<String, BurstEvent>();
107
108    /**
109     * Hidden constructor.<p>
110     */
111    private CmsBurstEventManager() {
112
113        // emtpy
114    }
115
116    /**
117     * Returns the singleton instance.<p>
118     *
119     * @return the singleton instance
120     */
121    protected static CmsBurstEventManager get() {
122
123        if (INSTANCE == null) {
124            INSTANCE = new CmsBurstEventManager();
125        }
126        return INSTANCE;
127    }
128
129    /**
130     * Adds an 'burst' event to the manager.<p>
131     *
132     * @param name the unique name, which identifies the event
133     * @param command command to execute when the timer expires
134     * @param delayMsec the timer delay (it's reseted if multiple events are added)
135     */
136    public void schedule(final String name, final Command command, final int delayMsec) {
137
138        BurstEvent e = m_memory.remove(name);
139        if (e != null) {
140            // disable the old event
141            e.getTimer().cancel();
142        }
143        // put the new event and schedule it
144        e = new BurstEvent(name, command);
145        m_memory.put(name, e);
146        e.getTimer().schedule(delayMsec);
147    }
148
149    /**
150     * Removes the event from the manager.<p>
151     *
152     * @param eventName the name of the event that we need to remove
153     */
154    public void cancel(final String eventName) {
155
156        BurstEvent oe = m_memory.remove(eventName);
157        if (oe != null) {
158            // disable the old event.
159            oe.getTimer().cancel();
160        }
161    }
162}