001/*
002 * GWTEventService
003 * Copyright (c) 2011 and beyond, strawbill UG (haftungsbeschr?nkt)
004 *
005 * This is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU Lesser General Public License as
007 * published by the Free Software Foundation; either version 3 of
008 * the License, or (at your option) any later version.
009 * Other licensing for GWTEventService may also be possible on request.
010 * Please view the license.txt of the project for more information.
011 *
012 * This software 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 * You should have received a copy of the GNU Lesser General Public
018 * License along with this software; if not, write to the Free
019 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021 */
022package de.novanic.eventservice.client.event.command.schedule;
023
024import de.novanic.eventservice.client.event.command.ClientCommand;
025import com.google.gwt.user.client.Timer;
026
027/**
028 * The ClientCommandScheduler can schedule the execution of {@link de.novanic.eventservice.client.event.command.ClientCommand}
029 * instances for a specified time/delay. GWTCommandScheduler is a implementation of {@link de.novanic.eventservice.client.event.command.schedule.ClientCommandScheduler}
030 * for GWT.
031 *
032 * @author sstrohschein
033 *         <br>Date: 04.04.2009
034 *         <br>Time: 22:25:02
035 */
036public class GWTCommandScheduler implements ClientCommandScheduler
037{
038    /**
039     * Creates a new thread for the execution of the {@link de.novanic.eventservice.client.event.command.ClientCommand}.
040     * @param aCommand {@link de.novanic.eventservice.client.event.command.ClientCommand} to schedule
041     */
042    public void schedule(ClientCommand<?> aCommand) {
043        schedule(aCommand, 1);
044    }
045
046    /**
047     * Creates a new thread for the execution of the {@link de.novanic.eventservice.client.event.command.ClientCommand}.
048     * The execution is started after the specified delay.
049     * @param aCommand {@link de.novanic.eventservice.client.event.command.ClientCommand} to schedule
050     * @param aDelay delay in milliseconds
051     */
052    public void schedule(final ClientCommand<?> aCommand, int aDelay) {
053        Timer theTimer = new GWTCommandTimer(aCommand);
054        theTimer.schedule(aDelay);
055    }
056
057    public static class GWTCommandTimer extends Timer
058    {
059        private ClientCommand<?> myClientCommand;
060
061        public GWTCommandTimer(ClientCommand<?> aClientCommand) {
062            myClientCommand = aClientCommand;
063        }
064
065        public void run() {
066            myClientCommand.execute();
067        }
068    }
069}