altosuilib: Add 'show marker' button to graphs
[fw/altos] / altosuilib / AltosUIEnable.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; 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.altosuilib_12;
20
21 import java.awt.*;
22 import java.awt.event.*;
23 import javax.swing.*;
24 import javax.swing.event.*;
25 import java.io.*;
26 import java.util.concurrent.*;
27 import java.util.*;
28 import org.altusmetrum.altoslib_12.*;
29
30 import org.jfree.ui.*;
31 import org.jfree.chart.*;
32 import org.jfree.chart.plot.*;
33 import org.jfree.chart.axis.*;
34 import org.jfree.chart.renderer.*;
35 import org.jfree.chart.renderer.xy.*;
36 import org.jfree.chart.labels.*;
37 import org.jfree.data.xy.*;
38 import org.jfree.data.*;
39
40 public class AltosUIEnable extends Container implements ChangeListener {
41
42         Insets          il, ir;
43         int             y;
44         int             x;
45         JCheckBox       imperial_units;
46         JCheckBox       show_shapes;
47         JLabel          speed_filter_label;
48         JSlider         speed_filter;
49         JLabel          accel_filter_label;
50         JSlider         accel_filter;
51         AltosFilterListener     filter_listener;
52         AltosShapeListener      shape_listener;
53
54         static final int max_rows = 14;
55
56         public void units_changed(boolean imperial_units) {
57                 if (this.imperial_units != null) {
58                         this.imperial_units.setSelected(imperial_units);
59                 }
60         }
61
62         class GraphElement implements ActionListener {
63                 AltosUIGrapher  grapher;
64                 JCheckBox       enable;
65                 String          name;
66
67                 public void actionPerformed(ActionEvent ae) {
68                         grapher.set_enable(enable.isSelected());
69                 }
70
71                 GraphElement (String name, AltosUIGrapher grapher, boolean enabled) {
72                         this.name = name;
73                         this.grapher = grapher;
74                         enable = new JCheckBox(name, enabled);
75                         grapher.set_enable(enabled);
76                         enable.addActionListener(this);
77                 }
78         }
79
80         LinkedList<GraphElement> elements = new LinkedList<GraphElement>();
81
82         public void add(String name, AltosUIGrapher grapher, boolean enabled) {
83
84                 GraphElement    e = new GraphElement(name, grapher, enabled);
85                 GridBagConstraints c = new GridBagConstraints();
86
87                 elements.add(e);
88
89                 /* Add element */
90                 c = new GridBagConstraints();
91                 c.gridx = x; c.gridy = y;
92                 c.fill = GridBagConstraints.HORIZONTAL;
93                 c.anchor = GridBagConstraints.CENTER;
94                 c.insets = ir;
95                 add(e.enable, c);
96
97                 /* Next row */
98                 y++;
99                 if (y == max_rows) {
100                         x++;
101                         y = 0;
102                 }
103         }
104
105         public void stateChanged(ChangeEvent e) {
106                 JSlider filter = (JSlider) e.getSource();
107                 if (!filter.getValueIsAdjusting()) {
108                         double  speed_value = (int) speed_filter.getValue() / 1000.0;
109                         double  accel_value = (int) accel_filter.getValue() / 1000.0;
110                         if (filter_listener != null) {
111                                 filter_listener.filter_changed(speed_value, accel_value);
112                         }
113                 }
114         }
115
116         public void set_shapes_visible(boolean visible) {
117                 System.out.printf("set shapes %b\n", visible);
118                 if (shape_listener != null)
119                         shape_listener.set_shapes_visible(visible);
120         }
121
122         public void register_shape_listener(AltosShapeListener shape_listener) {
123                 this.shape_listener = shape_listener;
124         }
125
126         public void add_units() {
127                 /* Imperial units setting */
128
129                 /* Add label */
130                 imperial_units = new JCheckBox("Imperial Units", AltosUIPreferences.imperial_units());
131                 imperial_units.addActionListener(new ActionListener() {
132                                 public void actionPerformed(ActionEvent e) {
133                                         JCheckBox item = (JCheckBox) e.getSource();
134                                         boolean enabled = item.isSelected();
135                                         AltosUIPreferences.set_imperial_units(enabled);
136                                 }
137                         });
138                 imperial_units.setToolTipText("Use Imperial units instead of metric");
139                 GridBagConstraints c = new GridBagConstraints();
140                 c.gridx = 0; c.gridy = 1000;
141                 c.fill = GridBagConstraints.NONE;
142                 c.anchor = GridBagConstraints.LINE_START;
143                 c.insets = il;
144                 add(imperial_units, c);
145
146                 show_shapes = new JCheckBox("Show Markers", false);
147                 show_shapes.addActionListener(new ActionListener() {
148                                 public void actionPerformed(ActionEvent e) {
149                                         JCheckBox item = (JCheckBox) e.getSource();
150                                         boolean enabled = item.isSelected();
151                                         set_shapes_visible(enabled);
152                                 }
153                         });
154                 show_shapes.setToolTipText("Show marker Use Imperial units instead of metric");
155                 c = new GridBagConstraints();
156                 c.gridx = 0; c.gridy = 1001;
157                 c.fill = GridBagConstraints.NONE;
158                 c.anchor = GridBagConstraints.LINE_START;
159                 c.insets = il;
160                 add(show_shapes, c);
161
162
163                 speed_filter_label = new JLabel("Speed Filter(ms)");
164                 c = new GridBagConstraints();
165                 c.gridx = 0; c.gridy = 1002;
166                 c.fill = GridBagConstraints.NONE;
167                 c.anchor = GridBagConstraints.LINE_START;
168                 c.insets = il;
169                 add(speed_filter_label, c);
170
171                 speed_filter = new JSlider(JSlider.HORIZONTAL, 0, 10000, (int) (filter_listener.speed_filter() * 1000.0));
172                 Hashtable<Integer,JLabel> label_table = new Hashtable<Integer,JLabel>();
173                 for (int i = 0; i <= 10000; i += 5000) {
174                         label_table.put(new Integer(i), new JLabel(String.format("%d", i)));
175                 }
176                 speed_filter.setPaintTicks(true);
177                 speed_filter.setMajorTickSpacing(1000);
178                 speed_filter.setMinorTickSpacing(250);
179                 speed_filter.setLabelTable(label_table);
180                 speed_filter.setPaintTrack(false);
181                 speed_filter.setSnapToTicks(true);
182                 speed_filter.setPaintLabels(true);
183                 speed_filter.addChangeListener(this);
184
185                 c = new GridBagConstraints();
186                 c.gridx = 1; c.gridy = 1002;
187                 c.gridwidth = 1000; c.gridheight = 1;
188                 c.fill = GridBagConstraints.BOTH;
189                 c.anchor = GridBagConstraints.LINE_START;
190                 c.insets = il;
191                 add(speed_filter, c);
192
193                 accel_filter_label = new JLabel("Acceleration Filter(ms)");
194                 c = new GridBagConstraints();
195                 c.gridx = 0; c.gridy = 1003;
196                 c.fill = GridBagConstraints.NONE;
197                 c.anchor = GridBagConstraints.LINE_START;
198                 c.insets = il;
199                 add(accel_filter_label, c);
200
201                 accel_filter = new JSlider(JSlider.HORIZONTAL, 0, 10000, (int) (filter_listener.accel_filter() * 1000.0));
202                 accel_filter.setPaintTicks(true);
203                 accel_filter.setMajorTickSpacing(1000);
204                 accel_filter.setMinorTickSpacing(250);
205                 accel_filter.setLabelTable(label_table);
206                 accel_filter.setPaintTrack(false);
207                 accel_filter.setSnapToTicks(true);
208                 accel_filter.setPaintLabels(true);
209                 accel_filter.addChangeListener(this);
210
211                 c = new GridBagConstraints();
212                 c.gridx = 1; c.gridy = 1003;
213                 c.gridwidth = 1000; c.gridheight = 1;
214                 c.fill = GridBagConstraints.BOTH;
215                 c.anchor = GridBagConstraints.LINE_START;
216                 c.insets = il;
217                 add(accel_filter, c);
218         }
219
220         public AltosUIEnable(AltosFilterListener filter_listener) {
221                 this.filter_listener = filter_listener;
222                 il = new Insets(4,4,4,4);
223                 ir = new Insets(4,4,4,4);
224                 x = 0;
225                 y = 0;
226                 setLayout(new GridBagLayout());
227                 add_units();
228         }
229 }