Merge branch 'master' of ssh://git.gag.com/scm/git/fw/altos
[fw/altos] / altosui / AltosIgnitor.java
1 /*
2  * Copyright © 2014 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 org.altusmetrum.altoslib_7.*;
24 import org.altusmetrum.altosuilib_7.*;
25
26 public class AltosIgnitor extends AltosUIFlightTab {
27
28         public class Ignitor extends AltosUIUnitsIndicator {
29                 int             ignitor;
30
31                 public double value(AltosState state, int i) {
32                         if (state.ignitor_voltage == null ||
33                             state.ignitor_voltage.length < ignitor)
34                                 return AltosLib.MISSING;
35                         return state.ignitor_voltage[ignitor];
36                 }
37
38                 public double good() { return AltosLib.ao_igniter_good; }
39
40                 public Ignitor (AltosUIFlightTab container, int y) {
41                         super(container, y, AltosConvert.voltage, String.format ("%s Voltage", AltosLib.ignitor_name(y)), 1, true, 1);
42                         ignitor = y;
43                 }
44         }
45
46         Ignitor[] ignitors;
47
48         public void show(AltosState state, AltosListenerState listener_state) {
49                 if (isShowing())
50                         make_ignitors(state);
51                 super.show(state, listener_state);
52         }
53
54         public boolean should_show(AltosState state) {
55                 if (state == null)
56                         return false;
57                 if (state.ignitor_voltage == null)
58                         return false;
59                 return state.ignitor_voltage.length > 0;
60         }
61
62         void make_ignitors(AltosState state) {
63                 int n = (state == null || state.ignitor_voltage == null) ? 0 : state.ignitor_voltage.length;
64                 int old_n = ignitors == null ? 0 : ignitors.length;
65
66                 if (n != old_n) {
67
68                         if (ignitors != null) {
69                                 for (int i = 0; i < ignitors.length; i++) {
70                                         remove(ignitors[i]);
71                                         ignitors[i].remove(this);
72                                         ignitors = null;
73                                 }
74                         }
75
76                         if (n > 0) {
77                                 setVisible(true);
78                                 ignitors = new Ignitor[n];
79                                 for (int i = 0; i < n; i++) {
80                                         ignitors[i] = new Ignitor(this, i);
81                                         add(ignitors[i]);
82                                 }
83                         } else
84                                 setVisible(false);
85                 }
86         }
87
88         public String getName() {
89                 return "Ignitors";
90         }
91 }