Merge branch 'buttonbox' of git://git.gag.com/fw/altos into buttonbox
[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         AltosAscent     ascent;
45         AltosDescent    descent;
46         AltosLanded     landed;
47     AltosSiteMap    sitemap;
48
49         private AltosFlightStatus flightStatus;
50         private JScrollPane flightInfoPane;
51         private AltosInfoTable flightInfo;
52
53         static final int tab_pad = 1;
54         static final int tab_ascent = 2;
55         static final int tab_descent = 3;
56         static final int tab_landed = 4;
57
58         int cur_tab = 0;
59
60         boolean exit_on_close = false;
61
62         int which_tab(AltosState state) {
63                 if (state.state < Altos.ao_flight_boost)
64                         return tab_pad;
65                 if (state.state <= Altos.ao_flight_coast)
66                         return tab_ascent;
67                 if (state.state <= Altos.ao_flight_main)
68                         return tab_descent;
69                 return tab_landed;
70         }
71
72         public int width() {
73                 return flightInfo.width();
74         }
75
76         public int height() {
77                 return flightStatus.height() + flightInfo.height();
78         }
79
80         void stop_display() {
81                 if (thread != null && thread.isAlive()) {
82                         thread.interrupt();
83                         try {
84                                 thread.join();
85                         } catch (InterruptedException ie) {}
86                 }
87                 thread = null;
88         }
89
90         void disconnect() {
91                 stop_display();
92         }
93
94         public void reset() {
95                 pad.reset();
96                 ascent.reset();
97                 descent.reset();
98                 landed.reset();
99                 flightInfo.clear();
100                 sitemap.reset();
101         }
102
103         public void show(AltosState state, int crc_errors) {
104                 int     tab = which_tab(state);
105                 pad.show(state, crc_errors);
106                 ascent.show(state, crc_errors);
107                 descent.show(state, crc_errors);
108                 landed.show(state, crc_errors);
109                 if (tab != cur_tab) {
110                         switch (tab) {
111                         case tab_pad:
112                                 pane.setSelectedComponent(pad);
113                                 break;
114                         case tab_ascent:
115                                 pane.setSelectedComponent(ascent);
116                                 break;
117                         case tab_descent:
118                                 pane.setSelectedComponent(descent);
119                                 break;
120                         case tab_landed:
121                                 pane.setSelectedComponent(landed);
122                         }
123                         cur_tab = tab;
124                 }
125                 flightStatus.show(state, crc_errors);
126                 flightInfo.show(state, crc_errors);
127                 sitemap.show(state, crc_errors);
128         }
129
130         public void set_exit_on_close() {
131                 exit_on_close = true;
132         }
133
134         public AltosFlightUI(AltosVoice in_voice, AltosFlightReader in_reader, final int serial) {
135                 AltosPreferences.init(this);
136
137                 voice = in_voice;
138                 reader = in_reader;
139
140                 java.net.URL imgURL = AltosUI.class.getResource("/altus-metrum-16x16.jpg");
141                 if (imgURL != null)
142                         setIconImage(new ImageIcon(imgURL).getImage());
143
144                 setTitle(String.format("AltOS %s", reader.name));
145
146                 flightStatus = new AltosFlightStatus();
147
148                 vbox = new Box (BoxLayout.Y_AXIS);
149                 vbox.add(flightStatus);
150
151                 pane = new JTabbedPane();
152
153                 pad = new AltosPad();
154                 pane.add("Launch Pad", pad);
155
156                 ascent = new AltosAscent();
157                 pane.add("Ascent", ascent);
158
159                 descent = new AltosDescent();
160                 pane.add("Descent", descent);
161
162                 landed = new AltosLanded();
163                 pane.add("Landed", landed);
164
165                 flightInfo = new AltosInfoTable();
166                 flightInfoPane = new JScrollPane(flightInfo.box());
167                 pane.add("Table", flightInfoPane);
168
169         sitemap = new AltosSiteMap();
170         pane.add("Site Map", sitemap);
171
172                 vbox.add(pane);
173
174                 this.add(vbox);
175
176                 if (serial >= 0) {
177                         JMenuBar menubar = new JMenuBar();
178
179                         // Channel menu
180                         {
181                                 JMenu menu = new AltosChannelMenu(AltosPreferences.channel(serial));
182                                 menu.addActionListener(new ActionListener() {
183                                                 public void actionPerformed(ActionEvent e) {
184                                                         int channel = Integer.parseInt(e.getActionCommand());
185                                                         reader.set_channel(channel);
186                                                         AltosPreferences.set_channel(serial, channel);
187                                                 }
188                                         });
189                                 menu.setMnemonic(KeyEvent.VK_C);
190                                 menubar.add(menu);
191                         }
192
193                         this.setJMenuBar(menubar);
194                 }
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 }