Project

General

Profile

Statistics
| Revision:

root / trunk / code / projects / diagnostic_station / server / ServerGUI.java @ 1606

History | View | Annotate | Download (11.7 KB)

1
import java.awt.BorderLayout;
2
import java.awt.Color;
3
import java.awt.Component;
4
import java.awt.Dimension;
5
import java.awt.GridLayout;
6
import java.awt.LayoutManager;
7
import java.awt.event.ActionEvent;
8
import java.awt.event.ActionListener;
9
import java.util.ArrayList;
10
import java.util.Enumeration;
11

    
12
import javax.swing.AbstractButton;
13
import javax.swing.BorderFactory;
14
import javax.swing.Box;
15
import javax.swing.BoxLayout;
16
import javax.swing.ButtonGroup;
17
import javax.swing.JButton;
18
import javax.swing.JFrame;
19
import javax.swing.JLabel;
20
import javax.swing.JPanel;
21
import javax.swing.JRadioButton;
22
import javax.swing.JScrollPane;
23
import javax.swing.JTextArea;
24
import javax.swing.SwingUtilities;
25
import javax.swing.border.Border;
26

    
27
import org.jfree.chart.ChartFactory;
28
import org.jfree.chart.ChartPanel;
29
import org.jfree.chart.JFreeChart;
30
import org.jfree.chart.plot.PlotOrientation;
31
import org.jfree.data.xy.XYSeries;
32
import org.jfree.data.xy.XYSeriesCollection;
33

    
34
public class ServerGUI implements ActionListener{
35
        
36
        private static final long serialVersionUID = 1L;
37

    
38
        // whether or not to use the console
39
        private static final boolean USE_CONSOLE = true;
40
        
41
        private int currentTest = -1;
42
        private int testNum = -1;
43
        
44
        // the area that shows messages
45
        private JTextArea console;
46
        // the DiagnosticsServer that does all the processing
47
        private DiagnosticsServer server;
48
        
49
        // a list of buttons
50
        private JButton[] buttons = new JButton[DiagnosticsServer.NUM_TESTS];
51
        // the stop button
52
        JButton stop;
53
        ButtonGroup numSelectGroup;
54
        private JRadioButton[] radios;
55
        private JFrame numSelectWindow, frame;
56
        private JPanel graphPanel, top;
57
        private LayoutManager layout;
58
        
59
        // Constructor - sets up the GUI
60
        public ServerGUI(DiagnosticsServer server){
61
                frame = new JFrame("Diagnostic Station Control Panel");
62
                this.server = server;
63
                
64
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
65
                
66
                frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
67
                
68
                top = new JPanel();
69
                layout = new BoxLayout(top, BoxLayout.X_AXIS);
70
                top.setLayout(layout);
71
                top.add(buttonPanel());
72
                top.add(Box.createGlue());
73
                graphPanel = graphPanel();
74
                top.add(graphPanel);
75
                
76
                frame.add(top);
77
                if(USE_CONSOLE)
78
                        frame.add(console());
79

    
80
                frame.pack();
81
                frame.setVisible(true);
82
        }
83
        
84
        // returns the Panel containing the graph
85
        private JPanel graphPanel(){
86
                if(currentTest == -1 || currentTest == DiagnosticsServer.ALL_TESTS)
87
                        return blankPanel();
88
        
89
                String[] labels = server.getLabels(currentTest, testNum);
90
                ArrayList[] data = server.getData(currentTest, testNum);
91
                
92
                int numSeries = labels.length - 3;
93
                XYSeries series[] = new XYSeries[numSeries];
94
                for(int i=0; i<numSeries; i++){
95
                        series[i] = new XYSeries(labels[i+3]);
96
                        ArrayList x = data[2*i];
97
                        ArrayList y = data[2*i+1];
98
                        for(int j=0; j<x.size(); j++){
99
                                series[i].add((Integer)x.get(j), (Integer)y.get(j));
100
                        }
101
                }
102
                XYSeriesCollection xyDataset = new XYSeriesCollection(series[0]);
103
                for(int i=1; i<numSeries; i++)
104
                        xyDataset.addSeries(series[i]);
105
                
106
                JFreeChart chart = ChartFactory.createXYLineChart(labels[0],
107
                                labels[1], labels[2], xyDataset, PlotOrientation.VERTICAL, true,
108
                                false, false);
109
                ChartPanel panel = new ChartPanel(chart);
110
                
111
                JPanel graphPanel = new JPanel();
112
                graphPanel.add(panel);
113
                return graphPanel;
114
        }
115
        
116
        // returns a blank panel
117
        private JPanel blankPanel(){
118
                JPanel blank = new JPanel();
119
                blank.setBorder(BorderFactory.createMatteBorder(20, 20, 20, 20, new Color(238, 238, 238)));
120
                blank.setBackground(Color.WHITE);
121
                blank.add(Box.createRigidArea(new Dimension(680, 420)));
122
                return blank;
123
        }
124
        
125
        // returns the console
126
        private JScrollPane console(){
127
                console = new JTextArea(10, 50);
128
                JScrollPane pane = new JScrollPane(console); 
129
                console.setEditable(false);
130
                console.setLineWrap(false);
131
                Border inner = BorderFactory.createLineBorder(Color.BLACK);
132
                Border outer = BorderFactory.createEmptyBorder(5, 15, 5, 15);
133
                Border border = BorderFactory.createCompoundBorder(outer, inner);
134
                pane.setBorder(border);
135
                return pane;
136
        }
137
        
138
        // adds the given String to the console
139
        public void addTextToConsole(final String s){
140
                SwingUtilities.invokeLater(new Runnable (){
141
                        public void run() {
142
                                console.append(s);
143
                        }
144
                });
145
        }
146
        
147
        // the panel with the control buttons
148
        private JPanel buttonPanel(){
149
                int maxButtonWidth = 200;
150
                int maxButtonHeight = 30;
151
                
152
                JPanel buttonPanel = new JPanel();
153
                buttonPanel.setBorder(BorderFactory.createEmptyBorder(10, 15, 5, 15));
154
                buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS));
