altos/telelco-v3.0: Merge info into one screen
[fw/altos] / altosuilib / AltosFlightPyroTable.java
1 /*
2  * Copyright © 2024 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 org.altusmetrum.altosuilib_14;
20
21 import java.awt.*;
22 import javax.swing.*;
23 import java.util.*;
24 import org.altusmetrum.altoslib_14.*;
25
26 public class AltosFlightPyroTable extends JComponent
27         implements AltosFontListener, AltosUnitsListener
28 {
29         GridBagLayout   layout;
30         AltosPyro[]     pyros;
31         int             npyro;
32         FlightPyro[]    flight_pyros;
33
34         class FlightPyro implements AltosFontListener {
35                 JLabel          label;
36                 JTextField[]    text_fields;
37
38                 public void font_size_changed(int font_size) {
39                         label.setFont(AltosUILib.label_font);
40                         for (int i = 0; i < text_fields.length; i++)
41                                 text_fields[i].setFont(AltosUILib.value_font);
42                 }
43
44                 public void set_value(int y, int p, String value) {
45                         GridBagConstraints      c = new GridBagConstraints();
46                         c.insets = new Insets(AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad);
47                         c.weighty = 1;
48                         JTextField      text_field;
49
50                         if (text_fields[p] == null) {
51                                 text_field = new JTextField(value);
52                                 text_field.setEditable(false);
53                                 text_field.setFont(AltosUILib.value_font);
54                                 text_field.setHorizontalAlignment(SwingConstants.RIGHT);
55                                 c.gridx = p+1; c.gridy = y;
56                                 c.anchor = GridBagConstraints.EAST;
57                                 c.fill = GridBagConstraints.BOTH;
58                                 c.weightx = 1;
59                                 layout.setConstraints(text_field, c);
60                                 add(text_field);
61                                 text_fields[p] = text_field;
62                         } else {
63                                 text_fields[p].setText(value);
64                         }
65                 }
66
67                 public void set_label(String text) {
68                         label.setText(text);
69                 }
70
71                 public FlightPyro(GridBagLayout layout, int y, String label_text, int npyro) {
72                         GridBagConstraints      c = new GridBagConstraints();
73                         c.insets = new Insets(AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad);
74                         c.weighty = 1;
75
76                         if (label_text != null) {
77                                 label = new JLabel(label_text);
78                                 label.setFont(AltosUILib.label_font);
79                                 label.setHorizontalAlignment(SwingConstants.LEFT);
80                                 c.gridx = 0; c.gridy = y;
81                                 c.anchor = GridBagConstraints.WEST;
82                                 c.fill = GridBagConstraints.VERTICAL;
83                                 c.weightx = 0;
84                                 layout.setConstraints(label, c);
85                                 add(label);
86                         }
87
88                         text_fields = new JTextField[npyro];
89                 }
90         }
91
92         public void font_size_changed(int font_size) {
93                 for (int i = 0; i < flight_pyros.length; i++)
94                         flight_pyros[i].font_size_changed(font_size);
95         }
96
97         public void set_pyros() {
98                 int nrow = 1;
99                 for (int flag = 1; flag < AltosPyro.pyro_all; flag <<= 1) {
100                         if ((AltosPyro.pyro_all_useful & flag) != 0) {
101                                 for (int p = 0; p < npyro; p++) {
102                                         if ((pyros[p].flags & flag) != 0) {
103                                                 String text;
104                                                 double  value = pyros[p].get_value(flag);
105                                                 if ((flag & AltosPyro.pyro_state_value) != 0) {
106                                                         text = AltosLib.state_name_capital((int) value);
107                                                 } else {
108                                                         double  scale = AltosPyro.pyro_to_scale(flag);
109                                                         double  unit_value = value;
110                                                         AltosUnits units = AltosPyro.pyro_to_units(flag);
111                                                         if (units != null)
112                                                                 unit_value = units.parse_value(value);
113                                                         String  format;
114                                                         if (scale >= 100)
115                                                                 format = "%6.2f";
116                                                         else if (scale >= 10)
117                                                                 format = "%6.1f";
118                                                         else
119                                                                 format = "%6.0f";
120                                                         text = String.format(format, unit_value);
121                                                 }
122                                                 flight_pyros[nrow].set_value(nrow, p, text);
123                                         }
124                                 }
125                                 nrow++;
126                         }
127                 }
128         }
129
130         public void units_changed(boolean imperial_units) {
131                 System.out.printf("units changed\n");
132                 int nrow = 1;
133                 for (int flag = 1; flag < AltosPyro.pyro_all; flag <<= 1) {
134                         if ((AltosPyro.pyro_all_useful & flag) != 0) {
135                                 String name = AltosPyro.pyro_to_name(flag);
136                                 flight_pyros[nrow].set_label(name);
137                         }
138                 }
139                 set_pyros();
140         }
141
142         public void tell_closing() {
143                 AltosUIPreferences.unregister_font_listener(this);
144         }
145
146         public AltosFlightPyroTable(AltosPyro[] pyros, int npyro) {
147                 layout = new GridBagLayout();
148
149                 int nrow = 0;
150
151                 for (int flag = 1; flag < AltosPyro.pyro_all; flag <<= 1) {
152                         if ((AltosPyro.pyro_all_useful & flag) != 0) {
153                                 nrow++;
154                         }
155                 }
156
157                 flight_pyros = new FlightPyro[nrow + 1];
158
159                 nrow = 0;
160
161                 flight_pyros[0] = new FlightPyro(layout, 0, null, npyro);
162
163                 for (int p = 0; p < npyro; p++) {
164                         flight_pyros[0].set_value(0, p, String.format("Channel %c", 'A' + p));
165                 }
166                 nrow++;
167                 for (int flag = 1; flag < AltosPyro.pyro_all; flag <<= 1) {
168                         if ((AltosPyro.pyro_all_useful & flag) != 0) {
169                                 String name = AltosPyro.pyro_to_name(flag);
170                                 flight_pyros[nrow] = new FlightPyro(layout, nrow, name, npyro);
171                                 nrow++;
172                         }
173                 }
174
175
176                 this.pyros = pyros;
177                 this.npyro = npyro;
178
179                 set_pyros();
180
181                 setLayout(layout);
182                 AltosUIPreferences.register_font_listener(this);
183         }
184 }