altosuilib: Build some common classes for displaying values in flight window
authorKeith Packard <keithp@keithp.com>
Fri, 13 Jun 2014 22:20:20 +0000 (15:20 -0700)
committerKeith Packard <keithp@keithp.com>
Fri, 13 Jun 2014 22:20:20 +0000 (15:20 -0700)
Right now, all of the flight displays have piles of custom code for
displaying values. These new widgets should be able to replace most of
that.

Signed-off-by: Keith Packard <keithp@keithp.com>
altosuilib/AltosUIIndicator.java [new file with mode: 0644]
altosuilib/AltosUIMap.java
altosuilib/AltosUIUnitsIndicator.java [new file with mode: 0644]
altosuilib/AltosUIVoltageIndicator.java [new file with mode: 0644]
altosuilib/Makefile.am

diff --git a/altosuilib/AltosUIIndicator.java b/altosuilib/AltosUIIndicator.java
new file mode 100644 (file)
index 0000000..59fe231
--- /dev/null
@@ -0,0 +1,159 @@
+/*
+ * 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_2;
+
+import java.awt.*;
+import javax.swing.*;
+import org.altusmetrum.altoslib_4.*;
+
+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 AltosUIIndicator (Container container, int y, String text, int number_values, boolean has_lights, int value_width) {
+               GridBagLayout           layout = (GridBagLayout)(container.getLayout());
+
+               GridBagConstraints      c = new GridBagConstraints();
+               c.weighty = 1;
+
+               if (has_lights) {
+                       lights = new AltosLights();
+                       c.gridx = 0; 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 = 1; c.gridy = y;
+               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 = 2 + i; 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 y, String text) {
+               this(container, y, text, 1, false, 1);
+       }
+
+       public AltosUIIndicator (Container container, int y, String text, int number_values) {
+               this(container, y, text, number_values, false, 1);
+       }
+
+       public AltosUIIndicator (Container container, int y, String text, int number_values, boolean has_lights) {
+               this(container, y, text, number_values, has_lights, 1);
+       }
+}
index 910874692044af2b13584e744598a1259233c340..aaa68f238c893549641585858a12dc291bff6ce0 100644 (file)
@@ -139,6 +139,10 @@ public class AltosUIMap extends JComponent implements AltosFlightDisplay, AltosU
        public static void prefetch_maps(double lat, double lon) {
        }
 
        public static void prefetch_maps(double lat, double lon) {
        }
 
+       public String getName() {
+               return "Map";
+       }
+
        public AltosUIMap() {
 
                view = new AltosUIMapView();
        public AltosUIMap() {
 
                view = new AltosUIMapView();
diff --git a/altosuilib/AltosUIUnitsIndicator.java b/altosuilib/AltosUIUnitsIndicator.java
new file mode 100644 (file)
index 0000000..433cc0c
--- /dev/null
@@ -0,0 +1,79 @@
+/*
+ * 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_2;
+
+import java.awt.*;
+import javax.swing.*;
+import org.altusmetrum.altoslib_4.*;
+
+public abstract class AltosUIUnitsIndicator extends AltosUIIndicator {
+
+       AltosUnits      units;
+
+       abstract public double value(AltosState state, int i);
+       public boolean good(double value) { return false; }
+
+       public double[] last_values;
+
+       public void show(double... v) {
+               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];
+
+               for (int i = 0; i < values.length; i++) {
+                       if (state != null)
+                               v[i] = value(state, i);
+                       else
+                               v[i] = AltosLib.MISSING;
+               }
+               show(v);
+
+       }
+
+       public AltosUIUnitsIndicator (Container container, int y, AltosUnits units, String name, int number_values, boolean has_lights, int width) {
+               super(container, y, 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;
+       }
+}
diff --git a/altosuilib/AltosUIVoltageIndicator.java b/altosuilib/AltosUIVoltageIndicator.java
new file mode 100644 (file)
index 0000000..3683566
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ * 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_2;
+
+import java.awt.*;
+import javax.swing.*;
+import org.altusmetrum.altoslib_4.*;
+
+public abstract class AltosUIVoltageIndicator extends AltosUIUnitsIndicator {
+
+       abstract public double voltage(AltosState state);
+       abstract public double good();
+
+       public double value(AltosState state, int i) {
+               return voltage(state);
+       }
+
+       public boolean good(double value) {
+               return value >= good();
+       }
+
+       double last_voltage = -1;
+
+       public AltosUIVoltageIndicator (Container container, int y, String name, int width) {
+               super(container, y, AltosConvert.voltage, name, 1, true, width);
+       }
+}
index 466cfd999e1449054af5407d599bf82833d735bd..3904af3c5c6106c8989acea541b0521675c39328 100644 (file)
@@ -76,7 +76,10 @@ altosuilib_JAVA = \
        AltosUIMapPreload.java \
        AltosUIMapStore.java \
        AltosUIMapStoreListener.java \
        AltosUIMapPreload.java \
        AltosUIMapStore.java \
        AltosUIMapStoreListener.java \
-       AltosUILatLon.java
+       AltosUILatLon.java \
+       AltosUIIndicator.java \
+       AltosUIUnitsIndicator.java \
+       AltosUIVoltageIndicator.java
 
 JAR=altosuilib_$(ALTOSUILIB_VERSION).jar
 
 
 JAR=altosuilib_$(ALTOSUILIB_VERSION).jar