package tmcsim.cadsimulator.managers;
import java.io.File;
import java.io.FileInputStream;
import java.util.Observable;
import java.util.Observer;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JWindow;
import javax.xml.parsers.DocumentBuilderFactory;
import tmcsim.cadsimulator.db.DVDPlayerDB;
import tmcsim.cadsimulator.db.StillImagesDB;
import tmcsim.cadsimulator.stillimagecontrol.ImageController;
import tmcsim.cadsimulator.videocontrol.DVDController;
import tmcsim.cadsimulator.videocontrol.DVDStatusUpdate;
import tmcsim.cadsimulator.videocontrol.DVDTitleUpdate;
import tmcsim.cadsimulator.viewer.model.CADSimulatorState;
import tmcsim.common.CCTVDirections;
import tmcsim.common.CCTVInfo;
/**
* MediaManager manages the control of media associated with the speed
* information received from the ParamicsCommunicator. The MediaManager
* instantiates the DVDPlayerDB and StillImageDB objects, which are initialized
* from XML data files to hold the information regarding which DVD titles and
* Still Images are available for display. Each title or image is associated
* with a specific speed range or Incident log number. The triggerIncident()
* method is called to notify the MediaManager that incident media is to be
* displayed. The updateCameraInfo() method is called when speed information
* is received for a specific camera. In both methods, a Controller object
* is found for the unique camera ID number. This Controller is updated with
* the new speed data. If the update causes a change in media to be displayed,
* the change is made.
*
* The MediaManager implements the Observer interface and observes the
* DVDPlayerDB and StillImageDB for updates. These updates are received, action
* is taken, and then the update is passed to the CADSimulatorViewer for display.
*
* @see tmcsim.cadsimulator.viewer.CADSimulatorViewer
* @author Matthew Cechini
* @version
*/
public class MediaManager implements Observer {
/** Error Logger. */
private static Logger mediaLogger = Logger.getLogger("tmcsim.simulator");
/**
* Enumeration of property names.
* @author Matthew Cechini
*/
private static enum MEDIA_PROPERTIES {
/** Filepath for xml file containing dvd control data. */
DVD_XML_FILE ("DVDPlayerXML"),
/** Filepath for xml file containing still image control data. */
IMAGE_XML_FILE ("StillImagesXML");
public String name;
private MEDIA_PROPERTIES(String n) {
name = n;
}
};
/** Instance of the DVD Player Database. */
private DVDPlayerDB theDVD_DB = null;
/** Instance of the Still Image Database. */
private StillImagesDB theImage_DB = null;
/** Reference to the CADSimulatorModel. */
private CADSimulatorState theModel;
/** Properties object for the Media portion of the CAD. */
private Properties mediaProperties;
/**
* Constructor. The target properties file is loaded and the DVD and Still
* Image DB objects are initialized with the XML data files containing
* the respective data.
*
* @param propertiesFile File path for target properties file.
* @param theATMSManager ATMSManager object.
* @param viewer CADSimulatorViewer object.
*/
public MediaManager(String propertiesFile, ATMSManager theATMSManager,
CADSimulatorState model) {
theDVD_DB = new DVDPlayerDB();
theImage_DB = new StillImagesDB();
theModel = model;
try {
mediaProperties = new Properties();
mediaProperties.load(new FileInputStream(new File(propertiesFile)));
}
catch (Exception e) {
mediaLogger.logp(Level.SEVERE, "MediaManager", "Constructor",
"Exception in loading properties file.", e);
}
// Load DVD Player Information from the XML file
try {
if (mediaProperties.getProperty(MEDIA_PROPERTIES.DVD_XML_FILE.name) != null) {
theDVD_DB.addObserver(this);
theDVD_DB.loadFromXML(DocumentBuilderFactory
.newInstance().newDocumentBuilder()
.parse(new File(mediaProperties.getProperty(
MEDIA_PROPERTIES.DVD_XML_FILE.name))));
}
} catch (Exception e) {
mediaLogger.logp(Level.SEVERE, "MediaManager", "Constructor",
"Exception in parsing DVDPlayer xml file.", e);
JOptionPane.showMessageDialog(new JWindow(), "An error occured " +
"opening and parsing the DVD xml data file: " +
mediaProperties.getProperty(MEDIA_PROPERTIES.DVD_XML_FILE.name),
"Initialization Error", JOptionPane.WARNING_MESSAGE);
}
// Load Still Image Information from the XML file
try {
if (mediaProperties
.getProperty(MEDIA_PROPERTIES.IMAGE_XML_FILE.name) != null) {
theImage_DB.addObserver(this);
theImage_DB.loadFromXML(DocumentBuilderFactory
.newInstance().newDocumentBuilder()
.parse(new File(mediaProperties.getProperty(
MEDIA_PROPERTIES.IMAGE_XML_FILE.name))),
theATMSManager);
}
} catch (Exception e) {
mediaLogger.logp(Level.SEVERE, "MediaManager", "Constructor",
"Exception in parsing StillImages xml file.", e);
JOptionPane.showMessageDialog(new JWindow(), "An error occured " +
"opening and parsing the Image xml data file: " +
mediaProperties.getProperty(MEDIA_PROPERTIES.IMAGE_XML_FILE.name),
"Initialization Error", JOptionPane.WARNING_MESSAGE);
}
}
/**
* Observer method. The update objects accepted and their associated
* actions are shown below.
*
*