Switch from GPLv2 to GPLv2+
[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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 package altosui;
20
21 import java.text.*;
22 import java.awt.*;
23 import java.awt.event.*;
24 import javax.swing.*;
25 import javax.swing.event.*;
26 import org.altusmetrum.altoslib_11.*;
27 import org.altusmetrum.altosuilib_11.*;
28
29 public class AltosConfigPyroUI
30         extends AltosUIDialog
31         implements ItemListener, DocumentListener, AltosUnitsListener, ActionListener
32 {
33         AltosConfigUI   owner;
34         Container       pane;
35
36         static Insets il = new Insets(4,4,4,4);
37         static Insets ir = new Insets(4,4,4,4);
38
39         static String[] state_names;
40
41         static void make_state_names() {
42                 if (state_names == null) {
43
44                         state_names = new String[AltosLib.ao_flight_landed - AltosLib.ao_flight_boost + 1];
45                         for (int state = AltosLib.ao_flight_boost; state <= AltosLib.ao_flight_landed; state++)
46                                 state_names[state - AltosLib.ao_flight_boost] = AltosLib.state_name_capital(state);
47                 }
48         }
49
50         class PyroItem implements ItemListener, DocumentListener, AltosUnitsListener
51         {
52                 public int              flag;
53                 public JCheckBox        enable;
54                 public JTextField       value;
55                 public JComboBox<String>        combo;
56                 AltosConfigPyroUI       ui;
57                 boolean                 setting;
58
59                 public void set_enable(boolean enable) {
60                         if (value != null)
61                                 value.setEnabled(enable);
62                         if (combo != null)
63                                 combo.setEnabled(enable);
64                 }
65
66                 public void itemStateChanged(ItemEvent e) {
67                         set_enable(enable.isSelected());
68                         if (!setting)
69                                 ui.set_dirty();
70                 }
71
72                 public void changedUpdate(DocumentEvent e) {
73                         if (!setting)
74                                 ui.set_dirty();
75                 }
76
77                 public void insertUpdate(DocumentEvent e) {
78                         if (!setting)
79                                 ui.set_dirty();
80                 }
81
82                 public void removeUpdate(DocumentEvent e) {
83                         if (!setting)
84                                 ui.set_dirty();
85                 }
86
87                 public void units_changed(boolean imperial_units) {
88                         AltosUnits units = AltosPyro.pyro_to_units(flag);
89
90                         if (units != null) {
91                                 try {
92                                         double v = units.parse_locale(value.getText(), !imperial_units);
93                                         set(enabled(), v);
94                                 } catch (ParseException pe) {
95                                         set(enabled(), 0.0);
96                                 }
97                         }
98                 }
99
100                 public void set(boolean new_enable, double new_value) {
101                         setting = true;
102                         enable.setSelected(new_enable);
103                         set_enable(new_enable);
104                         if (value != null) {
105                                 double  scale = AltosPyro.pyro_to_scale(flag);
106                                 double  unit_value = new_value;
107                                 AltosUnits units = AltosPyro.pyro_to_units(flag);
108                                 if (units != null)
109                                         unit_value = units.parse_value(new_value);
110                                 String  format;
111                                 if (scale >= 100)
112                                         format = "%6.2f";
113                                 else if (scale >= 10)
114                                         format = "%6.1f";
115                                 else
116                                         format = "%6.0f";
117                                 value.setText(String.format(format, unit_value));
118                         }
119                         if (combo != null)
120                                 if (new_value >= AltosLib.ao_flight_boost && new_value <= AltosLib.ao_flight_landed)
121                                         combo.setSelectedIndex((int) new_value - AltosLib.ao_flight_boost);
122                         setting = false;
123                 }
124
125                 public boolean enabled() {
126                         return enable.isSelected();
127                 }
128
129                 public double value() throws AltosConfigDataException {
130                         if (value != null) {
131                                 AltosUnits units = AltosPyro.pyro_to_units(flag);
132                                 try {
133                                         if (units != null)
134                                                 return units.parse_locale(value.getText());
135                                         return AltosParse.parse_double_locale(value.getText());
136                                 } catch (ParseException e) {
137                                         throw new AltosConfigDataException("\"%s\": %s\n", value.getText(), e.getMessage());
138                                 }
139                         }
140                         if (combo != null)
141                                 return combo.getSelectedIndex() + AltosLib.ao_flight_boost;
142                         return 0;
143                 }
144
145                 public PyroItem(AltosConfigPyroUI in_ui, int in_flag, int x, int y) {
146
147                         ui = in_ui;
148                         flag = in_flag;
149
150                         GridBagConstraints      c;
151                         c = new GridBagConstraints();
152                         c.gridx = x; c.gridy = y;
153                         c.gridwidth = 1;
154                         c.fill = GridBagConstraints.NONE;
155                         c.anchor = GridBagConstraints.LINE_START;
156                         c.insets = il;
157                         enable = new JCheckBox();
158                         enable.addItemListener(this);
159                         pane.add(enable, c);
160
161                         if ((flag & AltosPyro.pyro_no_value) == 0) {
162                                 c = new GridBagConstraints();
163                                 c.gridx = x+1; c.gridy = y;
164                                 c.gridwidth = 1;
165                                 c.fill = GridBagConstraints.NONE;
166                                 c.anchor = GridBagConstraints.LINE_START;
167                                 c.insets = il;
168                                 if ((flag & AltosPyro.pyro_state_value) != 0) {
169                                         make_state_names();
170                                         combo = new JComboBox<String>(state_names);
171                                         combo.addItemListener(this);
172                                         pane.add(combo, c);
173                                 } else {
174                                         value = new JTextField(10);
175                                         value.getDocument().addDocumentListener(this);
176                                         pane.add(value, c);
177                                 }
178                         }
179                 }
180         }
181
182         class PyroColumn implements AltosUnitsListener {
183                 public PyroItem[]       items;
184                 public JLabel           label;
185                 int                     channel;
186
187                 public void set(AltosPyro pyro) {
188                         int row = 0;
189                         for (int flag = 1; flag < AltosPyro.pyro_all; flag <<= 1) {
190                                 if ((AltosPyro.pyro_all & flag) != 0) {
191                                         items[row].set((pyro.flags & flag) != 0,
192                                                        pyro.get_value(flag));
193                                         row++;
194                                 }
195                         }
196                 }
197
198                 public AltosPyro get() throws AltosConfigDataException {
199                         AltosPyro       p = new AltosPyro(channel);
200
201                         int row = 0;
202                         for (int flag = 1; flag < AltosPyro.pyro_all; flag <<= 1) {
203                                 if ((AltosPyro.pyro_all & flag) != 0) {
204                                         if (items[row].enabled()) {
205                                                 try {
206                                                 p.flags |= flag;
207                                                 p.set_value(flag, items[row].value());
208                                                 } catch (AltosConfigDataException ae) {
209                                                         throw new AltosConfigDataException("%s, %s",
210                                                                                            AltosPyro.pyro_to_name(flag),
211                                                                                            ae.getMessage());
212                                                 }
213                                         }
214                                         row++;
215                                 }
216                         }
217                         return p;
218                 }
219
220                 public void units_changed(boolean imperial_units) {
221                         int row = 0;
222                         for (int flag = 1; flag < AltosPyro.pyro_all; flag <<= 1) {
223                                 if ((AltosPyro.pyro_all & flag) != 0) {
224                                         items[row].units_changed(imperial_units);
225                                         row++;
226                                 }
227                         }
228                 }
229
230                 public PyroColumn(AltosConfigPyroUI ui, int x, int y, int in_channel) {
231
232                         channel = in_channel;
233
234                         int     nrow = 0;
235                         for (int flag = 1; flag < AltosPyro.pyro_all; flag <<= 1)
236                                 if ((flag & AltosPyro.pyro_all) != 0)
237                                         nrow++;
238
239                         items = new PyroItem[nrow];
240                         int row = 0;
241
242                         GridBagConstraints      c;
243                         c = new GridBagConstraints();
244                         c.gridx = x; c.gridy = y;
245                         c.gridwidth = 2;
246                         c.fill = GridBagConstraints.NONE;
247                         c.anchor = GridBagConstraints.CENTER;
248                         c.insets = il;
249                         label = new JLabel(String.format("Pyro Channel %c", 'A' + channel));
250                         pane.add(label, c);
251                         y++;
252
253                         for (int flag = 1; flag < AltosPyro.pyro_all; flag <<= 1)
254                                 if ((flag & AltosPyro.pyro_all) != 0) {
255                                         items[row] = new PyroItem(ui, flag, x, y + row);
256                                         row++;
257                                 }
258                 }
259         }
260
261         PyroColumn[]    columns;
262         JLabel[]        labels;
263
264         public void set_pyros(AltosPyro[] pyros) {
265                 for (int i = 0; i < pyros.length; i++) {
266                         if (pyros[i].channel < columns.length)
267                                 columns[pyros[i].channel].set(pyros[i]);
268                 }
269         }
270
271         public AltosPyro[] get_pyros() throws AltosConfigDataException {
272                 AltosPyro[]     pyros = new AltosPyro[columns.length];
273                 for (int c = 0; c < columns.length; c++) {
274                         try {
275                                 pyros[c] = columns[c].get();
276                         } catch (AltosConfigDataException ae) {
277                                 throw new AltosConfigDataException ("Channel %c, %s", c + 'A', ae.getMessage());
278                         }
279                 }
280                 return pyros;
281         }
282
283         JLabel                  pyro_firing_time_label;
284         JComboBox<String>       pyro_firing_time_value;
285
286         static String[]         pyro_firing_time_values = {
287                 "0.050", "0.100", "0.250", "0.500", "1.0", "2.0"
288         };
289
290         boolean initializing;
291
292         public void set_pyro_firing_time(double new_pyro_firing_time) {
293                 initializing = true;
294                 pyro_firing_time_value.setSelectedItem(Double.toString(new_pyro_firing_time));
295                 pyro_firing_time_value.setEnabled(new_pyro_firing_time >= 0);
296                 initializing = false;
297         }
298
299         public double get_pyro_firing_time() throws AltosConfigDataException {
300                 String  v = pyro_firing_time_value.getSelectedItem().toString();
301
302                 try {
303                         return AltosParse.parse_double_locale(v);
304                 } catch (ParseException e) {
305                         throw new AltosConfigDataException("Invalid pyro firing time \"%s\"", v);
306                 }
307         }
308
309         public void set_dirty() {
310                 if (!initializing)
311                         owner.set_dirty();
312         }
313
314         public void itemStateChanged(ItemEvent e) {
315                 if (!initializing)
316                         owner.set_dirty();
317         }
318
319         public void changedUpdate(DocumentEvent e) {
320                 if (!initializing)
321                         owner.set_dirty();
322         }
323
324         public void insertUpdate(DocumentEvent e) {
325                 if (!initializing)
326                         owner.set_dirty();
327         }
328
329         public void removeUpdate(DocumentEvent e) {
330                 if (!initializing)
331                         owner.set_dirty();
332         }
333
334         public void units_changed(boolean imperial_units) {
335                 for (int c = 0; c < columns.length; c++)
336                         columns[c].units_changed(imperial_units);
337                 int r = 0;
338                 for (int flag = 1; flag <= AltosPyro.pyro_all; flag <<= 1) {
339                         String n = AltosPyro.pyro_to_name(flag);
340                         if (n != null) {
341                                 labels[r].setText(n);
342                                 r++;
343                         }
344                 }
345         }
346
347         /* A window listener to catch closing events and tell the config code */
348         class ConfigListener extends WindowAdapter {
349                 AltosConfigPyroUI       ui;
350                 AltosConfigUI           owner;
351
352                 public ConfigListener(AltosConfigPyroUI this_ui, AltosConfigUI this_owner) {
353                         ui = this_ui;
354                         owner = this_owner;
355                 }
356
357                 public void windowClosing(WindowEvent e) {
358                         ui.setVisible(false);
359                 }
360         }
361
362         /* Listen for events from our buttons */
363         public void actionPerformed(ActionEvent e) {
364                 String  cmd = e.getActionCommand();
365
366                 if (cmd.equals("Close"))
367                         setVisible(false);
368         }
369
370         public AltosConfigPyroUI(AltosConfigUI in_owner, AltosPyro[] pyros, double pyro_firing_time) {
371
372                 super(in_owner, "Configure Pyro Channels", false);
373
374                 owner = in_owner;
375
376                 GridBagConstraints      c;
377
378                 pane = getContentPane();
379                 pane.setLayout(new GridBagLayout());
380
381                 int     nrow = 0;
382                 for (int flag = 1; flag < AltosPyro.pyro_all; flag <<= 1)
383                         if ((flag & AltosPyro.pyro_all) != 0)
384                                 nrow++;
385
386                 labels = new JLabel[nrow];
387
388                 int     row = 1;
389
390                 for (int flag = 1; flag <= AltosPyro.pyro_all; flag <<= 1) {
391                         String  n;
392
393                         n = AltosPyro.pyro_to_name(flag);
394                         if (n != null) {
395                                 c = new GridBagConstraints();
396                                 c.gridx = 0; c.gridy = row;
397                                 c.gridwidth = 1;
398                                 c.fill = GridBagConstraints.NONE;
399                                 c.anchor = GridBagConstraints.LINE_START;
400                                 c.insets = il;
401                                 JLabel label = new JLabel(n);
402                                 pane.add(label, c);
403                                 labels[row-1] = label;
404                                 row++;
405                         }
406                 }
407
408                 columns = new PyroColumn[pyros.length];
409
410                 for (int i = 0; i < pyros.length; i++) {
411                         columns[i] = new PyroColumn(this, i*2 + 1, 0, i);
412                         columns[i].set(pyros[i]);
413                 }
414
415                 /* Pyro firing time */
416                 c = new GridBagConstraints();
417                 c.gridx = 0; c.gridy = row;
418                 c.gridwidth = 2;
419                 c.fill = GridBagConstraints.NONE;
420                 c.anchor = GridBagConstraints.LINE_START;
421                 c.insets = il;
422                 c.ipady = 5;
423                 pyro_firing_time_label = new JLabel("Pyro Firing Time(s):");
424                 pane.add(pyro_firing_time_label, c);
425
426                 c = new GridBagConstraints();
427                 c.gridx = 2; c.gridy = row;
428                 c.gridwidth = 7;
429                 c.fill = GridBagConstraints.HORIZONTAL;
430                 c.weightx = 1;
431                 c.anchor = GridBagConstraints.LINE_START;
432                 c.insets = ir;
433                 c.ipady = 5;
434                 pyro_firing_time_value = new JComboBox<String>(pyro_firing_time_values);
435                 pyro_firing_time_value.setEditable(true);
436                 pyro_firing_time_value.addItemListener(this);
437                 set_pyro_firing_time(pyro_firing_time);
438                 pane.add(pyro_firing_time_value, c);
439                 pyro_firing_time_value.setToolTipText("Length of extra pyro channel firing pulse");
440
441                 c = new GridBagConstraints();
442                 c.gridx = pyros.length*2-1;
443                 c.fill = GridBagConstraints.HORIZONTAL;
444                 c.gridwidth = 2;
445                 c.gridy = 1000;
446                 JButton close = new JButton("Close");
447                 pane.add(close, c);
448                 close.addActionListener(this);
449                 close.setActionCommand("Close");
450
451                 addWindowListener(new ConfigListener(this, owner));
452                 AltosPreferences.register_units_listener(this);
453         }
454
455         public void dispose() {
456                 AltosPreferences.unregister_units_listener(this);
457                 super.dispose();
458         }
459
460         public void make_visible() {
461                 pack();
462                 setVisible(true);
463         }
464 }