com.bluemarsh.jswat
Class CommandManager

java.lang.Object
  |
  +--com.bluemarsh.jswat.DefaultManager
        |
        +--com.bluemarsh.jswat.CommandManager
All Implemented Interfaces:
Manager, SessionListener

public class CommandManager
extends DefaultManager

Class CommandManager is responsible for parsing text-based commands entered by the user. It breaks up the input into tokens and determines which command is to be executed. It then calls on the appropriate command object to perform the action.

Author:
Nathan Fiedler

Field Summary
protected  java.util.Hashtable aliasList
          Table of command aliases, keyed by their alias name.
protected  java.util.Hashtable commandList
          Table of commands keyed by their action command string.
protected  java.util.ArrayList commandNames
          Sorted list of all command names.
protected  int currentHistory
          Index into historyChain indicating current old command being examined by the user.
protected  JSwatCommand grabInputCommand
          Command object that wishes to grab the next input.
protected  java.util.LinkedList historyChain
          List of commands executed by the user, stored in order.
protected  MacroManager macroManager
          Reference to the manager of macros.
protected  Log outputLog
          Log to write to when commands perform their tasks or have errors to report.
protected  Session owningSession
          JSwat session that we're associated with.
 
Fields inherited from class com.bluemarsh.jswat.DefaultManager
swat
 
Constructor Summary
CommandManager()
          Constructs a CommandManager with the default input field.
 
Method Summary
protected  java.lang.String checkPrefix(java.lang.String input)
          Check for a possible matching command prefix.
 void createAlias(java.lang.String name, java.lang.String cmnd)
          Creates a command alias.
 void displayHistory()
          Displays a list of the commands in the history chain to the main message window.
protected  java.lang.Class findCommand(java.lang.String commandName)
          Find the class of the command, given the command name.
 java.lang.String getAlias(java.lang.String name)
          Returns the definition of the named alias.
 JSwatCommand getCommand(java.lang.String commandName)
          Fetches an instance of the command by the given name.
 JSwatCommand getCommand(java.lang.String commandName, boolean displayErrors)
          Fetches an instance of the command by the given name.
 java.lang.String getHistoryNext()
          Retrieves the command following the current position within the command history.
 java.lang.String getHistoryPrev()
          Retrieves the command preceeding the current position within the command history.
 java.lang.String getInputPrompt()
          Get the prompt string that should be used for input from the user.
 void grabInput(JSwatCommand command)
          Tell the CommandManager that the given command should be passed the next input from the user.
 void handleInput(java.lang.String inputStr)
          Parse the input and execute the command.
protected  boolean handleInputGrab(java.lang.String inputStr)
          Handle the input grab of some arbitrary command.
 void init(Session session)
          Called after the Session has instantiated this mananger.
 void listAliases()
          List all of the created command aliases to the main message window.
 void listCommands()
          List all of the loaded commands with a brief description of each.
 void listMacros()
          List all of the created command macros to the main message window.
protected  java.lang.String parseBang(java.lang.String inputStr)
          Handle the '!' and '!!' operators by looking for a matching command in the command history and returning the input string to be executed.
 void parseInput(java.lang.String inputStr)
          Parse the command input string and perform the appropriate action.
 void removeAlias(java.lang.String alias)
          Remove the specified alias.
protected  void runCommand(java.lang.String inputStr)
          Runs the given command input, without handling '!' or command aliases.
 void runScript(java.lang.String filepath)
          Read the lines from the given file and parse each line as if it were a command.
 
Methods inherited from class com.bluemarsh.jswat.DefaultManager
activate, close, deactivate
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

commandList

protected java.util.Hashtable commandList
Table of commands keyed by their action command string.

commandNames

protected java.util.ArrayList commandNames
Sorted list of all command names.

aliasList

protected java.util.Hashtable aliasList
Table of command aliases, keyed by their alias name.

outputLog

protected Log outputLog
Log to write to when commands perform their tasks or have errors to report.

owningSession

protected Session owningSession
JSwat session that we're associated with. This is passed to the commands to assist them in performing their tasks.

historyChain

protected java.util.LinkedList historyChain
List of commands executed by the user, stored in order.

currentHistory

protected int currentHistory
Index into historyChain indicating current old command being examined by the user.

grabInputCommand

protected JSwatCommand grabInputCommand
Command object that wishes to grab the next input. Set by the grabInput() method and reset by handleInputGrab().

macroManager

protected MacroManager macroManager
Reference to the manager of macros.
Constructor Detail

CommandManager

public CommandManager()
Constructs a CommandManager with the default input field.
Method Detail

checkPrefix

