altosui: Split out flight monitoring to separate window
[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 {
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         private AltosStatusTable flightStatus;
41         private AltosInfoTable flightInfo;
42
43         public int width() {
44                 return flightInfo.width();
45         }
46
47         public int height() {
48                 return flightStatus.height() + flightInfo.height();
49         }
50
51         void stop_display() {
52                 if (thread != null && thread.isAlive()) {
53                         thread.interrupt();
54                         try {
55                                 thread.join();
56                         } catch (InterruptedException ie) {}
57                 }
58                 thread = null;
59         }
60
61         void disconnect() {
62                 stop_display();
63         }
64
65         public AltosFlightUI(AltosVoice in_voice, AltosFlightReader in_reader, final int serial) {
66                 voice = in_voice;
67                 reader = in_reader;
68
69                 java.net.URL imgURL = AltosUI.class.getResource("/altus-metrum-16x16.jpg");
70                 if (imgURL != null)
71                         setIconImage(new ImageIcon(imgURL).getImage());
72
73                 setTitle(String.format("AltOS %s", reader.name));
74
75                 flightStatus = new AltosStatusTable();
76
77                 vbox = new Box (BoxLayout.Y_AXIS);
78                 vbox.add(flightStatus);
79
80                 flightInfo = new AltosInfoTable();
81                 vbox.add(flightInfo.box());
82
83                 this.add(vbox);
84
85                 if (serial >= 0) {
86                         JMenuBar menubar = new JMenuBar();
87
88                         // Channel menu
89                         {
90                                 JMenu menu = new AltosChannelMenu(AltosPreferences.channel(serial));
91                                 menu.addActionListener(new ActionListener() {
92                                                 public void actionPerformed(ActionEvent e) {
93                                                         int channel = Integer.parseInt(e.getActionCommand());
94                                                         reader.set_channel(channel);
95                                                         AltosPreferences.set_channel(serial, channel);
96                                                 }
97                                         });
98                                 menu.setMnemonic(KeyEvent.VK_C);
99                                 menubar.add(menu);
100                         }
101
102                         this.setJMenuBar(menubar);
103                 }
104
105                 this.setSize(new Dimension (width(), height()));
106                 this.validate();
107
108                 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
109                 addWindowListener(new WindowAdapter() {
110                         @Override
111                         public void windowClosing(WindowEvent e) {
112                                 disconnect();
113                                 setVisible(false);
114                                 dispose();
115                         }
116                 });
117
118                 this.setVisible(true);
119
120                 thread = new AltosDisplayThread(this, voice, flightStatus, flightInfo, reader);
121
122                 thread.start();
123         }
124
125         public AltosFlightUI (AltosVoice in_voice, AltosFlightReader in_reader) {
126                 this(in_voice, in_reader, -1);
127         }
128 }