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