source: tmcsimulator/trunk/src/tmcsim/cadsimulator/managers/TrafficModelViewer.java @ 207

Revision 207, 14.9 KB checked in by jdalbey, 9 years ago (diff)

TrafficModelViewer?.java: Elaborate confirmation message on reload, adjust window start position to not overlap CAD Server window.

Line 
1package tmcsim.cadsimulator.managers;
2
3import tmcsim.client.*;
4import atmsdriver.model.TrafficEvent;
5import java.text.ParseException;
6import java.util.ArrayList;
7import java.util.LinkedList;
8import java.util.List;
9import java.util.Observable;
10import java.util.Observer;
11import java.util.Vector;
12import java.util.logging.Level;
13import java.util.logging.Logger;
14import javax.swing.AbstractListModel;
15import javax.swing.JOptionPane;
16
17/**
18 * GUI for the Traffic Model Manager.
19 * @author jdalbey
20 */
21public class TrafficModelViewer extends javax.swing.JFrame implements Observer
22{
23
24    /**
25     * Reference to the driver associated with this GUI
26     */
27    private TrafficModelManager driver;
28    private List<String> incidents;
29
30    /**
31     * Creates new form ATMSBatchViewer.
32     *
33     * @param incidents a list of incidents to show in a list.
34     */
35    public TrafficModelViewer(TrafficModelManager driver)
36    {
37        initComponents();
38        this.driver = driver;
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//        });
56    }
57
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        }
81    }
82
83    class MyListModel extends AbstractListModel<String>
84    {
85
86        TrafficEvent[] strings;// = { "item 1", "item 2" };
87
88        public MyListModel(List<TrafficEvent> events)
89        {
90            strings = events.toArray(new TrafficEvent[events.size()]);
91        }
92
93        public int getSize()
94        {
95            return strings.length;
96        }
97
98        public String getElementAt(int i)
99        {
100            return strings[i].rawString;
101        }
102    }
103
104    /**
105     * This method is called from within the constructor to initialize the form.
106     * WARNING: Do NOT modify this code. The content of this method is always
107     * regenerated by the Form Editor.
108     */
109    @SuppressWarnings("unchecked")
110    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
111    private void initComponents()
112    {
113
114        pnlEvents = new javax.swing.JPanel();
115        scrollEvents = new javax.swing.JScrollPane();
116        lstEvents = new javax.swing.JList<>();
117        txtClockTime = new javax.swing.JLabel();
118        btnReload = new javax.swing.JButton();
119        pnlIncidents = new javax.swing.JPanel();
120        jScrollPane1 = new javax.swing.JScrollPane();
121        lstIncidents = new javax.swing.JList<>();
122        btnClear = new javax.swing.JButton();
123
124        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
125        setTitle("Traffic Modeler");
126        setLocation(new java.awt.Point(100, 100));
127
128        lstEvents.setFont(new java.awt.Font("Noto Mono", 0, 12)); // NOI18N
129        lstEvents.setModel(new javax.swing.AbstractListModel<String>()
130        {
131            String[] strings = { "item 1", "item 2" };
132            public int getSize() { return strings.length; }
133            public String getElementAt(int i) { return strings[i]; }
134        });
135        scrollEvents.setViewportView(lstEvents);
136
137        txtClockTime.setFont(new java.awt.Font("Noto Sans", 1, 14)); // NOI18N
138        txtClockTime.setText("0:00:00");
139
140        btnReload.setText("Reload");
141        btnReload.addActionListener(new java.awt.event.ActionListener()
142        {
143            public void actionPerformed(java.awt.event.ActionEvent evt)
144            {
145                btnReloadActionPerformed(evt);
146            }
147        });
148
149        javax.swing.GroupLayout pnlEventsLayout = new javax.swing.GroupLayout(pnlEvents);
150        pnlEvents.setLayout(pnlEventsLayout);
151        pnlEventsLayout.setHorizontalGroup(
152            pnlEventsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
153            .addGroup(pnlEventsLayout.createSequentialGroup()
154                .addContainerGap()
155                .addComponent(scrollEvents, javax.swing.GroupLayout.PREFERRED_SIZE, 430, javax.swing.GroupLayout.PREFERRED_SIZE)
156                .addContainerGap(20, Short.MAX_VALUE))
157            .addGroup(pnlEventsLayout.createSequentialGroup()
158                .addGap(112, 112, 112)
159                .addComponent(txtClockTime, javax.swing.GroupLayout.PREFERRED_SIZE, 225, javax.swing.GroupLayout.PREFERRED_SIZE)
160                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
161                .addComponent(btnReload)
162                .addGap(32, 32, 32))
163        );
164        pnlEventsLayout.setVerticalGroup(
165            pnlEventsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
166            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlEventsLayout.createSequentialGroup()
167                .addGroup(pnlEventsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
168                    .addComponent(txtClockTime, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
169                    .addComponent(btnReload))
170                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
171                .addComponent(scrollEvents, javax.swing.GroupLayout.DEFAULT_SIZE, 172, Short.MAX_VALUE)
172                .addContainerGap())
173        );
174
175        lstIncidents.setFont(new java.awt.Font("Noto Mono", 0, 15)); // NOI18N
176        lstIncidents.setModel(new javax.swing.AbstractListModel<String>()
177        {
178            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
179            public int getSize() { return strings.length; }
180            public String getElementAt(int i) { return strings[i]; }
181        });
182        lstIncidents.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
183        jScrollPane1.setViewportView(lstIncidents);
184
185        btnClear.setText("Clear Incident");
186        btnClear.addActionListener(new java.awt.event.ActionListener()
187        {
188            public void actionPerformed(java.awt.event.ActionEvent evt)
189            {
190                btnClearActionPerformed(evt);
191            }
192        });
193
194        javax.swing.GroupLayout pnlIncidentsLayout = new javax.swing.GroupLayout(pnlIncidents);
195        pnlIncidents.setLayout(pnlIncidentsLayout);
196        pnlIncidentsLayout.setHorizontalGroup(
197            pnlIncidentsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
198            .addGroup(pnlIncidentsLayout.createSequentialGroup()
199                .addGroup(pnlIncidentsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
200                    .addComponent(btnClear)
201                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 211, javax.swing.GroupLayout.PREFERRED_SIZE))
202                .addGap(0, 0, Short.MAX_VALUE))
203        );
204        pnlIncidentsLayout.setVerticalGroup(
205            pnlIncidentsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
206            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlIncidentsLayout.createSequentialGroup()
207                .addContainerGap()
208                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE)
209                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
210                .addComponent(btnClear)
211                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
212        );
213
214        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
215        getContentPane().setLayout(layout);
216        layout.setHorizontalGroup(
217            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
218            .addGroup(layout.createSequentialGroup()
219                .addGap(1, 1, 1)
220                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
221                    .addGroup(layout.createSequentialGroup()
222                        .addGap(12, 12, 12)
223                        .addComponent(pnlIncidents, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
224                    .addComponent(pnlEvents, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
225                .addGap(0, 26, Short.MAX_VALUE))
226        );
227        layout.setVerticalGroup(
228            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
229            .addGroup(layout.createSequentialGroup()
230                .addComponent(pnlEvents, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
231                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
232                .addComponent(pnlIncidents, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
233                .addContainerGap())
234        );
235
236        pack();
237    }// </editor-fold>//GEN-END:initComponents
238
239    private void btnClearActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnClearActionPerformed
240    {//GEN-HEADEREND:event_btnClearActionPerformed
241        String selectedItem = lstIncidents.getSelectedValue();
242        if (selectedItem == null)
243        {
244            JOptionPane.showMessageDialog(this, "No Incident Selected", "Warning", JOptionPane.INFORMATION_MESSAGE);
245        }
246        else
247        {
248            System.out.println("Clicked Incident:" + selectedItem);
249            if (driver != null)
250            {
251                driver.clearIncident(selectedItem);
252            }
253            lstIncidents.clearSelection();
254            // Remove selectedItem from list model;
255//            incidents.remove(selectedItem);
256            // Update the list displayed
257//            String[] array = new String[incidents.size()];
258//            array = incidents.toArray(array);
259//            lstIncidents.setListData(array);
260        }
261    }//GEN-LAST:event_btnClearActionPerformed
262
263    private void btnReloadActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnReloadActionPerformed
264    {//GEN-HEADEREND:event_btnReloadActionPerformed
265        String message = "Reload will delete all events in the queue and reload the events file.\n"
266                +"This will not effect the simulation manager, and will run all events up to\n"
267                +"current simulation time.";
268        String title = "Please Confirm";
269        // display the JOptionPane showConfirmDialog
270        int reply = JOptionPane.showConfirmDialog(this, message, title, JOptionPane.YES_NO_OPTION);
271        if (reply == JOptionPane.YES_OPTION)
272        {
273            driver.loadEvents();
274        }
275    }//GEN-LAST:event_btnReloadActionPerformed
276   
277    /**
278     * @param args the command line arguments
279     */
280    public static void main(String args[])
281    {
282        /* Set the Nimbus look and feel */
283        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
284        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
285         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
286         */
287        try
288        {
289            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels())
290            {
291                if ("Nimbus".equals(info.getName()))
292                {
293                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
294                    break;
295                }
296            }
297        }
298        catch (ClassNotFoundException ex)
299        {
300            java.util.logging.Logger.getLogger(TrafficModelViewer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
301        }
302        catch (InstantiationException ex)
303        {
304            java.util.logging.Logger.getLogger(TrafficModelViewer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
305        }
306        catch (IllegalAccessException ex)
307        {
308            java.util.logging.Logger.getLogger(TrafficModelViewer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
309        }
310        catch (javax.swing.UnsupportedLookAndFeelException ex)
311        {
312            java.util.logging.Logger.getLogger(TrafficModelViewer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
313        }
314        //</editor-fold>
315        //</editor-fold>
316        //</editor-fold>
317        //</editor-fold>
318
319        /* Create and display the form */
320        java.awt.EventQueue.invokeLater(new Runnable()
321        {
322            public void run()
323            {
324                try
325                {
326                    TrafficEvent alpha = new TrafficEvent("181 00:01:30 405 S 0.6 11.0 G");
327                    TrafficEvent beta = new TrafficEvent("183 00:12:30 405 S 0.6 11.0 G");
328                    TrafficEvent charly = new TrafficEvent("181 00:22:00 5 S 0.6 11.0 G");
329                    LinkedList<TrafficEvent> sample = new LinkedList<TrafficEvent>();
330                    sample.add(alpha);
331                    sample.add(beta);
332                    List<String> items = new ArrayList<String>();
333                    items.add("180");
334                    items.add("1291");
335                    TrafficModelViewer view = new TrafficModelViewer(null);
336                    view.setVisible(true);
337                    //view.update("01", "02", sample);
338                }
339                catch (ParseException ex)
340                {
341                    Logger.getLogger(TrafficModelViewer.class.getName()).log(Level.SEVERE, null, ex);
342                }
343
344            }
345        });
346    }
347
348    // Variables declaration - do not modify//GEN-BEGIN:variables
349    private javax.swing.JButton btnClear;
350    private javax.swing.JButton btnReload;
351    private javax.swing.JScrollPane jScrollPane1;
352    private javax.swing.JList<String> lstEvents;
353    private javax.swing.JList<String> lstIncidents;
354    private javax.swing.JPanel pnlEvents;
355    private javax.swing.JPanel pnlIncidents;
356    private javax.swing.JScrollPane scrollEvents;
357    private javax.swing.JLabel txtClockTime;
358    // End of variables declaration//GEN-END:variables
359}
Note: See TracBrowser for help on using the repository browser.