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