Bump java lib versions in preparation for 1.9.2
[fw/altos] / telegps / TeleGPSInfo.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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 package org.altusmetrum.telegps;
20
21 import java.util.*;
22 import java.awt.*;
23 import java.awt.event.*;
24 import javax.swing.*;
25 import org.altusmetrum.altoslib_14.*;
26 import org.altusmetrum.altosuilib_14.*;
27
28 public class TeleGPSInfo extends AltosUIFlightTab {
29
30         JLabel                          cur, max;
31
32         abstract class Value extends AltosUIUnitsIndicator {
33                 public abstract void show(AltosState state, AltosListenerState listener_state);
34
35                 public Value (Container container, int y, AltosUnits units, String text) {
36                         super(container, y, units, text, 1, false, 2);
37                 }
38         }
39
40         abstract class DualValue extends AltosUIUnitsIndicator {
41                 public DualValue (Container container, int y, AltosUnits units, String text) {
42                         super(container, y, units, text, 2, false, 1);
43                 }
44         }
45
46         abstract class ValueHold extends DualValue {
47                 public void reset() {
48                         super.reset();
49                 }
50                 public ValueHold (Container container, int y, AltosUnits units, String text) {
51                         super(container, y, units, text);
52                 }
53         }
54
55         class Altitude extends ValueHold {
56                 public double value(AltosState state, int i) {
57                         if (i == 0)
58                                 return state.altitude();
59                         else
60                                 return state.max_altitude();
61                 }
62
63                 public Altitude (Container container, int y) {
64                         super (container, y, AltosConvert.height, "Altitude");
65                 }
66         }
67
68         class AscentRate extends ValueHold {
69                 public double value(AltosState state, int i) {
70                         if (i == 0)
71                                 return state.gps_ascent_rate();
72                         else
73                                 return state.max_gps_ascent_rate();
74                 }
75                 public AscentRate (Container container, int y) {
76                         super (container, y, AltosConvert.speed, "Ascent Rate");
77                 }
78         }
79
80         class GroundSpeed extends ValueHold {
81                 public double value(AltosState state, int i) {
82                         if (i == 0)
83                                 return state.gps_ground_speed();
84                         else
85                                 return state.max_gps_ground_speed();
86                 }
87                 public GroundSpeed (Container container, int y) {
88                         super (container, y, AltosConvert.speed, "Ground Speed");
89                 }
90         }
91
92         class Course extends AltosUIIndicator {
93
94                 public void show (AltosState state, AltosListenerState listener_state) {
95                         double  course = state.gps_course();
96                         if (course == AltosLib.MISSING)
97                                 show("Missing", "Missing");
98                         else
99                                 show( String.format("%3.0f°", course),
100                                       AltosConvert.bearing_to_words(
101                                               AltosConvert.BEARING_LONG,
102                                               course));
103                 }
104                 public Course (Container container, int y) {
105                         super (container, y, "Course", 2, false, 1);
106                 }
107         }
108
109         class Lat extends AltosUIIndicator {
110
111                 String pos(double p, String pos, String neg) {
112                         String  h = pos;
113                         if (p < 0) {
114                                 h = neg;
115                                 p = -p;
116                         }
117                         int deg = (int) Math.floor(p);
118                         double min = (p - Math.floor(p)) * 60.0;
119                         return String.format("%s %4d° %9.6f", h, deg, min);
120                 }
121
122                 public void show (AltosState state, AltosListenerState listener_state) {
123                         if (state.gps != null && state.gps.connected && state.gps.lat != AltosLib.MISSING)
124                                 show(pos(state.gps.lat,"N", "S"));
125                         else
126                                 show("Missing");
127                 }
128                 public Lat (Container container, int y) {
129                         super (container, y, "Latitude", 1, false, 2);
130                 }
131         }
132
133         class Lon extends AltosUIIndicator {
134
135                 String pos(double p, String pos, String neg) {
136                         String  h = pos;
137                         if (p < 0) {
138                                 h = neg;
139                                 p = -p;
140                         }
141                         int deg = (int) Math.floor(p);
142                         double min = (p - Math.floor(p)) * 60.0;
143                         return String.format("%s %4d° %9.6f", h, deg, min);
144                 }
145
146                 public void show (AltosState state, AltosListenerState listener_state) {
147                         if (state.gps != null && state.gps.connected && state.gps.lon != AltosLib.MISSING)
148                                 show(pos(state.gps.lon,"E", "W"));
149                         else
150                                 show("Missing");
151                 }
152                 public Lon (Container container, int y) {
153                         super (container, y, "Longitude", 1, false, 2);
154                 }
155         }
156
157         class GPSLocked extends AltosUIIndicator {
158
159                 public void show (AltosState state, AltosListenerState listener_state) {
160                         if (state == null || state.gps == null)
161                                 hide();
162                         else {
163                                 int soln = state.gps.nsat;
164                                 int nsat = state.gps.cc_gps_sat != null ? state.gps.cc_gps_sat.length : 0;
165                                 show("%4d in solution", soln,
166                                      "%4d in view", nsat);
167                                 set_lights(state.gps.locked && soln >= 4);
168                         }
169                 }
170                 public GPSLocked (Container container, int y) {
171                         super (container, y, "GPS Locked", 2, true, 1);
172                 }
173         }
174
175         public void font_size_changed(int font_size) {
176                 cur.setFont(AltosUILib.label_font);
177                 max.setFont(AltosUILib.label_font);
178                 super.font_size_changed(font_size);
179         }
180
181         public void labels(Container container, int y) {
182                 GridBagLayout           layout = (GridBagLayout)(container.getLayout());
183                 GridBagConstraints      c;
184
185                 cur = new JLabel("Current");
186                 cur.setFont(AltosUILib.label_font);
187                 c = new GridBagConstraints();
188                 c.gridx = 2; c.gridy = y;
189                 c.insets = new Insets(AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad);
190                 layout.setConstraints(cur, c);
191                 add(cur);
192
193                 max = new JLabel("Maximum");
194                 max.setFont(AltosUILib.label_font);
195                 c.gridx = 3; c.gridy = y;
196                 layout.setConstraints(max, c);
197                 add(max);
198         }
199
200         public String getName() {
201                 return "Location";
202         }
203
204         public TeleGPSInfo() {
205                 int y = 0;
206                 labels(this, y++);
207                 add(new Altitude(this, y++));
208                 add(new GroundSpeed(this, y++));
209                 add(new AscentRate(this, y++));
210                 add(new Course(this, y++));
211                 add(new Lat(this, y++));
212                 add(new Lon(this, y++));
213                 add(new GPSLocked(this, y++));
214         }
215 }