com.swath
Class UserDefinedCommand

java.lang.Object
  extended bycom.swath.UserDefinedCommand
All Implemented Interfaces:
CommandIfc
Direct Known Subclasses:
ExampleCommand1

public abstract class UserDefinedCommand
extends java.lang.Object
implements CommandIfc

User Defined Command base class.

Since:
SWATH 1.7

Field Summary
static java.lang.String RETURN_KEY
          The return key character (0x0D).
 
Constructor Summary
UserDefinedCommand()
          Constructor.
 
Method Summary
 boolean atPrompt(int validPrompts)
          Checks if you are at any of the valid prompts.
 void endCommand(boolean finished)
          Called by the framework when the command is finished.
 java.lang.Object execInstance()
          Executes a command instance.
 java.lang.String getClassName()
          Gets the command class name.
 java.lang.String getName()
          Called by the framework to get the name of the command.
 java.util.Enumeration getParams()
          Gets the command parameters.
 java.net.URL getResource(java.lang.String resource)
          Finds a resource with a given name.
 boolean initCommand()
          Called by the framework to initialise the command.
 void initInstance()
          Initialises a command instance.
 boolean isInternal()
          Indicates if this is an internal SWATH command or not.
 boolean isUserDefined()
          Indicates if this command is user defined or not.
 void onEvent(EventIfc event)
          This method is called for each posted event.
 void onText(java.lang.String buffer, java.lang.String text)
          This method is called when new text has arrived from the game server.
 void postEvent(UserDefinedEvent event)
          This method posts an event in the SWATH event queue.
 void printTrace(java.lang.Exception e)
          Print an exception and the complete stack trace in the SWATH console window.
 void printTrace(java.lang.String text)
          Print trace text in the SWATH console window.
 void registerParam(Parameter param)
          This method is used to register a command parameter.
 void sendString(java.lang.String str)
          Sends a string to the TW server.
 void setBufferText(java.lang.String text)
          Sets the current text buffer content.
 void setResult(java.lang.Object result)
          Sets the result value that will be returned when the command is finished.
 void skipBufferText(int count)
          Removes characters from the current text buffer.
 void startCommand()
          Called by the framework to start executing the command.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

RETURN_KEY

public static final java.lang.String RETURN_KEY
The return key character (0x0D).

See Also:
Constant Field Values
Constructor Detail

UserDefinedCommand

public UserDefinedCommand()
Constructor.

Method Detail

isUserDefined

public final boolean isUserDefined()
Description copied from interface: CommandIfc
Indicates if this command is user defined or not.

Specified by:
isUserDefined in interface CommandIfc
Returns:
true if user defined, otherwise false.

isInternal

public final boolean isInternal()
Description copied from interface: CommandIfc
Indicates if this is an internal SWATH command or not.

Specified by:
isInternal in interface CommandIfc
Returns:
true if internal, otherwise false.

getClassName

public final java.lang.String getClassName()
Description copied from interface: CommandIfc
Gets the command class name.

Specified by:
getClassName in interface CommandIfc
Returns:
The class name.

registerParam

public final void registerParam(Parameter param)
                         throws CommandException
This method is used to register a command parameter.

Parameters:
param - The parameter to register.
Throws:
CommandException - If no more parameters could be added.
Since:
SWATH 1.7

getParams

public final java.util.Enumeration getParams()
Gets the command parameters.

Specified by:
getParams in interface CommandIfc
Returns:
The command parameters.
Since:
SWATH 1.7

initInstance

public final void initInstance()
                        throws CommandException,
                               java.lang.Exception
Initialises a command instance.
Should only be called from static exec() methods.

Throws:
CommandException - If the command initialisation fails for some reason.
java.lang.Exception
Since:
SWATH 1.7
See Also:
execInstance()

execInstance

public final java.lang.Object execInstance()
                                    throws CommandException,
                                           java.lang.Exception
Executes a command instance.
Should only be called from static exec() methods.

Returns:
The command return value.
Throws:
CommandException - If the command execution fails for some reason.
java.lang.Exception
Since:
SWATH 1.7
See Also:
initInstance()

skipBufferText

public final void skipBufferText(int count)
Removes characters from the current text buffer.

Parameters:
count - Number of characters to remove.
Since:
SWATH 1.7
See Also:
setBufferText(java.lang.String), onText(java.lang.String, java.lang.String)

setBufferText

public final void setBufferText(java.lang.String text)
Sets the current text buffer content.

Parameters:
text - The new content.
Since:
SWATH 1.7
See Also:
skipBufferText(int), onText(java.lang.String, java.lang.String)

