altosuilib: Build some common classes for displaying values in flight window
[fw/altos] / altosuilib / AltosUIUnitsIndicator.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 org.altusmetrum.altosuilib_2;
19
20 import java.awt.*;
21 import javax.swing.*;
22 import org.altusmetrum.altoslib_4.*;
23
24 public abstract class AltosUIUnitsIndicator extends AltosUIIndicator {
25
26         AltosUnits      units;
27
28         abstract public double value(AltosState state, int i);
29         public boolean good(double value) { return false; }
30
31         public double[] last_values;
32
33         public void show(double... v) {
34                 for (int i = 0; i < values.length; i++) {
35                         if (v[i] != last_values[i]) {
36                                 String  value_text;
37                                 boolean good = false;
38
39                                 if (v[i] == AltosLib.MISSING) {
40                                         value_text = "Missing";
41                                 } else {
42                                         value_text = units.show(8, v[i]);
43                                         if (i == 0)
44                                                 good = good(v[i]);
45                                 }
46                                 last_values[i] = v[i];
47                                 if (i == 0 && lights != null)
48                                         set_lights(good);
49                                 values[i].setText(value_text);
50                         }
51                 }
52         }
53
54         public void units_changed(boolean imperial_units) {
55                 show(last_values);
56         }
57
58         public void show (AltosState state, AltosListenerState listener_state) {
59
60                 double[] v = new double[values.length];
61
62                 for (int i = 0; i < values.length; i++) {
63                         if (state != null)
64                                 v[i] = value(state, i);
65                         else
66                                 v[i] = AltosLib.MISSING;
67                 }
68                 show(v);
69
70         }
71
72         public AltosUIUnitsIndicator (Container container, int y, AltosUnits units, String name, int number_values, boolean has_lights, int width) {
73                 super(container, y, name, number_values, has_lights, width);
74                 this.units = units;
75                 last_values = new double[values.length];
76                 for (int i = 0; i < last_values.length; i++)
77                         last_values[i] = AltosLib.MISSING - 1;
78         }
79 }