import javax.swing.DefaultListModel; import java.util.*; /** * Project #8.6 * CS 2334, Section 001 * October 12, 2003 *
* This class models a list of states. We will use this * as our data model for holding a list of states. *
* @author unknown * @version 1.0 */ public class StateListModel { /** A list that holds the states. */ private ArrayList states; /** * Default constructor. This creates an empty list. */ public StateListModel() { states = new ArrayList(); } /** * Returns the list as a vector. ** @return The list as a vector of states. */ public Vector getStatesVector() { Vector temp = new Vector(); ListIterator statesIterator = states.listIterator(); while( statesIterator.hasNext() ) { temp.add(statesIterator.next()); } return temp; } /** * Returns the list as an ArrayList. This is the data model used * for the list. *
* @return The list as an ArrayList of states. */ public ArrayList getStatesModel() { return states; } /** * Removes a state from the list. *
* @param index The index of the state to remove. */ public void removeState(int index) { if( index > -1 ) { states.remove(index); } } /** * Returns a reference to the specified state. *
* @return A reference to the state object stored
* in position index
. Returns
* null if the index is < 0.
*/
public State getState(int index)
{
if( index > -1 )
{
return (State)states.get(index);
}
else
{
return null;
}
}
/**
* Adds a state to the list.
*
* @param State Reference to the state to add to the list. */ public void addState(State newState) { states.add(newState); sort(); } /** * Sort the list. */ private void sort() { Collections.sort(states); } }