setResult

public final void setResult(java.lang.Object result)
Sets the result value that will be returned when the command is finished.

Parameters:
result - The result.
Since:
SWATH 1.7
See Also:
execInstance()

atPrompt

public final boolean atPrompt(int validPrompts)
                       throws java.lang.Exception
Checks if you are at any of the valid prompts.

Parameters:
validPrompts - The valid prompts. (Combine several prompts with logical OR)
Returns:
true if you are at a valid prompt, otherwise false.
Throws:
java.lang.Exception
Since:
SWATH 1.7
See Also:
Swath.NO_PROMPT, Swath.COMMAND_PROMPT, Swath.COMPUTER_PROMPT, Swath.PLANET_PROMPT, Swath.CITADEL_PROMPT, Swath.CORP_PROMPT, Swath.STARDOCK_PROMPT, Swath.ALL_PROMPTS

getResource

public final java.net.URL getResource(java.lang.String resource)
Finds a resource with a given name. The search is done using the class path.

Parameters:
resource - The name of the wanted resource.
Returns:
The URL to the wanted resource or null if not found.
Since:
SWATH 1.7

sendString

public final void sendString(java.lang.String str)
                      throws java.lang.Exception
Sends a string to the TW server.

Parameters:
str - The string to send.
Throws:
java.lang.Exception
Since:
SWATH 1.7
See Also:
RETURN_KEY

postEvent

public final void postEvent(UserDefinedEvent event)
This method posts an event in the SWATH event queue. Everyone that listens for this event will be notified.

Parameters:
event - The event to post.
Since:
SWATH 1.7

printTrace

public final void printTrace(java.lang.String text)
                      throws java.lang.Exception
Print trace text in the SWATH console window.

Parameters:
text - The text to print.
Throws:
java.lang.Exception
Since:
SWATH 1.7

printTrace

public final void printTrace(java.lang.Exception e)
                      throws java.lang.Exception
Print an exception and the complete stack trace in the SWATH console window.

Parameters:
e - The exception to print.
Throws:
java.lang.Exception
Since:
SWATH 1.7

getName

public java.lang.String getName()
Called by the framework to get the name of the command.
You should override this method.

Specified by:
getName in interface CommandIfc
Returns:
The command name.
Since:
SWATH 1.7

initCommand

public boolean initCommand()
                    throws java.lang.Exception
Called by the framework to initialise the command.
You should override this method to make your own initialisation.

Returns:
true if the initialisation was successful, otherwise false.
Throws:
java.lang.Exception
Since:
SWATH 1.7
See Also:
atPrompt(int), printTrace(java.lang.String), setResult(java.lang.Object)

startCommand

public void startCommand()
                  throws java.lang.Exception
Called by the framework to start executing the command.
You should override this method.

Normally you send some text to the TW server and then handle all incoming text and events using the methods onText and onEvent.

Throws:
java.lang.Exception
Since:
SWATH 1.7
See Also:
sendString(java.lang.String), postEvent(com.swath.UserDefinedEvent), printTrace(java.lang.String), setResult(java.lang.Object), onText(java.lang.String, java.lang.String), onEvent(com.swath.EventIfc)

endCommand

public void endCommand(boolean finished)
                throws java.lang.Exception
Called by the framework when the command is finished.
You can override this method to clean up after the command if needed.

Parameters:
finished - true if the command finished correctly or false if the command was interrupted in some way.
Throws:
java.lang.Exception
Since:
SWATH 1.7

onText

public void onText(java.lang.String buffer,
                   java.lang.String text)
            throws java.lang.Exception
This method is called when new text has arrived from the game server.
You can override this method to scan for incoming text.

The current text buffer contains both the previous text and the new text that just arrived. You can use it when searching for information using the string helper methods. To remove text from the current buffer or reset its content, use the skipBufferText and setBufferText methods.

Parameters:
buffer - The current text buffer.
text - The new incoming text.
Throws:
java.lang.Exception
Since:
SWATH 1.7
See Also:
skipBufferText(int), setBufferText(java.lang.String), sendString(java.lang.String), postEvent(com.swath.UserDefinedEvent), printTrace(java.lang.String), setResult(java.lang.Object)

onEvent

public void onEvent(EventIfc event)
             throws java.lang.Exception
This method is called for each posted event.
You can override this method to handle events.

Parameters:
event - The event.
Throws:
java.lang.Exception
Since:
SWATH 1.7
See Also:
sendString(java.lang.String), postEvent(com.swath.UserDefinedEvent), printTrace(java.lang.String), setResult(java.lang.Object)