Index: trunk/src/tmcsim/client/ATMSBatchDriver.java
===================================================================
--- trunk/src/tmcsim/client/ATMSBatchDriver.java	(revision 180)
+++ trunk/src/tmcsim/client/ATMSBatchDriver.java	(revision 183)
@@ -1,9 +1,7 @@
 package tmcsim.client;
 
-import atmsdriver.ATMSDriver;
 import atmsdriver.ConsoleTrafficDriver;
-import atmsdriver.ExchangeInfo;
 import atmsdriver.model.Highways;
-import atmsdriver.model.Station;
+import atmsdriver.model.TrafficEvent;
 import atmsdriver.model.LoopDetector.DOTCOLOR;
 import java.awt.event.ActionEvent;
@@ -17,12 +15,11 @@
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.Date;
 import java.util.HashMap;
-import java.util.InputMismatchException;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.Properties;
-import java.util.Queue;
 import java.util.Scanner;
 import java.util.concurrent.TimeUnit;
@@ -33,5 +30,4 @@
 import javax.swing.Timer;
 import javax.swing.UIManager;
-import tmcsim.cadsimulator.CADServer;
 import tmcsim.common.SimulationException;
 import tmcsim.interfaces.CADClientInterface;
@@ -39,14 +35,11 @@
 
 /**
- * Skeleton for ATMS Driver that reads a "batch" file of highway
- * status update commands. 
- * It operates as a client of the
- * CAD server, using RMI to poll the server every second for the current
- * simulation clock time.  It uses the simulation clock time
- * to fire update commands at the desired time.
- * Note: Sim Mgr must be running before starting this application.
- * TODO: We probably want to be able to "override" a command, to force 
- * clearing an incident.
-
+ * Skeleton for ATMS Driver that reads a "batch" file of highway status update
+ * commands. It operates as a client of the CAD server, using RMI to poll the
+ * server every second for the current simulation clock time. It uses the
+ * simulation clock time to fire update commands at the desired time. Note: Sim
+ * Mgr must be running before starting this application. TODO: We probably want
+ * to be able to "override" a command, to force clearing an incident.
+ *
  * @author jdalbey
  */
