summaryrefslogtreecommitdiff
path: root/altosuilib/AltosUIUnitsIndicator.java
blob: 50c30851fea62ac52e895b403407e7e6a68b51f1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
 * Copyright © 2014 Keith Packard <keithp@keithp.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 */

package org.altusmetrum.altosuilib_3;

import java.awt.*;
import javax.swing.*;
import org.altusmetrum.altoslib_5.*;

public abstract class AltosUIUnitsIndicator extends AltosUIIndicator {

	AltosUnits	units;

	abstract public double value(AltosState state, int i);
	public double good() { return 0; }
	public boolean good(double value) { return value != AltosLib.MISSING && value >= good(); }
	public boolean hide(double value) { return false; }

	public boolean hide(AltosState state, int i) {
		if (state == null)
			return hide(AltosLib.MISSING);
		return hide(value(state, i));
	}

	public double value (AltosState state, AltosListenerState listener_state, int i) {
		return value(state, i);
	}

	public double[] last_values;

	public void show(double... v) {
		show();
		for (int i = 0; i < values.length; i++) {
			if (v[i] != last_values[i]) {
				String	value_text;
				boolean	good = false;

				if (v[i] == AltosLib.MISSING) {
					value_text = "Missing";
				} else {
					value_text = units.show(8, v[i]);
					if (i == 0)
						good = good(v[i]);
				}
				last_values[i] = v[i];
				if (i == 0 && lights != null)
					set_lights(good);
				values[i].setText(value_text);
			}
		}
	}

	public void units_changed(boolean imperial_units) {
		show(last_values);
	}

	public void show (AltosState state, AltosListenerState listener_state) {
		double[] v = new double[values.length];
		boolean hide = false;

		for (int i = 0; i < values.length; i++) {
			if (state != null)
				v[i] = value(state, listener_state, i);
			else
				v[i] = AltosLib.MISSING;
			if (hide(state, i))
				hide = true;
		}

		if (hide)
			hide();
		else
			show(v);
	}

	public void reset() {
		for (int i = 0; i < last_values.length; i++)
			last_values[i] = AltosLib.MISSING - 1;
	}

	public AltosUIUnitsIndicator (Container container, int x, int y, int label_width, AltosUnits units, String name, int number_values, boolean has_lights, int width) {
		super(container, x, y, label_width, name, number_values, has_lights, width);
		this.units = units;
		last_values = new double[values.length];
		for (int i = 0; i < last_values.length; i++)
			last_values[i] = AltosLib.MISSING - 1;
	}

	public AltosUIUnitsIndicator (Container container, int x, int y, AltosUnits units, String name, int number_values, boolean has_lights, int width) {
		this(container, x, y, 1, units, name, number_values, has_lights, width);
	}

	public AltosUIUnitsIndicator (Container container, int y, AltosUnits units, String name, int number_values, boolean has_lights, int width) {
		this(container, 0, y, units, name, number_values, has_lights, width);
	}

	public AltosUIUnitsIndicator (Container container, int y, AltosUnits units, String name, int width) {
		this(container, 0, y, units, name, 1, false, width);
	}

	public AltosUIUnitsIndicator (Container container, int y, AltosUnits units, String name) {
		this(container, 0, y, units, name, 1, false, 1); 
	}

	public AltosUIUnitsIndicator (Container container, int x,int y, AltosUnits units, String name) {
		this(container, x, y, units, name, 1, false, 1); 
	}
}