summaryrefslogtreecommitdiff
path: root/ao-tools/altosui/AltosFlightUI.java
blob: 84ba9dca5c1902fdf3f9eef7118563748f8458ed (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
 * Copyright © 2010 Keith Packard <keithp@keithp.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 */

package altosui;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.table.*;
import java.io.*;
import java.util.*;
import java.text.*;
import java.util.prefs.*;
import java.util.concurrent.LinkedBlockingQueue;

public class AltosFlightUI extends JFrame {
	String[] statusNames = { "Height (m)", "State", "RSSI (dBm)", "Speed (m/s)" };
	Object[][] statusData = { { "0", "pad", "-50", "0" } };

	AltosVoice		voice;
	AltosFlightReader	reader;
	AltosDisplayThread	thread;

	private Box vbox;
	private AltosStatusTable flightStatus;
	private AltosInfoTable flightInfo;

	public int width() {
		return flightInfo.width();
	}

	public int height() {
		return flightStatus.height() + flightInfo.height();
	}

	void stop_display() {
		if (thread != null && thread.isAlive()) {
			thread.interrupt();
			try {
				thread.join();
			} catch (InterruptedException ie) {}
		}
		thread = null;
	}

	void disconnect() {
		stop_display();
	}

	public AltosFlightUI(AltosVoice in_voice, AltosFlightReader in_reader, final int serial) {
		voice = in_voice;
		reader = in_reader;

		java.net.URL imgURL = AltosUI.class.getResource("/altus-metrum-16x16.jpg");
		if (imgURL != null)
			setIconImage(new ImageIcon(imgURL).getImage());

		setTitle(String.format("AltOS %s", reader.name));

		flightStatus = new AltosStatusTable();

		vbox = new Box (BoxLayout.Y_AXIS);
		vbox.add(flightStatus);

		flightInfo = new AltosInfoTable();
		vbox.add(flightInfo.box());

		this.add(vbox);

		if (serial >= 0) {
			JMenuBar menubar = new JMenuBar();

			// Channel menu
			{
				JMenu menu = new AltosChannelMenu(AltosPreferences.channel(serial));
				menu.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent e) {
							int channel = Integer.parseInt(e.getActionCommand());
							reader.set_channel(channel);
							AltosPreferences.set_channel(serial, channel);
						}
					});
				menu.setMnemonic(KeyEvent.VK_C);
				menubar.add(menu);
			}

			this.setJMenuBar(menubar);
		}

		this.setSize(new Dimension (width(), height()));
		this.validate();

		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				disconnect();
				setVisible(false);
				dispose();
			}
		});

		this.setVisible(true);

		thread = new AltosDisplayThread(this, voice, flightStatus, flightInfo, reader);

		thread.start();
	}

	public AltosFlightUI (AltosVoice in_voice, AltosFlightReader in_reader) {
		this(in_voice, in_reader, -1);
	}
}