From 7ed63b6c3d5878a59f52f4114b5b01942735805f Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Fri, 13 Jun 2014 15:20:20 -0700 Subject: altosuilib: Build some common classes for displaying values in flight window 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 --- altosuilib/AltosUIIndicator.java | 159 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 altosuilib/AltosUIIndicator.java (limited to 'altosuilib/AltosUIIndicator.java') diff --git a/altosuilib/AltosUIIndicator.java b/altosuilib/AltosUIIndicator.java new file mode 100644 index 00000000..59fe231b --- /dev/null +++ b/altosuilib/AltosUIIndicator.java @@ -0,0 +1,159 @@ +/* + * Copyright © 2014 Keith Packard + * + * 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); + } +} -- cgit v1.2.3 From 8250777e6e869bcee9781691caa1f2a7cfb33b43 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Sat, 14 Jun 2014 14:39:26 -0700 Subject: 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(-) (limited to 'altosuilib/AltosUIIndicator.java') 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); } } -- cgit v1.2.3