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