altos: ADS124S0X driver compiles now
[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_13;
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_13.*;
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          line_width_label;
48         JSpinner        line_width;
49         JLabel          speed_filter_label;
50         JSlider         speed_filter;
51         JLabel          accel_filter_label;
52         JSlider         accel_filter;
53         AltosFilterListener     filter_listener;
54         AltosShapeListener      shape_listener;
55
56         static final int max_rows = 14;
57
58         public void units_changed(boolean imperial_units) {
59                 if (this.imperial_units != null) {
60                         this.imperial_units.setSelected(imperial_units);
61                 }
62         }
63
64         class GraphElement implements ActionListener {
65                 AltosUIGrapher  grapher;
66                 JCheckBox       enable;
67                 String          name;
68
69                 public void actionPerformed(ActionEvent ae) {
70                         grapher.set_enable(enable.isSelected());
71                 }
72
73                 GraphElement (String name, AltosUIGrapher grapher, boolean enabled) {
74                         this.name = name;
75                         this.grapher = grapher;
76                         enable = new JCheckBox(name, enabled);
77                         grapher.set_enable(enabled);
78                         enable.addActionListener(this);
79                 }
80         }
81
82         LinkedList<GraphElement> elements = new LinkedList<GraphElement>();
83
84         public void add(String name, AltosUIGrapher grapher, boolean enabled) {
85
86                 GraphElement    e = new GraphElement(name, grapher, enabled);
87                 GridBagConstraints c = new GridBagConstraints();
88
89                 elements.add(e);
90
91                 /* Add element */
92                 c = new GridBagConstraints();
93                 c.gridx = x; c.gridy = y;
94                 c.fill = GridBagConstraints.HORIZONTAL;
95                 c.anchor = GridBagConstraints.CENTER;
96                 c.insets = ir;
97                 add(e.enable, c);
98
99                 /* Next row */
100                 y++;
101                 if (y == max_rows) {
102                         x++;
103                         y = 0;
104                 }
105         }
106
107         public void stateChanged(ChangeEvent e) {
108                 JSlider filter = (JSlider) e.getSource();
109                 if (!filter.getValueIsAdjusting()) {
110                         double  speed_value = (int) speed_filter.getValue() / 1000.0;
111                         double  accel_value = (int) accel_filter.getValue() / 1000.0;
112                         if (filter_listener != null) {
113                                 filter_listener.filter_changed(speed_value, accel_value);
114                         }
115                 }
116         }
117
118         public void set_shapes_visible(boolean visible) {
119                 if (shape_listener != null)
120                         shape_listener.set_shapes_visible(visible);
121         }
122
123         public void set_line_width(float width) {
124                 if (shape_listener != null)
125                         shape_listener.set_line_width(width);
126         }
127
128         public void register_shape_listener(AltosShapeListener shape_listener) {
129                 this.shape_listener = shape_listener;
130         }
131
132         public void add_units() {
133                 /* Imperial units setting */
134
135                 /* Add label */
136                 imperial_units = new JCheckBox("Imperial Units", AltosUIPreferences.imperial_units());
137                 imperial_units.addActionListener(new ActionListener() {
138                                 public void actionPerformed(ActionEvent e) {
139                                         JCheckBox item = (JCheckBox) e.getSource();
140                                         boolean enabled = item.isSelected();
141                                         AltosUIPreferences.set_imperial_units(enabled);
142                                 }
143                         });
144                 imperial_units.setToolTipText("Use Imperial units instead of metric");
145                 GridBagConstraints c = new GridBagConstraints();
146                 c.gridx = 0; c.gridy = 1000;
147                 c.fill = GridBagConstraints.NONE;
148                 c.anchor = GridBagConstraints.LINE_START;
149                 c.insets = il;
150                 add(imperial_units, c);
151
152                 show_shapes = new JCheckBox("Show Markers", false);
153                 show_shapes.addActionListener(new ActionListener() {
154                                 public void actionPerformed(ActionEvent e) {
155                                         JCheckBox item = (JCheckBox) e.getSource();
156                                         boolean enabled = item.isSelected();
157                                         set_shapes_visible(enabled);
158                                 }
159                         });
160                 show_shapes.setToolTipText("Show marker Use Imperial units instead of metric");
161                 c = new GridBagConstraints();
162                 c.gridx = 0; c.gridy = 1001;
163                 c.fill = GridBagConstraints.NONE;
164                 c.anchor = GridBagConstraints.LINE_START;
165                 c.insets = il;
166                 add(show_shapes, c);
167
168
169                 line_width_label = new JLabel("Line Width");
170                 c = new GridBagConstraints();
171                 c.gridx = 1; c.gridy = 1001;
172                 c.fill = GridBagConstraints.NONE;
173                 c.anchor = GridBagConstraints.LINE_START;
174                 c.insets = il;
175                 add(line_width_label, c);
176
177                 line_width = new JSpinner();
178                 line_width.setValue(new Integer(1));
179                 line_width.addChangeListener(new ChangeListener() {
180                                 public void stateChanged(ChangeEvent e) {
181                                         int w = (Integer) line_width.getValue();
182                                         if (w < 1) {
183                                                 w = 1;
184                                                 line_width.setValue(new Integer(w));
185                                         }
186                                         System.out.printf("line width set to %d\n", w);
187                                         set_line_width(w);
188                                 }
189                         });
190                 c = new GridBagConstraints();
191                 c.gridx = 2; c.gridy = 1001;
192                 c.fill = GridBagConstraints.NONE;
193                 c.anchor = GridBagConstraints.LINE_START;
194                 c.insets = il;
195                 add(line_width, c);
196
197                 speed_filter_label = new JLabel("Speed Filter(ms)");
198                 c = new GridBagConstraints();
199                 c.gridx = 0; c.gridy = 1002;
200                 c.fill = GridBagConstraints.NONE;
201                 c.anchor = GridBagConstraints.LINE_START;
202                 c.insets = il;
203                 add(speed_filter_label, c);
204
205                 speed_filter = new JSlider(JSlider.HORIZONTAL, 0, 10000, (int) (filter_listener.speed_filter() * 1000.0));
206                 Hashtable<Integer,JLabel> label_table = new Hashtable<Integer,JLabel>();
207                 for (int i = 0; i <= 10000; i += 5000) {
208                         label_table.put(new Integer(i), new JLabel(String.format("%d", i)));
209                 }
210                 speed_filter.setPaintTicks(true);
211                 speed_filter.setMajorTickSpacing(1000);
212                 speed_filter.setMinorTickSpacing(250);
213                 speed_filter.setLabelTable(label_table);
214                 speed_filter.setPaintTrack(false);
215                 speed_filter.setSnapToTicks(true);
216                 speed_filter.setPaintLabels(true);
217                 speed_filter.addChangeListener(this);
218
219                 c = new GridBagConstraints();
220                 c.gridx = 1; c.gridy = 1002;
221                 c.gridwidth = 1000; c.gridheight = 1;
222                 c.fill = GridBagConstraints.BOTH;
223                 c.anchor = GridBagConstraints.LINE_START;
224                 c.insets = il;
225                 add(speed_filter, c);
226
227                 accel_filter_label = new JLabel("Acceleration Filter(ms)");
228                 c = new GridBagConstraints();
229                 c.gridx = 0; c.gridy = 1003;
230                 c.fill = GridBagConstraints.NONE;
231                 c.anchor = GridBagConstraints.LINE_START;
232                 c.insets = il;
233                 add(accel_filter_label, c);
234
235                 accel_filter = new JSlider(JSlider.HORIZONTAL, 0, 10000, (int) (filter_listener.accel_filter() * 1000.0));
236                 accel_filter.setPaintTicks(true);
237                 accel_filter.setMajorTickSpacing(1000);
238                 accel_filter.setMinorTickSpacing(250);
239                 accel_filter.setLabelTable(label_table);
240                 accel_filter.setPaintTrack(false);
241                 accel_filter.setSnapToTicks(true);
242                 accel_filter.setPaintLabels(true);
243                 accel_filter.addChangeListener(this);
244
245                 c = new GridBagConstraints();
246                 c.gridx = 1; c.gridy = 1003;
247                 c.gridwidth = 1000; c.gridheight = 1;
248                 c.fill = GridBagConstraints.BOTH;
249                 c.anchor = GridBagConstraints.LINE_START;
250                 c.insets = il;
251                 add(accel_filter, c);
252         }
253
254         public AltosUIEnable(AltosFilterListener filter_listener) {
255                 this.filter_listener = filter_listener;
256                 il = new Insets(4,4,4,4);
257                 ir = new Insets(4,4,4,4);
258                 x = 0;
259                 y = 0;
260                 setLayout(new GridBagLayout());
261                 add_units();
262         }
263 }