2 * Copyright © 2017 Keith Packard <keithp@keithp.com>
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 3 of the License, or
7 * (at your option) any later version.
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.
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 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
19 package org.altusmetrum.altosuilib_12;
22 import java.awt.event.*;
25 import javax.swing.event.*;
26 import org.altusmetrum.altoslib_12.*;
28 public class AltosUIAccelCal
30 implements AltosAccelCalListener, ActionListener
35 AltosConfigValues config_values;
44 int accel_plus, accel_minus;
46 private void make_visible() {
52 public boolean doit() {
58 public int accel_cal_plus() {
61 return AltosLib.MISSING;
64 public int accel_cal_minus() {
67 return AltosLib.MISSING;
70 private void setDefaultButton(JButton button) {
71 this.getRootPane().setDefaultButton(button);
74 /* AltosAccelCalListener interface */
75 public void set_thread(AltosAccelCal cal, Thread thread) {
79 public void set_phase(AltosAccelCal cal, final int phase) {
80 SwingUtilities.invokeLater(new Runnable() {
83 case AltosAccelCal.phase_antenna_up:
84 message.setText("Orient antenna upwards and click on Antenna Up");
85 antenna_up.setEnabled(true);
86 setDefaultButton(antenna_up);
87 antenna_down.setEnabled(false);
90 case AltosAccelCal.phase_antenna_down:
91 message.setText("Orient antenna downwards and click on Antenna Down");
92 antenna_up.setEnabled(false);
93 antenna_down.setEnabled(true);
94 setDefaultButton(antenna_down);
102 public void cal_done(AltosAccelCal cal, int plus, int minus) {
106 SwingUtilities.invokeLater(new Runnable() {
108 message.setText(String.format("Calibration succeeded, plus %d minus %d, press OK to continue", accel_plus, accel_minus));
109 antenna_up.setEnabled(false);
110 antenna_down.setEnabled(false);
112 setDefaultButton(ok);
117 public void message(AltosAccelCal cal, final String msg) {
118 SwingUtilities.invokeLater(new Runnable() {
120 message.setText(msg);
125 public void error(AltosAccelCal cal, String msg) {
129 /* ActionListener interface */
130 public void actionPerformed(ActionEvent e) {
131 String cmd = e.getActionCommand();
133 if ("up".equals(cmd)) {
135 antenna_up.setEnabled(false);
136 } else if ("down".equals(cmd)) {
138 antenna_down.setEnabled(false);
139 this.setDefaultButton(antenna_down);
140 } else if ("ok".equals(cmd)) {
142 this.setVisible(false);
144 config_values.set_accel_cal(accel_plus, accel_minus);
145 config_values.set_dirty();
149 } catch (InterruptedException ie) {
151 } else if ("cancel".equals(cmd)) {
153 this.setVisible(false);
156 } catch (InterruptedException ie) {
160 public AltosUIAccelCal(Frame owner, AltosLink link, AltosConfigValues config_values) {
161 super(owner, "Calibrate Accelerometer", true);
165 this.config_values = config_values;
167 pane = getContentPane();
168 pane.setLayout(new GridBagLayout());
170 GridBagConstraints c = new GridBagConstraints();
171 c.insets = new Insets(4,4,4,4);
175 c.fill = GridBagConstraints.HORIZONTAL;
176 c.anchor = GridBagConstraints.WEST;
183 message = new JTextField(64);
184 pane.add(message, c);
188 c.fill = GridBagConstraints.NONE;
189 c.anchor = GridBagConstraints.WEST;
196 antenna_up = new JButton("Antenna Up");
197 antenna_up.setActionCommand("up");
198 antenna_up.setEnabled(false);
199 antenna_up.addActionListener(this);
200 pane.add(antenna_up, c);
203 c.fill = GridBagConstraints.NONE;
204 c.anchor = GridBagConstraints.WEST;
211 antenna_down = new JButton("Antenna Down");
212 antenna_down.setActionCommand("down");
213 antenna_down.setEnabled(false);
214 antenna_down.addActionListener(this);
215 pane.add(antenna_down, c);
218 c.fill = GridBagConstraints.NONE;
219 c.anchor = GridBagConstraints.WEST;
226 ok = new JButton("OK");
227 ok.setActionCommand("ok");
228 ok.setEnabled(false);
229 ok.addActionListener(this);
233 c.fill = GridBagConstraints.NONE;
234 c.anchor = GridBagConstraints.WEST;
241 cancel = new JButton("Cancel");
242 cancel.setActionCommand("cancel");
243 cancel.setEnabled(true);
244 cancel.addActionListener(this);
247 cal = new AltosAccelCal(this.link, this);