X-Git-Url: https://git.gag.com/?p=fw%2Faltos;a=blobdiff_plain;f=altosui%2FAltosAscent.java;fp=altosui%2FAltosAscent.java;h=3bc804068fef165716cb60686fd70dc5df0deebf;hp=c3225709c21f651fd10423ecfbcef902698c9009;hb=c11b2f5caa3fbe2bc977e716ec1c3ccee9e75884;hpb=f5887a3e7cf993e23dbb1e0f6b9ebece78c34413 diff --git a/altosui/AltosAscent.java b/altosui/AltosAscent.java index c3225709..3bc80406 100644 --- a/altosui/AltosAscent.java +++ b/altosui/AltosAscent.java @@ -17,451 +17,151 @@ package altosui; +import java.util.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import org.altusmetrum.altoslib_4.*; import org.altusmetrum.altosuilib_2.*; -public class AltosAscent extends JComponent implements AltosFlightDisplay, HierarchyListener { - GridBagLayout layout; - JLabel cur, max; +public class AltosAscent extends AltosUIFlightTab { + JLabel cur, max; - private AltosState last_state; - private AltosListenerState last_listener_state; + class Height extends AltosUIUnitsIndicator { - public class AscentStatus implements AltosFontListener, AltosUnitsListener { - JLabel label; - JTextField value; - AltosLights lights; - double v; - AltosUnits units; - - void show() { - value.setVisible(true); - lights.setVisible(true); - label.setVisible(true); - } - - void hide() { - value.setVisible(false); - lights.setVisible(false); - label.setVisible(false); - } - - void show(AltosState state, AltosListenerState listener_state) {} - - void show(String s) { - show(); - value.setText(s); - } - - void show(double v) { - this.v = v; - show(units.show(8, v)); - } - - void show(String format, double v) { - show(String.format(format, v)); - } - - void reset() { - value.setText(""); - lights.set(false); - } - - public void font_size_changed(int font_size) { - label.setFont(Altos.label_font); - value.setFont(Altos.value_font); - } - - public void units_changed(boolean imperial_units) { - if (units != null) - show(v); + public double value(AltosState state, int i) { + if (i == 0) + return state.height(); + else + return state.max_height(); } - public AscentStatus (GridBagLayout layout, int y, AltosUnits units, String text) { - this.units = units; - GridBagConstraints c = new GridBagConstraints(); - c.weighty = 1; - - lights = new AltosLights(); - c.gridx = 0; c.gridy = y; - c.anchor = GridBagConstraints.CENTER; - c.fill = GridBagConstraints.VERTICAL; - c.weightx = 0; - layout.setConstraints(lights, c); - add(lights); - - label = new JLabel(text); - label.setFont(Altos.label_font); - label.setHorizontalAlignment(SwingConstants.LEFT); - c.gridx = 1; c.gridy = y; - c.insets = new Insets(Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad); - c.anchor = GridBagConstraints.WEST; - c.fill = GridBagConstraints.VERTICAL; - c.weightx = 0; - layout.setConstraints(label, c); - add(label); - - value = new JTextField(Altos.text_width); - value.setEditable(false); - value.setFont(Altos.value_font); - value.setHorizontalAlignment(SwingConstants.RIGHT); - c.gridx = 2; c.gridy = y; - c.gridwidth = 2; - c.anchor = GridBagConstraints.WEST; - c.fill = GridBagConstraints.BOTH; - c.weightx = 1; - layout.setConstraints(value, c); - add(value); - + public Height(Container container, int y) { + super(container, y, AltosConvert.height, "Height", 2, false, 1); } } - public abstract class AscentValue implements AltosFontListener, AltosUnitsListener { - JLabel label; - JTextField value; - double v; - AltosUnits units; - - abstract void show(AltosState state, AltosListenerState listener_state); - - void reset() { - value.setText(""); - } - - void show() { - label.setVisible(true); - value.setVisible(true); - } - - void show(String s) { - show(); - value.setText(s); - } - - void show(double v) { - this.v = v; - show(units.show(8, v)); - } - - void show(String format, double v) { - show(String.format(format, v)); - } - - void hide() { - label.setVisible(false); - value.setVisible(false); + class Speed extends AltosUIUnitsIndicator { + public double value(AltosState state, int i) { + if (i == 0) + return state.speed(); + else + return state.max_speed(); } - public void font_size_changed(int font_size) { - label.setFont(Altos.label_font); - value.setFont(Altos.value_font); + public Speed(Container container, int y) { + super(container, y, AltosConvert.speed, "Speed", 2, false, 1); } + } - public void units_changed(boolean imperial_units) { - if (units != null) - show(v); - } + class Accel extends AltosUIUnitsIndicator { + public boolean hide(double v) { return v == AltosLib.MISSING; } - public AscentValue (GridBagLayout layout, int y, AltosUnits units, String text) { - GridBagConstraints c = new GridBagConstraints(); - c.weighty = 1; - - label = new JLabel(text); - label.setFont(Altos.label_font); - label.setHorizontalAlignment(SwingConstants.LEFT); - c.gridx = 1; c.gridy = y; - c.insets = new Insets(Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad); - c.anchor = GridBagConstraints.WEST; - c.fill = GridBagConstraints.VERTICAL; - c.weightx = 0; - layout.setConstraints(label, c); - add(label); - - value = new JTextField(Altos.text_width); - value.setEditable(false); - value.setFont(Altos.value_font); - value.setHorizontalAlignment(SwingConstants.RIGHT); - c.gridx = 2; c.gridy = y; - c.anchor = GridBagConstraints.WEST; - c.fill = GridBagConstraints.BOTH; - c.gridwidth = 2; - c.weightx = 1; - layout.setConstraints(value, c); - add(value); + public double value(AltosState state, int i) { + if (i == 0) + return state.acceleration(); + else + return state.max_acceleration(); } - public AscentValue (GridBagLayout layout, int y, String text) { - this(layout, y, null, text); + public Accel(Container container, int y) { + super(container, y, AltosConvert.accel, "Acceleration", 2, false, 1); } } - public class AscentValueHold implements AltosFontListener, AltosUnitsListener { - JLabel label; - JTextField value; - JTextField max_value; - double max; - AltosUnits units; - double v; + class Orient extends AltosUIUnitsIndicator { - void show(AltosState state, AltosListenerState listener_state) {} - - void reset() { - value.setText(""); - max_value.setText(""); - max = AltosLib.MISSING; - } - - public void font_size_changed(int font_size) { - label.setFont(Altos.label_font); - value.setFont(Altos.value_font); - max_value.setFont(Altos.value_font); - } + public boolean hide(double v) { return v == AltosLib.MISSING; } - public void units_changed(boolean imperial_units) { - show(v, max); - } - - void show(double v, double max) { - this.v = v; - this.max = max; - if (v == AltosLib.MISSING) { - value.setText("Missing"); - } else { - value.setText(units.show(8, v)); - } - if (max == AltosLib.MISSING) - max_value.setText("Missing"); + public double value(AltosState state, int i) { + if (i == 0) + return state.orient(); else - max_value.setText(units.show(8, max)); + return state.max_orient(); } - void hide() { - label.setVisible(false); - value.setVisible(false); - max_value.setVisible(false); + public Orient(Container container, int y) { + super(container, y, AltosConvert.orient, "Tilt Angle", 2, false, 1); } - public AscentValueHold (GridBagLayout layout, int y, AltosUnits units, String text) { - this.units = units; - GridBagConstraints c = new GridBagConstraints(); - c.weighty = 1; - - label = new JLabel(text); - label.setFont(Altos.label_font); - label.setHorizontalAlignment(SwingConstants.LEFT); - c.gridx = 1; c.gridy = y; - c.insets = new Insets(Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad); - c.anchor = GridBagConstraints.WEST; - c.fill = GridBagConstraints.VERTICAL; - c.weightx = 0; - layout.setConstraints(label, c); - add(label); - - value = new JTextField(Altos.text_width); - value.setEditable(false); - value.setFont(Altos.value_font); - value.setHorizontalAlignment(SwingConstants.RIGHT); - c.gridx = 2; c.gridy = y; - c.anchor = GridBagConstraints.EAST; - c.fill = GridBagConstraints.BOTH; - c.weightx = 1; - layout.setConstraints(value, c); - add(value); - - max_value = new JTextField(Altos.text_width); - max_value.setEditable(false); - max_value.setFont(Altos.value_font); - max_value.setHorizontalAlignment(SwingConstants.RIGHT); - c.gridx = 3; c.gridy = y; - c.anchor = GridBagConstraints.EAST; - c.fill = GridBagConstraints.BOTH; - c.weightx = 1; - layout.setConstraints(max_value, c); - add(max_value); - } } - class Height extends AscentValueHold { - void show (AltosState state, AltosListenerState listener_state) { - show(state.height(), state.max_height()); - } - public Height (GridBagLayout layout, int y) { - super (layout, y, AltosConvert.height, "Height"); + class Apogee extends AltosUIUnitsIndicator { + + public double value(AltosState state, int i) { + return state.apogee_voltage; } - } - Height height; + public boolean good(double v) { return v >= AltosLib.ao_igniter_good; } + public boolean hide(double v) { return v == AltosLib.MISSING; } - class Speed extends AscentValueHold { - void show (AltosState state, AltosListenerState listener_state) { - show(state.speed(), state.max_speed()); - } - public Speed (GridBagLayout layout, int y) { - super (layout, y, AltosConvert.speed, "Speed"); + public Apogee (Container container, int y) { + super(container, y, AltosConvert.voltage, "Apogee Igniter Voltage", 1, true, 2); } } - Speed speed; - - class Accel extends AscentValueHold { - void show (AltosState state, AltosListenerState listener_state) { - show(state.acceleration(), state.max_acceleration()); + class Main extends AltosUIUnitsIndicator { + public double value(AltosState state, int i) { + return state.main_voltage; } - public Accel (GridBagLayout layout, int y) { - super (layout, y, AltosConvert.accel, "Acceleration"); - } - } - Accel accel; + public boolean good(double v) { return v >= AltosLib.ao_igniter_good; } + public boolean hide(double v) { return v == AltosLib.MISSING; } - class Orient extends AscentValueHold { - void show (AltosState state, AltosListenerState listener_state) { - show(state.orient(), state.max_orient()); - } - public Orient (GridBagLayout layout, int y) { - super (layout, y, AltosConvert.orient, "Tilt Angle"); + public Main (Container container, int y) { + super(container, y, AltosConvert.voltage, "Main Igniter Voltage", 1, true, 2); } } - Orient orient; + class Lat extends AltosUIUnitsIndicator { - String pos(double p, String pos, String neg) { - String h = pos; - if (p < 0) { - h = neg; - p = -p; + public boolean hide(AltosState state, int i) { + return state.gps == null || !state.gps.connected; } - int deg = (int) Math.floor(p); - double min = (p - Math.floor(p)) * 60.0; - return String.format("%s %4d° %9.6f", h, deg, min); - } - class Apogee extends AscentStatus { - void show (AltosState state, AltosListenerState listener_state) { - show("%4.2f V", state.apogee_voltage); - lights.set(state.apogee_voltage >= AltosLib.ao_igniter_good); - } - public Apogee (GridBagLayout layout, int y) { - super(layout, y, null, "Apogee Igniter Voltage"); + public double value(AltosState state, int i) { + if (state.gps == null) + return AltosLib.MISSING; + if (!state.gps.connected) + return AltosLib.MISSING; + return state.gps.lat; } - } - - Apogee apogee; - class Main extends AscentStatus { - void show (AltosState state, AltosListenerState listener_state) { - show("%4.2f V", state.main_voltage); - lights.set(state.main_voltage >= AltosLib.ao_igniter_good); - } - public Main (GridBagLayout layout, int y) { - super(layout, y, null, "Main Igniter Voltage"); + Lat (Container container, int y) { + super (container, y, AltosConvert.latitude, "Latitude", 1, false, 2); } } - Main main; + class Lon extends AltosUIUnitsIndicator { - class Lat extends AscentValue { - void show (AltosState state, AltosListenerState listener_state) { - if (state.gps != null && state.gps.connected && state.gps.lat != AltosLib.MISSING) - show(pos(state.gps.lat,"N", "S")); - else - show("???"); + public boolean hide(AltosState state, int i) { + return state.gps == null || !state.gps.connected; } - public Lat (GridBagLayout layout, int y) { - super (layout, y, "Latitude"); - } - } - Lat lat; - - class Lon extends AscentValue { - void show (AltosState state, AltosListenerState listener_state) { - if (state.gps != null && state.gps.connected && state.gps.lon != AltosLib.MISSING) - show(pos(state.gps.lon,"E", "W")); - else - show("???"); - } - public Lon (GridBagLayout layout, int y) { - super (layout, y, "Longitude"); + public double value(AltosState state, int i) { + if (state.gps == null) + return AltosLib.MISSING; + if (!state.gps.connected) + return AltosLib.MISSING; + return state.gps.lon; } - } - Lon lon; - - public void reset() { - lat.reset(); - lon.reset(); - main.reset(); - apogee.reset(); - height.reset(); - speed.reset(); - accel.reset(); - orient.reset(); + Lon (Container container, int y) { + super (container, y, AltosConvert.longitude, "Longitude", 1, false, 2); + } } public void font_size_changed(int font_size) { - cur.setFont(Altos.label_font); - max.setFont(Altos.label_font); - lat.font_size_changed(font_size); - lon.font_size_changed(font_size); - main.font_size_changed(font_size); - apogee.font_size_changed(font_size); - height.font_size_changed(font_size); - speed.font_size_changed(font_size); - accel.font_size_changed(font_size); - orient.font_size_changed(font_size); - } - - public void units_changed(boolean imperial_units) { - lat.units_changed(imperial_units); - lon.units_changed(imperial_units); - main.units_changed(imperial_units); - apogee.units_changed(imperial_units); - height.units_changed(imperial_units); - speed.units_changed(imperial_units); - accel.units_changed(imperial_units); - orient.units_changed(imperial_units); - } - - public void show(AltosState state, AltosListenerState listener_state) { - if (!isShowing()) { - last_state = state; - last_listener_state = listener_state; - return; - } - - if (state.gps != null && state.gps.connected) { - lat.show(state, listener_state); - lon.show(state, listener_state); - } else { - lat.hide(); - lon.hide(); - } - height.show(state, listener_state); - if (state.main_voltage != AltosLib.MISSING) - main.show(state, listener_state); - else - main.hide(); - if (state.apogee_voltage != AltosLib.MISSING) - apogee.show(state, listener_state); - else - apogee.hide(); - speed.show(state, listener_state); - accel.show(state, listener_state); - if (state.orient() != AltosLib.MISSING) - orient.show(state, listener_state); - else - orient.hide(); + super.font_size_changed(font_size); + cur.setFont(AltosUILib.label_font); + max.setFont(AltosUILib.label_font); } public void labels(GridBagLayout layout, int y) { GridBagConstraints c; cur = new JLabel("Current"); - cur.setFont(Altos.label_font); + cur.setFont(AltosUILib.label_font); c = new GridBagConstraints(); c.gridx = 2; c.gridy = y; c.insets = new Insets(Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad); @@ -469,7 +169,7 @@ public class AltosAscent extends JComponent implements AltosFlightDisplay, Hiera add(cur); max = new JLabel("Maximum"); - max.setFont(Altos.label_font); + max.setFont(AltosUILib.label_font); c.gridx = 3; c.gridy = y; layout.setConstraints(max, c); add(max); @@ -479,38 +179,16 @@ public class AltosAscent extends JComponent implements AltosFlightDisplay, Hiera return "Ascent"; } - public void hierarchyChanged(HierarchyEvent e) { - if (last_state != null && isShowing()) { - AltosState state = last_state; - AltosListenerState listener_state = last_listener_state; - - last_state = null; - last_listener_state = null; - show(state, listener_state); - } - } - public AltosAscent() { - layout = new GridBagLayout(); - - setLayout(layout); - - /* Elements in ascent display: - * - * lat - * lon - * height - */ int y = 0; labels(layout, y++); - height = new Height(layout, y++); - speed = new Speed(layout, y++); - accel = new Accel(layout, y++); - orient = new Orient(layout, y++); - lat = new Lat(layout, y++); - lon = new Lon(layout, y++); - apogee = new Apogee(layout, y++); - main = new Main(layout, y++); - addHierarchyListener(this); + add(new Height(this, y++)); + add(new Speed(this, y++)); + add(new Accel(this, y++)); + add(new Orient(this, y++)); + add(new Lat(this, y++)); + add(new Lon(this, y++)); + add(new Apogee(this, y++)); + add(new Main(this, y++)); } }