doc: new TeleLaunch system manual
[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; 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_13;
20
21 import java.awt.*;
22 import javax.swing.*;
23 import org.altusmetrum.altoslib_13.*;
24
25 public abstract class AltosUIUnitsIndicator extends AltosUIIndicator {
26
27         AltosUnits      units;
28
29         abstract public double value(AltosState state, int i);
30         public double good() { return 0; }
31         public boolean good(double value) { return value != AltosLib.MISSING && value >= good(); }
32         public boolean hide(double value) { return false; }
33
34         public boolean hide(AltosState state, int i) {
35                 if (state == null)
36                         return hide(AltosLib.MISSING);
37                 return hide(value(state, i));
38         }
39
40         public boolean hide(AltosState state, AltosListenerState listener_state, int i) {
41                 return hide(state, i);
42         }
43
44         public double value (AltosState state, AltosListenerState listener_state, int i) {
45                 return value(state, i);
46         }
47
48         public double[] last_values;
49
50         private void show(boolean force, double... v) {
51                 show();
52                 for (int i = 0; i < values.length; i++) {
53                         if (force || v[i] != last_values[i]) {
54                                 String  value_text;
55                                 boolean good = false;
56
57                                 if (v[i] == AltosLib.MISSING) {
58                                         value_text = "Missing";
59                                 } else {
60                                         value_text = units.show(8, v[i]);
61                                         if (i == 0)
62                                                 good = good(v[i]);
63                                 }
64                                 last_values[i] = v[i];
65                                 if (i == 0 && lights != null)
66                                         set_lights(good);
67                                 values[i].setText(value_text);
68                         }
69                 }
70         }
71
72         boolean hide = false;
73
74         public void show(double... v) {
75                 show(false, v);
76         }
77
78         public void units_changed(boolean imperial_units) {
79                 if (!hide)
80                         show(true, last_values);
81         }
82
83         public void show (AltosState state, AltosListenerState listener_state) {
84                 double[] v = new double[values.length];
85
86                 hide = false;
87                 for (int i = 0; i < values.length; i++) {
88                         if (state != null)
89                                 v[i] = value(state, listener_state, i);
90                         else
91                                 v[i] = AltosLib.MISSING;
92                         if (hide(state, listener_state, i))
93                                 hide = true;
94                 }
95
96                 if (hide)
97                         hide();
98                 else
99                         show(v);
100         }
101
102         public void reset() {
103                 for (int i = 0; i < last_values.length; i++)
104                         last_values[i] = AltosLib.MISSING;
105         }
106
107         public AltosUIUnitsIndicator (Container container, int x, int y, int label_width, AltosUnits units, String name, int number_values, boolean has_lights, int width) {
108                 super(container, x, y, label_width, name, number_values, has_lights, width);
109                 this.units = units;
110                 last_values = new double[values.length];
111                 for (int i = 0; i < last_values.length; i++)
112                         last_values[i] = AltosLib.MISSING;
113         }
114
115         public AltosUIUnitsIndicator (Container container, int x, int y, AltosUnits units, String name, int number_values, boolean has_lights, int width) {
116                 this(container, x, y, 1, units, name, number_values, has_lights, width);
117         }
118
119         public AltosUIUnitsIndicator (Container container, int y, AltosUnits units, String name, int number_values, boolean has_lights, int width) {
120                 this(container, 0, y, units, name, number_values, has_lights, width);
121         }
122
123         public AltosUIUnitsIndicator (Container container, int y, AltosUnits units, String name, int width) {
124                 this(container, 0, y, units, name, 1, false, width);
125         }
126
127         public AltosUIUnitsIndicator (Container container, int y, AltosUnits units, String name) {
128                 this(container, 0, y, units, name, 1, false, 1); 
129         }
130
131         public AltosUIUnitsIndicator (Container container, int x,int y, AltosUnits units, String name) {
132                 this(container, x, y, units, name, 1, false, 1); 
133         }
134 }