2 * Copyright © 2012 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 2 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 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
23 import java.awt.event.*;
25 import javax.swing.event.*;
26 import org.altusmetrum.altoslib_11.*;
27 import org.altusmetrum.altosuilib_11.*;
29 public class AltosConfigPyroUI
31 implements ItemListener, DocumentListener, AltosUnitsListener, ActionListener
36 static Insets il = new Insets(4,4,4,4);
37 static Insets ir = new Insets(4,4,4,4);
39 static String[] state_names;
41 static void make_state_names() {
42 if (state_names == null) {
44 state_names = new String[AltosLib.ao_flight_landed - AltosLib.ao_flight_boost + 1];
45 for (int state = AltosLib.ao_flight_boost; state <= AltosLib.ao_flight_landed; state++)
46 state_names[state - AltosLib.ao_flight_boost] = AltosLib.state_name_capital(state);
50 class PyroItem implements ItemListener, DocumentListener, AltosUnitsListener
53 public JCheckBox enable;
54 public JTextField value;
55 public JComboBox<String> combo;
59 public void set_enable(boolean enable) {
61 value.setEnabled(enable);
63 combo.setEnabled(enable);
66 public void itemStateChanged(ItemEvent e) {
67 set_enable(enable.isSelected());
72 public void changedUpdate(DocumentEvent e) {
77 public void insertUpdate(DocumentEvent e) {
82 public void removeUpdate(DocumentEvent e) {
87 public void units_changed(boolean imperial_units) {
88 AltosUnits units = AltosPyro.pyro_to_units(flag);
92 double v = units.parse_locale(value.getText(), !imperial_units);
94 } catch (ParseException pe) {
100 public void set(boolean new_enable, double new_value) {
102 enable.setSelected(new_enable);
103 set_enable(new_enable);
105 double scale = AltosPyro.pyro_to_scale(flag);
106 double unit_value = new_value;
107 AltosUnits units = AltosPyro.pyro_to_units(flag);
109 unit_value = units.parse_value(new_value);
113 else if (scale >= 10)
117 value.setText(String.format(format, unit_value));
120 if (new_value >= AltosLib.ao_flight_boost && new_value <= AltosLib.ao_flight_landed)
121 combo.setSelectedIndex((int) new_value - AltosLib.ao_flight_boost);
125 public boolean enabled() {
126 return enable.isSelected();
129 public double value() throws AltosConfigDataException {
131 AltosUnits units = AltosPyro.pyro_to_units(flag);
134 return units.parse_locale(value.getText());
135 return AltosParse.parse_double_locale(value.getText());
136 } catch (ParseException e) {
137 throw new AltosConfigDataException("\"%s\": %s\n", value.getText(), e.getMessage());
141 return combo.getSelectedIndex() + AltosLib.ao_flight_boost;
145 public PyroItem(AltosConfigPyroUI in_ui, int in_flag, int x, int y) {
150 GridBagConstraints c;
151 c = new GridBagConstraints();
152 c.gridx = x; c.gridy = y;
154 c.fill = GridBagConstraints.NONE;
155 c.anchor = GridBagConstraints.LINE_START;
157 enable = new JCheckBox();
158 enable.addItemListener(this);
161 if ((flag & AltosPyro.pyro_no_value) == 0) {
162 c = new GridBagConstraints();
163 c.gridx = x+1; c.gridy = y;
165 c.fill = GridBagConstraints.NONE;
166 c.anchor = GridBagConstraints.LINE_START;
168 if ((flag & AltosPyro.pyro_state_value) != 0) {
170 combo = new JComboBox<String>(state_names);
171 combo.addItemListener(this);
174 value = new JTextField(10);
175 value.getDocument().addDocumentListener(this);
182 class PyroColumn implements AltosUnitsListener {
183 public PyroItem[] items;
187 public void set(AltosPyro pyro) {
189 for (int flag = 1; flag < AltosPyro.pyro_all; flag <<= 1) {
190 if ((AltosPyro.pyro_all & flag) != 0) {
191 items[row].set((pyro.flags & flag) != 0,
192 pyro.get_value(flag));
198 public AltosPyro get() throws AltosConfigDataException {
199 AltosPyro p = new AltosPyro(channel);
202 for (int flag = 1; flag < AltosPyro.pyro_all; flag <<= 1) {
203 if ((AltosPyro.pyro_all & flag) != 0) {
204 if (items[row].enabled()) {
207 p.set_value(flag, items[row].value());
208 } catch (AltosConfigDataException ae) {
209 throw new AltosConfigDataException("%s, %s",
210 AltosPyro.pyro_to_name(flag),
220 public void units_changed(boolean imperial_units) {
222 for (int flag = 1; flag < AltosPyro.pyro_all; flag <<= 1) {
223 if ((AltosPyro.pyro_all & flag) != 0) {
224 items[row].units_changed(imperial_units);
230 public PyroColumn(AltosConfigPyroUI ui, int x, int y, int in_channel) {
232 channel = in_channel;
235 for (int flag = 1; flag < AltosPyro.pyro_all; flag <<= 1)
236 if ((flag & AltosPyro.pyro_all) != 0)
239 items = new PyroItem[nrow];
242 GridBagConstraints c;
243 c = new GridBagConstraints();
244 c.gridx = x; c.gridy = y;
246 c.fill = GridBagConstraints.NONE;
247 c.anchor = GridBagConstraints.CENTER;
249 label = new JLabel(String.format("Pyro Channel %c", 'A' + channel));
253 for (int flag = 1; flag < AltosPyro.pyro_all; flag <<= 1)
254 if ((flag & AltosPyro.pyro_all) != 0) {
255 items[row] = new PyroItem(ui, flag, x, y + row);
261 PyroColumn[] columns;
264 public void set_pyros(AltosPyro[] pyros) {
265 for (int i = 0; i < pyros.length; i++) {
266 if (pyros[i].channel < columns.length)
267 columns[pyros[i].channel].set(pyros[i]);
271 public AltosPyro[] get_pyros() throws AltosConfigDataException {
272 AltosPyro[] pyros = new AltosPyro[columns.length];
273 for (int c = 0; c < columns.length; c++) {
275 pyros[c] = columns[c].get();
276 } catch (AltosConfigDataException ae) {
277 throw new AltosConfigDataException ("Channel %c, %s", c + 'A', ae.getMessage());
283 JLabel pyro_firing_time_label;
284 JComboBox<String> pyro_firing_time_value;
286 static String[] pyro_firing_time_values = {
287 "0.050", "0.100", "0.250", "0.500", "1.0", "2.0"
290 boolean initializing;
292 public void set_pyro_firing_time(double new_pyro_firing_time) {
294 pyro_firing_time_value.setSelectedItem(Double.toString(new_pyro_firing_time));
295 pyro_firing_time_value.setEnabled(new_pyro_firing_time >= 0);
296 initializing = false;
299 public double get_pyro_firing_time() throws AltosConfigDataException {
300 String v = pyro_firing_time_value.getSelectedItem().toString();
303 return AltosParse.parse_double_locale(v);
304 } catch (ParseException e) {
305 throw new AltosConfigDataException("Invalid pyro firing time \"%s\"", v);
309 public void set_dirty() {
314 public void itemStateChanged(ItemEvent e) {
319 public void changedUpdate(DocumentEvent e) {
324 public void insertUpdate(DocumentEvent e) {
329 public void removeUpdate(DocumentEvent e) {
334 public void units_changed(boolean imperial_units) {
335 for (int c = 0; c < columns.length; c++)
336 columns[c].units_changed(imperial_units);
338 for (int flag = 1; flag <= AltosPyro.pyro_all; flag <<= 1) {
339 String n = AltosPyro.pyro_to_name(flag);
341 labels[r].setText(n);
347 /* A window listener to catch closing events and tell the config code */
348 class ConfigListener extends WindowAdapter {
349 AltosConfigPyroUI ui;
352 public ConfigListener(AltosConfigPyroUI this_ui, AltosConfigUI this_owner) {
357 public void windowClosing(WindowEvent e) {
358 ui.setVisible(false);
362 /* Listen for events from our buttons */
363 public void actionPerformed(ActionEvent e) {
364 String cmd = e.getActionCommand();
366 if (cmd.equals("Close"))
370 public AltosConfigPyroUI(AltosConfigUI in_owner, AltosPyro[] pyros, double pyro_firing_time) {
372 super(in_owner, "Configure Pyro Channels", false);
376 GridBagConstraints c;
378 pane = getContentPane();
379 pane.setLayout(new GridBagLayout());
382 for (int flag = 1; flag < AltosPyro.pyro_all; flag <<= 1)
383 if ((flag & AltosPyro.pyro_all) != 0)
386 labels = new JLabel[nrow];
390 for (int flag = 1; flag <= AltosPyro.pyro_all; flag <<= 1) {
393 n = AltosPyro.pyro_to_name(flag);
395 c = new GridBagConstraints();
396 c.gridx = 0; c.gridy = row;
398 c.fill = GridBagConstraints.NONE;
399 c.anchor = GridBagConstraints.LINE_START;
401 JLabel label = new JLabel(n);
403 labels[row-1] = label;
408 columns = new PyroColumn[pyros.length];
410 for (int i = 0; i < pyros.length; i++) {
411 columns[i] = new PyroColumn(this, i*2 + 1, 0, i);
412 columns[i].set(pyros[i]);
415 /* Pyro firing time */
416 c = new GridBagConstraints();
417 c.gridx = 0; c.gridy = row;
419 c.fill = GridBagConstraints.NONE;
420 c.anchor = GridBagConstraints.LINE_START;
423 pyro_firing_time_label = new JLabel("Pyro Firing Time(s):");
424 pane.add(pyro_firing_time_label, c);
426 c = new GridBagConstraints();
427 c.gridx = 2; c.gridy = row;
429 c.fill = GridBagConstraints.HORIZONTAL;
431 c.anchor = GridBagConstraints.LINE_START;
434 pyro_firing_time_value = new JComboBox<String>(pyro_firing_time_values);
435 pyro_firing_time_value.setEditable(true);
436 pyro_firing_time_value.addItemListener(this);
437 set_pyro_firing_time(pyro_firing_time);
438 pane.add(pyro_firing_time_value, c);
439 pyro_firing_time_value.setToolTipText("Length of extra pyro channel firing pulse");
441 c = new GridBagConstraints();
442 c.gridx = pyros.length*2-1;
443 c.fill = GridBagConstraints.HORIZONTAL;
446 JButton close = new JButton("Close");
448 close.addActionListener(this);
449 close.setActionCommand("Close");
451 addWindowListener(new ConfigListener(this, owner));
452 AltosPreferences.register_units_listener(this);
455 public void dispose() {
456 AltosPreferences.unregister_units_listener(this);
460 public void make_visible() {