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

Revision 5, 16.2 KB checked in by jdalbey, 10 years ago (diff)

CADSimulatorTest: added test of info messages text area.

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