protected java.lang.String checkPrefix(java.lang.String input)
                                throws com.bluemarsh.jswat.CommandManager.AmbiguousMatchException
Check for a possible matching command prefix. That is, using the input, find a command whose name starts with the given input.
Parameters:
input - possible command prefix to look for.
Returns:
name of command that matches prefix, or null if none.
Throws:
AmbiguousMatchException - Thrown if the input matched more than one command.

createAlias

public void createAlias(java.lang.String name,
                        java.lang.String cmnd)
Creates a command alias. Subsequent uses of the alias name will result in executing the matching command.
Parameters:
name - Name of new alias.
cmnd - Command string to alias.

displayHistory

public void displayHistory()
Displays a list of the commands in the history chain to the main message window.

findCommand

protected java.lang.Class findCommand(java.lang.String commandName)
Find the class of the command, given the command name.
Parameters:
commandName - Name of command to find.
Returns:
Command class, or null if error.

getAlias

public java.lang.String getAlias(java.lang.String name)
Returns the definition of the named alias.
Parameters:
name - alias name.
Returns:
alias value, or null if no alias defined by that name.

getCommand

public JSwatCommand getCommand(java.lang.String commandName)
Fetches an instance of the command by the given name. If the command object does not exist, it will be instantiated. This method will display errors to the screen, if they occur.
Parameters:
commandName - Name of command to fetch.
Returns:
Command object, or null if error.

getCommand

public JSwatCommand getCommand(java.lang.String commandName,
                               boolean displayErrors)
Fetches an instance of the command by the given name. If the command object does not exist, it will be instantiated.
Parameters:
commandName - Name of command to fetch.
displayErrors - True to display errors if they occur; otherwise be silent and simply return null.
Returns:
Command object, or null if error.

getHistoryNext

public java.lang.String getHistoryNext()
Retrieves the command following the current position within the command history. If null is returned, that indicates that the current position is at the end of the history.
Returns:
next command in history, or null if none.

getHistoryPrev

public java.lang.String getHistoryPrev()
Retrieves the command preceeding the current position within the command history. If null is returned, that indicates that the current position is at the start of the history.
Returns:
previous command in history, or null if none.

grabInput

public void grabInput(JSwatCommand command)
Tell the CommandManager that the given command should be passed the next input from the user. The input will not be processed by the CommandManager. The CommandManger will call the receiveInput() method of the command. Subsequent user input will be processed normally.

getInputPrompt

public java.lang.String getInputPrompt()
Get the prompt string that should be used for input from the user. This will change when a specific command has grabbed the input focus. Returning null means the default should be used.
Returns:
String to use for a prompt, or null to use the default

handleInput

public void handleInput(java.lang.String inputStr)
Parse the input and execute the command. The given input will also be added to the command history. The command input may start with an exclamation mark (!) and will be processed appropriately.
Parameters:
inputStr - command input.

handleInputGrab

protected boolean handleInputGrab(java.lang.String inputStr)
Handle the input grab of some arbitrary command. This will check if an input grab is in progress, call the interested command, and return true. Otherwise, it returns false and does nothing.
Parameters:
inputStr - Command input string.
Returns:
True if input was grabbed, false otherwise.

init

public void init(Session session)
Called after the Session has instantiated this mananger. To avoid problems with circular dependencies between managers, iniitialize data members before calling Session.getManager().
Overrides:
init in class DefaultManager
Parameters:
session - Session initializing this manager.

listAliases

public void listAliases()
List all of the created command aliases to the main message window.

listCommands

public void listCommands()
List all of the loaded commands with a brief description of each.

listMacros

public void listMacros()
List all of the created command macros to the main message window.

parseBang

protected java.lang.String parseBang(java.lang.String inputStr)
Handle the '!' and '!!' operators by looking for a matching command in the command history and returning the input string to be executed.
Parameters:
inputStr - Command input string.
Returns:
Command input string to execute, or null if no matching command was found (an error condition).

parseInput

public void parseInput(java.lang.String inputStr)
Parse the command input string and perform the appropriate action. Handles command aliases and macros. Does not handle the ! or !! operators, nor is the command inserted into the command history.
Parameters:
inputStr - Input command string.

removeAlias

public void removeAlias(java.lang.String alias)
Remove the specified alias.
Parameters:
alias - Name of alias to remove.

runCommand

protected void runCommand(java.lang.String inputStr)
Runs the given command input, without handling '!' or command aliases. This does, however, handle macros.
Parameters:
inputStr - Original command input string.

runScript

public void runScript(java.lang.String filepath)
Read the lines from the given file and parse each line as if it were a command.
Parameters:
filepath - Path and filename of script file.