b4d8d81e0631f8248785ef62f42e862e79a55b1a
[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; 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_8;
19
20 import java.awt.*;
21 import java.awt.event.*;
22 import javax.swing.*;
23 import java.io.*;
24 import java.util.concurrent.*;
25 import java.util.*;
26 import org.altusmetrum.altoslib_8.*;
27
28 import org.jfree.ui.*;
29 import org.jfree.chart.*;
30 import org.jfree.chart.plot.*;
31 import org.jfree.chart.axis.*;
32 import org.jfree.chart.renderer.*;
33 import org.jfree.chart.renderer.xy.*;
34 import org.jfree.chart.labels.*;
35 import org.jfree.data.xy.*;
36 import org.jfree.data.*;
37
38 public class AltosUIEnable extends Container {
39
40         Insets          il, ir;
41         int             y;
42         int             x;
43         JCheckBox       imperial_units;
44
45         static final int max_rows = 14;
46
47         public void units_changed(boolean imperial_units) {
48                 if (this.imperial_units != null) {
49                         this.imperial_units.setSelected(imperial_units);
50                 }
51         }
52
53         class GraphElement implements ActionListener {
54                 AltosUIGrapher  grapher;
55                 JCheckBox       enable;
56                 String          name;
57
58                 public void actionPerformed(ActionEvent ae) {
59                         grapher.set_enable(enable.isSelected());
60                 }
61
62                 GraphElement (String name, AltosUIGrapher grapher, boolean enabled) {
63                         this.name = name;
64                         this.grapher = grapher;
65                         enable = new JCheckBox(name, enabled);
66                         grapher.set_enable(enabled);
67                         enable.addActionListener(this);
68                 }
69         }
70
71         public void add(String name, AltosUIGrapher grapher, boolean enabled) {
72
73                 GraphElement    e = new GraphElement(name, grapher, enabled);
74                 GridBagConstraints c = new GridBagConstraints();
75
76                 /* Add element */
77                 c = new GridBagConstraints();
78                 c.gridx = x; c.gridy = y;
79                 c.fill = GridBagConstraints.HORIZONTAL;
80                 c.anchor = GridBagConstraints.CENTER;
81                 c.insets = ir;
82                 add(e.enable, c);
83
84                 /* Next row */
85                 y++;
86                 if (y == max_rows) {
87                         x++;
88                         y = 0;
89                 }
90         }
91
92         public void add_units() {
93                 /* Imperial units setting */
94
95                 /* Add label */
96                 imperial_units = new JCheckBox("Imperial Units", AltosUIPreferences.imperial_units());
97                 imperial_units.addActionListener(new ActionListener() {
98                                 public void actionPerformed(ActionEvent e) {
99                                         JCheckBox item = (JCheckBox) e.getSource();
100                                         boolean enabled = item.isSelected();
101                                         AltosUIPreferences.set_imperial_units(enabled);
102                                 }
103                         });
104                 imperial_units.setToolTipText("Use Imperial units instead of metric");
105                 GridBagConstraints c = new GridBagConstraints();
106                 c.gridx = 0; c.gridy = 1000;
107                 c.fill = GridBagConstraints.NONE;
108                 c.anchor = GridBagConstraints.LINE_START;
109                 c.insets = il;
110                 add(imperial_units, c);
111         }
112
113         public AltosUIEnable() {
114                 il = new Insets(4,4,4,4);
115                 ir = new Insets(4,4,4,4);
116                 x = 0;
117                 y = 0;
118                 setLayout(new GridBagLayout());
119                 add_units();
120         }
121 }