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