Warning: Can't use blame annotator:
svn blame failed on trunk/src/tmcsim/highwaymodel/TrafficEventsAnimator.java: ("Can't find a temporary directory: Internal error", 20014)

source: tmcsimulator/trunk/src/tmcsim/highwaymodel/TrafficEventsAnimator.java @ 457

Revision 457, 7.0 KB checked in by jdalbey, 7 years ago (diff)

Source file re-org. Move files from obsolete atmsdriver package to highwaymodel package.

RevLine 
1package tmcsim.highwaymodel;
2
3import tmcsim.highwaymodel.Highways;
4import tmcsim.highwaymodel.TrafficEvent;
5import java.io.FileInputStream;
6import java.io.FileNotFoundException;
7import java.rmi.RemoteException;
8import java.util.ArrayList;
9import java.util.HashMap;
10import java.util.LinkedList;
11import java.util.List;
12import java.util.Map;
13import java.util.Properties;
14import java.util.Scanner;
15import java.util.logging.Level;
16import java.util.logging.Logger;
17import javax.swing.JFrame;
18import tmcsim.cadsimulator.managers.TrafficModelManager;
19import tmcsim.common.SimulationException;
20
21/**
22 * Read and process all Traffic Events in a file and show
23 * resulting highway network.
24 * A snapshot of the highway model after each event is saved in a buffer
25 * and a graphical display allows the user to scroll through the history.
26 * This application is used by Traffic Event authors to assist
27 * in verifying the correctness of their data file.
28 * @author jdalbey
29 */
30public class TrafficEventsAnimator extends javax.swing.JPanel
31{
32    /**
33     * Error logger.
34     */
35    private final static Logger logger = Logger.getLogger("trafficmodeleventdriver");
36
37    /**
38     * Highways in traffic network
39     */
40    final private Highways highways;
41
42    /**
43     * LinkedList of batch events
44     */
45    private final LinkedList<TrafficEvent> eventQueue;
46    /**
47     * Map of incidents to events
48     */
49    private Map<String, List<TrafficEvent>> incidents;
50    /**
51     * Highway history
52     */
53    private List<String> history;
54    /**
55     * name of events file obtained from properties
56     */
57    private String events_file = ""; 
58    /**
59     * Creates the JPanel and builds the highways and events.
60     */
61    public TrafficEventsAnimator() throws RemoteException, SimulationException
62    {
63        initComponents();
64
65        // Initialize the highway model
66        incidents = new HashMap<String, List<TrafficEvent>>();
67        highways = new Highways(
68                "config/vds_data/highways_fullmap.txt");
69        final String CONFIG_FILE_NAME = "traffic_model_config.properties";
70        String propertiesFile = "config" + System.getProperty("file.separator") 
71                 + CONFIG_FILE_NAME;
72        Properties props = TrafficModelManager.loadProperties(propertiesFile);
73        events_file = props.getProperty("Events_File");
74        FileInputStream fis = null;
75        try
76        {
77            fis = new FileInputStream(events_file);
78        } catch (FileNotFoundException ex)
79        {
80            Logger.getLogger(TrafficModelManager.class.getName()).log(Level.SEVERE, null, 
81                    "Missing Traffic Events file " + events_file);
82            System.exit(-1);
83        }
84        Scanner fileScanner = new Scanner(fis);       
85        // Read all lines from the file of events and put in a queue
86        eventQueue = TrafficModelManager.readBatchFile(fileScanner);
87    }
88    /** Load the traffic events into a history buffer */
89    public void load()
90    {
91        txtDisplay.setText("Loading traffic events from: " + events_file);
92        history = new ArrayList<String>();
93        // If we have any events left to process
94        while (!eventQueue.isEmpty())
95        {
96            // Get next event
97            TrafficEvent nextEvent = eventQueue.peek();
98            System.out.println(nextEvent.eventTime);
99            // apply colorization to highways
100            highways.applyColorToHighwayStretch(nextEvent.routeNumber, nextEvent.dir,
101                    nextEvent.postmile, nextEvent.range, nextEvent.color);
102            history.add(highways.toString()+"\n"+nextEvent.eventTime);
103            // Remove this event from the queue, we're done with it.
104            eventQueue.remove();
105        }
106        scrollBar.setMaximum(history.size());
107        // display the first item from the history
108        txtDisplay.setText(history.get(0));
109    }
110    /**
111     * This method is called from within the constructor to initialize the form.
112     * WARNING: Do NOT modify this code. The content of this method is always
113     * regenerated by the Form Editor.
114     */
115    @SuppressWarnings("unchecked")
116    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
117    private void initComponents() {
118
119        scrollBar = new javax.swing.JScrollBar();
120        jScrollPane1 = new javax.swing.JScrollPane();
121        txtDisplay = new javax.swing.JTextArea();
122
123        scrollBar.addAdjustmentListener(new java.awt.event.AdjustmentListener() {
124            public void adjustmentValueChanged(java.awt.event.AdjustmentEvent evt) {
125                scrollbarValueChanged(evt);
126            }
127        });
128
129        txtDisplay.setColumns(20);
130        txtDisplay.setFont(new java.awt.Font("Courier New", 0, 15)); // NOI18N
131        txtDisplay.setRows(5);
132        jScrollPane1.setViewportView(txtDisplay);
133
134        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
135        this.setLayout(layout);
136        layout.setHorizontalGroup(
137            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
138            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
139                .addComponent(scrollBar, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
140                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
141                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 388, Short.MAX_VALUE))
142        );
143        layout.setVerticalGroup(
144            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
145            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
146            .addComponent(scrollBar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
147        );
148    }// </editor-fold>//GEN-END:initComponents
149
150    private void scrollbarValueChanged(java.awt.event.AdjustmentEvent evt)//GEN-FIRST:event_scrollbarValueChanged
151    {//GEN-HEADEREND:event_scrollbarValueChanged
152        // when the scroll bar is moved show the corresponding snapshot from history
153        txtDisplay.setText(history.get(evt.getValue()) );
154        repaint();
155    }//GEN-LAST:event_scrollbarValueChanged
156
157    /** Entry point for the application.  Builds a surrounding JFrame and
158     * runs the application.
159     * @param args
160     * @throws RemoteException
161     * @throws SimulationException
162     */
163    public static void main(String[] args) throws RemoteException, SimulationException
164    {
165        JFrame frame = new JFrame("Traffic Events Animator");
166        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
167        frame.setSize(1050,450);
168        TrafficEventsAnimator display = new TrafficEventsAnimator();
169        frame.add(display);
170        frame.setVisible(true);
171        display.load();
172    }
173
174    // Variables declaration - do not modify//GEN-BEGIN:variables
175    private javax.swing.JScrollPane jScrollPane1;
176    private javax.swing.JScrollBar scrollBar;
177    private javax.swing.JTextArea txtDisplay;
178    // End of variables declaration//GEN-END:variables
179}
Note: See TracBrowser for help on using the repository browser.