3cac56c3a576425f2d826c43bf9e3e91aebe21fc
[fw/altos] / altosui / AltosConfigPyroUI.java
1 /*
2  * Copyright © 2012 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 package altosui;
19
20 import java.awt.*;
21 import java.awt.event.*;
22 import javax.swing.*;
23 import javax.swing.event.*;
24 import org.altusmetrum.altoslib_1.*;
25 import org.altusmetrum.altosuilib_1.*;
26
27 public class AltosConfigPyroUI
28         extends AltosUIDialog
29         implements ItemListener, DocumentListener
30 {
31         AltosConfigUI   owner;
32         Container       pane;
33
34         static Insets il = new Insets(4,4,4,4);
35         static Insets ir = new Insets(4,4,4,4);
36
37         static String[] state_names;
38
39         static void make_state_names() {
40                 if (state_names == null) {
41
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);
45                 }
46         }
47
48         class PyroItem implements ItemListener, DocumentListener
49         {
50                 public int              flag;
51                 public JRadioButton     enable;
52                 public JTextField       value;
53                 public JComboBox        combo;
54                 AltosConfigPyroUI       ui;
55
56                 public void set_enable(boolean enable) {
57                         if (value != null)
58                                 value.setEnabled(enable);
59                         if (combo != null)
60                                 combo.setEnabled(enable);
61                 }
62
63                 public void itemStateChanged(ItemEvent e) {
64                         set_enable(enable.isSelected());
65                         ui.set_dirty();
66                 }
67
68                 public void changedUpdate(DocumentEvent e) {
69                         ui.set_dirty();
70                 }
71
72                 public void insertUpdate(DocumentEvent e) {
73                         ui.set_dirty();
74                 }
75
76                 public void removeUpdate(DocumentEvent e) {
77                         ui.set_dirty();
78                 }
79
80                 public void set(boolean new_enable, double new_value) {
81                         enable.setSelected(new_enable);
82                         set_enable(new_enable);
83                         if (value != null) {
84                                 double  scale = AltosPyro.pyro_to_scale(flag);
85                                 String  format = "%6.0f";
86                                 if (scale >= 10)
87                                         format = "%6.1f";
88                                 else if (scale >= 100)
89                                         format = "%6.2f";
90                                 value.setText(String.format(format, new_value));
91                         }
92                         if (combo != null)
93                                 if (new_value >= AltosLib.ao_flight_boost && new_value <= AltosLib.ao_flight_landed)
94                                         combo.setSelectedIndex((int) new_value - AltosLib.ao_flight_boost);
95                 }
96
97                 public boolean enabled() {
98                         return enable.isSelected();
99                 }
100
101                 public double value() {
102                         if (value != null)
103                                 return Double.parseDouble(value.getText());
104                         if (combo != null)
105                                 return combo.getSelectedIndex() + AltosLib.ao_flight_boost;
106                         return 0;
107                 }
108
109                 public PyroItem(AltosConfigPyroUI in_ui, int in_flag, int x, int y) {
110
111                         ui = in_ui;
112                         flag = in_flag;
113
114                         GridBagConstraints      c;
115                         c = new GridBagConstraints();
116                         c.gridx = x; c.gridy = y;
117                         c.gridwidth = 1;
118                         c.fill = GridBagConstraints.NONE;
119                         c.anchor = GridBagConstraints.LINE_START;
120                         c.insets = il;
121                         enable = new JRadioButton();
122                         enable.addItemListener(this);
123                         pane.add(enable, c);
124                         
125                         if ((flag & AltosPyro.pyro_no_value) == 0) {
126                                 c = new GridBagConstraints();
127                                 c.gridx = x+1; c.gridy = y;
128                                 c.gridwidth = 1;
129                                 c.fill = GridBagConstraints.NONE;
130                                 c.anchor = GridBagConstraints.LINE_START;
131                                 c.insets = il;
132                                 if ((flag & AltosPyro.pyro_state_value) != 0) {
133                                         make_state_names();
134                                         combo = new JComboBox(state_names);
135                                         combo.addItemListener(this);
136                                         pane.add(combo, c);
137                                 } else {
138                                         value = new JTextField(10);
139                                         value.getDocument().addDocumentListener(this);
140                                         pane.add(value, c);
141                                 }
142                         }
143                 }
144         }
145
146         class PyroColumn {
147                 public PyroItem[]       items;
148                 public JLabel           label;
149                 int                     channel;
150
151                 public void set(AltosPyro pyro) {
152                         int row = 0;
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));
157                                         row++;
158                                 }
159                         }
160                 }
161
162                 public AltosPyro get() {
163                         AltosPyro       p = new AltosPyro(channel);
164
165                         int row = 0;
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);
170                                                 p.flags |= flag;
171                                                 p.set_value(flag, items[row].value());
172                                         }
173                                         row++;
174                                 }
175                         }
176                         System.out.printf ("Pyro %x %s\n", p.flags, p.toString());
177                         return p;
178                 }
179
180                 public PyroColumn(AltosConfigPyroUI ui, int x, int y, int in_channel) {
181
182                         channel = in_channel;
183
184                         int     nrow = 0;
185                         for (int flag = 1; flag < AltosPyro.pyro_all; flag <<= 1)
186                                 if ((flag & AltosPyro.pyro_all) != 0)
187                                         nrow++;
188
189                         items = new PyroItem[nrow];
190                         int row = 0;
191                         
192                         GridBagConstraints      c;
193                         c = new GridBagConstraints();
194                         c.gridx = x; c.gridy = y;
195                         c.gridwidth = 2;
196                         c.fill = GridBagConstraints.NONE;
197                         c.anchor = GridBagConstraints.CENTER;
198                         c.insets = il;
199                         label = new JLabel(String.format("Pyro Channel %d", channel));
200                         pane.add(label, c);
201                         y++;
202
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);
206                                         row++;
207                                 }
208                 }
209         }
210
211         PyroColumn[]    columns;
212
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]);
217                 }
218         }
219
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();
224                 return pyros;
225         }
226
227         public void set_dirty() {
228                 owner.set_dirty();
229         }
230
231         public void itemStateChanged(ItemEvent e) {
232                 owner.set_dirty();
233         }
234
235         public void changedUpdate(DocumentEvent e) {
236                 owner.set_dirty();
237         }
238
239         public void insertUpdate(DocumentEvent e) {
240                 owner.set_dirty();
241         }
242
243         public void removeUpdate(DocumentEvent e) {
244                 owner.set_dirty();
245         }
246
247         public AltosConfigPyroUI(AltosConfigUI in_owner, AltosPyro[] pyros) {
248
249                 super(in_owner, "Configure Pyro Channels", false);
250
251                 owner = in_owner;
252
253                 GridBagConstraints      c;
254
255                 pane = getContentPane();
256                 pane.setLayout(new GridBagLayout());
257
258                 int     row = 1;
259
260                 for (int flag = 1; flag <= AltosPyro.pyro_all; flag <<= 1) {
261                         String  n;
262
263                         n = AltosPyro.pyro_to_name(flag);
264                         if (n != null) {
265                                 c = new GridBagConstraints();
266                                 c.gridx = 0; c.gridy = row;
267                                 c.gridwidth = 1;
268                                 c.fill = GridBagConstraints.NONE;
269                                 c.anchor = GridBagConstraints.LINE_START;
270                                 c.insets = il;
271                                 JLabel label = new JLabel(n);
272                                 pane.add(label, c);
273                                 row++;
274                         }
275                 }
276
277                 columns = new PyroColumn[pyros.length];
278
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]);
282                 }
283         }
284
285         public void make_visible() {
286                 pack();
287                 setVisible(true);
288         }
289 }