net.kolls.railworld.play.script
Class ScriptManager

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractList<E>
          extended by java.util.ArrayList<Script>
              extended by net.kolls.railworld.play.script.ScriptManager
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.lang.Iterable<Script>, java.util.Collection<Script>, java.util.List<Script>, java.util.RandomAccess

public class ScriptManager
extends java.util.ArrayList<Script>

The "master" script access point. Handles loading scripts and will also dispatch calls to all scripts within it.

Author:
Steve Kollmansberger
See Also:
Serialized Form

Field Summary
protected  java.util.ArrayList<DrawListener> drawables
          All drawing listeners
protected  java.util.ArrayList<TalCondition> tals
          All train listeners
 
Fields inherited from class java.util.AbstractList
modCount
 
Constructor Summary
ScriptManager()
          Create an empty script manager.
 
Method Summary
 void addDrawListener(DrawListener d)
          Add a listener to be notified when the canvas is drawn.
 void addTrainActionListener(TrainActionListener tal, Train t, java.lang.String event)
          Add a train action listener in the form of a condition.
static ScriptManager allScripts(java.io.File loc)
          Return a ScriptManager containing all internal scripts as well as those found in the given directory.
 void draw(java.awt.Graphics2D gc, java.awt.Rectangle onScreen)
          The canvas is being drawn.
static ScriptManager getScripts(java.io.File loc)
          Return a ScriptManager containing all scripts found in the given directory.
 void init(PlayFrame pf)
          The game is being started.
static Script loadScript(java.io.File script)
          Load a script from a File.
static Script loadScript(java.net.URL script)
          Load a script from a URL.
 boolean playFrameAction(java.lang.String s)
          An action has occurred in the PlayFrame.
 void removeDrawListener(DrawListener d)
          Remove a draw listener.
 void removeTrainActionListener(TrainActionListener tal, Train t, java.lang.String event)
          Remove a train action listener.
 boolean trainAction(Train t, java.lang.String s)
          A train has performed an action.
 void trainStep(Train t)
          A train has taken a "step".
 
Methods inherited from class java.util.ArrayList
add, add, addAll, addAll, clear, clone, contains, ensureCapacity, get, indexOf, isEmpty, lastIndexOf, remove, remove, removeRange, set, size, toArray, toArray, trimToSize
 
Methods inherited from class java.util.AbstractList
equals, hashCode, iterator, listIterator, listIterator, subList
 
Methods inherited from class java.util.AbstractCollection
containsAll, removeAll, retainAll, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.List
containsAll, equals, hashCode, iterator, listIterator, listIterator, removeAll, retainAll, subList
 

Field Detail

drawables

protected java.util.ArrayList<DrawListener> drawables
All drawing listeners


tals

protected java.util.ArrayList<TalCondition> tals
All train listeners

Constructor Detail

ScriptManager

public ScriptManager()
Create an empty script manager.

Method Detail

loadScript

public static Script loadScript(java.io.File script)
                         throws javax.script.ScriptException,
                                java.io.IOException
Load a script from a File. The script must be in the JavaScript language and implement the Script interface.

Parameters:
script - File of the script to load.
Returns:
The script instance
Throws:
javax.script.ScriptException - If the script file cannot be run or does not conform to the interface
java.io.IOException - If the script file cannot be accessed

loadScript

public static Script loadScript(java.net.URL script)
                         throws javax.script.ScriptException,
                                java.io.IOException
Load a script from a URL. The script must be in the JavaScript language and implement the Script interface.

Parameters:
script - URL of the script to load.
Returns:
The script instance
Throws:
javax.script.ScriptException - If the script file cannot be run or does not conform to the interface
java.io.IOException - If the script file cannot be accessed

getScripts

public static ScriptManager getScripts(java.io.File loc)
Return a ScriptManager containing all scripts found in the given directory. Does not include internal scripts.

Parameters:
loc - The directory to search.
Returns:
A ScriptManager containing any scripts found.

allScripts

public static ScriptManager allScripts(java.io.File loc)
Return a ScriptManager containing all internal scripts as well as those found in the given directory.

Parameters:
loc - Directory to search for additional scripts. May be null to indicate only internal scripts should be used.
Returns:
A new ScriptManager with the scripts.

addDrawListener

public void addDrawListener(DrawListener d)
Add a listener to be notified when the canvas is drawn.

Parameters:
d - A DrawListener to add.

removeDrawListener

public void removeDrawListener(DrawListener d)
Remove a draw listener.

Parameters:
d - The DrawListener to remove.

addTrainActionListener

public void addTrainActionListener(TrainActionListener tal,
                                   Train t,
                                   java.lang.String event)
Add a train action listener in the form of a condition.

Parameters:
tal - The train action listener.
t - The train to notify on, or null for all trains.
event - The event name to notify on.
See Also:
TalCondition

removeTrainActionListener

public void removeTrainActionListener(TrainActionListener tal,
                                      Train t,
                                      java.lang.String event)
Remove a train action listener. Will remove a listener that matches the three given values.

Parameters:
tal - Action listener
t - Train
event - Event name

draw

public void draw(java.awt.Graphics2D gc,
                 java.awt.Rectangle onScreen)
The canvas is being drawn. Notify all draw listeners.

Parameters:
gc - The canvas' graphics context
onScreen - The on-screen rectangle

init

public void init(PlayFrame pf)
The game is being started.

Parameters:
pf - The game's PlayFrame.

playFrameAction

public boolean playFrameAction(java.lang.String s)
An action has occurred in the PlayFrame.

Parameters:
s - The name of the event.
Returns:
Should the action be canceled? True means cancel, false means proceed.

trainAction

public boolean trainAction(Train t,
                           java.lang.String s)
A train has performed an action.

Parameters:
t - The train involved
s - Name of event
Returns:
True if the event should be canceled, False otherwise.

trainStep

public void trainStep(Train t)
               throws RailAccident
A train has taken a "step". This could be a regular trainAction except that this one can cause an accident.

Parameters:
t - The train that is being processed
Throws:
RailAccident - An accident, if one occurs. Note that this exception is not thrown by Java code but by the scripts, so that is why you don't see a throw anywhere here. It isn't checked so doesn't need to be declared.