source: tmcsimulator/trunk/src/tmcsim/cadsimulator/viewer/SimulationStatusPanel.java @ 36

Revision 36, 16.0 KB checked in by dmiller, 10 years ago (diff)

Added Config Tab Window

Line 
1package tmcsim.cadsimulator.viewer;
2
3import java.awt.BorderLayout;
4import java.awt.Color;
5import java.awt.Dimension;
6import java.awt.Font;
7import java.util.logging.Handler;
8import java.util.logging.Level;
9import java.util.logging.LogRecord;
10import java.util.logging.Logger;
11import javax.swing.BorderFactory;
12import javax.swing.Box;
13import javax.swing.BoxLayout;
14import javax.swing.JLabel;
15import javax.swing.JPanel;
16import javax.swing.JScrollPane;
17import javax.swing.JTextArea;
18import javax.swing.JTextField;
19import javax.swing.border.EtchedBorder;
20import javax.swing.border.TitledBorder;
21import tmcsim.common.CADEnums.PARAMICS_STATUS;
22import tmcsim.common.CADEnums.SCRIPT_STATUS;
23
24/**
25 * SimulationStatusPanel is a GUI object used for displaying information for the
26 * current simulation. This information includes:
27 *
28 * <ul>
29 * <li>Current simulation time.</li>
30 * <li>Current simulation status.</li>
31 * <li>Number of remote CAD Clients connected.</li>
32 * <li>Status of Simulation Manager connection.</li>
33 * <li>Status of Paramics connection.</li>
34 * <li>Paramics Network Loaded</li>
35 * <li>Information log messages.</li>
36 * <li>Error log messages</li>
37 * </ul>
38 *
39 * @author Matthew Cechini
40 * @version
41 */
42@SuppressWarnings("serial")
43public class SimulationStatusPanel extends JPanel
44{
45
46    /**
47     * Logging Handler to listen for Information and Error messages logged for
48     * the CAD Simulator. Received LogRecords are displayed in the info or error
49     * message Text Area.
50     *
51     * @author Matthew Cechini
52     */
53    private class SimulatorErrorHandler extends Handler
54    {
55
56        public void close() throws SecurityException
57        {
58        }
59
60        public void flush()
61        {
62        }
63
64        public void publish(LogRecord rec)
65        {
66            StringBuffer msgBuffer = new StringBuffer();
67
68            msgBuffer.append(rec.getSourceClassName() + "."
69                    + rec.getSourceMethodName() + " = "
70                    + rec.getMessage());
71
72            if (rec.getLevel() == Level.INFO)
73            {
74                infoMessagesTA.setText(infoMessagesTA.getText()
75                        + msgBuffer.toString() + "\n");
76            } else
77            {
78                errorMessagesTA.setText(errorMessagesTA.getText()
79                        + msgBuffer.toString() + "\n");
80            }
81        }
82    }
83    /**
84     * Count of how many CAD clients have connected.
85     */
86    private int numClientsConnected = 0;
87    /**
88     * Logging ErrorHandler.
89     */
90    private SimulatorErrorHandler errorHandler;
91
92    /**
93     * Constructor. Initialize GUI Objects. Register the logging handler to
94     * listen for log records from all loggers that exist in the
95     * "tmcsim.cadsimulator" package structure.
96     */
97    public SimulationStatusPanel()
98    {
99
100        initTimeAndStatus();
101        initAdditionalInfo();
102        initMessagesPanes();
103
104        errorHandler = new SimulatorErrorHandler();
105        Logger.getLogger("tmcsim.cadsimulator").addHandler(errorHandler);
106
107        CADSimulatorViewerBox = Box.createVerticalBox();
108        CADSimulatorViewerBox.add(simulationTimeAndStatusBox);
109        CADSimulatorViewerBox.add(additionalInfoBox);
110        CADSimulatorViewerBox.add(infoMessagesPane);
111        CADSimulatorViewerBox.add(errorMessagesPane);
112
113        add(CADSimulatorViewerBox);
114    }
115
116    /**
117     * Method is called when a CAD Client disconnects from the CAD Simulator.
118     * The displayed number of connected clients is incremented by one.
119     */
120    public void connectClient()
121    {
122
123        numClientsConnected++;
124
125        termConnectedTF.setText(String.valueOf(numClientsConnected));
126    }
127
128    /**
129     * Method is called when a CAD Client disconnects from the CAD Simulator.
130     * The displayed number of connected clients is decremented by one.
131     */
132    public void disconnectClient()
133    {
134
135        if (numClientsConnected > 0)
136        {
137            numClientsConnected--;
138        }
139
140        termConnectedTF.setText(String.valueOf(numClientsConnected));
141    }
142
143    /**
144     * Method is called when Simulation Manager connects or disconnects.
145     *
146     * @param connection True if simulation manager is connected, false if not.
147     */
148    public void setSimManagerStatus(boolean connection)
149    {
150
151        if (connection)
152        {
153            managerConnectedTF.setText("Yes");
154        } else
155        {
156            managerConnectedTF.setText("No");
157        }
158
159    }
160
161    /**
162     * Method called to convert current simulation time (parameter long value)
163     * to a string of format H:MM:SS. Time is then updated on GUI.
164     *
165     * @param seconds Long value of current time
166     */
167    public void setTime(long seconds)
168    {
169        String time = new String();
170        long timeSegment;
171
172        timeSegment = seconds / 3600;
173        time += String.valueOf(timeSegment) + ":";
174
175        seconds = seconds % 3600;
176
177        timeSegment = seconds / 60;
178        if (timeSegment < 10)
179        {
180            time += "0";
181        }
182
183        time += String.valueOf(timeSegment) + ":";
184        seconds = seconds % 60;
185
186        timeSegment = seconds;
187        if (timeSegment < 10)
188        {
189            time += "0";
190        }
191
192        time += String.valueOf(timeSegment);
193
194        simulationClockLabel.setText(time);
195
196    }
197
198    /**
199     * This method is called within the CADSimulator whenever an error occurs.
200     * The message is then displayed to the user in the "Error Messages" portion
201     * of the CAD Simulator Viewer. Invoke method with null parameter to clear
202     * messages.
203     *
204     * @param errorMessage String message that will be displayed
205     */
206    protected void displayError(String errorMessage)
207    {
208        if (errorMessage == null)
209        {
210            errorMessagesTA.setText("");
211        } else
212        {
213            errorMessagesTA.append(errorMessage + "\n");
214        }
215    }
216
217    /**
218     * Method is called to display the current status of the simulation.
219     *
220     * @param newStatus Current status of simulation. The following table
221     * describes each possible status and what is displayed. Each status code is
222     * found as a public static int in the Coordinator Class.
223     *
224     * <table cellpadding="2" cellspacing="2" border="1" style="text-align:
225     * left; width: 250px;">
226     * <tbody>
227     * <tr>
228     * <th>Status<br></th>
229     * <th>Actions Taken<br></th>
230     * </tr>
231     * <tr>
232     * <td>NO_SCRIPT<br></td>
233     * <td>Set the simulation status text to a black "No Script". <br></td>
234     * </tr>
235     * <tr>
236     * <td>SCRIPT_STOPPED_NOT_STARTED<br></td>
237     * <td>Set the simulation status text to a red "Ready". <br></td>
238     * </tr>
239     * <tr>
240     * <td>SCRIPT_PAUSED_STARTED<br></td>
241     * <td>Set the simulation status text to a red "Paused". <br></td>
242     * </tr>
243     * <tr>
244     * <td>SCRIPT_RUNNING<br></td>
245     * <td>Set the simulation status text to a green "Running". <br></td>
246     * </tr>
247     * <tr>
248     * <td>ATMS_SYNCHRONIZATION<br></td>
249     * <td>Set the simulation status text to an orange "Synchronizing".
250     * <br></td>
251     * </tr>
252     * </tbody>
253     * </table>
254     */
255    public void setScriptStatus(SCRIPT_STATUS newStatus)
256    {
257
258        switch (newStatus)
259        {
260            case NO_SCRIPT:
261                simulationStatusText.setText("No Script");
262                simulationStatusText.setForeground(Color.BLACK);
263                break;
264
265            case SCRIPT_STOPPED_NOT_STARTED:
266                simulationStatusText.setText("Ready");
267                simulationStatusText.setForeground(Color.RED);
268                break;
269
270            case SCRIPT_PAUSED_STARTED:
271                simulationStatusText.setText("Paused");
272                simulationStatusText.setForeground(Color.RED);
273                break;
274
275            case SCRIPT_RUNNING:
276                simulationStatusText.setText("Running");
277                simulationStatusText.setForeground(Color.GREEN);
278                break;
279            case ATMS_SYNCHRONIZATION:
280                simulationStatusText.setText("Synchronizing");
281                simulationStatusText.setForeground(Color.ORANGE);
282                break;
283
284        }
285    }
286
287    /**
288     * Method is called when a connection to paramics is made or dropped.
289     *
290     * @param newStatus The status denoting whether a connection has been made
291     * or dropped.
292     */
293    public void setParamicsStatus(PARAMICS_STATUS newStatus)
294    {
295
296        switch (newStatus)
297        {
298            case CONNECTED:
299                paramicsConnectedTF.setText("Yes");
300                break;
301            case DISCONNECTED:
302                paramicsConnectedTF.setText("No");
303                break;
304        }
305    }
306
307    /**
308     * Method is called when a paramics network is loaded.
309     *
310     * @param networkID Unique ID for Paramics network that has been loaded.
311     */
312    public void setParamicsNetworkLoaded(String networkID)
313    {
314        networkLoadedTF.setText(networkID);
315    }
316
317    /**
318     * Initialize Time and Status GUI Components
319     */
320    private void initTimeAndStatus()
321    {
322
323        simulationTime = new JPanel();
324        simulationClock = new JPanel();
325        simulationStatus = new JLabel("Simulation Status");
326        simulationStatusText = new JLabel("No Script");
327
328        simulationTime.setLayout(new BorderLayout());
329        simulationClock.setPreferredSize(new Dimension(100, 60));
330        simulationTimeAndStatusBox = new Box(BoxLayout.X_AXIS);
331        simulationStatusBox = new Box(BoxLayout.Y_AXIS);
332        simulationTimeBox = new Box(BoxLayout.Y_AXIS);
333        simulationClockBox = new Box(BoxLayout.X_AXIS);
334
335        simulationStatus.setAlignmentX(Box.CENTER_ALIGNMENT);
336        simulationStatusText.setAlignmentX(Box.CENTER_ALIGNMENT);
337        simulationStatusText.setName("simulationStatusText");
338
339        TitledBorder title = BorderFactory.createTitledBorder(
340                BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), "Status");
341        title.setTitleJustification(TitledBorder.LEFT);
342        simulationStatusBox.setBorder(title);
343
344        simulationStatusBox.setMaximumSize(new Dimension(140, 60));
345        simulationStatusBox.setAlignmentX(Box.CENTER_ALIGNMENT);
346
347        simulationStatusBox.add(Box.createHorizontalStrut(120));
348        simulationStatusBox.add(Box.createVerticalGlue());
349        simulationStatusBox.add(simulationStatusText);
350        simulationStatusBox.add(Box.createVerticalGlue());
351
352        simulationClockLabel = new JLabel("0:00:00");
353        simulationClockLabel.setFont(new Font("Geneva", Font.BOLD, 70));
354        simulationClockLabel.setName("simulationClockLabel");
355        simulationClockLabel.setForeground(Color.BLACK);
356        simulationClockLabel.setBackground(Color.BLACK);
357        simulationClockBox.setForeground(Color.BLACK);
358        simulationClockBox.setBackground(Color.BLACK);
359        simulationClockBox.add(simulationClockLabel);
360        simulationClockBox.setAlignmentX(Box.CENTER_ALIGNMENT);
361        simulationTimeBox.add(simulationClockBox);
362
363        simulationTimeAndStatusBox.add(Box.createHorizontalStrut(20));
364        simulationTimeAndStatusBox.add(simulationTimeBox);
365        simulationTimeAndStatusBox.add(Box.createHorizontalStrut(20));
366        simulationTimeAndStatusBox.add(simulationStatusBox);
367        simulationTimeAndStatusBox.add(Box.createHorizontalStrut(20));
368    }
369
370    /**
371     * Initialize Additional Info Label GUI Components
372     */
373    private void initAdditionalInfo()
374    {
375
376        terminalsConnectedLabel = new JLabel("Connected CAD Terminals: ");
377        termConnectedTF = new JTextField("   " + String.valueOf(numClientsConnected));
378        termConnectedTF.setEditable(false);
379        termConnectedTF.setName("termConnectedTF");
380
381        termConnectedBox = new Box(BoxLayout.X_AXIS);
382        termConnectedBox.add(terminalsConnectedLabel);
383        termConnectedBox.add(Box.createHorizontalGlue());
384        termConnectedBox.add(termConnectedTF);
385
386
387        managerConnectedLabel = new JLabel("Simulation Manager Connected: ");
388        managerConnectedTF = new JTextField("  No");
389        managerConnectedTF.setEditable(false);
390        managerConnectedTF.setName("managerConnectedTF");
391
392        managerConnectedBox = new Box(BoxLayout.X_AXIS);
393        managerConnectedBox.add(managerConnectedLabel);
394        managerConnectedBox.add(Box.createHorizontalGlue());
395        managerConnectedBox.add(managerConnectedTF);
396
397
398        paramicsConnectedLabel = new JLabel("Connected to Paramics: ");
399        paramicsConnectedTF = new JTextField("  No");
400        paramicsConnectedTF.setEditable(false);
401        paramicsConnectedTF.setName("paramicsConnectedTF");
402
403        paramicsConnectedBox = new Box(BoxLayout.X_AXIS);
404        paramicsConnectedBox.add(paramicsConnectedLabel);
405        paramicsConnectedBox.add(Box.createHorizontalGlue());
406        paramicsConnectedBox.add(paramicsConnectedTF);
407
408
409        networkLoadedLabel = new JLabel("Network Loaded: ");
410        networkLoadedTF = new JTextField("None");
411        networkLoadedTF.setEditable(false);
412        networkLoadedTF.setName("networkLoadedTF");
413
414        networkLoadedBox = new Box(BoxLayout.X_AXIS);
415        networkLoadedBox.add(networkLoadedLabel);
416        networkLoadedBox.add(Box.createHorizontalGlue());
417        networkLoadedBox.add(networkLoadedTF);
418
419
420        additionalInfoBox = new Box(BoxLayout.Y_AXIS);
421        additionalInfoBox.setMinimumSize(new Dimension(300, 150));
422
423        additionalInfoBox.add(Box.createVerticalStrut(10));
424        additionalInfoBox.add(termConnectedBox);
425        additionalInfoBox.add(Box.createVerticalStrut(10));
426        additionalInfoBox.add(managerConnectedBox);
427        additionalInfoBox.add(Box.createVerticalStrut(10));
428        additionalInfoBox.add(paramicsConnectedBox);
429        additionalInfoBox.add(Box.createVerticalStrut(10));
430        additionalInfoBox.add(networkLoadedBox);
431        additionalInfoBox.add(Box.createVerticalStrut(20));
432
433
434    }
435
436    /**
437     * Initialize Info & Error Messages GUI Components
438     */
439    private void initMessagesPanes()
440    {
441
442        infoMessagesTA = new JTextArea(6, 30);
443        infoMessagesTA.setEditable(false);
444        infoMessagesTA.setName("infoMessagesTA");
445        infoMessagesTA.setLineWrap(true);
446        infoMessagesTA.setWrapStyleWord(true);
447        infoMessagesPane = new JScrollPane(infoMessagesTA);
448        infoMessagesPane.setPreferredSize(new Dimension(300, 100));
449        infoMessagesPane.setName("infoMessagesPane");
450        infoMessagesPane.setBorder(BorderFactory.createTitledBorder(
451                BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), "Info Messages"));
452        infoMessagesPane.setName("infoMessagesPane");
453
454        errorMessagesTA = new JTextArea(6, 30);
455        errorMessagesTA.setForeground(Color.RED);
456        errorMessagesTA.setEditable(false);
457        errorMessagesTA.setName("errorMessagesTA");
458        errorMessagesTA.setLineWrap(true);
459        errorMessagesTA.setWrapStyleWord(true);
460        errorMessagesPane = new JScrollPane(errorMessagesTA);
461        errorMessagesPane.setPreferredSize(new Dimension(300, 150));
462        errorMessagesPane.setName("errorMessagesPane");
463        errorMessagesPane.setBorder(BorderFactory.createTitledBorder(
464                BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), "Error Messages"));
465
466    }
467    private Box additionalInfoBox;
468    private Box termConnectedBox;
469    private Box managerConnectedBox;
470    private Box paramicsConnectedBox;
471    private Box networkLoadedBox;
472    private Box CADSimulatorViewerBox;
473    private Box simulationTimeAndStatusBox;
474    private Box simulationStatusBox;
475    private Box simulationTimeBox;
476    private Box simulationClockBox;
477    private JLabel managerConnectedLabel;
478    private JLabel paramicsConnectedLabel;
479    private JLabel simulationStatus;
480    private JLabel simulationClockLabel;
481    private JLabel simulationStatusText;
482    private JLabel terminalsConnectedLabel;
483    private JLabel networkLoadedLabel;
484    private JPanel simulationTime;
485    private JPanel simulationClock;
486    private JTextField managerConnectedTF;
487    private JTextField paramicsConnectedTF;
488    private JTextField termConnectedTF;
489    private JTextField networkLoadedTF;
490    private JScrollPane infoMessagesPane;
491    private JScrollPane errorMessagesPane;
492    private JTextArea infoMessagesTA;
493    private JTextArea errorMessagesTA;
494}
Note: See TracBrowser for help on using the repository browser.