6a79604efff6b33f894ebaba09e111c652befb9d
[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.event.*;
24 import java.io.*;
25 import java.util.concurrent.*;
26 import java.util.Arrays;
27 import org.altusmetrum.altoslib_2.*;
28 import org.altusmetrum.altosuilib_1.*;
29
30 public class AltosIdleMonitorUI extends AltosUIFrame implements AltosFlightDisplay, AltosFontListener, AltosIdleMonitorListener, DocumentListener {
31         AltosDevice             device;
32         JTabbedPane             pane;
33         AltosPad                pad;
34         AltosInfoTable          flightInfo;
35         AltosFlightStatus       flightStatus;
36         AltosIdleMonitor        thread;
37         int                     serial;
38         boolean                 remote;
39
40         void stop_display() {
41                 if (thread != null) {
42                         try {
43                                 thread.abort();
44                         } catch (InterruptedException ie) {
45                         }
46                 }
47                 thread = null;
48         }
49
50         void disconnect() {
51                 stop_display();
52         }
53
54         public void reset() {
55                 pad.reset();
56                 flightInfo.clear();
57         }
58
59         public void set_font() {
60                 pad.set_font();
61                 flightInfo.set_font();
62         }
63
64         public void font_size_changed(int font_size) {
65                 set_font();
66         }
67
68         AltosFlightStatusUpdate status_update;
69
70         public void show(AltosState state, AltosListenerState listener_state) {
71                 status_update.saved_state = state;
72 //              try {
73                         pad.show(state, listener_state);
74                         flightStatus.show(state, listener_state);
75                         flightInfo.show(state, listener_state);
76 //              } catch (Exception e) {
77 //                      System.out.print("Show exception " + e);
78 //              }
79         }
80
81         public void update(final AltosState state, final AltosListenerState listener_state) {
82                 Runnable r = new Runnable() {
83                                 public void run() {
84                                         show(state, listener_state);
85                                 }
86                         };
87                 SwingUtilities.invokeLater(r);
88         }
89
90         Container       bag;
91         AltosFreqList   frequencies;
92         JTextField      callsign_value;
93
94         /* DocumentListener interface methods */
95         public void changedUpdate(DocumentEvent e) {
96                 if (callsign_value != null) {
97                         String  callsign = callsign_value.getText();
98                         thread.set_callsign(callsign);
99                         AltosUIPreferences.set_callsign(callsign);
100                 }
101         }
102
103         public void insertUpdate(DocumentEvent e) {
104                 changedUpdate(e);
105         }
106
107         public void removeUpdate(DocumentEvent e) {
108                 changedUpdate(e);
109         }
110
111         int row = 0;
112
113         public GridBagConstraints constraints (int x, int width, int fill) {
114                 GridBagConstraints c = new GridBagConstraints();
115                 Insets insets = new Insets(4, 4, 4, 4);
116
117                 c.insets = insets;
118                 c.fill = fill;
119                 if (width == 3)
120                         c.anchor = GridBagConstraints.CENTER;
121                 else if (x == 2)
122                         c.anchor = GridBagConstraints.EAST;
123                 else
124                         c.anchor = GridBagConstraints.WEST;
125                 c.gridx = x;
126                 c.gridwidth = width;
127                 c.gridy = row;
128                 return c;
129         }
130
131         public GridBagConstraints constraints(int x, int width) {
132                 return constraints(x, width, GridBagConstraints.NONE);
133         }
134
135         public AltosIdleMonitorUI(JFrame in_owner)
136                 throws FileNotFoundException, AltosSerialInUseException, TimeoutException, InterruptedException {
137
138                 device = AltosDeviceUIDialog.show(in_owner, Altos.product_any);
139                 remote = false;
140                 if (!device.matchProduct(Altos.product_altimeter))
141                         remote = true;
142
143                 serial = device.getSerial();
144                 bag = getContentPane();
145                 bag.setLayout(new GridBagLayout());
146
147                 setTitle(String.format("AltOS %s", device.toShortString()));
148
149                 /* Stick frequency selector at top of table for telemetry monitoring */
150                 if (remote && serial >= 0) {
151                         // Frequency menu
152                         frequencies = new AltosFreqList(AltosUIPreferences.frequency(serial));
153                         frequencies.addActionListener(new ActionListener() {
154                                         public void actionPerformed(ActionEvent e) {
155                                                 double frequency = frequencies.frequency();
156                                                 thread.set_frequency(frequency);
157                                                 AltosUIPreferences.set_frequency(device.getSerial(),
158                                                                                frequency);
159                                         }
160                         });
161                         bag.add (frequencies, constraints(0, 1));
162                         bag.add (new JLabel("Callsign:"), constraints(1, 1));
163                         /* Add callsign configuration */
164                         callsign_value = new JTextField(AltosUIPreferences.callsign());
165                         callsign_value.getDocument().addDocumentListener(this);
166                         callsign_value.setToolTipText("Callsign sent in packet mode");
167                         bag.add(callsign_value, constraints(2, 1, GridBagConstraints.BOTH));
168                         row++;
169                 }
170
171
172                 /* Flight status is always visible */
173                 flightStatus = new AltosFlightStatus();
174                 bag.add(flightStatus, constraints(0, 3, GridBagConstraints.HORIZONTAL));
175                 row++;
176
177                 /* The rest of the window uses a tabbed pane to
178                  * show one of the alternate data views
179                  */
180                 pane = new JTabbedPane();
181
182                 pad = new AltosPad();
183                 pane.add("Launch Pad", pad);
184
185                 flightInfo = new AltosInfoTable();
186                 pane.add("Table", new JScrollPane(flightInfo));
187
188                 /* Make the tabbed pane use the rest of the window space */
189                 bag.add(pane, constraints(0, 3, GridBagConstraints.BOTH));
190
191                 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
192
193                 AltosUIPreferences.register_font_listener(this);
194
195                 addWindowListener(new WindowAdapter() {
196                                 @Override
197                                 public void windowClosing(WindowEvent e) {
198                                         try {
199                                                 disconnect();
200                                         } catch (Exception ex) {
201                                                 System.out.println(Arrays.toString(ex.getStackTrace()));
202                                         }
203                                         setVisible(false);
204                                         dispose();
205                                         AltosUIPreferences.unregister_font_listener(AltosIdleMonitorUI.this);
206                                 }
207                         });
208
209                 pack();
210                 setVisible(true);
211
212                 thread = new AltosIdleMonitor((AltosIdleMonitorListener) this, (AltosLink) new AltosSerial (device), (boolean) remote);
213
214                 status_update = new AltosFlightStatusUpdate(flightStatus);
215
216                 new javax.swing.Timer(100, status_update).start();
217
218                 thread.start();
219         }
220 }