8f52728bf13cfe1ea9d6c5e95a0e104d341e3440
[fw/altos] / altosui / AltosGraphUI.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 altosui;
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 org.altusmetrum.altoslib_14.*;
28 import org.altusmetrum.altosuilib_14.*;
29
30 import org.jfree.chart.ChartPanel;
31 import org.jfree.chart.JFreeChart;
32 import org.jfree.ui.RefineryUtilities;
33
34 public class AltosGraphUI extends AltosUIFrame implements AltosFontListener, AltosUnitsListener, AltosFilterListener
35 {
36         JTabbedPane             pane;
37         AltosGraph              graph;
38         AltosUIEnable           enable;
39         AltosUIMap              map;
40         AltosFlightStats        stats;
41         AltosFlightStatsTable   statsTable;
42         AltosFlightConfigTable  configTable;
43         AltosFlightPyroTable    pyroTable;
44         AltosGPS                gps;
45         boolean                 has_gps;
46
47         void fill_map(AltosFlightSeries flight_series) {
48                 boolean                 any_gps = false;
49                 AltosGPSTimeValue       gtv_last = null;
50                 double gps_pad_altitude = flight_series.cal_data().gps_pad_altitude;;
51
52                 if (flight_series.gps_series != null) {
53                         for (AltosGPSTimeValue gtv : flight_series.gps_series) {
54                                 AltosGPS gps = gtv.gps;
55                                 if (gps != null &&
56                                     gps.locked &&
57                                     gps.nsat >= 4) {
58                                         if (map == null)
59                                                 map = new AltosUIMap();
60                                         double gps_height = gps.alt - gps_pad_altitude;
61                                         int state = (int) flight_series.value_before(AltosFlightSeries.state_name, gtv.time);
62                                         map.show(gps, gtv.time, state, gps_height);
63                                         this.gps = gps;
64                                         gtv_last = gtv;
65                                         has_gps = true;
66                                 }
67                         }
68                 }
69                 if (gtv_last != null) {
70                         int state = (int) flight_series.value_after(AltosFlightSeries.state_name, gtv_last.time);
71                         double gps_height = gps.alt - gps_pad_altitude;
72                         if (state == AltosLib.ao_flight_landed)
73                                 map.show(gtv_last.gps, gtv_last.time, state,gps_height);
74                 }
75         }
76
77         public void font_size_changed(int font_size) {
78                 if (map != null)
79                         map.font_size_changed(font_size);
80                 if (statsTable != null)
81                         statsTable.font_size_changed(font_size);
82                 if (configTable != null)
83                         configTable.font_size_changed(font_size);
84                 if (pyroTable != null)
85                         pyroTable.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                 if (configTable != null)
94                         configTable.units_changed(imperial_units);
95                 if (pyroTable != null)
96                         pyroTable.units_changed(imperial_units);
97         }
98
99         AltosUIFlightSeries flight_series;
100
101         public void filter_changed(double speed_filter, double accel_filter) {
102                 flight_series.set_filter(speed_filter, accel_filter);
103                 graph.filter_changed();
104                 stats = new AltosFlightStats(flight_series);
105                 statsTable.filter_changed(stats);
106         }
107
108         public double speed_filter() {
109                 return flight_series.speed_filter_width;
110         }
111
112         public double accel_filter() {
113                 return flight_series.accel_filter_width;
114         }
115
116         AltosGraphUI(AltosRecordSet set, File file) throws InterruptedException, IOException {
117                 super(file.getName());
118                 AltosCalData    cal_data = set.cal_data();
119                 AltosConfigData config_data = set.config_data();
120
121                 pane = new JTabbedPane();
122
123                 flight_series = new AltosUIFlightSeries(cal_data);
124
125                 enable = new AltosUIEnable(this);
126
127                 set.capture_series(flight_series);
128
129                 flight_series.finish();
130
131                 stats = new AltosFlightStats(flight_series);
132
133                 graph = new AltosGraph(enable, stats, flight_series);
134
135                 statsTable = new AltosFlightStatsTable(stats);
136
137                 pane.add("Flight Graph", graph.panel);
138                 pane.add("Configure Graph", enable);
139                 pane.add("Flight Statistics", statsTable);
140                 if (config_data != null) {
141                         configTable = new AltosFlightConfigTable(config_data);
142                         pane.add("Configuration", configTable);
143                         if (config_data.npyro > 0) {
144                                 pyroTable = new AltosFlightPyroTable(config_data.pyros, config_data.npyro);
145                                 pane.add("Pyros", pyroTable);
146                         }
147                 }
148
149                 has_gps = false;
150                 fill_map(flight_series);
151                 if (has_gps)
152                         pane.add("Map", map);
153
154                 setContentPane (pane);
155
156                 AltosUIPreferences.register_font_listener(this);
157                 AltosPreferences.register_units_listener(this);
158
159                 addWindowListener(new WindowAdapter() {
160                                 @Override
161                                 public void windowClosing(WindowEvent e) {
162                                         AltosUIPreferences.unregister_font_listener(AltosGraphUI.this);
163                                         AltosPreferences.unregister_units_listener(AltosGraphUI.this);
164                                 }
165                         });
166                 pack();
167
168                 setVisible(true);
169                 if (gps != null)
170                         map.centre(gps);
171         }
172 }