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.*;
26 public class AltosConfigPyroUI
28 implements ItemListener, DocumentListener
33 static Insets il = new Insets(4,4,4,4);
34 static Insets ir = new Insets(4,4,4,4);
36 static String[] state_names;
38 static void make_state_names() {
39 if (state_names == null) {
41 state_names = new String[AltosLib.ao_flight_landed - AltosLib.ao_flight_boost + 1];
42 for (int state = AltosLib.ao_flight_boost; state <= AltosLib.ao_flight_landed; state++)
43 state_names[state - AltosLib.ao_flight_boost] = AltosLib.state_name_capital(state);
47 class PyroItem implements ItemListener, DocumentListener
50 public JRadioButton enable;
51 public JTextField value;
52 public JComboBox combo;
55 public void set_enable(boolean enable) {
57 value.setEnabled(enable);
59 combo.setEnabled(enable);
62 public void itemStateChanged(ItemEvent e) {
63 set_enable(enable.isSelected());
67 public void changedUpdate(DocumentEvent e) {
71 public void insertUpdate(DocumentEvent e) {
75 public void removeUpdate(DocumentEvent e) {
79 public void set(boolean new_enable, double new_value) {
80 enable.setSelected(new_enable);
81 set_enable(new_enable);
83 double scale = AltosPyro.pyro_to_scale(flag);
84 String format = "%6.0f";
87 else if (scale >= 100)
89 value.setText(String.format(format, new_value));
92 if (new_value >= AltosLib.ao_flight_boost && new_value <= AltosLib.ao_flight_landed)
93 combo.setSelectedIndex((int) new_value - AltosLib.ao_flight_boost);
96 public boolean enabled() {
97 return enable.isSelected();
100 public double value() {
102 return Double.parseDouble(value.getText());
104 return combo.getSelectedIndex() + AltosLib.ao_flight_boost;
108 public PyroItem(AltosConfigPyroUI in_ui, int in_flag, int x, int y) {
113 GridBagConstraints c;
114 c = new GridBagConstraints();
115 c.gridx = x; c.gridy = y;
117 c.fill = GridBagConstraints.NONE;
118 c.anchor = GridBagConstraints.LINE_START;
120 enable = new JRadioButton();
121 enable.addItemListener(this);
124 if ((flag & AltosPyro.pyro_no_value) == 0) {
125 c = new GridBagConstraints();
126 c.gridx = x+1; c.gridy = y;
128 c.fill = GridBagConstraints.NONE;
129 c.anchor = GridBagConstraints.LINE_START;
131 if ((flag & AltosPyro.pyro_state_value) != 0) {
133 combo = new JComboBox(state_names);
134 combo.addItemListener(this);
137 value = new JTextField(10);
138 value.getDocument().addDocumentListener(this);
146 public PyroItem[] items;
150 public void set(AltosPyro pyro) {
152 for (int flag = 1; flag < AltosPyro.pyro_all; flag <<= 1) {
153 if ((AltosPyro.pyro_all & flag) != 0) {
154 items[row].set((pyro.flags & flag) != 0,
155 pyro.get_value(flag));
161 public AltosPyro get() {
162 AltosPyro p = new AltosPyro(channel);
165 for (int flag = 1; flag < AltosPyro.pyro_all; flag <<= 1) {
166 if ((AltosPyro.pyro_all & flag) != 0) {
167 if (items[row].enabled()) {
168 System.out.printf ("Flag %x enabled\n", flag);
170 p.set_value(flag, items[row].value());
175 System.out.printf ("Pyro %x %s\n", p.flags, p.toString());
179 public PyroColumn(AltosConfigPyroUI ui, int x, int y, int in_channel) {
181 channel = in_channel;
184 for (int flag = 1; flag < AltosPyro.pyro_all; flag <<= 1)
185 if ((flag & AltosPyro.pyro_all) != 0)
188 items = new PyroItem[nrow];
191 GridBagConstraints c;
192 c = new GridBagConstraints();
193 c.gridx = x; c.gridy = y;
195 c.fill = GridBagConstraints.NONE;
196 c.anchor = GridBagConstraints.CENTER;
198 label = new JLabel(String.format("Pyro Channel %d", channel));
202 for (int flag = 1; flag < AltosPyro.pyro_all; flag <<= 1)
203 if ((flag & AltosPyro.pyro_all) != 0) {
204 items[row] = new PyroItem(ui, flag, x, y + row);
210 PyroColumn[] columns;
212 public void set_pyros(AltosPyro[] pyros) {
213 for (int i = 0; i < pyros.length; i++) {
214 if (pyros[i].channel < columns.length)
215 columns[pyros[i].channel].set(pyros[i]);
219 public AltosPyro[] get_pyros() {
220 AltosPyro[] pyros = new AltosPyro[columns.length];
221 for (int c = 0; c < columns.length; c++)
222 pyros[c] = columns[c].get();
226 public void set_dirty() {
230 public void itemStateChanged(ItemEvent e) {
234 public void changedUpdate(DocumentEvent e) {
238 public void insertUpdate(DocumentEvent e) {
242 public void removeUpdate(DocumentEvent e) {
246 public AltosConfigPyroUI(AltosConfigUI in_owner, AltosPyro[] pyros) {
248 super(in_owner, "Configure Pyro Channels", false);
252 GridBagConstraints c;
254 pane = getContentPane();
255 pane.setLayout(new GridBagLayout());
259 for (int flag = 1; flag <= AltosPyro.pyro_all; flag <<= 1) {
262 n = AltosPyro.pyro_to_name(flag);
264 c = new GridBagConstraints();
265 c.gridx = 0; c.gridy = row;
267 c.fill = GridBagConstraints.NONE;
268 c.anchor = GridBagConstraints.LINE_START;
270 JLabel label = new JLabel(n);
276 columns = new PyroColumn[pyros.length];
278 for (int i = 0; i < pyros.length; i++) {
279 columns[i] = new PyroColumn(this, i*2 + 1, 0, i);
280 columns[i].set(pyros[i]);
284 public void make_visible() {