Package net.sf.jguiraffe.gui.cmd
Class CommandQueueImpl
- java.lang.Object
-
- net.sf.jguiraffe.gui.cmd.CommandQueueImpl
-
- All Implemented Interfaces:
CommandQueue
public class CommandQueueImpl extends Object implements CommandQueue
A command queue implementation for GUI applications.
This class maintains a queue, in which
Commandobjects can be inserted by an application. At least one worker thread monitors the queue and executes the commands.In an application usually the
execute()method is of most importance. Here newCommandobjects are passed. Then the application need not bother about execution of these commands in a background thread. The other methods will be used by worker threads to obtain commands and send notifications about executed commands.- Version:
- $Id: CommandQueueImpl.java 205 2012-01-29 18:29:57Z oheger $
- Author:
- Oliver Heger
-
-
Constructor Summary
Constructors Constructor Description CommandQueueImpl(GUISynchronizer sync)Creates a new instance ofCommandQueueand initializes it with theGUISynchronizer.CommandQueueImpl(GUISynchronizer sync, ExecutorService execSrvc)Creates a new instance ofCommandQueueImpland initializes it with theGUISynchronizerand theExecutorServiceto be used.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddQueueListener(CommandQueueListener l)Adds an event listener to this queue.protected RunnablecreateTask(Command cmd)Creates a task object for executing the passed in command.voidexecute(Command cmd)Executes the specified command object.protected voidfireQueueEvent(Command cmd, CommandQueueEvent.Type eventType)Notifies all registered listeners about a change in the state of this queue.ExecutorServicegetExecutorService()Returns theExecutorServiceused by this command queue.GUISynchronizergetGUISynchronizer()Returns theGUISynchronizer.booleanisPending()Tests whether there are commands that have not been executed.booleanisShutdown()Tests whether this command queue was shutdown.voidprocessingFinished(Command cmd)This method is called by a worker thread if a command has been executed.voidremoveQueueListener(CommandQueueListener l)Removes the specified event listener from this queue.voidsetGUISynchronizer(GUISynchronizer sync)Sets theGUISynchronizer.voidshutdown(boolean immediate)Shuts down this command queue.
-
-
-
Constructor Detail
-
CommandQueueImpl
public CommandQueueImpl(GUISynchronizer sync)
Creates a new instance ofCommandQueueand initializes it with theGUISynchronizer. A defaultExecutorServicewill be created.- Parameters:
sync- the GUI synchronizer object (must not be null)- Throws:
IllegalArgumentException- if a required parameter is missing
-
CommandQueueImpl
public CommandQueueImpl(GUISynchronizer sync, ExecutorService execSrvc)
Creates a new instance ofCommandQueueImpland initializes it with theGUISynchronizerand theExecutorServiceto be used.- Parameters:
sync- theGUISynchronizer(must not be null)execSrvc- theExecutorService(must not be null)- Throws:
IllegalArgumentException- if a required parameter is missing
-
-
Method Detail
-
getGUISynchronizer
public GUISynchronizer getGUISynchronizer()
Returns theGUISynchronizer.- Specified by:
getGUISynchronizerin interfaceCommandQueue- Returns:
- the GUI synchronizer
-
setGUISynchronizer
public void setGUISynchronizer(GUISynchronizer sync)
Sets theGUISynchronizer.- Specified by:
setGUISynchronizerin interfaceCommandQueue- Parameters:
sync- the GUI synchronizer (must not be null)- Throws:
IllegalArgumentException- if the synchronizer is null
-
getExecutorService
public ExecutorService getExecutorService()
Returns theExecutorServiceused by this command queue.- Returns:
- the executor service
-
addQueueListener
public void addQueueListener(CommandQueueListener l)
Adds an event listener to this queue.- Specified by:
addQueueListenerin interfaceCommandQueue- Parameters:
l- the listener to add (must not be null)- Throws:
IllegalArgumentException- if the listener is undefined
-
removeQueueListener
public void removeQueueListener(CommandQueueListener l)
Removes the specified event listener from this queue.- Specified by:
removeQueueListenerin interfaceCommandQueue- Parameters:
l- the listener to remove
-
execute
public void execute(Command cmd)
Executes the specified command object. This implementation callscreateTask()to create a task object for actually executing the command. This task is then passed to theExecutorService, so it will be processed by a background thread.- Specified by:
executein interfaceCommandQueue- Parameters:
cmd- the command to be executed (must not be null)- Throws:
IllegalArgumentException- if the command is nullIllegalStateException- ifshutdown()has already been called
-
processingFinished
public void processingFinished(Command cmd)
This method is called by a worker thread if a command has been executed.- Parameters:
cmd- the command that has been processed
-
isPending
public boolean isPending()
Tests whether there are commands that have not been executed. This implementation uses a counter that keeps track of the scheduled commands (i.e. the number of commands passed to theexecute()method) that have not yet been completed.- Specified by:
isPendingin interfaceCommandQueue- Returns:
- a flag whether there are pending commands
-
isShutdown
public boolean isShutdown()
Tests whether this command queue was shutdown. This information can be obtained from the underlying executor service.- Specified by:
isShutdownin interfaceCommandQueue- Returns:
- a flag whether
shutdown()was called
-
shutdown
public void shutdown(boolean immediate)
Shuts down this command queue. For this implementation, a shutdown means that the underlying executor has to be shut down. Depending on the boolean parameter this method will block until the executor service's shutdown is complete.- Specified by:
shutdownin interfaceCommandQueue- Parameters:
immediate- a flag whether an immediate shutdown should be performed
-
fireQueueEvent
protected void fireQueueEvent(Command cmd, CommandQueueEvent.Type eventType)
Notifies all registered listeners about a change in the state of this queue.- Parameters:
cmd- the affected commandeventType- the type of the event to fire
-
-