AltosSiteMap: add autoscroll and grabndrag scroll
[fw/altos] / ao-tools / 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.LinkedBlockingQueue;
30
31 public class AltosFlightUI extends JFrame implements AltosFlightDisplay {
32         String[] statusNames = { "Height (m)", "State", "RSSI (dBm)", "Speed (m/s)" };
33         Object[][] statusData = { { "0", "pad", "-50", "0" } };
34
35         AltosVoice              voice;
36         AltosFlightReader       reader;
37         AltosDisplayThread      thread;
38
39         JTabbedPane     pane;
40
41         AltosPad        pad;
42         AltosAscent     ascent;
43         AltosDescent    descent;
44         AltosLanded     landed;
45     AltosSiteMap    sitemap;
46
47         private AltosFlightStatus flightStatus;
48         private JScrollPane flightInfoPane;
49         private AltosInfoTable flightInfo;
50
51         static final int tab_pad = 1;
52         static final int tab_ascent = 2;
53         static final int tab_descent = 3;
54         static final int tab_landed = 4;
55
56         int cur_tab = 0;
57
58         boolean exit_on_close = false;
59
60         int which_tab(AltosState state) {
61                 if (state.state < Altos.ao_flight_boost)
62                         return tab_pad;
63                 if (state.state <= Altos.ao_flight_coast)
64                         return tab_ascent;
65                 if (state.state <= Altos.ao_flight_main)
66                         return tab_descent;
67                 return tab_landed;
68         }
69
70         public int width() {
71                 return flightInfo.width();
72         }
73
74         public int height() {
75                 return flightStatus.height() + flightInfo.height();
76         }
77
78         void stop_display() {
79                 if (thread != null && thread.isAlive()) {
80                         thread.interrupt();
81                         try {
82                                 thread.join();
83                         } catch (InterruptedException ie) {}
84                 }
85                 thread = null;
86         }
87
88         void disconnect() {
89                 stop_display();
90         }
91
92         public void reset() {
93                 pad.reset();
94                 ascent.reset();
95                 descent.reset();
96                 landed.reset();
97                 flightInfo.clear();
98                 sitemap.reset();
99         }
100
101         public void show(AltosState state, int crc_errors) {
102                 int     tab = which_tab(state);
103                 pad.show(state, crc_errors);
104                 ascent.show(state, crc_errors);
105                 descent.show(state, crc_errors);
106                 landed.show(state, crc_errors);
107                 if (tab != cur_tab) {
108                         switch (tab) {
109                         case tab_pad:
110                                 pane.setSelectedComponent(pad);
111                                 break;
112                         case tab_ascent:
113                                 pane.setSelectedComponent(ascent);
114                                 break;
115                         case tab_descent:
116                                 pane.setSelectedComponent(descent);
117                                 break;
118                         case tab_landed:
119                                 pane.setSelectedComponent(landed);
120                         }
121                         cur_tab = tab;
122                 }
123                 flightStatus.show(state, crc_errors);
124                 flightInfo.show(state, crc_errors);
125                 sitemap.show(state, crc_errors);
126         }
127
128         public void set_exit_on_close() {
129                 exit_on_close = true;
130         }
131
132         Container       bag;
133         JComboBox       channels;
134
135         public AltosFlightUI(AltosVoice in_voice, AltosFlightReader in_reader, final int serial) {
136                 AltosPreferences.init(this);
137
138                 voice = in_voice;
139                 reader = in_reader;
140
141                 bag = getContentPane();
142                 bag.setLayout(new GridBagLayout());
143
144                 GridBagConstraints c = new GridBagConstraints();
145
146                 java.net.URL imgURL = AltosUI.class.getResource("/altus-metrum-16x16.jpg");
147                 if (imgURL != null)
148                         setIconImage(new ImageIcon(imgURL).getImage());
149
150                 setTitle(String.format("AltOS %s", reader.name));
151
152                 if (serial >= 0) {
153                         // Channel menu
154                         channels = new AltosChannelMenu(AltosPreferences.channel(serial));
155                         channels.addActionListener(new ActionListener() {
156                                         public void actionPerformed(ActionEvent e) {
157                                                 int channel = channels.getSelectedIndex();
158                                                 reader.set_channel(channel);
159                                                 AltosPreferences.set_channel(serial, channel);
160                                         }
161                                 });
162                         c.gridx = 0;
163                         c.gridy = 0;
164                         c.anchor = GridBagConstraints.WEST;
165                         bag.add (channels, c);
166                 }
167
168                 flightStatus = new AltosFlightStatus();
169                 c.gridx = 0;
170                 c.gridy = 1;
171                 c.fill = GridBagConstraints.HORIZONTAL;
172                 c.weightx = 1;
173                 bag.add(flightStatus, c);
174
175                 pane = new JTabbedPane();
176
177                 pad = new AltosPad();
178                 pane.add("Launch Pad", pad);
179
180                 ascent = new AltosAscent();
181                 pane.add("Ascent", ascent);
182
183                 descent = new AltosDescent();
184                 pane.add("Descent", descent);
185
186                 landed = new AltosLanded();
187                 pane.add("Landed", landed);
188
189                 flightInfo = new AltosInfoTable();
190                 flightInfoPane = new JScrollPane(flightInfo.box());
191                 pane.add("Table", flightInfoPane);
192
193         sitemap = new AltosSiteMap();
194         pane.add("Site Map", sitemap);
195
196                 c.gridx = 0;
197                 c.gridy = 2;
198                 c.fill = GridBagConstraints.BOTH;
199                 c.weightx = 1;
200                 c.weighty = 1;
201                 bag.add(pane, c);
202
203                 this.setSize(this.getPreferredSize());
204                 this.validate();
205
206                 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
207                 addWindowListener(new WindowAdapter() {
208                         @Override
209                         public void windowClosing(WindowEvent e) {
210                                 disconnect();
211                                 setVisible(false);
212                                 dispose();
213                                 if (exit_on_close)
214                                         System.exit(0);
215                         }
216                 });
217
218                 this.setVisible(true);
219
220                 thread = new AltosDisplayThread(this, voice, this, reader);
221
222                 thread.start();
223         }
224
225         public AltosFlightUI (AltosVoice in_voice, AltosFlightReader in_reader) {
226                 this(in_voice, in_reader, -1);
227         }
228 }