altosuilib: Add more options to AltosUIIndicator to suit AltosUI
authorKeith Packard <keithp@keithp.com>
Sat, 14 Jun 2014 21:39:26 +0000 (14:39 -0700)
committerKeith Packard <keithp@keithp.com>
Sat, 14 Jun 2014 21:39:26 +0000 (14:39 -0700)
This makes AltosUIIndicator capable of displaying most stuff in AltosUI

Signed-off-by: Keith Packard <keithp@keithp.com>
altosuilib/AltosUIIndicator.java
altosuilib/AltosUIUnitsIndicator.java
altosuilib/AltosUIVoltageIndicator.java

index 59fe231b5f4be56c5ec0e097b8448b8e36d2ef2a..b1626cba8ea29517098529554a297f745c96e8a5 100644 (file)
@@ -60,6 +60,7 @@ public abstract class AltosUIIndicator implements AltosFontListener, AltosUnitsL
 
        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]);
@@ -102,7 +103,15 @@ public abstract class AltosUIIndicator implements AltosFontListener, AltosUnitsL
                label.setText(text);
        }
 
-       public AltosUIIndicator (Container container, int y, String text, int number_values, boolean has_lights, int value_width) {
+       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();
@@ -110,7 +119,7 @@ public abstract class AltosUIIndicator implements AltosFontListener, AltosUnitsL
 
                if (has_lights) {
                        lights = new AltosLights();
-                       c.gridx = 0; c.gridy = y;
+                       c.gridx = x; c.gridy = y;
                        c.anchor = GridBagConstraints.CENTER;
                        c.fill = GridBagConstraints.VERTICAL;
                        c.weightx = 0;
@@ -121,7 +130,8 @@ public abstract class AltosUIIndicator implements AltosFontListener, AltosUnitsL
                label = new JLabel(text);
                label.setFont(AltosUILib.label_font);
                label.setHorizontalAlignment(SwingConstants.LEFT);
-               c.gridx = 1; c.gridy = y;
+               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;
@@ -135,7 +145,7 @@ public abstract class AltosUIIndicator implements AltosFontListener, AltosUnitsL
                        values[i].setEditable(false);
                        values[i].setFont(AltosUILib.value_font);
                        values[i].setHorizontalAlignment(SwingConstants.RIGHT);
-                       c.gridx = 2 + i; c.gridy = y;
+                       c.gridx = 1 + label_width + x + i * value_space; c.gridy = y;
                        c.anchor = GridBagConstraints.WEST;
                        c.fill = GridBagConstraints.BOTH;
                        c.weightx = 1;
@@ -145,15 +155,27 @@ public abstract class AltosUIIndicator implements AltosFontListener, AltosUnitsL
                }
        }
 
-       public AltosUIIndicator (Container container, int y, String text) {
-               this(container, y, text, 1, false, 1);
+       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 y, String text, int number_values) {
-               this(container, y, text, number_values, false, 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, y, text, number_values, has_lights, 1);
+               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);
        }
 }
index 433cc0c25f603c447cf063dca199e97b526090f3..2285b6fc046af8f5b8ac3fb6cf8f5b1b712738a0 100644 (file)
@@ -26,11 +26,24 @@ 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 good() { return 0; }
+       public boolean good(double value) { return value != AltosLib.MISSING && value >= good(); }
+       public boolean hide(double value) { return false; }
+
+       public boolean hide(AltosState state, int i) {
+               if (state == null)
+                       return hide(AltosLib.MISSING);
+               return hide(value(state, i));
+       }
+
+       public double value (AltosState state, AltosListenerState listener_state, int i) {
+               return value(state, i);
+       }
 
        public double[] last_values;
 
        public void show(double... v) {
+               show();
                for (int i = 0; i < values.length; i++) {
                        if (v[i] != last_values[i]) {
                                String  value_text;
@@ -56,24 +69,54 @@ public abstract class AltosUIUnitsIndicator extends AltosUIIndicator {
        }
 
        public void show (AltosState state, AltosListenerState listener_state) {
-
                double[] v = new double[values.length];
+               boolean hide = false;
 
                for (int i = 0; i < values.length; i++) {
                        if (state != null)
-                               v[i] = value(state, i);
+                               v[i] = value(state, listener_state, i);
                        else
                                v[i] = AltosLib.MISSING;
+                       if (hide(state, i))
+                               hide = true;
                }
-               show(v);
 
+               if (hide)
+                       hide();
+               else
+                       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);
+       public void reset() {
+               for (int i = 0; i < last_values.length; i++)
+                       last_values[i] = AltosLib.MISSING - 1;
+       }
+
+       public AltosUIUnitsIndicator (Container container, int x, int y, int label_width, AltosUnits units, String name, int number_values, boolean has_lights, int width) {
+               super(container, x, y, label_width, 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;
        }
+
+       public AltosUIUnitsIndicator (Container container, int x, int y, AltosUnits units, String name, int number_values, boolean has_lights, int width) {
+               this(container, x, y, 1, units, name, number_values, has_lights, width);
+       }
+
+       public AltosUIUnitsIndicator (Container container, int y, AltosUnits units, String name, int number_values, boolean has_lights, int width) {
+               this(container, 0, y, units, name, number_values, has_lights, width);
+       }
+
+       public AltosUIUnitsIndicator (Container container, int y, AltosUnits units, String name, int width) {
+               this(container, 0, y, units, name, 1, false, width);
+       }
+
+       public AltosUIUnitsIndicator (Container container, int y, AltosUnits units, String name) {
+               this(container, 0, y, units, name, 1, false, 1); 
+       }
+
+       public AltosUIUnitsIndicator (Container container, int x,int y, AltosUnits units, String name) {
+               this(container, x, y, units, name, 1, false, 1); 
+       }
 }
index 3683566394289ce3993a2f9a474e5750df6e99c7..3ff17213c8cdb975118634110427eaa27a97174c 100644 (file)
@@ -30,13 +30,13 @@ public abstract class AltosUIVoltageIndicator extends AltosUIUnitsIndicator {
                return voltage(state);
        }
 
-       public boolean good(double value) {
-               return value >= good();
-       }
-
        double last_voltage = -1;
 
+       public AltosUIVoltageIndicator (Container container, int x, int y, String name, int width) {
+               super(container, x, y, AltosConvert.voltage, name, 1, true, width);
+       }
+
        public AltosUIVoltageIndicator (Container container, int y, String name, int width) {
-               super(container, y, AltosConvert.voltage, name, 1, true, width);
+               this(container, 0, y, name, width);
        }
 }