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_3.*;
25 import org.altusmetrum.altosuilib_1.*;
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 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);
108 String format = "%6.0f";
111 else if (scale >= 100)
113 value.setText(String.format(format, unit_value));
116 if (new_value >= AltosLib.ao_flight_boost && new_value <= AltosLib.ao_flight_landed)
117 combo.setSelectedIndex((int) new_value - AltosLib.ao_flight_boost);
121 public boolean enabled() {
122 return enable.isSelected();
125 public double value() {
127 AltosUnits units = AltosPyro.pyro_to_units(flag);
129 return units.parse(value.getText());
130 return Double.parseDouble(value.getText());
133 return combo.getSelectedIndex() + AltosLib.ao_flight_boost;
137 public PyroItem(AltosConfigPyroUI in_ui, int in_flag, int x, int y) {
142 GridBagConstraints c;
143 c = new GridBagConstraints();
144 c.gridx = x; c.gridy = y;
146 c.fill = GridBagConstraints.NONE;
147 c.anchor = GridBagConstraints.LINE_START;
149 enable = new JCheckBox();
150 enable.addItemListener(this);
153 if ((flag & AltosPyro.pyro_no_value) == 0) {
154 c = new GridBagConstraints();
155 c.gridx = x+1; c.gridy = y;
157 c.fill = GridBagConstraints.NONE;
158 c.anchor = GridBagConstraints.LINE_START;
160 if ((flag & AltosPyro.pyro_state_value) != 0) {
162 combo = new JComboBox(state_names);
163 combo.addItemListener(this);
166 value = new JTextField(10);
167 value.getDocument().addDocumentListener(this);
174 class PyroColumn implements AltosUnitsListener {
175 public PyroItem[] items;
179 public void set(AltosPyro pyro) {
181 for (int flag = 1; flag < AltosPyro.pyro_all; flag <<= 1) {
182 if ((AltosPyro.pyro_all & flag) != 0) {
183 items[row].set((pyro.flags & flag) != 0,
184 pyro.get_value(flag));
190 public AltosPyro get() {
191 AltosPyro p = new AltosPyro(channel);
194 for (int flag = 1; flag < AltosPyro.pyro_all; flag <<= 1) {
195 if ((AltosPyro.pyro_all & flag) != 0) {
196 if (items[row].enabled()) {
198 p.set_value(flag, items[row].value());
206 public void units_changed(boolean imperial_units) {
208 for (int flag = 1; flag < AltosPyro.pyro_all; flag <<= 1) {
209 if ((AltosPyro.pyro_all & flag) != 0) {
210 items[row].units_changed(imperial_units);
216 public PyroColumn(AltosConfigPyroUI ui, int x, int y, int in_channel) {
218 channel = in_channel;
221 for (int flag = 1; flag < AltosPyro.pyro_all; flag <<= 1)
222 if ((flag & AltosPyro.pyro_all) != 0)
225 items = new PyroItem[nrow];
228 GridBagConstraints c;
229 c = new GridBagConstraints();
230 c.gridx = x; c.gridy = y;
232 c.fill = GridBagConstraints.NONE;
233 c.anchor = GridBagConstraints.CENTER;
235 label = new JLabel(String.format("Pyro Channel %c", 'A' + channel));
239 for (int flag = 1; flag < AltosPyro.pyro_all; flag <<= 1)
240 if ((flag & AltosPyro.pyro_all) != 0) {
241 items[row] = new PyroItem(ui, flag, x, y + row);
247 PyroColumn[] columns;
250 public void set_pyros(AltosPyro[] pyros) {
251 for (int i = 0; i < pyros.length; i++) {
252 if (pyros[i].channel < columns.length)
253 columns[pyros[i].channel].set(pyros[i]);
257 public AltosPyro[] get_pyros() {
258 AltosPyro[] pyros = new AltosPyro[columns.length];
259 for (int c = 0; c < columns.length; c++)
260 pyros[c] = columns[c].get();
264 public void set_dirty() {
268 public void itemStateChanged(ItemEvent e) {
272 public void changedUpdate(DocumentEvent e) {
276 public void insertUpdate(DocumentEvent e) {
280 public void removeUpdate(DocumentEvent e) {
284 public void units_changed(boolean imperial_units) {
285 for (int c = 0; c < columns.length; c++)
286 columns[c].units_changed(imperial_units);
288 for (int flag = 1; flag <= AltosPyro.pyro_all; flag <<= 1) {
289 String n = AltosPyro.pyro_to_name(flag);
291 labels[r].setText(n);
297 /* A window listener to catch closing events and tell the config code */
298 class ConfigListener extends WindowAdapter {
299 AltosConfigPyroUI ui;
302 public ConfigListener(AltosConfigPyroUI this_ui, AltosConfigUI this_owner) {
307 public void windowClosing(WindowEvent e) {
308 ui.setVisible(false);
312 /* Listen for events from our buttons */
313 public void actionPerformed(ActionEvent e) {
314 String cmd = e.getActionCommand();
316 if (cmd.equals("Close"))
320 public AltosConfigPyroUI(AltosConfigUI in_owner, AltosPyro[] pyros) {
322 super(in_owner, "Configure Pyro Channels", false);
326 GridBagConstraints c;
328 pane = getContentPane();
329 pane.setLayout(new GridBagLayout());
332 for (int flag = 1; flag < AltosPyro.pyro_all; flag <<= 1)
333 if ((flag & AltosPyro.pyro_all) != 0)
336 labels = new JLabel[nrow];
340 for (int flag = 1; flag <= AltosPyro.pyro_all; flag <<= 1) {
343 n = AltosPyro.pyro_to_name(flag);
345 c = new GridBagConstraints();
346 c.gridx = 0; c.gridy = row;
348 c.fill = GridBagConstraints.NONE;
349 c.anchor = GridBagConstraints.LINE_START;
351 JLabel label = new JLabel(n);
353 labels[row-1] = label;
358 columns = new PyroColumn[pyros.length];
360 for (int i = 0; i < pyros.length; i++) {
361 columns[i] = new PyroColumn(this, i*2 + 1, 0, i);
362 columns[i].set(pyros[i]);
365 c = new GridBagConstraints();
366 c.gridx = pyros.length*2-1;
367 c.fill = GridBagConstraints.HORIZONTAL;
370 JButton close = new JButton("Close");
372 close.addActionListener(this);
373 close.setActionCommand("Close");
375 addWindowListener(new ConfigListener(this, owner));
376 AltosPreferences.register_units_listener(this);
379 public void dispose() {
380 AltosPreferences.unregister_units_listener(this);
384 public void make_visible() {