a7caf7e9620d9f504f5d673a0e3a5b1779734993
[fw/altos] / ao-tools / altosui / AltosFlightUI.java
1 /*
2  * Copyright © 2010 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 package altosui;
19
20 import java.awt.*;
21 import java.awt.event.*;
22 import javax.swing.*;
23 import javax.swing.filechooser.FileNameExtensionFilter;
24 import javax.swing.table.*;
25 import java.io.*;
26 import java.util.*;
27 import java.text.*;
28 import java.util.prefs.*;
29 import java.util.concurrent.LinkedBlockingQueue;
30
31 public class AltosFlightUI extends JFrame implements AltosFlightDisplay {
32         String[] statusNames = { "Height (m)", "State", "RSSI (dBm)", "Speed (m/s)" };
33         Object[][] statusData = { { "0", "pad", "-50", "0" } };
34
35         AltosVoice              voice;
36         AltosFlightReader       reader;
37         AltosDisplayThread      thread;
38
39         private Box vbox;
40
41         JTabbedPane     pane;
42
43         AltosPad        pad;
44
45         private AltosStatusTable flightStatus;
46         private AltosInfoTable flightInfo;
47
48         public int width() {
49                 return flightInfo.width();
50         }
51
52         public int height() {
53                 return flightStatus.height() + flightInfo.height();
54         }
55
56         void stop_display() {
57                 if (thread != null && thread.isAlive()) {
58                         thread.interrupt();
59                         try {
60                                 thread.join();
61                         } catch (InterruptedException ie) {}
62                 }
63                 thread = null;
64         }
65
66         void disconnect() {
67                 stop_display();
68         }
69
70         public void reset() {
71                 pad.reset();
72                 flightInfo.clear();
73         }
74
75         public void show(AltosState state, int crc_errors) {
76                 pad.show(state, crc_errors);
77                 flightStatus.set(state);
78                 flightInfo.show(state, crc_errors);
79         }
80
81         public AltosFlightUI(AltosVoice in_voice, AltosFlightReader in_reader, final int serial) {
82                 voice = in_voice;
83                 reader = in_reader;
84
85                 java.net.URL imgURL = AltosUI.class.getResource("/altus-metrum-16x16.jpg");
86                 if (imgURL != null)
87                         setIconImage(new ImageIcon(imgURL).getImage());
88
89                 setTitle(String.format("AltOS %s", reader.name));
90
91                 flightStatus = new AltosStatusTable();
92
93                 vbox = new Box (BoxLayout.Y_AXIS);
94                 vbox.add(flightStatus);
95
96                 pane = new JTabbedPane();
97
98                 pad = new AltosPad();
99                 pane.add("Launch Pad", pad);
100
101                 flightInfo = new AltosInfoTable();
102                 pane.add("Table", flightInfo.box());
103
104                 vbox.add(pane);
105
106                 this.add(vbox);
107
108                 if (serial >= 0) {
109                         JMenuBar menubar = new JMenuBar();
110
111                         // Channel menu
112                         {
113                                 JMenu menu = new AltosChannelMenu(AltosPreferences.channel(serial));
114                                 menu.addActionListener(new ActionListener() {
115                                                 public void actionPerformed(ActionEvent e) {
116                                                         int channel = Integer.parseInt(e.getActionCommand());
117                                                         reader.set_channel(channel);
118                                                         AltosPreferences.set_channel(serial, channel);
119                                                 }
120                                         });
121                                 menu.setMnemonic(KeyEvent.VK_C);
122                                 menubar.add(menu);
123                         }
124
125                         this.setJMenuBar(menubar);
126                 }
127
128                 this.setSize(new Dimension (width(), height()));
129                 this.validate();
130
131                 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
132                 addWindowListener(new WindowAdapter() {
133                         @Override
134                         public void windowClosing(WindowEvent e) {
135                                 disconnect();
136                                 setVisible(false);
137                                 dispose();
138                         }
139                 });
140
141                 this.setVisible(true);
142
143                 thread = new AltosDisplayThread(this, voice, this, reader);
144
145                 thread.start();
146         }
147
148         public AltosFlightUI (AltosVoice in_voice, AltosFlightReader in_reader) {
149                 this(in_voice, in_reader, -1);
150         }
151 }