f67644fc81df8689217aec0e6d6184c525426e1c
[fw/altos] / altosui / AltosIdleMonitorUI.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.*;
30 import org.altusmetrum.AltosLib.*;
31
32 public class AltosIdleMonitorUI extends AltosFrame implements AltosFlightDisplay, AltosFontListener, AltosIdleMonitorListener {
33         AltosDevice             device;
34         JTabbedPane             pane;
35         AltosPad                pad;
36         AltosInfoTable          flightInfo;
37         AltosFlightStatus       flightStatus;
38         AltosIdleMonitor        thread;
39         int                     serial;
40         boolean                 remote;
41
42         void stop_display() {
43                 if (thread != null && thread.isAlive()) {
44                         thread.interrupt();
45                         try {
46                                 thread.join();
47                         } catch (InterruptedException ie) {}
48                 }
49                 thread = null;
50         }
51
52         void disconnect() {
53                 stop_display();
54         }
55
56         public void reset() {
57                 pad.reset();
58                 flightInfo.clear();
59         }
60
61         public void set_font() {
62                 pad.set_font();
63                 flightInfo.set_font();
64         }
65
66         public void font_size_changed(int font_size) {
67                 set_font();
68         }
69
70         AltosFlightStatusUpdate status_update;
71
72         public void show(AltosState state, int crc_errors) {
73                 status_update.saved_state = state;
74                 try {
75                         pad.show(state, crc_errors);
76                         flightStatus.show(state, crc_errors);
77                         flightInfo.show(state, crc_errors);
78                 } catch (Exception e) {
79                         System.out.print("Show exception" + e);
80                 }
81         }
82
83         public void update(final AltosState state) {
84                 Runnable r = new Runnable() {
85                                 public void run() {
86                                         show(state, 0);
87                                 }
88                         };
89                 SwingUtilities.invokeLater(r);
90         }
91
92         Container       bag;
93         AltosFreqList   frequencies;
94
95         public AltosIdleMonitorUI(JFrame in_owner)
96                 throws FileNotFoundException, AltosSerialInUseException, TimeoutException, InterruptedException {
97
98                 device = AltosDeviceDialog.show(in_owner, Altos.product_any);
99                 remote = false;
100                 if (!device.matchProduct(Altos.product_altimeter))
101                         remote = true;
102
103                 serial = device.getSerial();
104                 bag = getContentPane();
105                 bag.setLayout(new GridBagLayout());
106
107                 GridBagConstraints c = new GridBagConstraints();
108
109                 java.net.URL imgURL = AltosUI.class.getResource("/altus-metrum-16x16.jpg");
110                 if (imgURL != null)
111                         setIconImage(new ImageIcon(imgURL).getImage());
112
113                 setTitle(String.format("AltOS %s", device.toShortString()));
114
115                 /* Stick frequency selector at top of table for telemetry monitoring */
116                 if (remote && serial >= 0) {
117                         // Frequency menu
118                         frequencies = new AltosFreqList(AltosUIPreferences.frequency(serial));
119                         frequencies.addActionListener(new ActionListener() {
120                                         public void actionPerformed(ActionEvent e) {
121                                                 double frequency = frequencies.frequency();
122                                                 thread.set_frequency(frequency);
123                                                 AltosUIPreferences.set_frequency(device.getSerial(),
124                                                                                frequency);
125                                         }
126                         });
127                         c.gridx = 0;
128                         c.gridy = 0;
129                         c.insets = new Insets(3, 3, 3, 3);
130                         c.anchor = GridBagConstraints.WEST;
131                         bag.add (frequencies, c);
132                 }
133
134
135                 /* Flight status is always visible */
136                 flightStatus = new AltosFlightStatus();
137                 c.gridx = 0;
138                 c.gridy = 1;
139                 c.fill = GridBagConstraints.HORIZONTAL;
140                 c.weightx = 1;
141                 c.gridwidth = 2;
142                 bag.add(flightStatus, c);
143                 c.gridwidth = 1;
144
145                 /* The rest of the window uses a tabbed pane to
146                  * show one of the alternate data views
147                  */
148                 pane = new JTabbedPane();
149
150                 pad = new AltosPad();
151                 pane.add("Launch Pad", pad);
152
153                 flightInfo = new AltosInfoTable();
154                 pane.add("Table", new JScrollPane(flightInfo));
155
156                 /* Make the tabbed pane use the rest of the window space */
157                 c.gridx = 0;
158                 c.gridy = 2;
159                 c.fill = GridBagConstraints.BOTH;
160                 c.weightx = 1;
161                 c.weighty = 1;
162                 c.gridwidth = 2;
163                 bag.add(pane, c);
164
165                 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
166
167                 AltosUIPreferences.register_font_listener(this);
168
169                 addWindowListener(new WindowAdapter() {
170                                 @Override
171                                 public void windowClosing(WindowEvent e) {
172                                         disconnect();
173                                         setVisible(false);
174                                         dispose();
175                                         AltosUIPreferences.unregister_font_listener(AltosIdleMonitorUI.this);
176                                 }
177                         });
178
179                 pack();
180                 setVisible(true);
181
182                 thread = new AltosIdleMonitor((AltosIdleMonitorListener) this, (AltosLink) new AltosSerial (device), (boolean) remote);
183
184                 status_update = new AltosFlightStatusUpdate(flightStatus);
185
186                 new javax.swing.Timer(100, status_update).start();
187
188                 thread.start();
189         }
190 }