56ab7ebc4ac01546d210b8446cb7c010d5816f9b
[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         JTabbedPane     pane;
40
41         AltosPad        pad;
42         AltosAscent     ascent;
43         AltosDescent    descent;
44         AltosLanded     landed;
45
46         private AltosFlightStatus flightStatus;
47         private JScrollPane flightInfoPane;
48         private AltosInfoTable flightInfo;
49
50         static final int tab_pad = 1;
51         static final int tab_ascent = 2;
52         static final int tab_descent = 3;
53         static final int tab_landed = 4;
54
55         int cur_tab = 0;
56
57         boolean exit_on_close = false;
58
59         int which_tab(AltosState state) {
60                 if (state.state < Altos.ao_flight_boost)
61                         return tab_pad;
62                 if (state.state <= Altos.ao_flight_coast)
63                         return tab_ascent;
64                 if (state.state <= Altos.ao_flight_main)
65                         return tab_descent;
66                 return tab_landed;
67         }
68
69         public int width() {
70                 return flightInfo.width();
71         }
72
73         public int height() {
74                 return flightStatus.height() + flightInfo.height();
75         }
76
77         void stop_display() {
78                 if (thread != null && thread.isAlive()) {
79                         thread.interrupt();
80                         try {
81                                 thread.join();
82                         } catch (InterruptedException ie) {}
83                 }
84                 thread = null;
85         }
86
87         void disconnect() {
88                 stop_display();
89         }
90
91         public void reset() {
92                 pad.reset();
93                 ascent.reset();
94                 descent.reset();
95                 landed.reset();
96                 flightInfo.clear();
97         }
98
99         public void show(AltosState state, int crc_errors) {
100                 int     tab = which_tab(state);
101                 pad.show(state, crc_errors);
102                 ascent.show(state, crc_errors);
103                 descent.show(state, crc_errors);
104                 landed.show(state, crc_errors);
105                 if (tab != cur_tab) {
106                         switch (tab) {
107                         case tab_pad:
108                                 pane.setSelectedComponent(pad);
109                                 break;
110                         case tab_ascent:
111                                 pane.setSelectedComponent(ascent);
112                                 break;
113                         case tab_descent:
114                                 pane.setSelectedComponent(descent);
115                                 break;
116                         case tab_landed:
117                                 pane.setSelectedComponent(landed);
118                         }
119                         cur_tab = tab;
120                 }
121                 flightStatus.show(state, crc_errors);
122                 flightInfo.show(state, crc_errors);
123         }
124
125         public void set_exit_on_close() {
126                 exit_on_close = true;
127         }
128
129         Container       bag;
130
131         public AltosFlightUI(AltosVoice in_voice, AltosFlightReader in_reader, final int serial) {
132                 AltosPreferences.init(this);
133
134                 voice = in_voice;
135                 reader = in_reader;
136
137                 bag = getContentPane();
138                 bag.setLayout(new GridBagLayout());
139
140                 GridBagConstraints c = new GridBagConstraints();
141
142                 java.net.URL imgURL = AltosUI.class.getResource("/altus-metrum-16x16.jpg");
143                 if (imgURL != null)
144                         setIconImage(new ImageIcon(imgURL).getImage());
145
146                 setTitle(String.format("AltOS %s", reader.name));
147
148                 if (serial >= 0) {
149                         // Channel menu
150                         JComboBox channels = new AltosChannelMenu(AltosPreferences.channel(serial));
151                         channels.addActionListener(new ActionListener() {
152                                         public void actionPerformed(ActionEvent e) {
153                                                 int channel = Integer.parseInt(e.getActionCommand());
154                                                 reader.set_channel(channel);
155                                                 AltosPreferences.set_channel(serial, channel);
156                                         }
157                                 });
158                         c.gridx = 0;
159                         c.gridy = 0;
160                         c.anchor = GridBagConstraints.WEST;
161                         bag.add (channels, c);
162                 }
163
164                 flightStatus = new AltosFlightStatus();
165                 c.gridx = 0;
166                 c.gridy = 1;
167                 c.fill = GridBagConstraints.HORIZONTAL;
168                 c.weightx = 1;
169                 bag.add(flightStatus, c);
170
171                 pane = new JTabbedPane();
172
173                 pad = new AltosPad();
174                 pane.add("Launch Pad", pad);
175
176                 ascent = new AltosAscent();
177                 pane.add("Ascent", ascent);
178
179                 descent = new AltosDescent();
180                 pane.add("Descent", descent);
181
182                 landed = new AltosLanded();
183                 pane.add("Landed", landed);
184
185                 flightInfo = new AltosInfoTable();
186                 flightInfoPane = new JScrollPane(flightInfo.box());
187                 pane.add("Table", flightInfoPane);
188
189                 c.gridx = 0;
190                 c.gridy = 2;
191                 c.fill = GridBagConstraints.BOTH;
192                 c.weightx = 1;
193                 c.weighty = 1;
194                 bag.add(pane, c);
195
196                 this.setSize(this.getPreferredSize());
197                 this.validate();
198
199                 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
200                 addWindowListener(new WindowAdapter() {
201                         @Override
202                         public void windowClosing(WindowEvent e) {
203                                 disconnect();
204                                 setVisible(false);
205                                 dispose();
206                                 if (exit_on_close)
207                                         System.exit(0);
208                         }
209                 });
210
211                 this.setVisible(true);
212
213                 thread = new AltosDisplayThread(this, voice, this, reader);
214
215                 thread.start();
216         }
217
218         public AltosFlightUI (AltosVoice in_voice, AltosFlightReader in_reader) {
219                 this(in_voice, in_reader, -1);
220         }
221 }