155
                buttonPanel.setMinimumSize(new Dimension(maxButtonWidth, 25 + ((maxButtonHeight + 5) * DiagnosticsServer.NUM_TESTS)));
156
                buttonPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
157
                buttonPanel.setAlignmentY(Component.BOTTOM_ALIGNMENT);
158
                
159
                buttons[DiagnosticsServer.ALL_TESTS] = new JButton("Run all tests");
160
                buttons[DiagnosticsServer.RANGEFINDER] = new JButton("Run rangefinder test");
161
                buttons[DiagnosticsServer.ENCODER] = new JButton("Run encoder test");
162
                buttons[DiagnosticsServer.MOTOR] = new JButton("Run motor test");
163
                buttons[DiagnosticsServer.BOM] = new JButton("Run bom test");
164
                buttons[DiagnosticsServer.BOM_EMITTER] = new JButton("Run bom emitter test");
165
                buttons[DiagnosticsServer.BOM_DETECTOR] = new JButton("Run bom detector test");
166
                for(int i=0; i<DiagnosticsServer.NUM_TESTS; i++){
167
                        buttons[i].setVerticalTextPosition(AbstractButton.CENTER);
168
                        buttons[i].setActionCommand("frame " + i);
169
                        buttons[i].addActionListener(this);
170
                        buttons[i].setAlignmentX(Component.LEFT_ALIGNMENT);
171
                        buttons[i].setAlignmentY(Component.TOP_ALIGNMENT);
172
                        buttons[i].setMaximumSize(new Dimension(maxButtonWidth, maxButtonHeight));
173
                        if(i != 0)
174
                                buttonPanel.add(Box.createRigidArea(new Dimension(0, 5)));
175
                        buttonPanel.add(buttons[i]);                        
176
                }
177
                stop = new JButton("Stop testing");
