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