java: Refactor AltosFlightDisplay units and font update handling
[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 implements AltosFontListener, AltosUnitsListener {
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 font_size_changed(int font_size) {
70                         label.setFont(Altos.label_font);
71                         value.setFont(Altos.value_font);
72                 }
73
74                 public void units_changed(boolean imperial_units) {
75                 }
76
77                 public void set_label(String text) {
78                         label.setText(text);
79                 }
80
81                 public LaunchStatus (GridBagLayout layout, int y, String text) {
82                         GridBagConstraints      c = new GridBagConstraints();
83                         c.weighty = 1;
84
85                         lights = new AltosLights();
86                         c.gridx = 0; c.gridy = y;
87                         c.anchor = GridBagConstraints.CENTER;
88                         c.fill = GridBagConstraints.VERTICAL;
89                         c.weightx = 0;
90                         layout.setConstraints(lights, c);
91                         add(lights);
92
93                         label = new JLabel(text);
94                         label.setFont(Altos.label_font);
95                         label.setHorizontalAlignment(SwingConstants.LEFT);
96                         c.gridx = 1; c.gridy = y;
97                         c.insets = new Insets(Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad);
98                         c.anchor = GridBagConstraints.WEST;
99                         c.fill = GridBagConstraints.VERTICAL;
100                         c.weightx = 0;
101                         layout.setConstraints(label, c);
102                         add(label);
103
104                         value = new JTextField(Altos.text_width);
105                         value.setFont(Altos.value_font);
106                         value.setHorizontalAlignment(SwingConstants.RIGHT);
107                         c.gridx = 2; c.gridy = y;
108                         c.anchor = GridBagConstraints.WEST;
109                         c.fill = GridBagConstraints.BOTH;
110                         c.weightx = 1;
111                         layout.setConstraints(value, c);
112                         add(value);
113
114                 }
115         }
116
117         public static String ignitor_name(int i) {
118                 return String.format("Ignitor %c", 'A' + i);
119         }
120
121         class Ignitor extends LaunchStatus {
122                 int ignitor;
123
124                 void show (AltosState state, AltosListenerState listener_state) {
125                         if (state == null || state.ignitor_voltage[ignitor] == AltosLib.MISSING) {
126                                 hide();
127                         } else {
128                                 show("%4.2f V", state.ignitor_voltage[ignitor]);
129                                 lights.set(state.ignitor_voltage[ignitor] >= AltosLib.ao_igniter_good);
130                         }
131                 }
132
133                 public Ignitor (GridBagLayout layout, int y) {
134                         super(layout, y, String.format ("%s Voltage", ignitor_name(y)));
135                         ignitor = y;
136                 }
137         }
138
139         Ignitor[] ignitors;
140
141         public void reset() {
142                 if (ignitors == null)
143                         return;
144                 for (int i = 0; i < ignitors.length; i++)
145                         ignitors[i].reset();
146         }
147
148         public void font_size_changed(int font_size) {
149                 if (ignitors == null)
150                         return;
151                 for (int i = 0; i < ignitors.length; i++)
152                         ignitors[i].font_size_changed(font_size);
153         }
154
155         public void units_changed(boolean imperial_units) {
156         }
157
158         public void show(AltosState state, AltosListenerState listener_state) {
159                 make_ignitors(state);
160                 if (ignitors == null)
161                         return;
162                 for (int i = 0; i < ignitors.length; i++)
163                         ignitors[i].show(state, listener_state);
164                 return;
165         }
166
167         public boolean should_show(AltosState state) {
168                 if (state == null)
169                         return false;
170                 if (state.ignitor_voltage == null)
171                         return false;
172                 return state.ignitor_voltage.length > 0;
173         }
174
175         void make_ignitors(AltosState state) {
176                 int n = state == null ? 0 : state.ignitor_voltage.length;
177
178                 if (n > 0) {
179
180                         if (ignitors == null || ignitors.length != n) {
181                                 layout = new GridBagLayout();
182
183                                 setLayout(layout);
184                                 ignitors = new Ignitor[n];
185                                 for (int i = 0; i < n; i++)
186                                         ignitors[i] = new Ignitor(layout, i);
187                         }
188                 } else {
189                         if (ignitors != null) {
190                                 for (int i = 0; i < n; i++)
191                                         ignitors[i].dispose();
192                                 ignitors = null;
193                                 setVisible(false);
194                         }
195                 }
196         }
197
198         public String getName() {
199                 return "Ignitors";
200         }
201
202         public AltosIgnitor() {
203                 make_ignitors(null);
204         }
205 }