net.kolls.railworld.opening
Class MapLoader

java.lang.Object
  extended by net.kolls.railworld.opening.MapLoader

public abstract class MapLoader
extends java.lang.Object

A class that loads maps and their images, along with other supporting data. Includes functionality to prompt user to select a local file. Instances of this class are created using factory methods which specify how to get the map. The image won't be loaded right away. It will wait for the first call to getImage(). This allows uses of the map without the delay of loading the image if it is not needed.

Author:
Steve Kollmansberger

Constructor Summary
protected MapLoader(RailSegment[] la, MetaData mmd)
          Protected constructor.
 
Method Summary
abstract  java.awt.image.BufferedImage getImage()
          Loads the map image on the first call; caches it for subsequent calls.
 MetaData getMetaData()
           
 RailSegment[] getSegments()
           
abstract  MapLoader loadAgain()
          Loads another instance of this map from its original source (file or URL).
static MapLoader loadFromFile(java.io.File file)
          Loads the map from the given file.
static MapLoader loadFromURL(java.net.URL url)
          Loads the map from the given URL.
static MapLoader loadFromUserPrompt(ScriptManager scripts, java.io.File directory)
          Popup a file chooser dialog to allow the user to select a map file locally.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

MapLoader

protected MapLoader(RailSegment[] la,
                    MetaData mmd)
Protected constructor. To create an instance, need to use static factory methods depending on how to acquire the data. Values given will be loaded; script manager will be null.

Parameters:
la - RailSegments
mmd - MetaData
Method Detail

getSegments

public RailSegment[] getSegments()
Returns:
Return the segments associated with the loaded map.

getMetaData

public MetaData getMetaData()
Returns:
Return the metadata associated with the loaded map.

loadAgain

public abstract MapLoader loadAgain()
                             throws java.io.IOException,
                                    org.xml.sax.SAXException
Loads another instance of this map from its original source (file or URL). Returns the new MapLoader containing the fresh instance.

Returns:
A new MapLoader containing the freshly loaded data.
Throws:
java.io.IOException
org.xml.sax.SAXException

getImage

public abstract java.awt.image.BufferedImage getImage()
                                               throws java.io.IOException
Loads the map image on the first call; caches it for subsequent calls.

Returns:
The map image
Throws:
java.io.IOException - If the map image cannot be loaded.

loadFromUserPrompt

public static MapLoader loadFromUserPrompt(ScriptManager scripts,
                                           java.io.File directory)
                                    throws java.io.IOException,
                                           org.xml.sax.SAXException
Popup a file chooser dialog to allow the user to select a map file locally.

Parameters:
scripts - If provided, indicates that a ScriptPanel should be shown allowing the user to choose scripts. The given script manager (assumed to be empty when passed in; but will be cleared if not) will then be loaded with the selected scripts.
directory - Specifies the directory to start in. May be null.
Returns:
A new MapLoader with the user selected map if one was selected, null otherwise.
Throws:
java.io.IOException - If the map selected by the user cannot be loaded.
org.xml.sax.SAXException - If the map selected by the user cannot be parsed.

loadFromFile

public static MapLoader loadFromFile(java.io.File file)
                              throws java.io.IOException,
                                     org.xml.sax.SAXException
Loads the map from the given file. The map image must be in the same directory.

Parameters:
file - The file containing a compatible map to load, either in Rail World or Yard Duty format.
Returns:
The map loader containing the file data.
Throws:
java.io.IOException - If the map cannot be loaded.
org.xml.sax.SAXException - If the map cannot be parsed.

loadFromURL

public static MapLoader loadFromURL(java.net.URL url)
                             throws java.io.IOException,
                                    org.xml.sax.SAXException
Loads the map from the given URL. The map image must be in the same directory.

Parameters:
url - The URL pointing to a compatible map to load, in Rail World format. Yard Duty files cannot be loaded over URLs.
Returns:
The map loader containing the file data.
Throws:
java.io.IOException - If the map cannot be loaded.
org.xml.sax.SAXException - If the map cannot be parsed.

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object