178
                stop.setVerticalTextPosition(AbstractButton.CENTER);
179
                stop.setActionCommand("stop");
180
                stop.addActionListener(this);
181
                stop.setAlignmentX(Component.LEFT_ALIGNMENT);
182
                stop.setAlignmentY(Component.TOP_ALIGNMENT);
183
                stop.setMaximumSize(new Dimension(maxButtonWidth, maxButtonHeight));
184
                //stop.setBackground(Color.RED);
185
                stop.setEnabled(false);
186
                buttonPanel.add(Box.createRigidArea(new Dimension(0, 5)));
187
                buttonPanel.add(stop);        
188
                
189
                buttonPanel.add(Box.createGlue());
190
                
191
                return buttonPanel;
192
        }
193
        
194
        // shows the dialog asking the user which specific parts to test
195
        private void getNumDialog(int testNum){
196
                String instr = "Choose which test to run";
197
                if(testNum == DiagnosticsServer.BOM){
198
                        instr = "Choose which bom to test";
199
                }
200
                else if(testNum == DiagnosticsServer.BOM_DETECTOR){
201
                        instr = "Choose which detector to test";
202
                }
203
                else if(testNum == DiagnosticsServer.BOM_EMITTER){
204
                        instr = "Choose which emitter to test";
205
                }
206
                else if(testNum == DiagnosticsServer.RANGEFINDER){
207
                        instr = "Choose which rangefinder to test";
208
                }
209
                else if(testNum == DiagnosticsServer.MOTOR){
210
                        instr = "Choose which motor to test";
211
                }
212
                else if(testNum == DiagnosticsServer.ENCODER){
213
                        instr = "Choose which encoder to test";
214
                }
215
                
216
        JLabel label = new JLabel(instr);
217
                radios = radios(testNum);
218

    
219
        JPanel grid = new JPanel(new GridLayout(0, 1));
220
        for (int i = 0; i < radios.length; i++) {
221
            grid.add(radios[i]);
222
        }
223

    
224
        JPanel box = new JPanel();
225
        box.setBorder(BorderFactory.createEmptyBorder(5, 15, 5, 15));
226
        box.setLayout(new BoxLayout(box, BoxLayout.PAGE_AXIS));
227
        label.setAlignmentX(Component.LEFT_ALIGNMENT);
228
        box.add(label);
229
        box.add(grid);
230
        grid.setAlignmentX(Component.LEFT_ALIGNMENT);
231

    
232
        JPanel buttons = new JPanel();
233
        buttons.setLayout(new BoxLayout(buttons, BoxLayout.Y_AXIS));
234
        box.add(buttons);
235
        JButton run = new JButton("Run test");
236
        run.addActionListener(this);
237
        run.setActionCommand("run");
238
        run.setMaximumSize(new Dimension(200, 30));
239
        buttons.add(run);
240
        buttons.add(Box.createRigidArea(new Dimension(0, 5)));
241
                JButton cancel = new JButton("Cancel");
242
                cancel.addActionListener(this);
243
                cancel.setActionCommand("cancel");
244
                cancel.setMaximumSize(new Dimension(200, 30));
245
                buttons.add(cancel);
246
        
247
        numSelectWindow = new JFrame("Select test");
248
        numSelectWindow.setLayout(new BoxLayout(numSelectWindow.getContentPane(), BoxLayout.Y_AXIS));
249
        numSelectWindow.add(box, BorderLayout.PAGE_START);
250
                numSelectWindow.pack();
251
                numSelectWindow.setVisible(true);
252
        }
253
        
254
        // returns the radio buttons for the getNumDialog
