update changelogs for Debian build
[fw/altos] / altosui / AltosLanded.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 AltosLanded extends JComponent implements AltosFlightDisplay {
32         GridBagLayout   layout;
33         Font            label_font;
34         Font            value_font;
35
36         public class LandedValue {
37                 JLabel          label;
38                 JTextField      value;
39                 void show(AltosState state, int crc_errors) {}
40
41                 void reset() {
42                         value.setText("");
43                 }
44
45                 void show(String format, double v) {
46                         value.setText(String.format(format, v));
47                 }
48
49                 public LandedValue (GridBagLayout layout, int y, String text) {
50                         GridBagConstraints      c = new GridBagConstraints();
51                         c.weighty = 1;
52
53                         label = new JLabel(text);
54                         label.setFont(label_font);
55                         label.setHorizontalAlignment(SwingConstants.LEFT);
56                         c.gridx = 0; c.gridy = y;
57                         c.insets = new Insets(10, 10, 10, 10);
58                         c.anchor = GridBagConstraints.WEST;
59                         c.weightx = 0;
60                         c.fill = GridBagConstraints.VERTICAL;
61                         layout.setConstraints(label, c);
62                         add(label);
63
64                         value = new JTextField(Altos.text_width);
65                         value.setFont(value_font);
66                         value.setHorizontalAlignment(SwingConstants.RIGHT);
67                         c.gridx = 1; c.gridy = y;
68                         c.anchor = GridBagConstraints.WEST;
69                         c.weightx = 1;
70                         c.fill = GridBagConstraints.BOTH;
71                         layout.setConstraints(value, c);
72                         add(value);
73                 }
74         }
75
76         String pos(double p, String pos, String neg) {
77                 String  h = pos;
78                 if (p < 0) {
79                         h = neg;
80                         p = -p;
81                 }
82                 int deg = (int) Math.floor(p);
83                 double min = (p - Math.floor(p)) * 60.0;
84                 return String.format("%s %4d° %9.6f", h, deg, min);
85         }
86
87         class Lat extends LandedValue {
88                 void show (AltosState state, int crc_errors) {
89                         if (state.gps != null)
90                                 value.setText(pos(state.gps.lat,"N", "S"));
91                         else
92                                 value.setText("???");
93                 }
94                 public Lat (GridBagLayout layout, int y) {
95                         super (layout, y, "Latitude");
96                 }
97         }
98
99         Lat lat;
100
101         class Lon extends LandedValue {
102                 void show (AltosState state, int crc_errors) {
103                         if (state.gps != null)
104                                 value.setText(pos(state.gps.lon,"E", "W"));
105                         else
106                                 value.setText("???");
107                 }
108                 public Lon (GridBagLayout layout, int y) {
109                         super (layout, y, "Longitude");
110                 }
111         }
112
113         Lon lon;
114
115         class Bearing extends LandedValue {
116                 void show (AltosState state, int crc_errors) {
117                         if (state.from_pad != null)
118                                 show("%3.0f°", state.from_pad.bearing);
119                         else
120                                 value.setText("???");
121                 }
122                 public Bearing (GridBagLayout layout, int y) {
123                         super (layout, y, "Bearing");
124                 }
125         }
126
127         Bearing bearing;
128
129         class Distance extends LandedValue {
130                 void show (AltosState state, int crc_errors) {
131                         if (state.from_pad != null)
132                                 show("%6.0f m", state.from_pad.distance);
133                         else
134                                 value.setText("???");
135                 }
136                 public Distance (GridBagLayout layout, int y) {
137                         super (layout, y, "Distance");
138                 }
139         }
140
141         Distance distance;
142
143         class Height extends LandedValue {
144                 void show (AltosState state, int crc_errors) {
145                         show("%6.0f m", state.max_height);
146                 }
147                 public Height (GridBagLayout layout, int y) {
148                         super (layout, y, "Maximum Height");
149                 }
150         }
151
152         Height  height;
153
154         class Speed extends LandedValue {
155                 void show (AltosState state, int crc_errors) {
156                         show("%6.0f m/s", state.max_speed);
157                 }
158                 public Speed (GridBagLayout layout, int y) {
159                         super (layout, y, "Maximum Speed");
160                 }
161         }
162
163         Speed   speed;
164
165         class Accel extends LandedValue {
166                 void show (AltosState state, int crc_errors) {
167                         show("%6.0f m/s²", state.max_acceleration);
168                 }
169                 public Accel (GridBagLayout layout, int y) {
170                         super (layout, y, "Maximum Acceleration");
171                 }
172         }
173
174         Accel   accel;
175
176         public void reset() {
177                 lat.reset();
178                 lon.reset();
179                 bearing.reset();
180                 distance.reset();
181                 height.reset();
182                 speed.reset();
183                 accel.reset();
184         }
185
186         public void show(AltosState state, int crc_errors) {
187                 bearing.show(state, crc_errors);
188                 distance.show(state, crc_errors);
189                 lat.show(state, crc_errors);
190                 lon.show(state, crc_errors);
191                 height.show(state, crc_errors);
192                 speed.show(state, crc_errors);
193                 accel.show(state, crc_errors);
194         }
195
196         public AltosLanded() {
197                 layout = new GridBagLayout();
198
199                 label_font = new Font("Dialog", Font.PLAIN, 22);
200                 value_font = new Font("Monospaced", Font.PLAIN, 22);
201                 setLayout(layout);
202
203                 /* Elements in descent display */
204                 bearing = new Bearing(layout, 0);
205                 distance = new Distance(layout, 1);
206                 lat = new Lat(layout, 2);
207                 lon = new Lon(layout, 3);
208                 height = new Height(layout, 4);
209                 speed = new Speed(layout, 5);
210                 accel = new Accel(layout, 6);
211         }
212 }