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