fcab7427f3aa060c97a37b919e1db1ba4e6fc86a
[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 javax.swing.*;
22 import org.altusmetrum.altoslib_3.*;
23
24 public class AltosIgnitor extends JComponent implements AltosFlightDisplay {
25         GridBagLayout   layout;
26
27         public class LaunchStatus {
28                 JLabel          label;
29                 JTextField      value;
30                 AltosLights     lights;
31
32                 void show(AltosState state, AltosListenerState listener_state) {}
33
34                 void reset() {
35                         value.setText("");
36                         lights.set(false);
37                 }
38
39                 public void show() {
40                         label.setVisible(true);
41                         value.setVisible(true);
42                         lights.setVisible(true);
43                 }
44
45                 void show(String s) {
46                         show();
47                         value.setText(s);
48                 }
49
50                 void show(String format, double value) {
51                         show(String.format(format, value));
52                 }
53
54                 void show(String format, int value) {
55                         show(String.format(format, value));
56                 }
57
58                 public void hide() {
59                         label.setVisible(false);
60                         value.setVisible(false);
61                         lights.setVisible(false);
62                 }
63
64                 public void dispose() {
65                         hide();
66                 }
67
68                 public void set_font() {
69                         label.setFont(Altos.label_font);
70                         value.setFont(Altos.value_font);
71                 }
72
73                 public void set_label(String text) {
74                         label.setText(text);
75                 }
76
77                 public LaunchStatus (GridBagLayout layout, int y, String text) {
78                         GridBagConstraints      c = new GridBagConstraints();
79                         c.weighty = 1;
80
81                         lights = new AltosLights();
82                         c.gridx = 0; c.gridy = y;
83                         c.anchor = GridBagConstraints.CENTER;
84                         c.fill = GridBagConstraints.VERTICAL;
85                         c.weightx = 0;
86                         layout.setConstraints(lights, c);
87                         add(lights);
88
89                         label = new JLabel(text);
90                         label.setFont(Altos.label_font);
91                         label.setHorizontalAlignment(SwingConstants.LEFT);
92                         c.gridx = 1; c.gridy = y;
93                         c.insets = new Insets(Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad);
94                         c.anchor = GridBagConstraints.WEST;
95                         c.fill = GridBagConstraints.VERTICAL;
96                         c.weightx = 0;
97                         layout.setConstraints(label, c);
98                         add(label);
99
100                         value = new JTextField(Altos.text_width);
101                         value.setFont(Altos.value_font);
102                         value.setHorizontalAlignment(SwingConstants.RIGHT);
103                         c.gridx = 2; c.gridy = y;
104                         c.anchor = GridBagConstraints.WEST;
105                         c.fill = GridBagConstraints.BOTH;
106                         c.weightx = 1;
107                         layout.setConstraints(value, c);
108                         add(value);
109
110                 }
111         }
112
113         public static String ignitor_name(int i) {
114                 return String.format("Ignitor %c", 'A' + i);
115         }
116
117         class Ignitor extends LaunchStatus {
118                 int ignitor;
119
120                 void show (AltosState state, AltosListenerState listener_state) {
121                         if (state == null || state.ignitor_voltage[ignitor] == AltosLib.MISSING) {
122                                 hide();
123                         } else {
124                                 show("%4.2f V", state.ignitor_voltage[ignitor]);
125                                 lights.set(state.ignitor_voltage[ignitor] >= AltosLib.ao_igniter_good);
126                         }
127                 }
128
129                 public Ignitor (GridBagLayout layout, int y) {
130                         super(layout, y, String.format ("%s Voltage", ignitor_name(y)));
131                         ignitor = y;
132                 }
133         }
134
135         Ignitor[] ignitors;
136
137         public void reset() {
138                 if (ignitors == null)
139                         return;
140                 for (int i = 0; i < ignitors.length; i++)
141                         ignitors[i].reset();
142         }
143
144         public void set_font() {
145                 if (ignitors == null)
146                         return;
147                 for (int i = 0; i < ignitors.length; i++)
148                         ignitors[i].set_font();
149         }
150
151         public void show(AltosState state, AltosListenerState listener_state) {
152                 make_ignitors(state);
153                 if (ignitors == null)
154                         return;
155                 for (int i = 0; i < ignitors.length; i++)
156                         ignitors[i].show(state, listener_state);
157                 return;
158         }
159
160         public boolean should_show(AltosState state) {
161                 if (state == null)
162                         return false;
163                 if (state.ignitor_voltage == null)
164                         return false;
165                 return state.ignitor_voltage.length > 0;
166         }
167
168         void make_ignitors(AltosState state) {
169                 int n = state == null ? 0 : state.ignitor_voltage.length;
170
171                 if (n > 0) {
172
173                         if (ignitors == null || ignitors.length != n) {
174                                 layout = new GridBagLayout();
175
176                                 setLayout(layout);
177                                 ignitors = new Ignitor[n];
178                                 for (int i = 0; i < n; i++)
179                                         ignitors[i] = new Ignitor(layout, i);
180                         }
181                 } else {
182                         if (ignitors != null) {
183                                 for (int i = 0; i < n; i++)
184                                         ignitors[i].dispose();
185                                 ignitors = null;
186                                 setVisible(false);
187                         }
188                 }
189         }
190
191         public String getName() {
192                 return "Ignitors";
193         }
194
195         public AltosIgnitor() {
196                 make_ignitors(null);
197         }
198 }