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; version 2 of the License.
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.
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.
21 import java.awt.event.*;
23 import javax.swing.event.*;
24 import org.altusmetrum.altoslib_5.*;
25 import org.altusmetrum.altosuilib_3.*;
27 public class AltosConfigPyroUI
29 implements ItemListener, DocumentListener, AltosUnitsListener, ActionListener
34 static Insets il = new Insets(4,4,4,4);
35 static Insets ir = new Insets(4,4,4,4);
37 static String[] state_names;
39 static void make_state_names() {
40 if (state_names == null) {
42 state_names = new String[AltosLib.ao_flight_landed - AltosLib.ao_flight_boost + 1];
43 for (int state = AltosLib.ao_flight_boost; state <= AltosLib.ao_flight_landed; state++)
44 state_names[state - AltosLib.ao_flight_boost] = AltosLib.state_name_capital(state);
48 class PyroItem implements ItemListener, DocumentListener, AltosUnitsListener
51 public JCheckBox enable;
52 public JTextField value;
53 public JComboBox<String> combo;
57 public void set_enable(boolean enable) {
59 value.setEnabled(enable);
61 combo.setEnabled(enable);
64 public void itemStateChanged(ItemEvent e) {
65 set_enable(enable.isSelected());
70 public void changedUpdate(DocumentEvent e) {
75 public void insertUpdate(DocumentEvent e) {
80 public void removeUpdate(DocumentEvent e) {
85 public void units_changed(boolean imperial_units) {
86 AltosUnits units = AltosPyro.pyro_to_units(flag);
90 double v = units.parse(value.getText(), !imperial_units);
92 } catch (NumberFormatException ne) {
98 public void set(boolean new_enable, double new_value) {
100 enable.setSelected(new_enable);
101 set_enable(new_enable);
103 double scale = AltosPyro.pyro_to_scale(flag);
104 double unit_value = new_value;
105 AltosUnits units = AltosPyro.pyro_to_units(flag);
107 unit_value = units.value(new_value);
111 else if (scale >= 10)
115 value.setText(String.format(format, unit_value));
118 if (new_value >= AltosLib.ao_flight_boost && new_value <= AltosLib.ao_flight_landed)
119 combo.setSelectedIndex((int) new_value - AltosLib.ao_flight_boost);
123 public boolean enabled() {
124 return enable.isSelected();
127 public double value() throws AltosConfigDataException {
129 AltosUnits units = AltosPyro.pyro_to_units(flag);
132 return units.parse(value.getText());
133 return Double.parseDouble(value.getText());
134 } catch (NumberFormatException e) {
135 throw new AltosConfigDataException("\"%s\": %s\n", value.getText(), e.getMessage());
139 return combo.getSelectedIndex() + AltosLib.ao_flight_boost;
143 public PyroItem(AltosConfigPyroUI in_ui, int in_flag, int x, int y) {
148 GridBagConstraints c;
149 c = new GridBagConstraints();
150 c.gridx = x; c.gridy = y;
152 c.fill = GridBagConstraints.NONE;
153 c.anchor = GridBagConstraints.LINE_START;
155 enable = new JCheckBox();
156 enable.addItemListener(this);
159 if ((flag & AltosPyro.pyro_no_value) == 0) {
160 c = new GridBagConstraints();
161 c.gridx = x+1; c.gridy = y;
163 c.fill = GridBagConstraints.NONE;
164 c.anchor = GridBagConstraints.LINE_START;
166 if ((flag & AltosPyro.pyro_state_value) != 0) {
168 combo = new JComboBox<String>(state_names);
169 combo.addItemListener(this);
172 value = new JTextField(10);
173 value.getDocument().addDocumentListener(this);
180 class PyroColumn implements AltosUnitsListener {
181 public PyroItem[] items;
185 public void set(AltosPyro pyro) {
187 for (int flag = 1; flag < AltosPyro.pyro_all; flag <<= 1) {
188 if ((AltosPyro.pyro_all & flag) != 0) {
189 items[row].set((pyro.flags & flag) != 0,
190 pyro.get_value(flag));
196 public AltosPyro get() throws AltosConfigDataException {
197 AltosPyro p = new AltosPyro(channel);
200 for (int flag = 1; flag < AltosPyro.pyro_all; flag <<= 1) {
201 if ((AltosPyro.pyro_all & flag) != 0) {
202 if (items[row].enabled()) {
205 p.set_value(flag, items[row].value());
206 } catch (AltosConfigDataException ae) {
207 throw new AltosConfigDataException("%s, %s",
208 AltosPyro.pyro_to_name(flag),
218 public void units_changed(boolean imperial_units) {
220 for (int flag = 1; flag < AltosPyro.pyro_all; flag <<= 1) {
221 if ((AltosPyro.pyro_all & flag) != 0) {
222 items[row].units_changed(imperial_units);
228 public PyroColumn(AltosConfigPyroUI ui, int x, int y, int in_channel) {
230 channel = in_channel;
233 for (int flag = 1; flag < AltosPyro.pyro_all; flag <<= 1)
234 if ((flag & AltosPyro.pyro_all) != 0)
237 items = new PyroItem[nrow];
240 GridBagConstraints c;
241 c = new GridBagConstraints();
242 c.gridx = x; c.gridy = y;
244 c.fill = GridBagConstraints.NONE;
245 c.anchor = GridBagConstraints.CENTER;
247 label = new JLabel(String.format("Pyro Channel %c", 'A' + channel));
251 for (int flag = 1; flag < AltosPyro.pyro_all; flag <<= 1)
252 if ((flag & AltosPyro.pyro_all) != 0) {
253 items[row] = new PyroItem(ui, flag, x, y + row);
259 PyroColumn[] columns;
262 public void set_pyros(AltosPyro[] pyros) {
263 for (int i = 0; i < pyros.length; i++) {
264 if (pyros[i].channel < columns.length)
265 columns[pyros[i].channel].set(pyros[i]);
269 public AltosPyro[] get_pyros() throws AltosConfigDataException {
270 AltosPyro[] pyros = new AltosPyro[columns.length];
271 for (int c = 0; c < columns.length; c++) {
273 pyros[c] = columns[c].get();
274 } catch (AltosConfigDataException ae) {
275 throw new AltosConfigDataException ("Channel %c, %s", c + 'A', ae.getMessage());
281 JLabel pyro_firing_time_label;
282 JComboBox<String> pyro_firing_time_value;
284 static String[] pyro_firing_time_values = {
285 "0.050", "0.100", "0.250", "0.500", "1.0", "2.0"
288 boolean initializing;
290 public void set_pyro_firing_time(double new_pyro_firing_time) {
292 pyro_firing_time_value.setSelectedItem(Double.toString(new_pyro_firing_time));
293 pyro_firing_time_value.setEnabled(new_pyro_firing_time >= 0);
294 initializing = false;
297 public double get_pyro_firing_time() throws AltosConfigDataException {
298 String v = pyro_firing_time_value.getSelectedItem().toString();
301 return Double.parseDouble(v);
302 } catch (NumberFormatException e) {
303 throw new AltosConfigDataException("Invalid pyro firing time \"%s\"", v);
307 public void set_dirty() {
312 public void itemStateChanged(ItemEvent e) {
317 public void changedUpdate(DocumentEvent e) {
322 public void insertUpdate(DocumentEvent e) {
327 public void removeUpdate(DocumentEvent e) {
332 public void units_changed(boolean imperial_units) {
333 for (int c = 0; c < columns.length; c++)
334 columns[c].units_changed(imperial_units);
336 for (int flag = 1; flag <= AltosPyro.pyro_all; flag <<= 1) {
337 String n = AltosPyro.pyro_to_name(flag);
339 labels[r].setText(n);
345 /* A window listener to catch closing events and tell the config code */
346 class ConfigListener extends WindowAdapter {
347 AltosConfigPyroUI ui;
350 public ConfigListener(AltosConfigPyroUI this_ui, AltosConfigUI this_owner) {
355 public void windowClosing(WindowEvent e) {
356 ui.setVisible(false);
360 /* Listen for events from our buttons */
361 public void actionPerformed(ActionEvent e) {
362 String cmd = e.getActionCommand();
364 if (cmd.equals("Close"))
368 public AltosConfigPyroUI(AltosConfigUI in_owner, AltosPyro[] pyros, double pyro_firing_time) {
370 super(in_owner, "Configure Pyro Channels", false);
374 GridBagConstraints c;
376 pane = getContentPane();
377 pane.setLayout(new GridBagLayout());
380 for (int flag = 1; flag < AltosPyro.pyro_all; flag <<= 1)
381 if ((flag & AltosPyro.pyro_all) != 0)
384 labels = new JLabel[nrow];
388 for (int flag = 1; flag <= AltosPyro.pyro_all; flag <<= 1) {
391 n = AltosPyro.pyro_to_name(flag);
393 c = new GridBagConstraints();
394 c.gridx = 0; c.gridy = row;
396 c.fill = GridBagConstraints.NONE;
397 c.anchor = GridBagConstraints.LINE_START;
399 JLabel label = new JLabel(n);
401 labels[row-1] = label;
406 columns = new PyroColumn[pyros.length];
408 for (int i = 0; i < pyros.length; i++) {
409 columns[i] = new PyroColumn(this, i*2 + 1, 0, i);
410 columns[i].set(pyros[i]);
413 /* Pyro firing time */
414 c = new GridBagConstraints();
415 c.gridx = 0; c.gridy = row;
417 c.fill = GridBagConstraints.NONE;
418 c.anchor = GridBagConstraints.LINE_START;
421 pyro_firing_time_label = new JLabel("Pyro Firing Time(s):");
422 pane.add(pyro_firing_time_label, c);
424 c = new GridBagConstraints();
425 c.gridx = 2; c.gridy = row;
427 c.fill = GridBagConstraints.HORIZONTAL;
429 c.anchor = GridBagConstraints.LINE_START;
432 pyro_firing_time_value = new JComboBox<String>(pyro_firing_time_values);
433 pyro_firing_time_value.setEditable(true);
434 pyro_firing_time_value.addItemListener(this);
435 set_pyro_firing_time(pyro_firing_time);
436 pane.add(pyro_firing_time_value, c);
437 pyro_firing_time_value.setToolTipText("Length of extra pyro channel firing pulse");
439 c = new GridBagConstraints();
440 c.gridx = pyros.length*2-1;
441 c.fill = GridBagConstraints.HORIZONTAL;
444 JButton close = new JButton("Close");
446 close.addActionListener(this);
447 close.setActionCommand("Close");
449 addWindowListener(new ConfigListener(this, owner));
450 AltosPreferences.register_units_listener(this);
453 public void dispose() {
454 AltosPreferences.unregister_units_listener(this);
458 public void make_visible() {