Changeset 206 in tmcsimulator for trunk


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.

Location:
trunk/src/tmcsim/cadsimulator
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/tmcsim/cadsimulator/CADServer.java

    r188 r206  
    2121import tmcsim.cadsimulator.managers.SimulationClockManager; 
    2222import tmcsim.cadsimulator.managers.TrafficModelManager; 
     23import tmcsim.cadsimulator.managers.TrafficModelViewer; 
    2324import tmcsim.cadsimulator.viewer.model.CADSimulatorState; 
    2425import tmcsim.common.SimulationException; 
     
    177178     */ 
    178179    protected static TrafficModelManager theTrafficMgr = null; 
     180    private TrafficModelViewer trafficView = null; 
     181     
    179182    /** 
    180183     * Properties file for the CADSimulator. 
     
    256259                    CAD_PROPERTIES.TRAFFICMGR_PROP_FILE.name), 
    257260                    theCoordinator); 
     261            trafficView = new TrafficModelViewer(theTrafficMgr); 
     262             theTrafficMgr.addObserver(trafficView); 
    258263 
    259264            theMediaMgr = new MediaManager( 
     
    310315 
    311316        theViewer.setVisible(true); 
     317        trafficView.setVisible(true); 
     318 
    312319 
    313320    } 
  • 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) 
  • trunk/src/tmcsim/cadsimulator/managers/TrafficModelViewer.form

    r188 r206  
    5656          <Group type="103" groupAlignment="0" attributes="0"> 
    5757              <Group type="102" attributes="0"> 
    58                   <Group type="103" groupAlignment="0" attributes="0"> 
    59                       <Group type="102" attributes="0"> 
    60                           <EmptySpace min="-2" pref="112" max="-2" attributes="0"/> 
    61                           <Component id="txtClockTime" min="-2" pref="225" max="-2" attributes="0"/> 
    62                       </Group> 
    63                       <Group type="102" alignment="0" attributes="0"> 
    64                           <EmptySpace max="-2" attributes="0"/> 
    65                           <Component id="scrollEvents" min="-2" pref="430" max="-2" attributes="0"/> 
    66                       </Group> 
    67                   </Group> 
     58                  <EmptySpace max="-2" attributes="0"/> 
     59                  <Component id="scrollEvents" min="-2" pref="430" max="-2" attributes="0"/> 
    6860                  <EmptySpace pref="20" max="32767" attributes="0"/> 
     61              </Group> 
     62              <Group type="102" alignment="0" attributes="0"> 
     63                  <EmptySpace min="-2" pref="112" max="-2" attributes="0"/> 
     64                  <Component id="txtClockTime" min="-2" pref="225" max="-2" attributes="0"/> 
     65                  <EmptySpace max="32767" attributes="0"/> 
     66                  <Component id="btnReload" min="-2" max="-2" attributes="0"/> 
     67                  <EmptySpace min="-2" pref="32" max="-2" attributes="0"/> 
    6968              </Group> 
    7069          </Group> 
     
    7372          <Group type="103" groupAlignment="0" attributes="0"> 
    7473              <Group type="102" alignment="1" attributes="0"> 
    75                   <Component id="txtClockTime" min="-2" pref="28" max="-2" attributes="0"/> 
     74                  <Group type="103" groupAlignment="3" attributes="0"> 
     75                      <Component id="txtClockTime" alignment="3" min="-2" pref="28" max="-2" attributes="0"/> 
     76                      <Component id="btnReload" alignment="3" min="-2" max="-2" attributes="0"/> 
     77                  </Group> 
    7678                  <EmptySpace max="-2" attributes="0"/> 
    7779                  <Component id="scrollEvents" pref="172" max="32767" attributes="0"/> 
     
    114116            <Property name="text" type="java.lang.String" value="0:00:00"/> 
    115117          </Properties> 
     118        </Component> 
     119        <Component class="javax.swing.JButton" name="btnReload"> 
     120          <Properties> 
     121            <Property name="text" type="java.lang.String" value="Reload"/> 
     122          </Properties> 
     123          <Events> 
     124            <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="btnReloadActionPerformed"/> 
     125          </Events> 
    116126        </Component> 
    117127      </SubComponents> 
  • trunk/src/tmcsim/cadsimulator/managers/TrafficModelViewer.java

    r188 r206  
    77import java.util.LinkedList; 
    88import java.util.List; 
     9import java.util.Observable; 
     10import java.util.Observer; 
     11import java.util.Vector; 
    912import java.util.logging.Level; 
    1013import java.util.logging.Logger; 
     
    1619 * @author jdalbey 
    1720 */ 
    18 public class TrafficModelViewer extends javax.swing.JFrame 
     21public class TrafficModelViewer extends javax.swing.JFrame implements Observer 
    1922{ 
    2023 
     
    2326     */ 
    2427    private TrafficModelManager driver; 
    25     private final List<String> incidents; 
     28    private List<String> incidents; 
    2629 
    2730    /** 
     
    3033     * @param incidents a list of incidents to show in a list. 
    3134     */ 
    32     public TrafficModelViewer(TrafficModelManager driver, List<String> incidentList) 
     35    public TrafficModelViewer(TrafficModelManager driver) 
    3336    { 
    3437        initComponents(); 
    3538        this.driver = driver; 
    36         this.incidents = incidentList; 
    37         lstIncidents.setModel(new javax.swing.AbstractListModel<String>() 
    38         { 
    39             public int getSize() 
    40             { 
    41                 return incidents.size(); 
    42             } 
    43  
    44             public String getElementAt(int i) 
    45             { 
    46                 return incidents.get(i); 
    47             } 
    48         }); 
     39            txtClockTime.setText(driver.getClockTime()); 
     40            lstIncidents.setListData(driver.getIncidents()); 
     41            lstEvents.setModel(new MyListModel(driver.getEventQueue())); 
     42         
     43//        this.incidents = incidentList; 
     44//        lstIncidents.setModel(new javax.swing.AbstractListModel<String>() 
     45//        { 
     46//            public int getSize() 
     47//            { 
     48//                return incidents.size(); 
     49//            } 
     50// 
     51//            public String getElementAt(int i) 
     52//            { 
     53//                return incidents.get(i); 
     54//            } 
     55//        }); 
    4956    } 
    5057 
    51     public void update(String currentTime, String atmsTime, List<TrafficEvent> events) 
    52     { 
    53         lstEvents.setModel(new MyListModel(events)); 
    54         txtClockTime.setText(currentTime + " / " + atmsTime); 
     58//    public void update(String currentTime, String atmsTime, List<TrafficEvent> events) 
     59//    { 
     60//        lstEvents.setModel(new MyListModel(events)); 
     61//        txtClockTime.setText(currentTime + " / " + atmsTime); 
     62//    } 
     63 
     64    @Override 
     65    public void update(Observable obs, Object obj) 
     66    { 
     67        if (obj == null) return; 
     68        if (obj instanceof String) 
     69        { 
     70            String currentTime = (String) obj; 
     71            txtClockTime.setText(currentTime); 
     72        } 
     73        if (obj instanceof Vector) 
     74        { 
     75            lstIncidents.setListData((Vector)obj); 
     76        } 
     77        if (obj instanceof LinkedList) 
     78        { 
     79            lstEvents.setModel(new MyListModel((LinkedList)obj)); 
     80        } 
    5581    } 
    5682 
     
    90116        lstEvents = new javax.swing.JList<>(); 
    91117        txtClockTime = new javax.swing.JLabel(); 
     118        btnReload = new javax.swing.JButton(); 
    92119        pnlIncidents = new javax.swing.JPanel(); 
    93120        jScrollPane1 = new javax.swing.JScrollPane(); 
     
    109136        txtClockTime.setFont(new java.awt.Font("Noto Sans", 1, 14)); // NOI18N 
    110137        txtClockTime.setText("0:00:00"); 
     138 
     139        btnReload.setText("Reload"); 
     140        btnReload.addActionListener(new java.awt.event.ActionListener() 
     141        { 
     142            public void actionPerformed(java.awt.event.ActionEvent evt) 
     143            { 
     144                btnReloadActionPerformed(evt); 
     145            } 
     146        }); 
    111147 
    112148        javax.swing.GroupLayout pnlEventsLayout = new javax.swing.GroupLayout(pnlEvents); 
     
    115151            pnlEventsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
    116152            .addGroup(pnlEventsLayout.createSequentialGroup() 
    117                 .addGroup(pnlEventsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
    118                     .addGroup(pnlEventsLayout.createSequentialGroup() 
    119                         .addGap(112, 112, 112) 
    120                         .addComponent(txtClockTime, javax.swing.GroupLayout.PREFERRED_SIZE, 225, javax.swing.GroupLayout.PREFERRED_SIZE)) 
    121                     .addGroup(pnlEventsLayout.createSequentialGroup() 
    122                         .addContainerGap() 
    123                         .addComponent(scrollEvents, javax.swing.GroupLayout.PREFERRED_SIZE, 430, javax.swing.GroupLayout.PREFERRED_SIZE))) 
     153                .addContainerGap() 
     154                .addComponent(scrollEvents, javax.swing.GroupLayout.PREFERRED_SIZE, 430, javax.swing.GroupLayout.PREFERRED_SIZE) 
    124155                .addContainerGap(20, Short.MAX_VALUE)) 
     156            .addGroup(pnlEventsLayout.createSequentialGroup() 
     157                .addGap(112, 112, 112) 
     158                .addComponent(txtClockTime, javax.swing.GroupLayout.PREFERRED_SIZE, 225, javax.swing.GroupLayout.PREFERRED_SIZE) 
     159                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
     160                .addComponent(btnReload) 
     161                .addGap(32, 32, 32)) 
    125162        ); 
    126163        pnlEventsLayout.setVerticalGroup( 
    127164            pnlEventsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
    128165            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlEventsLayout.createSequentialGroup() 
    129                 .addComponent(txtClockTime, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE) 
     166                .addGroup(pnlEventsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
     167                    .addComponent(txtClockTime, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE) 
     168                    .addComponent(btnReload)) 
    130169                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
    131170                .addComponent(scrollEvents, javax.swing.GroupLayout.DEFAULT_SIZE, 172, Short.MAX_VALUE) 
     
    213252            lstIncidents.clearSelection(); 
    214253            // Remove selectedItem from list model; 
    215             incidents.remove(selectedItem); 
     254//            incidents.remove(selectedItem); 
    216255            // Update the list displayed 
    217             String[] array = new String[incidents.size()]; 
    218             array = incidents.toArray(array); 
    219             lstIncidents.setListData(array); 
     256//            String[] array = new String[incidents.size()]; 
     257//            array = incidents.toArray(array); 
     258//            lstIncidents.setListData(array); 
    220259        } 
    221260    }//GEN-LAST:event_btnClearActionPerformed 
    222261 
     262    private void btnReloadActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnReloadActionPerformed 
     263    {//GEN-HEADEREND:event_btnReloadActionPerformed 
     264        String message = "Reload will delete all events in the queue and reload the events file."; 
     265        String title = "Please Confirm"; 
     266        // display the JOptionPane showConfirmDialog 
     267        int reply = JOptionPane.showConfirmDialog(this, message, title, JOptionPane.YES_NO_OPTION); 
     268        if (reply == JOptionPane.YES_OPTION) 
     269        { 
     270            driver.loadEvents(); 
     271        } 
     272    }//GEN-LAST:event_btnReloadActionPerformed 
     273     
    223274    /** 
    224275     * @param args the command line arguments 
     
    279330                    items.add("180"); 
    280331                    items.add("1291"); 
    281                     TrafficModelViewer view = new TrafficModelViewer(null, items); 
     332                    TrafficModelViewer view = new TrafficModelViewer(null); 
    282333                    view.setVisible(true); 
    283                     view.update("01", "02", sample); 
     334                    //view.update("01", "02", sample); 
    284335                } 
    285336                catch (ParseException ex) 
     
    294345    // Variables declaration - do not modify//GEN-BEGIN:variables 
    295346    private javax.swing.JButton btnClear; 
     347    private javax.swing.JButton btnReload; 
    296348    private javax.swing.JScrollPane jScrollPane1; 
    297349    private javax.swing.JList<String> lstEvents; 
Note: See TracChangeset for help on using the changeset viewer.