qflib 0.98.1

de.qfs.lib.command
Class CommandDistributor

java.lang.Object
  |
  +--de.qfs.lib.command.CommandDistributor

public class CommandDistributor
extends java.lang.Object

This class is responsible for the distribution of high level commands among possible command handlers.

Commands will either be invoked directly, if they implement the Invokable interface, or passed through a series of CommandHandlers until one calls the Command's setHandled method.

Command handlers may be registered for all types of commands, for commands originating from a certain source or for commands whose source is of a certain class. The usefulness of this mechanism has yet to be proved.

There are several reasons for having a high level command distribution system in addition to the GUI's event loop:

Version:
$Revision: 1.8 $
Author:
Gregor Schmid

Constructor Summary
CommandDistributor()
          Create a new CommandDistributor.
 
Method Summary
 void addCommandHandler(CommandHandler handler)
          Register a general CommandHandler.
 void addCommandHandler(CommandHandler handler, java.lang.Class sourceClass)
          Register a CommandHandler for commands whose source is of a certain class.
 void addCommandHandler(CommandHandler handler, java.lang.Object source)
          Register a CommandHandler for Commands originating from a certain source.
 void addExceptionHandler(ExceptionHandler handler)
          Add an ExceptionHandler to handle RuntimeExceptions.
 void assignAsyncCommand(AsyncCommand command)
          Start a background thread and assign a Command in it.
 java.lang.Object assignCommand(Command command)
          Either invoke a command directly or assign it to the registered CommandHandlers.
 java.lang.Object assignCommandSilently(Command command)
          Either invoke a command directly or assign it to the registered CommandHandlers.
static CommandDistributor getDefaultDistributor()
          Get the default CommandDistributor.
 void removeCommandHandler(CommandHandler handler)
          Remove a general CommandHandler.
 void removeCommandHandler(CommandHandler handler, java.lang.Class sourceClass)
          Remove a source class specific CommandHandler.
 void removeCommandHandler(CommandHandler handler, java.lang.Object source)
          Remove a source specific CommandHandler.
 void removeExceptionHandler(ExceptionHandler handler)
          Remove an ExceptionHandler.
static void setDefaultDistributor(CommandDistributor distributor)
          Set the default CommandDistributor.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CommandDistributor

public CommandDistributor()
Create a new CommandDistributor.
Method Detail

getDefaultDistributor

public static CommandDistributor getDefaultDistributor()
Get the default CommandDistributor.
Returns:
The default CommandDistributor.

setDefaultDistributor

public static void setDefaultDistributor(CommandDistributor distributor)
Set the default CommandDistributor.
Parameters:
distributor - The CommandDistributor to set.

assignCommand

public java.lang.Object assignCommand(Command command)
Either invoke a command directly or assign it to the registered CommandHandlers. CommandExceptions thrown during the execution of the command will be caught and handled as follows:
MessageCommandException
The resources of the exception will be used to create a message dialog via Message.
DialogCommandException
The Dialog for the exception will be displayed.
CancelCommandException
The exception will be silently ignored.
Other CommandExceptions
An error will be logged via the Log mechanism
To handle RuntimeExceptions or Errors thrown by a Command, ExceptionHandlers can be registered with addExceptionHandler.
Parameters:
command - The command to assign.
Returns:
Whatever is returned from the execution of the command.

assignCommandSilently

public java.lang.Object assignCommandSilently(Command command)
                                       throws CommandException
Either invoke a command directly or assign it to the registered CommandHandlers. This methos is similar to assignCommand, except that no Exception handling takes place, neither for CommandExceptions, nor for RuntimeExceptions.
Parameters:
command - The command to assign.
Returns:
Whatever is returned from the execution of the command.
Throws:
CommandException - If something goes wrong during the execution of the command.

assignAsyncCommand

public void assignAsyncCommand(AsyncCommand command)
Start a background thread and assign a Command in it. Exception handling is similar to assignCommand.
Parameters:
command - The Command to run asynchronously.

addCommandHandler

public void addCommandHandler(CommandHandler handler)
Register a general CommandHandler.
Parameters:
handler - The CommandHandler to register.

addCommandHandler

public void addCommandHandler(CommandHandler handler,
                              java.lang.Object source)
Register a CommandHandler for Commands originating from a certain source.
Parameters:
handler - The CommandHandler to register.
source - The source of the commands.

addCommandHandler

public void addCommandHandler(CommandHandler handler,
                              java.lang.Class sourceClass)
Register a CommandHandler for commands whose source is of a certain class.
Parameters:
handler - The CommandHandler to register.
sourceClass - The Class to look for.

removeCommandHandler

public void removeCommandHandler(CommandHandler handler)
Remove a general CommandHandler.
Parameters:
handler - The CommandHandler to remove.

removeCommandHandler

public void removeCommandHandler(CommandHandler handler,
                                 java.lang.Object source)
Remove a source specific CommandHandler.
Parameters:
handler - The CommandHandler to remove.
source - The source of the commands.

removeCommandHandler

public void removeCommandHandler(CommandHandler handler,
                                 java.lang.Class sourceClass)
Remove a source class specific CommandHandler.
Parameters:
handler - The CommandHandler to remove.
sourceClass - The source's Class.

addExceptionHandler

public void addExceptionHandler(ExceptionHandler handler)
Add an ExceptionHandler to handle RuntimeExceptions. Handlers added later will be called first.
Parameters:
handler - The ExceptionHandler to add.

removeExceptionHandler

public void removeExceptionHandler(ExceptionHandler handler)
Remove an ExceptionHandler.
Parameters:
handler - The ExceptionHandler to remove.

qflib 0.98.1