Switch from GPLv2 to GPLv2+
[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_11;
20
21 import java.awt.*;
22 import javax.swing.*;
23 import org.altusmetrum.altoslib_11.*;
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                 for (int i = 0; i < values.length; i++) {
87                         if (state != null)
88                                 v[i] = value(state, listener_state, i);
89                         else
90                                 v[i] = AltosLib.MISSING;
91                         if (hide(state, listener_state, i))
92                                 hide = true;
93                 }
94
95                 if (hide)
96                         hide();
97                 else
98                         show(v);
99         }
100
101         public void reset() {
102                 for (int i = 0; i < last_values.length; i++)
103                         last_values[i] = AltosLib.MISSING;
104         }
105
106         public AltosUIUnitsIndicator (Container container, int x, int y, int label_width, AltosUnits units, String name, int number_values, boolean has_lights, int width) {
107                 super(container, x, y, label_width, name, number_values, has_lights, width);
108                 this.units = units;
109                 last_values = new double[values.length];
110                 for (int i = 0; i < last_values.length; i++)
111                         last_values[i] = AltosLib.MISSING;
112         }
113
114         public AltosUIUnitsIndicator (Container container, int x, int y, AltosUnits units, String name, int number_values, boolean has_lights, int width) {
115                 this(container, x, y, 1, units, name, number_values, has_lights, width);
116         }
117
118         public AltosUIUnitsIndicator (Container container, int y, AltosUnits units, String name, int number_values, boolean has_lights, int width) {
119                 this(container, 0, y, units, name, number_values, has_lights, width);
120         }
121
122         public AltosUIUnitsIndicator (Container container, int y, AltosUnits units, String name, int width) {
123                 this(container, 0, y, units, name, 1, false, width);
124         }
125
126         public AltosUIUnitsIndicator (Container container, int y, AltosUnits units, String name) {
127                 this(container, 0, y, units, name, 1, false, 1); 
128         }
129
130         public AltosUIUnitsIndicator (Container container, int x,int y, AltosUnits units, String name) {
131                 this(container, x, y, units, name, 1, false, 1); 
132         }
133 }