59fe231b5f4be56c5ec0e097b8448b8e36d2ef2a
[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_2;
19
20 import java.awt.*;
21 import javax.swing.*;
22 import org.altusmetrum.altoslib_4.*;
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                 show();
64                 for (int i = 0; i < n; i++)
65                         values[i].setText(s[i]);
66         }
67
68         public void show(String format, double value) {
69                 show(String.format(format, value));
70         }
71
72         public void show(String format, int value) {
73                 show(String.format(format, value));
74         }
75
76         public void show(String format1, double value1, String format2, double value2) {
77                 show(String.format(format1, value1), String.format(format2, value2));
78         }
79
80         public void show(String format1, int value1, String format2, int value2) {
81                 show(String.format(format1, value1), String.format(format2, value2));
82         }
83
84         public void hide() {
85                 if (lights != null)
86                         lights.setVisible(false);
87                 label.setVisible(false);
88                 for (int i = 0; i < values.length; i++)
89                         values[i].setVisible(false);
90         }
91
92         public void font_size_changed(int font_size) {
93                 label.setFont(AltosUILib.label_font);
94                 for (int i = 0; i < values.length; i++)
95                         values[i].setFont(AltosUILib.value_font);
96         }
97
98         public void units_changed(boolean imperial_units) {
99         }
100
101         public void set_label(String text) {
102                 label.setText(text);
103         }
104
105         public AltosUIIndicator (Container container, int y, String text, int number_values, boolean has_lights, int value_width) {
106                 GridBagLayout           layout = (GridBagLayout)(container.getLayout());
107
108                 GridBagConstraints      c = new GridBagConstraints();
109                 c.weighty = 1;
110
111                 if (has_lights) {
112                         lights = new AltosLights();
113                         c.gridx = 0; c.gridy = y;
114                         c.anchor = GridBagConstraints.CENTER;
115                         c.fill = GridBagConstraints.VERTICAL;
116                         c.weightx = 0;
117                         layout.setConstraints(lights, c);
118                         container.add(lights);
119                 }
120
121                 label = new JLabel(text);
122                 label.setFont(AltosUILib.label_font);
123                 label.setHorizontalAlignment(SwingConstants.LEFT);
124                 c.gridx = 1; c.gridy = y;
125                 c.insets = new Insets(AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad);
126                 c.anchor = GridBagConstraints.WEST;
127                 c.fill = GridBagConstraints.VERTICAL;
128                 c.weightx = 0;
129                 layout.setConstraints(label, c);
130                 container.add(label);
131
132                 values = new JTextField[number_values];
133                 for (int i = 0; i < values.length; i++) {
134                         values[i] = new JTextField(AltosUILib.text_width);
135                         values[i].setEditable(false);
136                         values[i].setFont(AltosUILib.value_font);
137                         values[i].setHorizontalAlignment(SwingConstants.RIGHT);
138                         c.gridx = 2 + i; c.gridy = y;
139                         c.anchor = GridBagConstraints.WEST;
140                         c.fill = GridBagConstraints.BOTH;
141                         c.weightx = 1;
142                         c.gridwidth = value_width;
143                         layout.setConstraints(values[i], c);
144                         container.add(values[i]);
145                 }
146         }
147
148         public AltosUIIndicator (Container container, int y, String text) {
149                 this(container, y, text, 1, false, 1);
150         }
151
152         public AltosUIIndicator (Container container, int y, String text, int number_values) {
153                 this(container, y, text, number_values, false, 1);
154         }
155
156         public AltosUIIndicator (Container container, int y, String text, int number_values, boolean has_lights) {
157                 this(container, y, text, number_values, has_lights, 1);
158         }
159 }