altosui: Add multi-sized icons to all windows
[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                 setTitle(String.format("AltOS %s", device.toShortString()));
110
111                 /* Stick frequency selector at top of table for telemetry monitoring */
112                 if (remote && serial >= 0) {
113                         // Frequency menu
114                         frequencies = new AltosFreqList(AltosUIPreferences.frequency(serial));
115                         frequencies.addActionListener(new ActionListener() {
116                                         public void actionPerformed(ActionEvent e) {
117                                                 double frequency = frequencies.frequency();
118                                                 thread.set_frequency(frequency);
119                                                 AltosUIPreferences.set_frequency(device.getSerial(),
120                                                                                frequency);
121                                         }
122                         });
123                         c.gridx = 0;
124                         c.gridy = 0;
125                         c.insets = new Insets(3, 3, 3, 3);
126                         c.anchor = GridBagConstraints.WEST;
127                         bag.add (frequencies, c);
128                 }
129
130
131                 /* Flight status is always visible */
132                 flightStatus = new AltosFlightStatus();
133                 c.gridx = 0;
134                 c.gridy = 1;
135                 c.fill = GridBagConstraints.HORIZONTAL;
136                 c.weightx = 1;
137                 c.gridwidth = 2;
138                 bag.add(flightStatus, c);
139                 c.gridwidth = 1;
140
141                 /* The rest of the window uses a tabbed pane to
142                  * show one of the alternate data views
143                  */
144                 pane = new JTabbedPane();
145
146                 pad = new AltosPad();
147                 pane.add("Launch Pad", pad);
148
149                 flightInfo = new AltosInfoTable();
150                 pane.add("Table", new JScrollPane(flightInfo));
151
152                 /* Make the tabbed pane use the rest of the window space */
153                 c.gridx = 0;
154                 c.gridy = 2;
155                 c.fill = GridBagConstraints.BOTH;
156                 c.weightx = 1;
157                 c.weighty = 1;
158                 c.gridwidth = 2;
159                 bag.add(pane, c);
160
161                 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
162
163                 AltosUIPreferences.register_font_listener(this);
164
165                 addWindowListener(new WindowAdapter() {
166                                 @Override
167                                 public void windowClosing(WindowEvent e) {
168                                         disconnect();
169                                         setVisible(false);
170                                         dispose();
171                                         AltosUIPreferences.unregister_font_listener(AltosIdleMonitorUI.this);
172                                 }
173                         });
174
175                 pack();
176                 setVisible(true);
177
178                 thread = new AltosIdleMonitor((AltosIdleMonitorListener) this, (AltosLink) new AltosSerial (device), (boolean) remote);
179
180                 status_update = new AltosFlightStatusUpdate(flightStatus);
181
182                 new javax.swing.Timer(100, status_update).start();
183
184                 thread.start();
185         }
186 }