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.*;
25 import org.altusmetrum.altosuilib.*;
27 public class AltosConfigPyroUI
29 implements ItemListener, DocumentListener
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
51 public JRadioButton enable;
52 public JTextField value;
53 public JComboBox combo;
56 public void set_enable(boolean enable) {
58 value.setEnabled(enable);
60 combo.setEnabled(enable);
63 public void itemStateChanged(ItemEvent e) {
64 set_enable(enable.isSelected());
68 public void changedUpdate(DocumentEvent e) {
72 public void insertUpdate(DocumentEvent e) {
76 public void removeUpdate(DocumentEvent e) {
80 public void set(boolean new_enable, double new_value) {
81 enable.setSelected(new_enable);
82 set_enable(new_enable);
84 double scale = AltosPyro.pyro_to_scale(flag);
85 String format = "%6.0f";
88 else if (scale >= 100)
90 value.setText(String.format(format, new_value));
93 if (new_value >= AltosLib.ao_flight_boost && new_value <= AltosLib.ao_flight_landed)
94 combo.setSelectedIndex((int) new_value - AltosLib.ao_flight_boost);
97 public boolean enabled() {
98 return enable.isSelected();
101 public double value() {
103 return Double.parseDouble(value.getText());
105 return combo.getSelectedIndex() + AltosLib.ao_flight_boost;
109 public PyroItem(AltosConfigPyroUI in_ui, int in_flag, int x, int y) {
114 GridBagConstraints c;
115 c = new GridBagConstraints();
116 c.gridx = x; c.gridy = y;
118 c.fill = GridBagConstraints.NONE;
119 c.anchor = GridBagConstraints.LINE_START;
121 enable = new JRadioButton();
122 enable.addItemListener(this);
125 if ((flag & AltosPyro.pyro_no_value) == 0) {
126 c = new GridBagConstraints();
127 c.gridx = x+1; c.gridy = y;
129 c.fill = GridBagConstraints.NONE;
130 c.anchor = GridBagConstraints.LINE_START;
132 if ((flag & AltosPyro.pyro_state_value) != 0) {
134 combo = new JComboBox(state_names);
135 combo.addItemListener(this);
138 value = new JTextField(10);
139 value.getDocument().addDocumentListener(this);
147 public PyroItem[] items;
151 public void set(AltosPyro pyro) {
153 for (int flag = 1; flag < AltosPyro.pyro_all; flag <<= 1) {
154 if ((AltosPyro.pyro_all & flag) != 0) {
155 items[row].set((pyro.flags & flag) != 0,
156 pyro.get_value(flag));
162 public AltosPyro get() {
163 AltosPyro p = new AltosPyro(channel);
166 for (int flag = 1; flag < AltosPyro.pyro_all; flag <<= 1) {
167 if ((AltosPyro.pyro_all & flag) != 0) {
168 if (items[row].enabled()) {
169 System.out.printf ("Flag %x enabled\n", flag);
171 p.set_value(flag, items[row].value());
176 System.out.printf ("Pyro %x %s\n", p.flags, p.toString());
180 public PyroColumn(AltosConfigPyroUI ui, int x, int y, int in_channel) {
182 channel = in_channel;
185 for (int flag = 1; flag < AltosPyro.pyro_all; flag <<= 1)
186 if ((flag & AltosPyro.pyro_all) != 0)
189 items = new PyroItem[nrow];
192 GridBagConstraints c;
193 c = new GridBagConstraints();
194 c.gridx = x; c.gridy = y;
196 c.fill = GridBagConstraints.NONE;
197 c.anchor = GridBagConstraints.CENTER;
199 label = new JLabel(String.format("Pyro Channel %d", channel));
203 for (int flag = 1; flag < AltosPyro.pyro_all; flag <<= 1)
204 if ((flag & AltosPyro.pyro_all) != 0) {
205 items[row] = new PyroItem(ui, flag, x, y + row);
211 PyroColumn[] columns;
213 public void set_pyros(AltosPyro[] pyros) {
214 for (int i = 0; i < pyros.length; i++) {
215 if (pyros[i].channel < columns.length)
216 columns[pyros[i].channel].set(pyros[i]);
220 public AltosPyro[] get_pyros() {
221 AltosPyro[] pyros = new AltosPyro[columns.length];
222 for (int c = 0; c < columns.length; c++)
223 pyros[c] = columns[c].get();
227 public void set_dirty() {
231 public void itemStateChanged(ItemEvent e) {
235 public void changedUpdate(DocumentEvent e) {
239 public void insertUpdate(DocumentEvent e) {
243 public void removeUpdate(DocumentEvent e) {
247 public AltosConfigPyroUI(AltosConfigUI in_owner, AltosPyro[] pyros) {
249 super(in_owner, "Configure Pyro Channels", false);
253 GridBagConstraints c;
255 pane = getContentPane();
256 pane.setLayout(new GridBagLayout());
260 for (int flag = 1; flag <= AltosPyro.pyro_all; flag <<= 1) {
263 n = AltosPyro.pyro_to_name(flag);
265 c = new GridBagConstraints();
266 c.gridx = 0; c.gridy = row;
268 c.fill = GridBagConstraints.NONE;
269 c.anchor = GridBagConstraints.LINE_START;
271 JLabel label = new JLabel(n);
277 columns = new PyroColumn[pyros.length];
279 for (int i = 0; i < pyros.length; i++) {
280 columns[i] = new PyroColumn(this, i*2 + 1, 0, i);
281 columns[i].set(pyros[i]);
285 public void make_visible() {