Changeset 206 in tmcsimulator for trunk/src/tmcsim/cadsimulator/managers
- Timestamp:
- 11/01/2017 12:31:34 PM (9 years ago)
- Location:
- trunk/src/tmcsim/cadsimulator/managers
- Files:
-
- 3 edited
-
TrafficModelManager.java (modified) (9 diffs)
-
TrafficModelViewer.form (modified) (3 diffs)
-
TrafficModelViewer.java (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/tmcsim/cadsimulator/managers/TrafficModelManager.java
r204 r206 18 18 import java.util.List; 19 19 import java.util.Map; 20 import java.util.Observable; 20 21 import java.util.Properties; 21 22 import java.util.Scanner; 23 import java.util.Vector; 22 24 import java.util.concurrent.TimeUnit; 23 25 import java.util.logging.Level; … … 38 40 * @version 2.0 39 41 */ 40 public class TrafficModelManager 42 public class TrafficModelManager extends Observable 41 43 { 42 44 private final static int ONE_SECOND = 1000; … … 89 91 90 92 /** 93 * Current simulation clock time 94 */ 95 private String currentClock = ""; 96 97 /** 91 98 * GUI for this driver 92 99 */ 93 private TrafficModelViewer theView;100 //private TrafficModelViewer theView; 94 101 95 102 /** … … 111 118 props.getProperty(PROPERTIES.FEPSIM_IP_ADDR.name), 112 119 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 135 123 // Create a timer that fetches the simulation time every second. 136 124 Timer timer = new Timer(ONE_SECOND, new ActionListener() … … 139 127 public void actionPerformed(ActionEvent e) 140 128 { 141 String currentClock = "";142 129 String currentATMStime = ""; 143 130 Date simClock = new Date(); … … 184 171 // Remove this event from the queue, we're done with it. 185 172 eventQueue.remove(); 186 } 187 188 theView.update(currentClock, currentATMStime, eventQueue); 173 setChanged(); 174 notifyObservers(getEventQueue()); 175 } 176 setChanged(); 177 notifyObservers(currentClock); 189 178 } 190 179 } … … 205 194 206 195 } 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 } 208 237 /** 209 238 * This method verifies that the needed configuration properties are not … … 327 356 328 357 } 358 incidents.remove(incidentNumber); 329 359 // 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()); 331 364 } 332 365 … … 363 396 { 364 397 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, 366 399 theCoordinator); 367 400 TrafficModelViewer view = new TrafficModelViewer(mgr); 401 view.setVisible(true); 402 mgr.addObserver(view); 368 403 } 369 404 catch (Exception e) -
trunk/src/tmcsim/cadsimulator/managers/TrafficModelViewer.form
r188 r206 56 56 <Group type="103" groupAlignment="0" attributes="0"> 57 57 <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"/> 68 60 <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"/> 69 68 </Group> 70 69 </Group> … … 73 72 <Group type="103" groupAlignment="0" attributes="0"> 74 73 <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> 76 78 <EmptySpace max="-2" attributes="0"/> 77 79 <Component id="scrollEvents" pref="172" max="32767" attributes="0"/> … … 114 116 <Property name="text" type="java.lang.String" value="0:00:00"/> 115 117 </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> 116 126 </Component> 117 127 </SubComponents> -
trunk/src/tmcsim/cadsimulator/managers/TrafficModelViewer.java
r188 r206 7 7 import java.util.LinkedList; 8 8 import java.util.List; 9 import java.util.Observable; 10 import java.util.Observer; 11 import java.util.Vector; 9 12 import java.util.logging.Level; 10 13 import java.util.logging.Logger; … … 16 19 * @author jdalbey 17 20 */ 18 public class TrafficModelViewer extends javax.swing.JFrame 21 public class TrafficModelViewer extends javax.swing.JFrame implements Observer 19 22 { 20 23 … … 23 26 */ 24 27 private TrafficModelManager driver; 25 private finalList<String> incidents;28 private List<String> incidents; 26 29 27 30 /** … … 30 33 * @param incidents a list of incidents to show in a list. 31 34 */ 32 public TrafficModelViewer(TrafficModelManager driver , List<String> incidentList)35 public TrafficModelViewer(TrafficModelManager driver) 33 36 { 34 37 initComponents(); 35 38 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 // }); 49 56 } 50 57 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 } 55 81 } 56 82 … … 90 116 lstEvents = new javax.swing.JList<>(); 91 117 txtClockTime = new javax.swing.JLabel(); 118 btnReload = new javax.swing.JButton(); 92 119 pnlIncidents = new javax.swing.JPanel(); 93 120 jScrollPane1 = new javax.swing.JScrollPane(); … … 109 136 txtClockTime.setFont(new java.awt.Font("Noto Sans", 1, 14)); // NOI18N 110 137 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 }); 111 147 112 148 javax.swing.GroupLayout pnlEventsLayout = new javax.swing.GroupLayout(pnlEvents); … … 115 151 pnlEventsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 116 152 .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) 124 155 .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)) 125 162 ); 126 163 pnlEventsLayout.setVerticalGroup( 127 164 pnlEventsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 128 165 .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)) 130 169 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 131 170 .addComponent(scrollEvents, javax.swing.GroupLayout.DEFAULT_SIZE, 172, Short.MAX_VALUE) … … 213 252 lstIncidents.clearSelection(); 214 253 // Remove selectedItem from list model; 215 incidents.remove(selectedItem);254 // incidents.remove(selectedItem); 216 255 // 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); 220 259 } 221 260 }//GEN-LAST:event_btnClearActionPerformed 222 261 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 223 274 /** 224 275 * @param args the command line arguments … … 279 330 items.add("180"); 280 331 items.add("1291"); 281 TrafficModelViewer view = new TrafficModelViewer(null , items);332 TrafficModelViewer view = new TrafficModelViewer(null); 282 333 view.setVisible(true); 283 view.update("01", "02", sample);334 //view.update("01", "02", sample); 284 335 } 285 336 catch (ParseException ex) … … 294 345 // Variables declaration - do not modify//GEN-BEGIN:variables 295 346 private javax.swing.JButton btnClear; 347 private javax.swing.JButton btnReload; 296 348 private javax.swing.JScrollPane jScrollPane1; 297 349 private javax.swing.JList<String> lstEvents;
Note: See TracChangeset
for help on using the changeset viewer.
