From db2443fdbf65b65703217174303027c439124a83 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Wed, 11 Jun 2014 18:46:47 -0700 Subject: altosuilib: Rewrite map GUI bits Use a single large Canvas and draw images on top by hand. Signed-off-by: Keith Packard --- altosuilib/AltosUIMap.java | 246 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 246 insertions(+) create mode 100644 altosuilib/AltosUIMap.java (limited to 'altosuilib/AltosUIMap.java') diff --git a/altosuilib/AltosUIMap.java b/altosuilib/AltosUIMap.java new file mode 100644 index 00000000..fa974d36 --- /dev/null +++ b/altosuilib/AltosUIMap.java @@ -0,0 +1,246 @@ +/* + * Copyright © 2010 Anthony Towns + * + * 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 java.awt.event.*; +import javax.swing.*; +import java.io.*; +import java.lang.Math; +import java.awt.geom.*; +import java.util.*; +import java.util.concurrent.*; +import org.altusmetrum.altoslib_4.*; + +public class AltosUIMap extends JComponent implements AltosFlightDisplay, AltosUIMapZoomListener { + + static final int px_size = 512; + + static final int maptype_hybrid = 0; + static final int maptype_roadmap = 1; + static final int maptype_satellite = 2; + static final int maptype_terrain = 3; + static final int maptype_default = maptype_hybrid; + + static final String[] maptype_names = { + "hybrid", + "roadmap", + "satellite", + "terrain" + }; + + public static final String[] maptype_labels = { + "Hybrid", + "Roadmap", + "Satellite", + "Terrain" + }; + + public static final Color stateColors[] = { + Color.WHITE, // startup + Color.WHITE, // idle + Color.WHITE, // pad + Color.RED, // boost + Color.PINK, // fast + Color.YELLOW, // coast + Color.CYAN, // drogue + Color.BLUE, // main + Color.BLACK, // landed + Color.BLACK, // invalid + Color.CYAN, // stateless + }; + + public void reset() { + // nothing + } + + public void font_size_changed(int font_size) { + view.set_font(); + } + + public void units_changed(boolean imperial_units) { + repaint(); + } + + JLabel zoom_label; + + private void set_zoom_label() { + zoom_label.setText(String.format("Zoom %d", view.zoom() - view.default_zoom)); + } + + public void zoom_changed(int zoom) { + set_zoom_label(); + } + + public void set_zoom(int zoom) { + view.set_zoom(zoom); + } + + public int get_zoom() { + return view.zoom(); + } + + public void set_maptype(int type) { + view.set_maptype(type); + maptype_combo.setSelectedIndex(type); + } + + public void show(AltosState state, AltosListenerState listener_state) { + view.show(state, listener_state); + } + + public void centre(double lat, double lon) { + view.centre(lat, lon); + } + + public void centre(AltosState state) { + if (!state.gps.locked && state.gps.nsat < 4) + return; + centre(state.gps.lat, state.gps.lon); + } + + public void add_mark(double lat, double lon, int state) { + view.add_mark(lat, lon, state); + } + + public void clear_marks() { + view.clear_marks(); + } + + AltosUIMapView view; + + private GridBagLayout layout = new GridBagLayout(); + + JComboBox maptype_combo; + + public void set_load_params(double lat, double lon, int radius, AltosUIMapTileListener listener) { + view.set_load_params(lat, lon, radius, listener); + } + + public boolean all_fetched() { + return view.all_fetched(); + } + + public static void prefetch_maps(double lat, double lon) { + } + + public AltosUIMap() { + + view = new AltosUIMapView(); + + view.setPreferredSize(new Dimension(500,500)); + view.setVisible(true); + view.setEnabled(true); + view.add_zoom_listener(this); + + GridBagLayout my_layout = new GridBagLayout(); + + setLayout(my_layout); + + GridBagConstraints c = new GridBagConstraints(); + c.anchor = GridBagConstraints.CENTER; + c.fill = GridBagConstraints.BOTH; + c.gridx = 0; + c.gridy = 0; + c.gridwidth = 1; + c.gridheight = 10; + c.weightx = 1; + c.weighty = 1; + add(view, c); + + int y = 0; + + zoom_label = new JLabel("", JLabel.CENTER); + set_zoom_label(); + + c = new GridBagConstraints(); + c.anchor = GridBagConstraints.CENTER; + c.fill = GridBagConstraints.HORIZONTAL; + c.gridx = 1; + c.gridy = y++; + c.weightx = 0; + c.weighty = 0; + add(zoom_label, c); + + JButton zoom_reset = new JButton("0"); + zoom_reset.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + set_zoom(view.default_zoom); + } + }); + + c = new GridBagConstraints(); + c.anchor = GridBagConstraints.CENTER; + c.fill = GridBagConstraints.HORIZONTAL; + c.gridx = 1; + c.gridy = y++; + c.weightx = 0; + c.weighty = 0; + add(zoom_reset, c); + + JButton zoom_in = new JButton("+"); + zoom_in.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + set_zoom(get_zoom() + 1); + } + }); + + c = new GridBagConstraints(); + c.anchor = GridBagConstraints.CENTER; + c.fill = GridBagConstraints.HORIZONTAL; + c.gridx = 1; + c.gridy = y++; + c.weightx = 0; + c.weighty = 0; + add(zoom_in, c); + + JButton zoom_out = new JButton("-"); + zoom_out.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + set_zoom(get_zoom() - 1); + } + }); + c = new GridBagConstraints(); + c.anchor = GridBagConstraints.CENTER; + c.fill = GridBagConstraints.HORIZONTAL; + c.gridx = 1; + c.gridy = y++; + c.weightx = 0; + c.weighty = 0; + add(zoom_out, c); + + maptype_combo = new JComboBox(maptype_labels); + + maptype_combo.setEditable(false); + maptype_combo.setMaximumRowCount(maptype_combo.getItemCount()); + maptype_combo.addItemListener(new ItemListener() { + public void itemStateChanged(ItemEvent e) { + view.set_maptype(maptype_combo.getSelectedIndex()); + } + }); + + c = new GridBagConstraints(); + c.anchor = GridBagConstraints.CENTER; + c.fill = GridBagConstraints.HORIZONTAL; + c.gridx = 1; + c.gridy = y++; + c.weightx = 0; + c.weighty = 0; + add(maptype_combo, c); + } +} -- cgit v1.2.3 From bfc0c65c9f9ec9547d71016fc897ba35bdb414f8 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Wed, 11 Jun 2014 20:36:49 -0700 Subject: altosuilib: Handle font and units changes in maps and stats table Add AltosFontListener and AltosUnitsListener bits as needed Signed-off-by: Keith Packard --- altosui/AltosGraphUI.java | 25 +++++++++++++++++++++++-- altosuilib/AltosFlightStatsTable.java | 33 ++++++++++++++++++++++++--------- altosuilib/AltosUIMap.java | 2 +- altosuilib/AltosUIMapView.java | 5 +++++ 4 files changed, 53 insertions(+), 12 deletions(-) (limited to 'altosuilib/AltosUIMap.java') diff --git a/altosui/AltosGraphUI.java b/altosui/AltosGraphUI.java index 0df92eae..07fe9317 100644 --- a/altosui/AltosGraphUI.java +++ b/altosui/AltosGraphUI.java @@ -21,6 +21,7 @@ import java.io.*; import java.util.ArrayList; import java.awt.*; +import java.awt.event.*; import javax.swing.*; import org.altusmetrum.altoslib_4.*; import org.altusmetrum.altosuilib_2.*; @@ -29,7 +30,7 @@ import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.ui.RefineryUtilities; -public class AltosGraphUI extends AltosUIFrame +public class AltosGraphUI extends AltosUIFrame implements AltosFontListener, AltosUnitsListener { JTabbedPane pane; AltosGraph graph; @@ -53,6 +54,15 @@ public class AltosGraphUI extends AltosUIFrame } } + public void font_size_changed(int font_size) { + map.font_size_changed(font_size); + statsTable.font_size_changed(font_size); + } + + public void units_changed(boolean imperial_units) { + map.units_changed(imperial_units); + } + AltosGraphUI(AltosStateIterable states, File file) throws InterruptedException, IOException { super(file.getName()); state = null; @@ -79,9 +89,20 @@ public class AltosGraphUI extends AltosUIFrame setContentPane (pane); + AltosUIPreferences.register_font_listener(this); + AltosPreferences.register_units_listener(this); + + addWindowListener(new WindowAdapter() { + @Override + public void windowClosing(WindowEvent e) { + setVisible(false); + dispose(); + AltosUIPreferences.unregister_font_listener(AltosGraphUI.this); + AltosPreferences.unregister_units_listener(AltosGraphUI.this); + } + }); pack(); - setDefaultCloseOperation(DISPOSE_ON_CLOSE); setVisible(true); if (state != null && has_gps) map.centre(state); diff --git a/altosuilib/AltosFlightStatsTable.java b/altosuilib/AltosFlightStatsTable.java index ec106cf1..b32e92a0 100644 --- a/altosuilib/AltosFlightStatsTable.java +++ b/altosuilib/AltosFlightStatsTable.java @@ -19,14 +19,23 @@ package org.altusmetrum.altosuilib_2; import java.awt.*; import javax.swing.*; +import java.util.*; import org.altusmetrum.altoslib_4.*; -public class AltosFlightStatsTable extends JComponent { +public class AltosFlightStatsTable extends JComponent implements AltosFontListener { GridBagLayout layout; - class FlightStat { + LinkedList flight_stats = new LinkedList(); + + class FlightStat implements AltosFontListener { JLabel label; - JTextField value; + JTextField[] value; + + public void font_size_changed(int font_size) { + label.setFont(AltosUILib.label_font); + for (int i = 0; i < value.length; i++) + value[i].setFont(AltosUILib.value_font); + } public FlightStat(GridBagLayout layout, int y, String label_text, String ... values) { GridBagConstraints c = new GridBagConstraints(); @@ -43,21 +52,28 @@ public class AltosFlightStatsTable extends JComponent { layout.setConstraints(label, c); add(label); + value = new JTextField[values.length]; for (int j = 0; j < values.length; j++) { - value = new JTextField(values[j]); - value.setFont(AltosUILib.value_font); - value.setHorizontalAlignment(SwingConstants.RIGHT); + value[j] = new JTextField(values[j]); + value[j].setFont(AltosUILib.value_font); + value[j].setHorizontalAlignment(SwingConstants.RIGHT); c.gridx = j+1; c.gridy = y; c.anchor = GridBagConstraints.EAST; c.fill = GridBagConstraints.BOTH; c.weightx = 1; - layout.setConstraints(value, c); - add(value); + layout.setConstraints(value[j], c); + add(value[j]); } + flight_stats.add(this); } } + public void font_size_changed(int font_size) { + for (FlightStat f : flight_stats) + f.font_size_changed(font_size); + } + static String pos(double p, String pos, String neg) { String h = pos; if (p < 0) { @@ -147,5 +163,4 @@ public class AltosFlightStatsTable extends JComponent { pos(stats.lon,"E","W")); } } - } diff --git a/altosuilib/AltosUIMap.java b/altosuilib/AltosUIMap.java index fa974d36..91087469 100644 --- a/altosuilib/AltosUIMap.java +++ b/altosuilib/AltosUIMap.java @@ -74,7 +74,7 @@ public class AltosUIMap extends JComponent implements AltosFlightDisplay, AltosU } public void units_changed(boolean imperial_units) { - repaint(); + view.set_units(); } JLabel zoom_label; diff --git a/altosuilib/AltosUIMapView.java b/altosuilib/AltosUIMapView.java index c558118b..edae3e1e 100644 --- a/altosuilib/AltosUIMapView.java +++ b/altosuilib/AltosUIMapView.java @@ -68,6 +68,11 @@ public class AltosUIMapView extends Canvas implements MouseMotionListener, Mouse line.set_font(AltosUILib.value_font); for (AltosUIMapTile tile : tiles.values()) tile.set_font(AltosUILib.value_font); + repaint(); + } + + public void set_units() { + repaint(); } private boolean is_drag_event(MouseEvent e) { -- cgit v1.2.3 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 ++++++++++++++++++++++++++++++++ altosuilib/AltosUIMap.java | 4 + altosuilib/AltosUIUnitsIndicator.java | 79 ++++++++++++++++ altosuilib/AltosUIVoltageIndicator.java | 42 +++++++++ altosuilib/Makefile.am | 5 +- 5 files changed, 288 insertions(+), 1 deletion(-) create mode 100644 altosuilib/AltosUIIndicator.java create mode 100644 altosuilib/AltosUIUnitsIndicator.java create mode 100644 altosuilib/AltosUIVoltageIndicator.java (limited to 'altosuilib/AltosUIMap.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); + } +} diff --git a/altosuilib/AltosUIMap.java b/altosuilib/AltosUIMap.java index 91087469..aaa68f23 100644 --- a/altosuilib/AltosUIMap.java +++ b/altosuilib/AltosUIMap.java @@ -139,6 +139,10 @@ public class AltosUIMap extends JComponent implements AltosFlightDisplay, AltosU public static void prefetch_maps(double lat, double lon) { } + public String getName() { + return "Map"; + } + public AltosUIMap() { view = new AltosUIMapView(); diff --git a/altosuilib/AltosUIUnitsIndicator.java b/altosuilib/AltosUIUnitsIndicator.java new file mode 100644 index 00000000..433cc0c2 --- /dev/null +++ b/altosuilib/AltosUIUnitsIndicator.java @@ -0,0 +1,79 @@ +/* + * 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 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 index 00000000..36835663 --- /dev/null +++ b/altosuilib/AltosUIVoltageIndicator.java @@ -0,0 +1,42 @@ +/* + * 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 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); + } +} diff --git a/altosuilib/Makefile.am b/altosuilib/Makefile.am index 466cfd99..3904af3c 100644 --- a/altosuilib/Makefile.am +++ b/altosuilib/Makefile.am @@ -76,7 +76,10 @@ altosuilib_JAVA = \ AltosUIMapPreload.java \ AltosUIMapStore.java \ AltosUIMapStoreListener.java \ - AltosUILatLon.java + AltosUILatLon.java \ + AltosUIIndicator.java \ + AltosUIUnitsIndicator.java \ + AltosUIVoltageIndicator.java JAR=altosuilib_$(ALTOSUILIB_VERSION).jar -- cgit v1.2.3