5314a3b6fb025e837593fc7b6653c3eac8683db5
[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_11.*;
28 import org.altusmetrum.altosuilib_11.*;
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
35 {
36         JTabbedPane             pane;
37         AltosGraphNew           graph;
38         AltosUIEnable           enable;
39         AltosUIMap              map;
40         AltosFlightStats        stats;
41         AltosFlightStatsTable   statsTable;
42         AltosGPS                gps;
43         boolean                 has_gps;
44
45         void fill_map(AltosFlightSeries flight_series) {
46                 boolean                 any_gps = false;
47
48                 for (AltosGPSTimeValue gtv : flight_series.gps_series) {
49                         AltosGPS gps = gtv.gps;
50                         if (gps != null &&
51                             gps.locked &&
52                             gps.nsat >= 4) {
53                                 if (map == null)
54                                         map = new AltosUIMap();
55                                 map.show(gps, AltosLib.ao_flight_pad);
56                                 this.gps = gps;
57                                 has_gps = true;
58                         }
59                 }
60         }
61
62         public void font_size_changed(int font_size) {
63                 if (map != null)
64                         map.font_size_changed(font_size);
65                 if (statsTable != null)
66                         statsTable.font_size_changed(font_size);
67         }
68
69         public void units_changed(boolean imperial_units) {
70                 if (map != null)
71                         map.units_changed(imperial_units);
72                 if (enable != null)
73                         enable.units_changed(imperial_units);
74         }
75
76         AltosGraphUI(AltosRecordSet set, File file) throws InterruptedException, IOException {
77                 super(file.getName());
78                 AltosCalData    cal_data = set.cal_data();
79
80
81                 pane = new JTabbedPane();
82
83                 enable = new AltosUIEnable();
84
85                 AltosUIFlightSeries flight_series = new AltosUIFlightSeries(cal_data);
86
87                 set.capture_series(flight_series);
88
89                 flight_series.fill_in();
90
91                 stats = new AltosFlightStats(flight_series);
92
93                 graph = new AltosGraphNew(enable, stats, flight_series, cal_data);
94
95                 statsTable = new AltosFlightStatsTable(stats);
96
97                 pane.add("Flight Graph", graph.panel);
98                 pane.add("Configure Graph", enable);
99                 pane.add("Flight Statistics", statsTable);
100
101                 has_gps = false;
102                 fill_map(flight_series);
103                 if (has_gps)
104                         pane.add("Map", map);
105
106                 setContentPane (pane);
107
108                 AltosUIPreferences.register_font_listener(this);
109                 AltosPreferences.register_units_listener(this);
110
111                 addWindowListener(new WindowAdapter() {
112                                 @Override
113                                 public void windowClosing(WindowEvent e) {
114                                         AltosUIPreferences.unregister_font_listener(AltosGraphUI.this);
115                                         AltosPreferences.unregister_units_listener(AltosGraphUI.this);
116                                 }
117                         });
118                 pack();
119
120                 setVisible(true);
121                 if (gps != null)
122                         map.centre(gps);
123         }
124 }