From 8250777e6e869bcee9781691caa1f2a7cfb33b43 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Sat, 14 Jun 2014 14:39:26 -0700 Subject: [PATCH] altosuilib: Add more options to AltosUIIndicator to suit AltosUI This makes AltosUIIndicator capable of displaying most stuff in AltosUI Signed-off-by: Keith Packard --- altosuilib/AltosUIIndicator.java | 40 ++++++++++++++---- altosuilib/AltosUIUnitsIndicator.java | 55 ++++++++++++++++++++++--- altosuilib/AltosUIVoltageIndicator.java | 10 ++--- 3 files changed, 85 insertions(+), 20 deletions(-) diff --git a/altosuilib/AltosUIIndicator.java b/altosuilib/AltosUIIndicator.java index 59fe231b..b1626cba 100644 --- a/altosuilib/AltosUIIndicator.java +++ b/altosuilib/AltosUIIndicator.java @@ -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); } } diff --git a/altosuilib/AltosUIUnitsIndicator.java b/altosuilib/AltosUIUnitsIndicator.java index 433cc0c2..2285b6fc 100644 --- a/altosuilib/AltosUIUnitsIndicator.java +++ b/altosuilib/AltosUIUnitsIndicator.java @@ -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); + } } diff --git a/altosuilib/AltosUIVoltageIndicator.java b/altosuilib/AltosUIVoltageIndicator.java index 36835663..3ff17213 100644 --- a/altosuilib/AltosUIVoltageIndicator.java +++ b/altosuilib/AltosUIVoltageIndicator.java @@ -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); } } -- 2.30.2