altosui: Make UI Look&Feel configurable
[fw/altos] / altosui / AltosFlightUI.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
31 public class AltosFlightUI extends AltosFrame implements AltosFlightDisplay, AltosFontListener {
32         AltosVoice              voice;
33         AltosFlightReader       reader;
34         AltosDisplayThread      thread;
35
36         JTabbedPane     pane;
37
38         AltosPad        pad;
39         AltosAscent     ascent;
40         AltosDescent    descent;
41         AltosLanded     landed;
42         AltosCompanionInfo      companion;
43         AltosSiteMap    sitemap;
44         boolean         has_map;
45         boolean         has_companion;
46
47         private AltosFlightStatus flightStatus;
48         private AltosInfoTable flightInfo;
49
50         boolean exit_on_close = false;
51
52         JComponent cur_tab = null;
53         JComponent which_tab(AltosState state) {
54                 if (state.state < Altos.ao_flight_boost)
55                         return pad;
56                 if (state.state <= Altos.ao_flight_coast)
57                         return ascent;
58                 if (state.state <= Altos.ao_flight_main)
59                         return descent;
60                 return landed;
61         }
62
63         void stop_display() {
64                 if (thread != null && thread.isAlive()) {
65                         thread.interrupt();
66                         try {
67                                 thread.join();
68                         } catch (InterruptedException ie) {}
69                 }
70                 thread = null;
71         }
72
73         void disconnect() {
74                 stop_display();
75         }
76
77         public void reset() {
78                 pad.reset();
79                 ascent.reset();
80                 descent.reset();
81                 landed.reset();
82                 flightInfo.clear();
83                 sitemap.reset();
84         }
85
86         public void set_font() {
87                 pad.set_font();
88                 ascent.set_font();
89                 descent.set_font();
90                 landed.set_font();
91                 flightStatus.set_font();
92                 flightInfo.set_font();
93                 sitemap.set_font();
94                 companion.set_font();
95         }
96
97         public void font_size_changed(int font_size) {
98                 set_font();
99         }
100
101         public void show(AltosState state, int crc_errors) {
102                 JComponent tab = which_tab(state);
103                 try {
104                 pad.show(state, crc_errors);
105                 ascent.show(state, crc_errors);
106                 descent.show(state, crc_errors);
107                 landed.show(state, crc_errors);
108                 if (tab != cur_tab) {
109                         if (cur_tab == pane.getSelectedComponent()) {
110                                 pane.setSelectedComponent(tab);
111                         }
112                         cur_tab = tab;
113                 }
114                 flightStatus.show(state, crc_errors);
115                 flightInfo.show(state, crc_errors);
116
117                 if (state.data.companion != null) {
118                         if (!has_companion) {
119                                 pane.add("Companion", companion);
120                                 has_companion= true;
121                         }
122                         companion.show(state, crc_errors);
123                 } else {
124                         if (has_companion) {
125                                 pane.remove(companion);
126                                 has_companion = false;
127                         }
128                 }
129                 if (state.gps != null && state.gps.connected) {
130                         if (!has_map) {
131                                 pane.add("Site Map", sitemap);
132                                 has_map = true;
133                         }
134                         sitemap.show(state, crc_errors);
135                 } else {
136                         if (has_map) {
137                                 pane.remove(sitemap);
138                                 has_map = false;
139                         }
140                 }
141                 } catch (Exception e) {
142                         System.out.print("Show exception" + e);
143                 }
144         }
145
146         public void set_exit_on_close() {
147                 exit_on_close = true;
148         }
149
150         Container       bag;
151         AltosFreqList   frequencies;
152         JComboBox       telemetries;
153
154         public AltosFlightUI(AltosVoice in_voice, AltosFlightReader in_reader, final int serial) {
155                 AltosPreferences.set_component(this);
156
157                 voice = in_voice;
158                 reader = in_reader;
159
160                 bag = getContentPane();
161                 bag.setLayout(new GridBagLayout());
162
163                 GridBagConstraints c = new GridBagConstraints();
164
165                 java.net.URL imgURL = AltosUI.class.getResource("/altus-metrum-16x16.jpg");
166                 if (imgURL != null)
167                         setIconImage(new ImageIcon(imgURL).getImage());
168
169                 setTitle(String.format("AltOS %s", reader.name));
170
171                 /* Stick channel selector at top of table for telemetry monitoring */
172                 if (serial >= 0) {
173                         // Channel menu
174                         frequencies = new AltosFreqList(AltosPreferences.frequency(serial));
175                         frequencies.set_product("Monitor");
176                         frequencies.set_serial(serial);
177                         frequencies.addActionListener(new ActionListener() {
178                                         public void actionPerformed(ActionEvent e) {
179                                                 double frequency = frequencies.frequency();
180                                                 try {
181                                                         reader.set_frequency(frequency);
182                                                 } catch (TimeoutException te) {
183                                                 } catch (InterruptedException ie) {
184                                                 }
185                                                 reader.save_frequency();
186                                         }
187                         });
188                         c.gridx = 0;
189                         c.gridy = 0;
190                         c.weightx = 0;
191                         c.weighty = 0;
192                         c.insets = new Insets(3, 3, 3, 3);
193                         c.fill = GridBagConstraints.NONE;
194                         c.anchor = GridBagConstraints.WEST;
195                         bag.add (frequencies, c);
196
197                         // Telemetry format menu
198                         telemetries = new JComboBox();
199                         for (int i = 1; i <= Altos.ao_telemetry_max; i++)
200                                 telemetries.addItem(Altos.telemetry_name(i));
201                         int telemetry = AltosPreferences.telemetry(serial);
202                         if (telemetry <= Altos.ao_telemetry_off ||
203                             telemetry > Altos.ao_telemetry_max)
204                                 telemetry = Altos.ao_telemetry_standard;
205                         telemetries.setSelectedIndex(telemetry - 1);
206                         telemetries.setMaximumRowCount(Altos.ao_telemetry_max);
207                         telemetries.setPreferredSize(null);
208                         telemetries.revalidate();
209                         telemetries.addActionListener(new ActionListener() {
210                                         public void actionPerformed(ActionEvent e) {
211                                                 int telemetry = telemetries.getSelectedIndex() + 1;
212                                                 reader.set_telemetry(telemetry);
213                                                 reader.save_telemetry();
214                                         }
215                                 });
216                         c.gridx = 1;
217                         c.gridy = 0;
218                         c.weightx = 0;
219                         c.weighty = 0;
220                         c.fill = GridBagConstraints.NONE;
221                         c.anchor = GridBagConstraints.WEST;
222                         bag.add (telemetries, c);
223                         c.insets = new Insets(0, 0, 0, 0);
224                 }
225
226                 /* Flight status is always visible */
227                 flightStatus = new AltosFlightStatus();
228                 c.gridx = 0;
229                 c.gridy = 1;
230                 c.fill = GridBagConstraints.HORIZONTAL;
231                 c.weightx = 1;
232                 c.gridwidth = 2;
233                 bag.add(flightStatus, c);
234                 c.gridwidth = 1;
235
236                 /* The rest of the window uses a tabbed pane to
237                  * show one of the alternate data views
238                  */
239                 pane = new JTabbedPane();
240
241                 pad = new AltosPad();
242                 pane.add("Launch Pad", pad);
243
244                 ascent = new AltosAscent();
245                 pane.add("Ascent", ascent);
246
247                 descent = new AltosDescent();
248                 pane.add("Descent", descent);
249
250                 landed = new AltosLanded(reader);
251                 pane.add("Landed", landed);
252
253                 flightInfo = new AltosInfoTable();
254                 pane.add("Table", new JScrollPane(flightInfo));
255
256                 companion = new AltosCompanionInfo();
257                 has_companion = false;
258
259                 sitemap = new AltosSiteMap();
260                 has_map = false;
261
262                 /* Make the tabbed pane use the rest of the window space */
263                 c.gridx = 0;
264                 c.gridy = 2;
265                 c.fill = GridBagConstraints.BOTH;
266                 c.weightx = 1;
267                 c.weighty = 1;
268                 c.gridwidth = 2;
269                 bag.add(pane, c);
270
271                 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
272
273                 AltosPreferences.register_font_listener(this);
274
275                 addWindowListener(new WindowAdapter() {
276                                 @Override
277                                 public void windowClosing(WindowEvent e) {
278                                         disconnect();
279                                         setVisible(false);
280                                         dispose();
281                                         AltosPreferences.unregister_font_listener(AltosFlightUI.this);
282                                         if (exit_on_close)
283                                                 System.exit(0);
284                                 }
285                         });
286
287                 pack();
288                 setVisible(true);
289
290                 thread = new AltosDisplayThread(this, voice, this, reader);
291
292                 thread.start();
293         }
294
295         public AltosFlightUI (AltosVoice in_voice, AltosFlightReader in_reader) {
296                 this(in_voice, in_reader, -1);
297         }
298 }