Ignore:
Timestamp:
11/01/2017 12:31:34 PM (9 years ago)
Author:
jdalbey
Message:

TrafficModelManager?.java Refactor to use Observable, along with the view. Add Reload button functionality.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/tmcsim/cadsimulator/managers/TrafficModelManager.java

    r204 r206  
    1818import java.util.List; 
    1919import java.util.Map; 
     20import java.util.Observable; 
    2021import java.util.Properties; 
    2122import java.util.Scanner; 
     23import java.util.Vector; 
    2224import java.util.concurrent.TimeUnit; 
    2325import java.util.logging.Level; 
     
    3840 * @version 2.0 
    3941 */ 
    40 public class TrafficModelManager 
     42public class TrafficModelManager extends Observable 
    4143{ 
    4244    private final static int ONE_SECOND = 1000; 
     
    8991 
    9092    /** 
     93     * Current simulation clock time 
     94     */ 
     95    private String currentClock = ""; 
     96     
     97    /** 
    9198     * GUI for this driver 
    9299     */ 
    93     private TrafficModelViewer theView; 
     100    //private TrafficModelViewer theView; 
    94101     
    95102    /** 
     
    111118                props.getProperty(PROPERTIES.FEPSIM_IP_ADDR.name), 
    112119                8080);  
    113  
    114         // Read the text file of events and put in a queue 
    115         FileInputStream fis = null; 
    116         try 
    117         { 
    118             fis = new FileInputStream(props.getProperty(PROPERTIES.EVENTS_FILE.name)); 
    119         } catch (FileNotFoundException ex) 
    120         { 
    121             Logger.getLogger(TrafficModelManager.class.getName()).log(Level.SEVERE, null,  
    122                     "Missing Traffic Events file " + props.getProperty(PROPERTIES.EVENTS_FILE.name)); 
    123             System.exit(-1); 
    124         } 
    125         // Read all lines from the file of events 
    126         Scanner fileScanner = new Scanner(fis);         
    127         eventQueue = readBatchFile(fileScanner); 
    128         // Extract the incidents and create a map 
    129         incidents = createIncidentMap(eventQueue); 
    130         // Launch the GUI display 
    131         theView = new TrafficModelViewer(this, new ArrayList<String>(incidents.keySet())); 
    132         theView.setVisible(true); 
    133         theView.update("0:00", "1:11", eventQueue); 
    134  
     120         
     121        loadEvents(); 
     122         
    135123        // Create a timer that fetches the simulation time every second. 
    136124        Timer timer = new Timer(ONE_SECOND, new ActionListener() 
     
    139127            public void actionPerformed(ActionEvent e) 
    140128            { 
    141                 String currentClock = ""; 
    142129                String currentATMStime = ""; 
    143130                Date simClock = new Date(); 
     
    184171                        // Remove this event from the queue, we're done with it. 
    185172                        eventQueue.remove(); 
    186                     } 
    187  
    188                     theView.update(currentClock, currentATMStime, eventQueue); 
     173                        setChanged(); 
     174                        notifyObservers(getEventQueue()); 
     175                    } 
     176                    setChanged(); 
     177                    notifyObservers(currentClock); 
    189178                } 
    190179            } 
     
    205194         
    206195    } 
    207  
     196    /** Accessor to event queue 
     197     *  
     198     * @return defensive copy of current queue of events 
     199     */ 
     200    public LinkedList<TrafficEvent> getEventQueue() 
     201    { 
     202        return new LinkedList<TrafficEvent>(eventQueue); 
     203    } 
     204    public Vector<String> getIncidents() 
     205    { 
     206        return new Vector<String>(incidents.keySet()); 
     207    } 
     208    public String getClockTime() 
     209    { 
     210        return currentClock; 
     211    } 
     212    public void loadEvents() 
     213    { 
     214        // Read the text file of events and put in a queue 
     215        FileInputStream fis = null; 
     216        try 
     217        { 
     218            fis = new FileInputStream(props.getProperty(PROPERTIES.EVENTS_FILE.name)); 
     219        } catch (FileNotFoundException ex) 
     220        { 
     221            Logger.getLogger(TrafficModelManager.class.getName()).log(Level.SEVERE, null,  
     222                    "Missing Traffic Events file " + props.getProperty(PROPERTIES.EVENTS_FILE.name)); 
     223            System.exit(-1); 
     224        } 
     225        // Read all lines from the file of events 
     226        Scanner fileScanner = new Scanner(fis);         
     227        eventQueue = readBatchFile(fileScanner); 
     228        // Extract the incidents and create a map 
     229        incidents = createIncidentMap(eventQueue); 
     230        setChanged(); 
     231        notifyObservers("-:--"); 
     232        setChanged(); 
     233        notifyObservers(getIncidents()); 
     234        setChanged(); 
     235        notifyObservers(getEventQueue()); 
     236    } 
    208237    /** 
    209238     * This method verifies that the needed configuration properties are not 
     
    327356 
    328357        } 
     358        incidents.remove(incidentNumber); 
    329359        // Now refresh the view with the updated queue of events 
    330         theView.update("0:00", "0:00", eventQueue); 
     360        setChanged(); 
     361        notifyObservers(getEventQueue()); 
     362        setChanged(); 
     363        notifyObservers(getIncidents()); 
    331364    } 
    332365 
     
    363396        { 
    364397            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
    365             new TrafficModelManager(System.getProperty("CONFIG_DIR") + System.getProperty("file.separator") + CONFIG_FILE_NAME, 
     398            TrafficModelManager mgr = new TrafficModelManager(System.getProperty("CONFIG_DIR") + System.getProperty("file.separator") + CONFIG_FILE_NAME,                     
    366399            theCoordinator); 
    367  
     400            TrafficModelViewer view = new TrafficModelViewer(mgr); 
     401            view.setVisible(true); 
     402            mgr.addObserver(view); 
    368403        } 
    369404        catch (Exception e) 
Note: See TracChangeset for help on using the changeset viewer.