summaryrefslogtreecommitdiff
path: root/altosuilib/AltosUIIndicator.java
blob: 6baa185c0cf44170095ebe126b6b74212bc8c09f (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
 * 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; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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_13;

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

public abstract class AltosUIIndicator implements AltosFontListener, AltosUnitsListener {
	JLabel		label;
	JTextField[]	values;
	AltosLights	lights;
	int		number_values;
	boolean		has_lights;
	int		value_width;

	abstract public void show(AltosState state, AltosListenerState listener_state);

	public void set_lights(boolean on) {
		lights.set(on);
	}

	public void setVisible(boolean visible) {
		if (lights != null)
			lights.setVisible(visible);
		label.setVisible(visible);
		for (int i = 0; i < values.length; i++)
			values[i].setVisible(visible);
	}

	public void reset() {
		for (int i = 0; i < values.length; i++)
			values[i].setText("");
		if (lights != null)
			lights.set(false);
	}

	public void show() {
		if (lights != null)
			lights.setVisible(true);
		label.setVisible(true);
		for (int i = 0; i < values.length; i++)
			values[i].setVisible(true);
	}

	public void show(String... s) {
		int	n = Math.min(s.length, values.length);

		show();
		for (int i = 0; i < n; i++)
			values[i].setText(s[i]);
	}

	public void show(String format, double value) {
		show(String.format(format, value));
	}

	public void show(String format, int value) {
		show(String.format(format, value));
	}

	public void show(String format1, double value1, String format2, double value2) {
		show(String.format(format1, value1), String.format(format2, value2));
	}

	public void show(String format1, int value1, String format2, int value2) {
		show(String.format(format1, value1), String.format(format2, value2));
	}

	public void hide() {
		if (lights != null)
			lights.setVisible(false);
		label.setVisible(false);
		for (int i = 0; i < values.length; i++)
			values[i].setVisible(false);
	}

	public void font_size_changed(int font_size) {
		label.setFont(AltosUILib.label_font);
		for (int i = 0; i < values.length; i++)
			values[i].setFont(AltosUILib.value_font);
	}

	public void units_changed(boolean imperial_units) {
	}

	public void set_label(String text) {
		label.setText(text);
	}

	public void remove(Container container) {
		if (lights != null)
			container.remove(lights);
		container.remove(label);
		for (int i = 0; i < values.length; i++)
			container.remove(values[i]);
	}

	public AltosUIIndicator (Container container, int x, int y, int label_width, String text, int number_values, boolean has_lights, int value_width, int value_space) {
		GridBagLayout		layout = (GridBagLayout)(container.getLayout());

		GridBagConstraints	c = new GridBagConstraints();
		c.weighty = 1;

		if (has_lights) {
			lights = new AltosLights();
			c.gridx = x; c.gridy = y;
			c.anchor = GridBagConstraints.CENTER;
			c.fill = GridBagConstraints.VERTICAL;
			c.weightx = 0;
			layout.setConstraints(lights, c);
			container.add(lights);
		}

		label = new JLabel(text);
		label.setFont(AltosUILib.label_font);
		label.setHorizontalAlignment(SwingConstants.LEFT);
		c.gridx = x + 1; c.gridy = y;
		c.gridwidth = label_width;
		c.insets = new Insets(AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad);
		c.anchor = GridBagConstraints.WEST;
		c.fill = GridBagConstraints.VERTICAL;
		c.weightx = 0;
		layout.setConstraints(label, c);
		container.add(label);

		values = new JTextField[number_values];
		for (int i = 0; i < values.length; i++) {
			values[i] = new JTextField(AltosUILib.text_width);
			values[i].setEditable(false);
			values[i].setFont(AltosUILib.value_font);
			values[i].setHorizontalAlignment(SwingConstants.RIGHT);
			c.gridx = 1 + label_width + x + i * value_space; c.gridy = y;
			c.anchor = GridBagConstraints.WEST;
			c.fill = GridBagConstraints.BOTH;
			c.weightx = 1;
			c.gridwidth = value_width;
			layout.setConstraints(values[i], c);
			container.add(values[i]);
		}
	}

	public AltosUIIndicator (Container container, int x, int y, int label_width, String text, int number_values, boolean has_lights, int value_width) {
		this(container, x, y, label_width, text, number_values, has_lights, value_width, 1);
	}

	public AltosUIIndicator (Container container, int x, int y, String text, int number_values, boolean has_lights, int value_width) {
		this(container, x, y, 1, text, number_values, has_lights, value_width);
	}

	public AltosUIIndicator (Container container, int y, String text, int number_values, boolean has_lights, int value_width) {
		this(container, 0, y, text, number_values, has_lights, value_width);
	}

	public AltosUIIndicator (Container container, int y, String text, int number_values, boolean has_lights) {
		this(container, 0, y, text, number_values, has_lights, 1);
	}

	public AltosUIIndicator (Container container, int y, String text, int number_values) {
		this(container, 0, y, text, number_values, false, 1);
	}

	public AltosUIIndicator (Container container, int y, String text) {
		this(container, 0, y, text, 1, false, 1);
	}
}