f47a2f29797041aa6698ed4cd786c1e501ea998d
[fw/altos] / altosuilib / AltosUIIndicator.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 AltosUIIndicator implements AltosFontListener, AltosUnitsListener {
26         JLabel          label;
27         JTextField[]    values;
28         AltosLights     lights;
29         int             number_values;
30         boolean         has_lights;
31         int             value_width;
32
33         abstract public void show(AltosState state, AltosListenerState listener_state);
34
35         public void set_lights(boolean on) {
36                 lights.set(on);
37         }
38
39         public void setVisible(boolean visible) {
40                 if (lights != null)
41                         lights.setVisible(visible);
42                 label.setVisible(visible);
43                 for (int i = 0; i < values.length; i++)
44                         values[i].setVisible(visible);
45         }
46
47         public void reset() {
48                 for (int i = 0; i < values.length; i++)
49                         values[i].setText("");
50                 if (lights != null)
51                         lights.set(false);
52         }
53
54         public void show() {
55                 if (lights != null)
56                         lights.setVisible(true);
57                 label.setVisible(true);
58                 for (int i = 0; i < values.length; i++)
59                         values[i].setVisible(true);
60         }
61
62         public void show(String... s) {
63                 int     n = Math.min(s.length, values.length);
64
65                 show();
66                 for (int i = 0; i < n; i++)
67                         values[i].setText(s[i]);
68         }
69
70         public void show(String format, double value) {
71                 show(String.format(format, value));
72         }
73
74         public void show(String format, int value) {
75                 show(String.format(format, value));
76         }
77
78         public void show(String format1, double value1, String format2, double value2) {
79                 show(String.format(format1, value1), String.format(format2, value2));
80         }
81
82         public void show(String format1, int value1, String format2, int value2) {
83                 show(String.format(format1, value1), String.format(format2, value2));
84         }
85
86         public void hide() {
87                 if (lights != null)
88                         lights.setVisible(false);
89                 label.setVisible(false);
90                 for (int i = 0; i < values.length; i++)
91                         values[i].setVisible(false);
92         }
93
94         public void font_size_changed(int font_size) {
95                 label.setFont(AltosUILib.label_font);
96                 for (int i = 0; i < values.length; i++)
97                         values[i].setFont(AltosUILib.value_font);
98         }
99
100         public void units_changed(boolean imperial_units) {
101         }
102
103         public void set_label(String text) {
104                 label.setText(text);
105         }
106
107         public void remove(Container container) {
108                 if (lights != null)
109                         container.remove(lights);
110                 container.remove(label);
111                 for (int i = 0; i < values.length; i++)
112                         container.remove(values[i]);
113         }
114
115         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) {
116                 GridBagLayout           layout = (GridBagLayout)(container.getLayout());
117
118                 GridBagConstraints      c = new GridBagConstraints();
119                 c.weighty = 1;
120
121                 if (has_lights) {
122                         lights = new AltosLights();
123                         c.gridx = x; c.gridy = y;
124                         c.anchor = GridBagConstraints.CENTER;
125                         c.fill = GridBagConstraints.VERTICAL;
126                         c.weightx = 0;
127                         layout.setConstraints(lights, c);
128                         container.add(lights);
129                 }
130
131                 label = new JLabel(text);
132                 label.setFont(AltosUILib.label_font);
133                 label.setHorizontalAlignment(SwingConstants.LEFT);
134                 c.gridx = x + 1; c.gridy = y;
135                 c.gridwidth = label_width;
136                 c.insets = new Insets(AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad);
137                 c.anchor = GridBagConstraints.WEST;
138                 c.fill = GridBagConstraints.VERTICAL;
139                 c.weightx = 0;
140                 layout.setConstraints(label, c);
141                 container.add(label);
142
143                 values = new JTextField[number_values];
144                 for (int i = 0; i < values.length; i++) {
145                         values[i] = new JTextField(AltosUILib.text_width);
146                         values[i].setEditable(false);
147                         values[i].setFont(AltosUILib.value_font);
148                         values[i].setHorizontalAlignment(SwingConstants.RIGHT);
149                         c.gridx = 1 + label_width + x + i * value_space; c.gridy = y;
150                         c.anchor = GridBagConstraints.WEST;
151                         c.fill = GridBagConstraints.BOTH;
152                         c.weightx = 1;
153                         c.gridwidth = value_width;
154                         layout.setConstraints(values[i], c);
155                         container.add(values[i]);
156                 }
157         }
158
159         public AltosUIIndicator (Container container, int x, int y, int label_width, String text, int number_values, boolean has_lights, int value_width) {
160                 this(container, x, y, label_width, text, number_values, has_lights, value_width, 1);
161         }
162
163         public AltosUIIndicator (Container container, int x, int y, String text, int number_values, boolean has_lights, int value_width) {
164                 this(container, x, y, 1, text, number_values, has_lights, value_width);
165         }
166
167         public AltosUIIndicator (Container container, int y, String text, int number_values, boolean has_lights, int value_width) {
168                 this(container, 0, y, text, number_values, has_lights, value_width);
169         }
170
171         public AltosUIIndicator (Container container, int y, String text, int number_values, boolean has_lights) {
172                 this(container, 0, y, text, number_values, has_lights, 1);
173         }
174
175         public AltosUIIndicator (Container container, int y, String text, int number_values) {
176                 this(container, 0, y, text, number_values, false, 1);
177         }
178
179         public AltosUIIndicator (Container container, int y, String text) {
180                 this(container, 0, y, text, 1, false, 1);
181         }
182 }