Merge remote-tracking branch 'origin/master'
[fw/altos] / altosuilib / AltosUIMarker.java
1 /*
2  * Copyright © 2013 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; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 package org.altusmetrum.altosuilib_1;
19
20 import java.io.*;
21 import java.util.ArrayList;
22
23 import java.awt.*;
24 import javax.swing.*;
25 import org.altusmetrum.altoslib_2.*;
26
27 import org.jfree.ui.*;
28 import org.jfree.chart.*;
29 import org.jfree.chart.plot.*;
30 import org.jfree.chart.axis.*;
31 import org.jfree.chart.renderer.*;
32 import org.jfree.chart.renderer.xy.*;
33 import org.jfree.chart.labels.*;
34 import org.jfree.data.xy.*;
35 import org.jfree.data.*;
36
37 public class AltosUIMarker implements AltosUIGrapher {
38         ArrayList<ValueMarker>  markers;
39         int                     last_id;
40         XYPlot                  plot;
41         boolean                 enabled;
42         int                     fetch;
43         Color                   color;
44         
45         private void remove_markers() {
46                 for (ValueMarker marker : markers)
47                         plot.removeDomainMarker(marker);
48         }
49
50         private void add_markers() {
51                 for (ValueMarker marker : markers)
52                         plot.addDomainMarker(marker);
53         }
54
55         public void set_units() {
56         }
57
58         public void set_enable(boolean enable) {
59                 if (enabled == enable)
60                         return;
61                 if (enable)
62                         add_markers();
63                 else
64                         remove_markers();
65                 enabled = enable;
66         }
67
68         public void clear() {
69                 if (enabled)
70                         remove_markers();
71                 markers = new ArrayList<ValueMarker>();
72         }
73
74         public void add(AltosUIDataPoint dataPoint) {
75                 try {
76                         int id = dataPoint.id(fetch);
77                         if (id < 0)
78                                 return;
79                         if (id == last_id)
80                                 return;
81                         ValueMarker marker = new ValueMarker(dataPoint.x());
82                         marker.setLabel(dataPoint.id_name(fetch));
83                         marker.setLabelAnchor(RectangleAnchor.TOP_RIGHT);
84                         marker.setLabelTextAnchor(TextAnchor.TOP_LEFT);
85                         marker.setPaint(color);
86                         if (enabled)
87                                 plot.addDomainMarker(marker);
88                         markers.add(marker);
89                         last_id = id;
90                 } catch (AltosUIDataMissing m) {
91                 }
92         }
93
94         public AltosUIMarker (int fetch, Color color, XYPlot plot, boolean enable) {
95                 markers = new ArrayList<ValueMarker>();
96                 last_id = -1;
97                 this.fetch = fetch;
98                 this.color = color;
99                 this.plot = plot;
100                 this.enabled = enable;
101         }
102
103         public void setNotify(boolean notify) {
104         }
105
106         public void fireSeriesChanged() {
107         }
108
109         public AltosUIMarker (int fetch, Color color, XYPlot plot) {
110                 this(fetch, color, plot, true);
111         }
112 }