255
        private JRadioButton[] radios(int testNum){
256
                int numTests = 0;
257
                int startIndex = 0;
258
                String allText = "All tests";
259
                if(testNum == DiagnosticsServer.BOM || testNum == DiagnosticsServer.BOM_DETECTOR || testNum == DiagnosticsServer.BOM_EMITTER){
260
                        numTests = DiagnosticsServer.NUM_BOMS;
261
                        startIndex = DiagnosticsServer.BOM_OFFSET;
262
                        if(testNum == DiagnosticsServer.BOM)
263
                                allText = "All boms";
264
                        else if(testNum == DiagnosticsServer.BOM_DETECTOR)
265
                                allText = "All detectors";
266
                        else if(testNum == DiagnosticsServer.BOM_EMITTER)
267
                                allText = "All emitters";
268
                }
269
                else if(testNum == DiagnosticsServer.RANGEFINDER){
270
                        numTests = DiagnosticsServer.NUM_RANGEFINDERS;
271
                        startIndex = DiagnosticsServer.RANGEFINDER_OFFSET;
272
                        allText = "All rangefinders";
273
                }
274
                else if(testNum == DiagnosticsServer.MOTOR){
275
                        numTests = DiagnosticsServer.NUM_MOTORS;
276
                        startIndex = DiagnosticsServer.MOTOR_OFFSET;
277
                        allText = "Both motors";
278
                }
279
                else if(testNum == DiagnosticsServer.ENCODER){
280
                        numTests = DiagnosticsServer.NUM_ENCODERS;
281
                        startIndex = DiagnosticsServer.ENCODER_OFFSET;
282
                        allText = "Both encoders";
283
                }
284
                        
285
                JRadioButton radios[] = new JRadioButton[numTests+1];
286
                numSelectGroup = new ButtonGroup();
287
                for(int i=0; i<numTests+1; i++){
288
                        if(i==0){
289
                                radios[i] = new JRadioButton(allText);
290
                                radios[i].setSelected(true);
291
                        }
292
                        else{
293
                                radios[i] = new JRadioButton((i+startIndex-1) + "");
294
                                radios[i].setSelected(false);
295
                        }
296
                        radios[i].addActionListener(this);
297
                        radios[i].setActionCommand(testNum + " " + (i-1));
298
                        numSelectGroup.add(radios[i]);
299
                }
300

    
301
                return radios;
302
        }
303

    
304
        // the Action handler
305
        public void actionPerformed(ActionEvent event) {
306
                String actionCommand = event.getActionCommand();
307
                try{
308
                        if(actionCommand.equals("stop")){
309
                                server.stopTest();
310
                                stop.setBackground(null);
311
                                stop.setEnabled(false);
312
                        }
313
                        else if(actionCommand.equals("cancel")){
314
                                numSelectWindow.dispose();
315
                                return;
316
                        }
317
                        else if(actionCommand.equals("run")){
318
                                Enumeration<AbstractButton> buttons = numSelectGroup.getElements();
319
                                while(buttons.hasMoreElements()){
320
                                        AbstractButton button = buttons.nextElement();
321
                                        if(button.isSelected()){
322
                                                String command = button.getActionCommand();
323
                                                int spaceIndex = command.indexOf(" ");
324
                                                Integer testNum = Integer.parseInt(command.substring(0, spaceIndex));
325
                                                Integer num = Integer.parseInt(command.substring(spaceIndex+1));
326
                                                numSelectWindow.dispose();
327
                                                currentTest = testNum;
328
                                                this.testNum = num;
329
                                                stop.setBackground(Color.RED);
330
                                                stop.setEnabled(true);
331
                                                frame.repaint();
332
                                                server.startTest(testNum, num);
333
                                                stop.setBackground(null);
334
                                                stop.setEnabled(false);
335
                                                top.remove(graphPanel);
336
                                                graphPanel = graphPanel();
337
                                                top.add(graphPanel);
338
                                                top.validate();
339
                                                break;
340
                                        }
341
                                }
342
                        }
343
                        else if(actionCommand.startsWith("frame ")){
344
                                Integer num = Integer.parseInt(actionCommand.substring(6));
345
                                getNumDialog(num);
346
                        }
347
                }
348
                catch(Exception e){
349
                        System.out.println(e.getMessage());
350
                }
351
        }
352
}