altosui: Create abstract interface for flight data display
[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         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 void reset() {
66                 flightInfo.clear();
67         }
68
69         public void show(AltosState state, int crc_errors) {
70                 flightStatus.set(state);
71                 flightInfo.show(state, crc_errors);
72         }
73
74         public AltosFlightUI(AltosVoice in_voice, AltosFlightReader in_reader, final int serial) {
75                 voice = in_voice;
76                 reader = in_reader;
77
78                 java.net.URL imgURL = AltosUI.class.getResource("/altus-metrum-16x16.jpg");
79                 if (imgURL != null)
80                         setIconImage(new ImageIcon(imgURL).getImage());
81
82                 setTitle(String.format("AltOS %s", reader.name));
83
84                 flightStatus = new AltosStatusTable();
85
86                 vbox = new Box (BoxLayout.Y_AXIS);
87                 vbox.add(flightStatus);
88
89                 flightInfo = new AltosInfoTable();
90                 vbox.add(flightInfo.box());
91
92                 this.add(vbox);
93
94                 if (serial >= 0) {
95                         JMenuBar menubar = new JMenuBar();
96
97                         // Channel menu
98                         {
99                                 JMenu menu = new AltosChannelMenu(AltosPreferences.channel(serial));
100                                 menu.addActionListener(new ActionListener() {
101                                                 public void actionPerformed(ActionEvent e) {
102                                                         int channel = Integer.parseInt(e.getActionCommand());
103                                                         reader.set_channel(channel);
104                                                         AltosPreferences.set_channel(serial, channel);
105                                                 }
106                                         });
107                                 menu.setMnemonic(KeyEvent.VK_C);
108                                 menubar.add(menu);
109                         }
110
111                         this.setJMenuBar(menubar);
112                 }
113
114                 this.setSize(new Dimension (width(), height()));
115                 this.validate();
116
117                 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
118                 addWindowListener(new WindowAdapter() {
119                         @Override
120                         public void windowClosing(WindowEvent e) {
121                                 disconnect();
122                                 setVisible(false);
123                                 dispose();
124                         }
125                 });
126
127                 this.setVisible(true);
128
129                 thread = new AltosDisplayThread(this, voice, this, reader);
130
131                 thread.start();
132         }
133
134         public AltosFlightUI (AltosVoice in_voice, AltosFlightReader in_reader) {
135                 this(in_voice, in_reader, -1);
136         }
137 }