@@ -54,4 +47,5 @@
         CADClientInterface
 {
+
     private static final String CONFIG_FILE_NAME = "cad_client_config.properties";
     private final static int ONE_SECOND = 1000;
@@ -79,7 +73,7 @@
     {
         CAD_SIM_HOST("CADSimulatorHost"), CAD_SIM_PORT("CADSimulatorSocketPort"), CAD_RMI_PORT(
-        "CADRmiPort"), CLIENT_CAD_POS("CADPosition"), CLIENT_USER_ID(
-        "CADUserID"), KEYBOARD_TYPE("KeyboardType"), DISPLAY_TYPE(
-        "DisplayType");
+                "CADRmiPort"), CLIENT_CAD_POS("CADPosition"), CLIENT_USER_ID(
+                "CADUserID"), KEYBOARD_TYPE("KeyboardType"), DISPLAY_TYPE(
+                "DisplayType");
         public String name;
 
@@ -112,20 +106,24 @@
      */
     final private Highways highways;
-    
-    /** 
-     * Queue of batch events
-     */
-    private Queue<String> eventQueue;
+
+    /**
+     * LinkedList of batch events
+     */
+    private LinkedList<TrafficEvent> eventQueue;
     /**
      * Map of incidents to events
      */
-    private Map<String, List<String>> incidents;
-    
-    /** Instance of ConsoleTrafficDriver that contains the highway model */
+    private Map<String, List<TrafficEvent>> incidents;
+
+    /**
+     * Instance of ConsoleTrafficDriver that contains the highway model
+     */
     private ConsoleTrafficDriver console;
-    
-    /** GUI for this driver */
+
+    /**
+     * GUI for this driver
+     */
     private ATMSBatchViewer theView;
-    
+
     /**
      * Constructor. Initialize data from parsed properties file. Create a socket
@@ -143,12 +141,12 @@
         }
         // Initialize the highway model
-        incidents = new HashMap<String, List<String>> ();
+        incidents = new HashMap<String, List<TrafficEvent>>();
         highways = new Highways(
-        "config/vds_data/lds.txt",
-        "config/vds_data/loop.txt",
-        "config/vds_data/highwaysMeta.txt",
-//        "192.168.251.46", 8080);  //IP address of FEP Sim Linux VM
-       "localhost", 8080);
-        
+                "config/vds_data/lds.txt",
+                "config/vds_data/loop.txt",
+                "config/vds_data/highwaysMeta.txt",
+                //        "192.168.251.46", 8080);  //IP address of FEP Sim Linux VM
+                "localhost", 8080);
+
         connect(cadClientProp.getProperty(PROPERTIES.CAD_SIM_HOST.name).trim(),
                 cadClientProp.getProperty(PROPERTIES.CAD_RMI_PORT.name).trim());
@@ -169,5 +167,5 @@
                 String currentClock = "";
                 String currentATMStime = "";
-                Date simClock = new Date();                
+                Date simClock = new Date();
                 // Obtain the simulation time from the CAD server
                 try
@@ -179,13 +177,17 @@
 //                    Date atmsdate = new Date(ATMStime);
 //                    currentATMStime = formatter.format(atmsdate);
-                    try {
+                    try
+                    {
                         simClock = formatter.parse(currentClock);
-                    } catch (ParseException ex) {
+                    }
+                    catch (ParseException ex)
+                    {
                         Logger.getLogger(ATMSBatchDriver.class.getName()).log(Level.SEVERE, null, ex);
                         System.out.println("Invalid simulation clock time found in ATMSDriverClient");
                         System.exit(-1);
-                    }                    
+                    }
                     //System.out.println("Current clock: " + currentClock);
-                } catch (RemoteException ex)
+                }
+                catch (RemoteException ex)
                 {
                     System.out.println("Remote Exception reading sim or ATMS clock time");
@@ -196,17 +198,6 @@
                 {
                     // Get the time to launch the next event
-                    String nextEvent = eventQueue.peek();
-//                    String eventTimeField = nextEvent.substring(0,8);
-                    Scanner evtScan = new Scanner(nextEvent);
-                    String inci = evtScan.next();      
-                    String eventTimeField = evtScan.next();
-                    Date eventTime = new Date();
-                    try {
-                        eventTime = formatter.parse(eventTimeField);
-                    } catch (ParseException ex) {
-                        Logger.getLogger(ATMSBatchDriver.class.getName()).log(Level.WARNING, null, ex);
-                        System.out.println("Unable to parse event time: " + nextEvent + " skipping.");
-                        eventQueue.remove();
-                    }
+                    TrafficEvent nextEvent = eventQueue.peek();
+                    Date eventTime = nextEvent.eventDate;
                     //System.out.println("Next event will be launched at: " + formatter.format(eventTime));
                     // Check the queue of events to see if the first
@@ -215,28 +206,12 @@
                     if (eventTime.before(simClock) || eventTime.equals(simClock))
                     {
-                        System.out.println("LAUNCHING EVENT at " + nextEvent );
-                        // Extract fields from event and prepare them 
-                        Scanner lineScan = new Scanner(nextEvent);
-                        try
-                        {
-                        lineScan.next(); // skip incident number field
-                        lineScan.next(); // skip time field
-                        int routeNumber = lineScan.nextInt();
-                        Station.DIRECTION dir = Station.DIRECTION.toDirection(lineScan.next());
-                        double postmile = lineScan.nextDouble();
-                        double range = lineScan.nextDouble();
-                        DOTCOLOR dotcolor = DOTCOLOR.toDotColor(lineScan.next());
+                        System.out.println("LAUNCHING EVENT: " + nextEvent.toString());
                         // apply colorization to highways
-                        highways.applyColorToHighwayStretch(routeNumber, dir, postmile, range, dotcolor);
+                        highways.applyColorToHighwayStretch(nextEvent.routeNumber, nextEvent.dir,
+                                nextEvent.postmile, nextEvent.range, nextEvent.color);
                         // Remove this event from the queue, we're done with it.
                         eventQueue.remove();
-                        }
-                        catch (InputMismatchException ex)
-                        {
-                            System.out.println("Wrong format data in batch event file: " + nextEvent + " \nskipping.");
-                            eventQueue.remove();
-                        }
-                    }
-                    
+                    }
+
                     theView.update(currentClock, currentATMStime, eventQueue);
                 }
@@ -255,7 +230,8 @@
     {
         FileInputStream fis;
-        try {
+        try
+        {
             fis = new FileInputStream("config/vds_data/atmsBatchEvents.txt");
-            eventQueue = new LinkedList<String>();
+            eventQueue = new LinkedList<TrafficEvent>();
             // Read all lines from the file of events
             Scanner scan = new Scanner(fis);
@@ -263,34 +239,49 @@
             {
                 // Read a line and add it to the event queue
-                String line = scan.nextLine();
+                String line = scan.nextLine().trim();
                 if (line.charAt(0) != '#')
                 {
-                    eventQueue.add(line);
-                    // Parse the incident from the line
-                    Scanner lineScan = new Scanner(line);
-                    String incident = lineScan.next();
-                    // Add the line to the list for the corresponding incident
-                    List evtList;
-                    if (incidents.containsKey(incident))
-                    {
-                        evtList = incidents.get(incident);
-                    }
-                    else
-                    {
-                        evtList = new ArrayList<String>();
-                    }               
-                    evtList.add(line);
-                    // and put it back in the map
-                    incidents.put(incident, evtList);
+                    TrafficEvent evt;
+                    try
+                    {
+                        evt = new TrafficEvent(line);
+                        eventQueue.add(evt);
+                        String incident = evt.incident;
+                        // Add the line to the list for the corresponding incident
+                        List evtList;
+                        if (incidents.containsKey(evt.incident))
+                        {
+                            evtList = incidents.get(evt.incident);
+                        }
+                        else
+                        {
+                            evtList = new ArrayList<String>();
+                        }
+                        evtList.add(evt);
+                        // and put it back in the map
+                        incidents.put(incident, evtList);
+                    }
+                    catch (ParseException ex)
+                    {
+                        Logger.getLogger(ATMSBatchDriver.class.getName()).log(Level.SEVERE, null, ex);
+                        System.out.println("Wrong format data in batch event file: " + line + " \nskipping.");
+                        System.out.println("Skipping badly formatted event.");
+                    }
                 }
             }
-        } catch (FileNotFoundException ex) {
+        }
+        catch (FileNotFoundException ex)
+        {
             Logger.getLogger(ATMSBatchDriver.class.getName()).log(Level.SEVERE, null, ex);
         }
         System.out.println("Events file read, " + eventQueue.size() + " events queued.");
-    }
-    
-    /** Clear an incident.  For each event associated with an incident,
-     * turn the dots in its range Green and remove it from the event queue.
+        // Put the events in chronological order
+        Collections.sort(eventQueue);
+    }
+
+    /**
+     * Clear an incident. For each event associated with an incident, turn the
+     * dots in its range Green and remove it from the event queue.
+     *
      * @param incidentNumber incident to be cleared.
      */
@@ -298,5 +289,5 @@
     {
         boolean ok = incidents.containsKey(incidentNumber);
-        if (!ok) 
+        if (!ok)
         {
             System.out.println("Sorry, that incident number isn't found.");
@@ -304,33 +295,20 @@
         }
         System.out.println("Clearing incident " + incidentNumber);
-        List<String> events = incidents.get(incidentNumber);
+        List<TrafficEvent> events = incidents.get(incidentNumber);
         // Process each event associated with this incident 
-        for (String event: events)
+        for (TrafficEvent event : events)
         {
             System.out.println("Event: " + event + " cleared.");
             eventQueue.remove(event);
-            // Extract fields from event and prepare them 
-            Scanner lineScan = new Scanner(event);
-            try
-            {
-                lineScan.next(); // skip incident number field
-                lineScan.next(); // skip time field
-                int routeNumber = lineScan.nextInt();
-                Station.DIRECTION dir = Station.DIRECTION.toDirection(lineScan.next());
-                double postmile = lineScan.nextDouble();
-                double range = lineScan.nextDouble();
-                // apply colorization to highways, forcing to green, indicating cleared
-                highways.applyColorToHighwayStretch(routeNumber, dir, postmile, range, DOTCOLOR.GREEN);
-            }
-            catch (InputMismatchException ex)
-            {
-                System.out.println("Internal error, please report to programmers." + event);
-            }
-            
+
+            // apply colorization to highways, forcing to green, indicating cleared
+            highways.applyColorToHighwayStretch(event.routeNumber, event.dir,
+                    event.postmile, event.range, DOTCOLOR.GREEN);
+
         }
         // Now refresh the view with the updated queue of events
         theView.update("0:00", "0:00", eventQueue);
     }
-    
+
     /**
      * Connect to the Coordinator's RMI object, and register this object for
@@ -354,9 +332,10 @@
             theCoorInt = (CoordinatorInterface) Naming.lookup(coorIntURL);
             theCoorInt.registerForCallback(this);
-        } catch (Exception e)
+        }
+        catch (Exception e)
         {
             throw new SimulationException(SimulationException.CAD_SIM_CONNECT,
                     e);
-            
+
         }
     }
@@ -384,5 +363,6 @@
             cadClientProp = new Properties();
             cadClientProp.load(new FileInputStream(propertiesFile));
-        } catch (Exception e)
+        }
+        catch (Exception e)
         {
             cadClientLogger.logp(Level.SEVERE, "SimulationManager",
@@ -392,5 +372,4 @@
                     e);
         }
-
 
         // Ensure that the properties file does not have null values for the
@@ -430,5 +409,6 @@
                 {
                     theCoorInt.unregisterForCallback(client);
-                } catch (RemoteException e)
+                }
+                catch (RemoteException e)
                 {
                     e.printStackTrace();
@@ -456,5 +436,6 @@
             new ATMSBatchDriver(System.getProperty("CONFIG_DIR") + System.getProperty("file.separator") + CONFIG_FILE_NAME);
 
-        } catch (Exception e)
+        }
+        catch (Exception e)
         {
             cadClientLogger.logp(Level.SEVERE, "SimulationManager", "Main",
@@ -478,8 +459,10 @@
             while (true)
             {
-                try {
+                try
+                {
                     // Write the highway network status to the FEP Simulator
                     highways.writeToFEP();
-                } catch (SimulationException ex) 
+                }
+                catch (SimulationException ex)
                 {
                     // Ask user if they want to proceed without FEP Sim connection
@@ -487,5 +470,5 @@
                     if (reply == JOptionPane.NO_OPTION)
                     {
-                      System.exit(0);
+                        System.exit(0);
                     }
                     System.out.println("Skipping writeToFEP...");
