altos/stm32f4-disco: Start hooking up stm32f413 USB for the disco board
[fw/altos] / telegps / TeleGPSGraphUI.java
1 /*
2  * Copyright © 2010 Anthony Towns
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.io.*;
22 import java.util.ArrayList;
23
24 import java.awt.*;
25 import java.awt.event.*;
26 import javax.swing.*;
27 import java.io.*;
28 import java.util.concurrent.*;
29 import java.util.*;
30 import org.altusmetrum.altoslib_13.*;
31 import org.altusmetrum.altosuilib_13.*;
32
33 import org.jfree.chart.ChartPanel;
34 import org.jfree.chart.JFreeChart;
35 import org.jfree.ui.RefineryUtilities;
36
37 public class TeleGPSGraphUI extends AltosUIFrame implements AltosFontListener, AltosUnitsListener, AltosFilterListener
38 {
39         JTabbedPane             pane;
40         AltosGraph              graph;
41         AltosUIEnable           enable;
42         AltosUIMap              map;
43         AltosState              state;
44         AltosFlightStats        stats;
45         AltosFlightStatsTable   statsTable;
46         AltosGPS                gps;
47         boolean                 has_gps;
48
49         void fill_map(AltosFlightSeries flight_series) {
50                 boolean                 any_gps = false;
51                 AltosGPSTimeValue       gtv_last = null;
52
53                 if (flight_series.gps_series != null) {
54                         for (AltosGPSTimeValue gtv : flight_series.gps_series) {
55                                 gtv_last = gtv;
56                                 AltosGPS gps = gtv.gps;
57                                 if (gps != null &&
58                                     gps.locked &&
59                                     gps.nsat >= 4) {
60                                         if (map == null)
61                                                 map = new AltosUIMap();
62                                         map.show(gps, (int) flight_series.value_before(AltosFlightSeries.state_name, gtv.time));
63                                         this.gps = gps;
64                                         has_gps = true;
65                                 }
66                         }
67                 }
68                 if (gtv_last != null) {
69                         int state = (int) flight_series.value_after(AltosFlightSeries.state_name, gtv_last.time);
70                         if (state == AltosLib.ao_flight_landed)
71                                 map.show(gtv_last.gps, state);
72                 }
73         }
74
75         private void close() {
76                 setVisible(false);
77                 dispose();
78                 TeleGPS.subtract_window();
79         }
80
81         public void font_size_changed(int font_size) {
82                 if (map != null)
83                         map.font_size_changed(font_size);
84                 if (statsTable != null)
85                         statsTable.font_size_changed(font_size);
86         }
87
88         public void units_changed(boolean imperial_units) {
89                 if (map != null)
90                         map.units_changed(imperial_units);
91                 if (enable != null)
92                         enable.units_changed(imperial_units);
93         }
94
95         AltosUIFlightSeries flight_series;
96
97         public void filter_changed(double speed_filter, double accel_filter) {
98                 flight_series.set_filter(speed_filter, accel_filter);
99                 graph.filter_changed();
100                 stats = new AltosFlightStats(flight_series);
101                 statsTable.filter_changed(stats);
102         }
103
104         public double speed_filter() {
105                 return flight_series.speed_filter_width;
106         }
107
108         public double accel_filter() {
109                 return flight_series.accel_filter_width;
110         }
111
112         TeleGPSGraphUI(AltosRecordSet set, File file) throws InterruptedException, IOException {
113                 super(file.getName());
114                 AltosCalData cal_data = set.cal_data();
115
116                 flight_series = new AltosUIFlightSeries(cal_data);
117                 set.capture_series(flight_series);
118                 flight_series.finish();
119
120                 pane = new JTabbedPane();
121
122                 graph = new AltosGraph(enable, stats, flight_series);
123
124                 stats = new AltosFlightStats(flight_series);
125
126                 enable = new AltosUIEnable(this);
127
128                 statsTable = new AltosFlightStatsTable(stats);
129
130                 map = new AltosUIMap();
131
132                 pane.add("Graph", graph.panel);
133                 pane.add("Configure Graph", enable);
134                 pane.add("Statistics", statsTable);
135                 fill_map(flight_series);
136                 pane.add("Map", map);
137
138                 setContentPane (pane);
139
140                 AltosUIPreferences.register_font_listener(this);
141                 AltosPreferences.register_units_listener(this);
142
143                 addWindowListener(new WindowAdapter() {
144                                 @Override
145                                 public void windowClosing(WindowEvent e) {
146                                         close();
147                                 }
148                         });
149
150                 pack();
151
152                 setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
153
154                 TeleGPS.add_window();
155
156                 setVisible(true);
157
158                 if (state != null)
159                         map.centre(state);
160
161         }
162 }