6cbac31677e243a009b419c4ffaad79467649a4d
[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_4.*;
25 import org.altusmetrum.altosuilib_2.*;
26
27 public class AltosConfigPyroUI
28         extends AltosUIDialog
29         implements ItemListener, DocumentListener, AltosUnitsListener, ActionListener
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, AltosUnitsListener
49         {
50                 public int              flag;
51                 public JCheckBox        enable;
52                 public JTextField       value;
53                 public JComboBox<String>        combo;
54                 AltosConfigPyroUI       ui;
55                 boolean                 setting;
56
57                 public void set_enable(boolean enable) {
58                         if (value != null)
59                                 value.setEnabled(enable);
60                         if (combo != null)
61                                 combo.setEnabled(enable);
62                 }
63
64                 public void itemStateChanged(ItemEvent e) {
65                         set_enable(enable.isSelected());
66                         if (!setting)
67                                 ui.set_dirty();
68                 }
69
70                 public void changedUpdate(DocumentEvent e) {
71                         if (!setting)
72                                 ui.set_dirty();
73                 }
74
75                 public void insertUpdate(DocumentEvent e) {
76                         if (!setting)
77                                 ui.set_dirty();
78                 }
79
80                 public void removeUpdate(DocumentEvent e) {
81                         if (!setting)
82                                 ui.set_dirty();
83                 }
84
85                 public void units_changed(boolean imperial_units) {
86                         AltosUnits units = AltosPyro.pyro_to_units(flag);
87
88                         if (units != null) {
89                                 try {
90                                         double v = units.parse(value.getText(), !imperial_units);
91                                         set(enabled(), v);
92                                 } catch (NumberFormatException ne) {
93                                         set(enabled(), 0.0);
94                                 }
95                         }
96                 }
97
98                 public void set(boolean new_enable, double new_value) {
99                         setting = true;
100                         enable.setSelected(new_enable);
101                         set_enable(new_enable);
102                         if (value != null) {
103                                 double  scale = AltosPyro.pyro_to_scale(flag);
104                                 double  unit_value = new_value;
105                                 AltosUnits units = AltosPyro.pyro_to_units(flag);
106                                 if (units != null)
107                                         unit_value = units.value(new_value);
108                                 String  format;
109                                 if (scale >= 100)
110                                         format = "%6.2f";
111                                 else if (scale >= 10)
112                                         format = "%6.1f";
113                                 else
114                                         format = "%6.0f";
115                                 value.setText(String.format(format, unit_value));
116                         }
117                         if (combo != null)
118                                 if (new_value >= AltosLib.ao_flight_boost && new_value <= AltosLib.ao_flight_landed)
119                                         combo.setSelectedIndex((int) new_value - AltosLib.ao_flight_boost);
120                         setting = false;
121                 }
122
123                 public boolean enabled() {
124                         return enable.isSelected();
125                 }
126
127                 public double value() {
128                         if (value != null) {
129                                 AltosUnits units = AltosPyro.pyro_to_units(flag);
130                                 if (units != null)
131                                         return units.parse(value.getText());
132                                 return Double.parseDouble(value.getText());
133                         }
134                         if (combo != null)
135                                 return combo.getSelectedIndex() + AltosLib.ao_flight_boost;
136                         return 0;
137                 }
138
139                 public PyroItem(AltosConfigPyroUI in_ui, int in_flag, int x, int y) {
140
141                         ui = in_ui;
142                         flag = in_flag;
143
144                         GridBagConstraints      c;
145                         c = new GridBagConstraints();
146                         c.gridx = x; c.gridy = y;
147                         c.gridwidth = 1;
148                         c.fill = GridBagConstraints.NONE;
149                         c.anchor = GridBagConstraints.LINE_START;
150                         c.insets = il;
151                         enable = new JCheckBox();
152                         enable.addItemListener(this);
153                         pane.add(enable, c);
154
155                         if ((flag & AltosPyro.pyro_no_value) == 0) {
156                                 c = new GridBagConstraints();
157                                 c.gridx = x+1; c.gridy = y;
158                                 c.gridwidth = 1;
159                                 c.fill = GridBagConstraints.NONE;
160                                 c.anchor = GridBagConstraints.LINE_START;
161                                 c.insets = il;
162                                 if ((flag & AltosPyro.pyro_state_value) != 0) {
163                                         make_state_names();
164                                         combo = new JComboBox<String>(state_names);
165                                         combo.addItemListener(this);
166                                         pane.add(combo, c);
167                                 } else {
168                                         value = new JTextField(10);
169                                         value.getDocument().addDocumentListener(this);
170                                         pane.add(value, c);
171                                 }
172                         }
173                 }
174         }
175
176         class PyroColumn implements AltosUnitsListener {
177                 public PyroItem[]       items;
178                 public JLabel           label;
179                 int                     channel;
180
181                 public void set(AltosPyro pyro) {
182                         int row = 0;
183                         for (int flag = 1; flag < AltosPyro.pyro_all; flag <<= 1) {
184                                 if ((AltosPyro.pyro_all & flag) != 0) {
185                                         items[row].set((pyro.flags & flag) != 0,
186                                                        pyro.get_value(flag));
187                                         row++;
188                                 }
189                         }
190                 }
191
192                 public AltosPyro get() {
193                         AltosPyro       p = new AltosPyro(channel);
194
195                         int row = 0;
196                         for (int flag = 1; flag < AltosPyro.pyro_all; flag <<= 1) {
197                                 if ((AltosPyro.pyro_all & flag) != 0) {
198                                         if (items[row].enabled()) {
199                                                 p.flags |= flag;
200                                                 p.set_value(flag, items[row].value());
201                                         }
202                                         row++;
203                                 }
204                         }
205                         return p;
206                 }
207
208                 public void units_changed(boolean imperial_units) {
209                         int row = 0;
210                         for (int flag = 1; flag < AltosPyro.pyro_all; flag <<= 1) {
211                                 if ((AltosPyro.pyro_all & flag) != 0) {
212                                         items[row].units_changed(imperial_units);
213                                         row++;
214                                 }
215                         }
216                 }
217
218                 public PyroColumn(AltosConfigPyroUI ui, int x, int y, int in_channel) {
219
220                         channel = in_channel;
221
222                         int     nrow = 0;
223                         for (int flag = 1; flag < AltosPyro.pyro_all; flag <<= 1)
224                                 if ((flag & AltosPyro.pyro_all) != 0)
225                                         nrow++;
226
227                         items = new PyroItem[nrow];
228                         int row = 0;
229
230                         GridBagConstraints      c;
231                         c = new GridBagConstraints();
232                         c.gridx = x; c.gridy = y;
233                         c.gridwidth = 2;
234                         c.fill = GridBagConstraints.NONE;
235                         c.anchor = GridBagConstraints.CENTER;
236                         c.insets = il;
237                         label = new JLabel(String.format("Pyro Channel %c", 'A' + channel));
238                         pane.add(label, c);
239                         y++;
240
241                         for (int flag = 1; flag < AltosPyro.pyro_all; flag <<= 1)
242                                 if ((flag & AltosPyro.pyro_all) != 0) {
243                                         items[row] = new PyroItem(ui, flag, x, y + row);
244                                         row++;
245                                 }
246                 }
247         }
248
249         PyroColumn[]    columns;
250         JLabel[]        labels;
251
252         public void set_pyros(AltosPyro[] pyros) {
253                 for (int i = 0; i < pyros.length; i++) {
254                         if (pyros[i].channel < columns.length)
255                                 columns[pyros[i].channel].set(pyros[i]);
256                 }
257         }
258
259         public AltosPyro[] get_pyros() {
260                 AltosPyro[]     pyros = new AltosPyro[columns.length];
261                 for (int c = 0; c < columns.length; c++)
262                         pyros[c] = columns[c].get();
263                 return pyros;
264         }
265
266         public void set_dirty() {
267                 owner.set_dirty();
268         }
269
270         public void itemStateChanged(ItemEvent e) {
271                 owner.set_dirty();
272         }
273
274         public void changedUpdate(DocumentEvent e) {
275                 owner.set_dirty();
276         }
277
278         public void insertUpdate(DocumentEvent e) {
279                 owner.set_dirty();
280         }
281
282         public void removeUpdate(DocumentEvent e) {
283                 owner.set_dirty();
284         }
285
286         public void units_changed(boolean imperial_units) {
287                 for (int c = 0; c < columns.length; c++)
288                         columns[c].units_changed(imperial_units);
289                 int r = 0;
290                 for (int flag = 1; flag <= AltosPyro.pyro_all; flag <<= 1) {
291                         String n = AltosPyro.pyro_to_name(flag);
292                         if (n != null) {
293                                 labels[r].setText(n);
294                                 r++;
295                         }
296                 }
297         }
298
299         /* A window listener to catch closing events and tell the config code */
300         class ConfigListener extends WindowAdapter {
301                 AltosConfigPyroUI       ui;
302                 AltosConfigUI           owner;
303
304                 public ConfigListener(AltosConfigPyroUI this_ui, AltosConfigUI this_owner) {
305                         ui = this_ui;
306                         owner = this_owner;
307                 }
308
309                 public void windowClosing(WindowEvent e) {
310                         ui.setVisible(false);
311                 }
312         }
313
314         /* Listen for events from our buttons */
315         public void actionPerformed(ActionEvent e) {
316                 String  cmd = e.getActionCommand();
317
318                 if (cmd.equals("Close"))
319                         setVisible(false);
320         }
321
322         public AltosConfigPyroUI(AltosConfigUI in_owner, AltosPyro[] pyros) {
323
324                 super(in_owner, "Configure Pyro Channels", false);
325
326                 owner = in_owner;
327
328                 GridBagConstraints      c;
329
330                 pane = getContentPane();
331                 pane.setLayout(new GridBagLayout());
332
333                 int     nrow = 0;
334                 for (int flag = 1; flag < AltosPyro.pyro_all; flag <<= 1)
335                         if ((flag & AltosPyro.pyro_all) != 0)
336                                 nrow++;
337
338                 labels = new JLabel[nrow];
339
340                 int     row = 1;
341
342                 for (int flag = 1; flag <= AltosPyro.pyro_all; flag <<= 1) {
343                         String  n;
344
345                         n = AltosPyro.pyro_to_name(flag);
346                         if (n != null) {
347                                 c = new GridBagConstraints();
348                                 c.gridx = 0; c.gridy = row;
349                                 c.gridwidth = 1;
350                                 c.fill = GridBagConstraints.NONE;
351                                 c.anchor = GridBagConstraints.LINE_START;
352                                 c.insets = il;
353                                 JLabel label = new JLabel(n);
354                                 pane.add(label, c);
355                                 labels[row-1] = label;
356                                 row++;
357                         }
358                 }
359
360                 columns = new PyroColumn[pyros.length];
361
362                 for (int i = 0; i < pyros.length; i++) {
363                         columns[i] = new PyroColumn(this, i*2 + 1, 0, i);
364                         columns[i].set(pyros[i]);
365                 }
366
367                 c = new GridBagConstraints();
368                 c.gridx = pyros.length*2-1;
369                 c.fill = GridBagConstraints.HORIZONTAL;
370                 c.gridwidth = 2;
371                 c.gridy = 1000;
372                 JButton close = new JButton("Close");
373                 pane.add(close, c);
374                 close.addActionListener(this);
375                 close.setActionCommand("Close");
376
377                 addWindowListener(new ConfigListener(this, owner));
378                 AltosPreferences.register_units_listener(this);
379         }
380
381         public void dispose() {
382                 AltosPreferences.unregister_units_listener(this);
383                 super.dispose();
384         }
385
386         public void make_visible() {
387                 pack();
388                 setVisible(true);
389         }
390 }