altoslib: Make sure AltosFlightSeries is filled in before use
[fw/altos] / altosuilib / AltosUIFlightSeries.java
1 /*
2  * Copyright © 2017 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
15 package org.altusmetrum.altosuilib_11;
16
17 import java.util.*;
18 import java.awt.*;
19 import javax.swing.*;
20 import org.altusmetrum.altoslib_11.*;
21
22 import org.jfree.ui.*;
23 import org.jfree.chart.*;
24 import org.jfree.chart.plot.*;
25 import org.jfree.chart.axis.*;
26 import org.jfree.chart.renderer.*;
27 import org.jfree.chart.renderer.xy.*;
28 import org.jfree.chart.labels.*;
29 import org.jfree.data.xy.*;
30 import org.jfree.data.*;
31
32 class AltosUITimeSeriesAxis {
33         Color           color;
34         boolean         enabled;
35         boolean         marker;
36         boolean         marker_top;
37         AltosUIAxis     axis;
38         XYPlot          plot;
39
40         public AltosUITimeSeriesAxis(Color color, boolean enabled, AltosUIAxis axis, XYPlot plot, boolean marker, boolean marker_top) {
41                 this.color = color;
42                 this.enabled = enabled;
43                 this.axis = axis;
44                 this.plot = plot;
45                 this.marker = marker;
46                 this.marker_top = marker_top;
47         }
48 }
49
50 public class AltosUIFlightSeries extends AltosFlightSeries {
51
52         Hashtable<String,AltosUITimeSeriesAxis> axes;
53
54         AltosUIFlightSeries flight_series;
55
56         void fill_axes(String label, AltosUITimeSeriesAxis axis) {
57                 for (AltosTimeSeries ts : series) {
58                         AltosUITimeSeries uts = (AltosUITimeSeries) ts;
59
60                         if (label.equals(ts.label) || (label.equals("default") && uts.color == null)) {
61                                 if (axis.marker)
62                                         uts.set_marker(axis.color, axis.enabled, axis.plot, axis.marker_top);
63                                 else
64                                         uts.set_axis(axis.color, axis.enabled, axis.axis);
65                         }
66                 }
67         }
68
69         public void register_axis(String label,
70                                   Color color,
71                                   boolean enabled,
72                                   AltosUIAxis axis) {
73                 AltosUITimeSeriesAxis tsa = new AltosUITimeSeriesAxis(color,
74                                                                       enabled,
75                                                                       axis,
76                                                                       null,
77                                                                       false,
78                                                                       false);
79                 axes.put(label, tsa);
80                 fill_axes(label, tsa);
81         }
82
83         public void register_marker(String label,
84                                     Color color,
85                                     boolean enabled,
86                                     XYPlot plot,
87                                     boolean marker_top) {
88                 AltosUITimeSeriesAxis tsa = new AltosUITimeSeriesAxis(color,
89                                                                       enabled,
90                                                                       null,
91                                                                       plot,
92                                                                       true,
93                                                                       marker_top);
94                 axes.put(label, tsa);
95                 fill_axes(label, tsa);
96         }
97
98         public AltosTimeSeries make_series(String label, AltosUnits units) {
99
100
101                 AltosUITimeSeries time_series = new AltosUITimeSeries(label, units);
102
103                 AltosUITimeSeriesAxis tsa = axes.get(label);
104                 if (tsa == null)
105                         tsa = axes.get("default");
106                 if (tsa != null) {
107                         if (tsa.marker)
108                                 time_series.set_marker(tsa.color, tsa.enabled, tsa.plot, tsa.marker_top);
109                         else
110                                 time_series.set_axis(tsa.color, tsa.enabled, tsa.axis);
111                 }
112                 return time_series;
113         }
114
115         public AltosUITimeSeries[] series(AltosCalData cal_data) {
116                 finish();
117                 return series.toArray(new AltosUITimeSeries[0]);
118         }
119
120         public AltosUIFlightSeries (AltosCalData cal_data) {
121                 super(cal_data);
122                 axes = new Hashtable<String,AltosUITimeSeriesAxis>();
123         }
124 }