net.kolls.railworld.play
Class Trains

java.lang.Object
  extended by net.kolls.railworld.play.Trains
All Implemented Interfaces:
java.lang.Iterable<Train>, javax.swing.ListModel

public class Trains
extends java.lang.Object
implements javax.swing.ListModel, java.lang.Iterable<Train>

The list of trains. Includes behavior for trains and train processing.

Author:
Steve Kollmansberger

Constructor Summary
Trains(javax.swing.JPanel tchr, javax.swing.JList tl)
          Create a train list with a particular controller panel and a visual list.
 
Method Summary
 void add(int index, Train t)
          Add a new train to the collection at the specified index.
 void add(Train t)
          Add a new a train to the collection.
 void addListDataListener(javax.swing.event.ListDataListener l)
           
 Train get(int index)
          Get a train
 java.lang.Object getElementAt(int index)
           
 Car getSelectedCar()
           
 Train getSelectedTrain()
           
 int getSize()
           
 java.util.Iterator<Train> iterator()
           
 Train join(Train t1, Train t2)
          Joins two trains.
 void refreshList()
          Inform the list listeners that the entire list may have changed.
 void remove(int index)
          Remove a train
 void remove(Train t)
          Remove a given train from the map.
 void removeListDataListener(javax.swing.event.ListDataListener l)
           
 Train reverse(Train t)
          Reverse the direction of a given train.
 void select(Train t, Car c)
          Select a particular train, and optionally, a particular car.
 int size()
          How many trains are being managed.
 Train split(Train t)
          Splits a given train based on the selected car.
 void step()
          Process all train behavior for one step.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Trains

public Trains(javax.swing.JPanel tchr,
              javax.swing.JList tl)
Create a train list with a particular controller panel and a visual list.

Parameters:
tchr - A JPanel to display the selected train's controller.
tl - A JList to display the list of trains.
Method Detail

getSelectedTrain

public Train getSelectedTrain()
Returns:
The selected Train, if any.

getSelectedCar

public Car getSelectedCar()
Returns:
The selected Car within the selected train, if any.

select

public void select(Train t,
                   Car c)
Select a particular train, and optionally, a particular car. If both are null, this means "deselect" whatever is selected.

Parameters:
t - The Train to select. May be null.
c - The Car to select. If null, the controller may select the first car in the train by default.

reverse

public Train reverse(Train t)
Reverse the direction of a given train. Modifies the trains list AND returns the new train.

Parameters:
t - The train to reverse. This train will be removed from the list
Returns:
The new train added to the list.

split

public Train split(Train t)
Splits a given train based on the selected car. All cars before the selected car go in one train, all remaining cars in the other.

Parameters:
t - The train to split.
Returns:
The primary (selected) train of the two resultant trains.

join

public Train join(Train t1,
                  Train t2)
Joins two trains. The two trains are removed from the list and a new train is added.

Parameters:
t1 -
t2 -
Returns:
The new train consisting of all cars of the both trains.

step

public void step()
          throws RailAccident
Process all train behavior for one step. Perform joins, splits, and reverses as requested. Move trains. Detect accidents.

Throws:
RailAccident - If an accident occurs.

addListDataListener

public void addListDataListener(javax.swing.event.ListDataListener l)
Specified by:
addListDataListener in interface javax.swing.ListModel

getElementAt

public java.lang.Object getElementAt(int index)
Specified by:
getElementAt in interface javax.swing.ListModel

getSize

public int getSize()
Specified by:
getSize in interface javax.swing.ListModel

removeListDataListener

public void removeListDataListener(javax.swing.event.ListDataListener l)
Specified by:
removeListDataListener in interface javax.swing.ListModel

add

public void add(Train t)
Add a new a train to the collection.

Parameters:
t - The train to add.

add

public void add(int index,
                Train t)
Add a new train to the collection at the specified index.

Parameters:
index - Index to add train at
t - The train to add

get

public Train get(int index)
Get a train

Parameters:
index - The index of the train to get
Returns:
The train at that index

remove

public void remove(int index)
Remove a train

Parameters:
index - The index of the train to remove

remove

public void remove(Train t)
Remove a given train from the map.

Parameters:
t - Train to remove.

size

public int size()
How many trains are being managed.

Returns:
The number of trains

iterator

public java.util.Iterator<Train> iterator()
Specified by:
iterator in interface java.lang.Iterable<Train>

refreshList

public void refreshList()
Inform the list listeners that the entire list may have changed.