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/AltosUIMapView.java | 462 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 462 insertions(+) create mode 100644 altosuilib/AltosUIMapView.java (limited to 'altosuilib/AltosUIMapView.java') diff --git a/altosuilib/AltosUIMapView.java b/altosuilib/AltosUIMapView.java new file mode 100644 index 00000000..c558118b --- /dev/null +++ b/altosuilib/AltosUIMapView.java @@ -0,0 +1,462 @@ +/* + * 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 java.awt.event.*; +import java.awt.image.*; +import javax.swing.*; +import java.io.*; +import java.lang.*; +import java.awt.geom.*; +import java.util.*; +import java.util.concurrent.*; +import org.altusmetrum.altoslib_4.*; + +public class AltosUIMapView extends Canvas implements MouseMotionListener, MouseListener, MouseWheelListener, ComponentListener, AltosUIMapTileListener, AltosUIMapStoreListener { + + AltosUIMapPath path = new AltosUIMapPath(); + + AltosUIMapLine line = new AltosUIMapLine(); + + LinkedList marks = new LinkedList(); + + LinkedList zoom_listeners = new LinkedList(); + + boolean have_boost = false; + boolean have_landed = false; + + ConcurrentHashMap tiles = new ConcurrentHashMap(); + + static final int default_zoom = 15; + static final int min_zoom = 3; + static final int max_zoom = 21; + static final int px_size = 512; + + int load_radius; + AltosUILatLon load_centre = null; + AltosUIMapTileListener load_listener; + + int zoom = default_zoom; + int maptype = AltosUIMap.maptype_default; + + long user_input_time; + + /* Milliseconds to wait after user action before auto-scrolling + */ + static final long auto_scroll_delay = 20 * 1000; + + AltosUIMapTransform transform; + AltosUILatLon centre; + + public void set_font() { + line.set_font(AltosUILib.value_font); + for (AltosUIMapTile tile : tiles.values()) + tile.set_font(AltosUILib.value_font); + } + + private boolean is_drag_event(MouseEvent e) { + return e.getModifiers() == InputEvent.BUTTON1_MASK; + } + + Point drag_start; + + private void drag(MouseEvent e) { + if (drag_start == null) + return; + + int dx = e.getPoint().x - drag_start.x; + int dy = e.getPoint().y - drag_start.y; + + AltosUILatLon new_centre = transform.screen_lat_lon(new Point(getWidth() / 2 - dx, getHeight() / 2 - dy)); + centre (new_centre.lat, new_centre.lon); + drag_start = e.getPoint(); + } + + private void drag_start(MouseEvent e) { + drag_start = e.getPoint(); + } + + private void notice_user_input() { + user_input_time = System.currentTimeMillis(); + } + + private boolean recent_user_input() { + return (System.currentTimeMillis() - user_input_time) < auto_scroll_delay; + } + + /* MouseMotionListener methods */ + + public void mouseDragged(MouseEvent e) { + notice_user_input(); + if (is_drag_event(e)) + drag(e); + else { + line.dragged(e, transform); + repaint(); + } + } + + public void mouseMoved(MouseEvent e) { + } + + /* MouseListener methods */ + public void mouseClicked(MouseEvent e) { + } + + public void mouseEntered(MouseEvent e) { + } + + public void mouseExited(MouseEvent e) { + } + + public void mousePressed(MouseEvent e) { + notice_user_input(); + if (is_drag_event(e)) + drag_start(e); + else + line.pressed(e, transform); + } + + public void mouseReleased(MouseEvent e) { + } + + /* MouseWheelListener methods */ + + public void mouseWheelMoved(MouseWheelEvent e) { + int zoom_change = e.getWheelRotation(); + + notice_user_input(); + AltosUILatLon mouse_lat_lon = transform.screen_lat_lon(e.getPoint()); + set_zoom(zoom() - zoom_change); + + Point2D.Double new_mouse = transform.screen(mouse_lat_lon); + + int dx = getWidth()/2 - e.getPoint().x; + int dy = getHeight()/2 - e.getPoint().y; + + AltosUILatLon new_centre = transform.screen_lat_lon(new Point((int) new_mouse.x + dx, (int) new_mouse.y + dy)); + + centre(new_centre.lat, new_centre.lon); + } + + /* ComponentListener methods */ + + public void componentHidden(ComponentEvent e) { + } + + public void componentMoved(ComponentEvent e) { + } + + public void componentResized(ComponentEvent e) { + set_transform(); + } + + public void componentShown(ComponentEvent e) { + set_transform(); + } + + public void repaint(Rectangle r, int pad) { + repaint(r.x - pad, r.y - pad, r.width + pad*2, r.height + pad*2); + } + + public void repaint(AltosUIMapRectangle rect, int pad) { + repaint (transform.screen(rect), pad); + } + + private boolean far_from_centre(AltosUILatLon lat_lon) { + + if (centre == null || transform == null) + return true; + + Point2D.Double screen = transform.screen(lat_lon); + + int width = getWidth(); + int dx = Math.abs ((int) screen.x - width/2); + + if (dx > width / 4) + return true; + + int height = getHeight(); + int dy = Math.abs ((int) screen.y - height/2); + + if (dy > height / 4) + return true; + + return false; + } + + public void show(AltosState state, AltosListenerState listener_state) { + + /* If insufficient gps data, nothing to update + */ + AltosGPS gps = state.gps; + + if (gps == null) + return; + + if (!gps.locked && gps.nsat < 4) + return; + + AltosUIMapRectangle damage = path.add(gps.lat, gps.lon, state.state); + + switch (state.state) { + case AltosLib.ao_flight_boost: + if (!have_boost) { + add_mark(gps.lat, gps.lon, state.state); + have_boost = true; + } + break; + case AltosLib.ao_flight_landed: + if (!have_landed) { + add_mark(gps.lat, gps.lon, state.state); + have_landed = true; + } + break; + } + + if (damage != null) + repaint(damage, AltosUIMapPath.stroke_width); + maybe_centre(gps.lat, gps.lon); + } + + private void set_transform() { + Rectangle bounds = getBounds(); + + transform = new AltosUIMapTransform(bounds.width, bounds.height, zoom, centre); + repaint(); + } + + public boolean set_zoom(int zoom) { + if (min_zoom <= zoom && zoom <= max_zoom && zoom != this.zoom) { + this.zoom = zoom; + tiles.clear(); + set_transform(); + + for (AltosUIMapZoomListener listener : zoom_listeners) + listener.zoom_changed(this.zoom); + + return true; + } + return false; + } + + public void add_zoom_listener(AltosUIMapZoomListener listener) { + if (!zoom_listeners.contains(listener)) + zoom_listeners.add(listener); + } + + public void remove_zoom_listener(AltosUIMapZoomListener listener) { + zoom_listeners.remove(listener); + } + + public void set_load_params(double lat, double lon, int radius, AltosUIMapTileListener listener) { + load_centre = new AltosUILatLon(lat, lon); + load_radius = radius; + load_listener = listener; + centre(lat, lon); + make_tiles(); + for (AltosUIMapTile tile : tiles.values()) { + tile.add_store_listener(this); + if (tile.store_status() != AltosUIMapStore.loading) + listener.notify_tile(tile, tile.store_status()); + } + repaint(); + } + + public boolean all_fetched() { + for (AltosUIMapTile tile : tiles.values()) { + if (tile.store_status() == AltosUIMapStore.loading) + return false; + } + return true; + } + + public boolean set_maptype(int maptype) { + if (maptype != this.maptype) { + this.maptype = maptype; + tiles.clear(); + repaint(); + return true; + } + return false; + } + + public int get_maptype() { + return maptype; + } + + public int zoom() { + return zoom; + } + + public void centre(AltosUILatLon lat_lon) { + centre = lat_lon; + set_transform(); + } + + public void centre(double lat, double lon) { + centre(new AltosUILatLon(lat, lon)); + } + + public void maybe_centre(double lat, double lon) { + AltosUILatLon lat_lon = new AltosUILatLon(lat, lon); + if (centre == null || (!recent_user_input() && far_from_centre(lat_lon))) + centre(lat_lon); + } + + private VolatileImage create_back_buffer() { + return getGraphicsConfiguration().createCompatibleVolatileImage(getWidth(), getHeight()); + } + + private Point floor(Point2D.Double point) { + return new Point ((int) Math.floor(point.x / px_size) * px_size, + (int) Math.floor(point.y / px_size) * px_size); + } + + private Point ceil(Point2D.Double point) { + return new Point ((int) Math.ceil(point.x / px_size) * px_size, + (int) Math.ceil(point.y / px_size) * px_size); + } + + private void make_tiles() { + Point upper_left; + Point lower_right; + + if (load_centre != null) { + Point centre = floor(transform.point(load_centre)); + + upper_left = new Point(centre.x - load_radius * px_size, + centre.y - load_radius * px_size); + lower_right = new Point(centre.x + load_radius * px_size, + centre.y + load_radius * px_size); + } else { + upper_left = floor(transform.screen_point(new Point(0, 0))); + lower_right = floor(transform.screen_point(new Point(getWidth(), getHeight()))); + } + LinkedList to_remove = new LinkedList(); + + for (Point point : tiles.keySet()) { + if (point.x < upper_left.x || lower_right.x < point.x || + point.y < upper_left.y || lower_right.y < point.y) { + to_remove.add(point); + } + } + + for (Point point : to_remove) + tiles.remove(point); + + AltosUIMapCache.set_cache_size(((lower_right.y - upper_left.y) / px_size + 1) * ((lower_right.x - upper_left.x) / px_size + 1)); + for (int y = upper_left.y; y <= lower_right.y; y += px_size) { + for (int x = upper_left.x; x <= lower_right.x; x += px_size) { + Point point = new Point(x, y); + + if (!tiles.containsKey(point)) { + AltosUILatLon ul = transform.lat_lon(new Point2D.Double(x, y)); + AltosUILatLon center = transform.lat_lon(new Point2D.Double(x + px_size/2, y + px_size/2)); + AltosUIMapTile tile = new AltosUIMapTile(this, ul, center, zoom, maptype, + px_size, AltosUILib.value_font); + tiles.put(point, tile); + } + } + } + } + + /* AltosUIMapTileListener methods */ + public void notify_tile(AltosUIMapTile tile, int status) { + for (Point point : tiles.keySet()) { + if (tile == tiles.get(point)) { + Point screen = transform.screen(point); + repaint(screen.x, screen.y, px_size, px_size); + } + } + } + + /* AltosUIMapStoreListener methods */ + public void notify_store(AltosUIMapStore store, int status) { + if (load_listener != null) { + for (AltosUIMapTile tile : tiles.values()) + if (store.equals(tile.store)) + load_listener.notify_tile(tile, status); + } + } + + private void do_paint(Graphics g) { + Graphics2D g2d = (Graphics2D) g; + + make_tiles(); + + for (AltosUIMapTile tile : tiles.values()) + tile.paint(g2d, transform); + + synchronized(marks) { + for (AltosUIMapMark mark : marks) + mark.paint(g2d, transform); + } + + path.paint(g2d, transform); + + line.paint(g2d, transform); + } + + public void paint(Graphics g) { + + VolatileImage back_buffer = create_back_buffer(); + do { + GraphicsConfiguration gc = getGraphicsConfiguration(); + int code = back_buffer.validate(gc); + if (code == VolatileImage.IMAGE_INCOMPATIBLE) + back_buffer = create_back_buffer(); + + Graphics g_back = back_buffer.getGraphics(); + g_back.setClip(g.getClip()); + do_paint(g_back); + g_back.dispose(); + + g.drawImage(back_buffer, 0, 0, this); + } while (back_buffer.contentsLost()); + back_buffer.flush(); + } + + public void update(Graphics g) { + paint(g); + } + + public void add_mark(double lat, double lon, int state) { + synchronized(marks) { + marks.add(new AltosUIMapMark(lat, lon, state)); + } + repaint(); + } + + public void clear_marks() { + synchronized(marks) { + marks.clear(); + } + } + + public AltosUIMapView() { + centre(0, 0); + + addComponentListener(this); + addMouseMotionListener(this); + addMouseListener(this); + addMouseWheelListener(this); + set_font(); + } +} -- 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/AltosUIMapView.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 6f306b267f63d0f59fb77b1ce41c678042dd6802 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Wed, 11 Jun 2014 23:04:46 -0700 Subject: altosuilib: Repaint map when starting line draw Starting line draw will remove any existing line, so repaint to get rid of it Signed-off-by: Keith Packard --- altosuilib/AltosUIMapView.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'altosuilib/AltosUIMapView.java') diff --git a/altosuilib/AltosUIMapView.java b/altosuilib/AltosUIMapView.java index edae3e1e..70198682 100644 --- a/altosuilib/AltosUIMapView.java +++ b/altosuilib/AltosUIMapView.java @@ -134,8 +134,10 @@ public class AltosUIMapView extends Canvas implements MouseMotionListener, Mouse notice_user_input(); if (is_drag_event(e)) drag_start(e); - else + else { line.pressed(e, transform); + repaint(); + } } public void mouseReleased(MouseEvent e) { -- cgit v1.2.3 From 8044eb8e23366e91c741060939baff5137f841c7 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Thu, 12 Jun 2014 14:12:08 -0700 Subject: altosui/telegps: Reduce CPU time needed for flight displays Don't update displays which aren't shown; track hierarchy changes to trigger display from most recent state data. Don't update values which haven't changed; remember previous values and compare with new before updating widget contents. Signed-off-by: Keith Packard --- altosui/AltosAscent.java | 45 ++++++++++++---- altosui/AltosDescent.java | 35 ++++++++++++- altosui/AltosFlightStatus.java | 83 ++++++++++++++++++++++------- altosui/AltosIgnitor.java | 1 + altosui/AltosLanded.java | 30 ++++++++++- altosui/AltosPad.java | 116 +++++++++++++++++++++++++---------------- altosuilib/AltosInfoTable.java | 25 ++++++++- altosuilib/AltosUIMapView.java | 3 +- telegps/TeleGPSInfo.java | 46 ++++++++++++---- telegps/TeleGPSStatus.java | 46 ++++++++++++---- 10 files changed, 326 insertions(+), 104 deletions(-) (limited to 'altosuilib/AltosUIMapView.java') diff --git a/altosui/AltosAscent.java b/altosui/AltosAscent.java index fb05fe11..c3225709 100644 --- a/altosui/AltosAscent.java +++ b/altosui/AltosAscent.java @@ -18,14 +18,18 @@ package altosui; 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 { +public class AltosAscent extends JComponent implements AltosFlightDisplay, HierarchyListener { GridBagLayout layout; JLabel cur, max; + private AltosState last_state; + private AltosListenerState last_listener_state; + public class AscentStatus implements AltosFontListener, AltosUnitsListener { JLabel label; JTextField value; @@ -101,6 +105,7 @@ public class AltosAscent extends JComponent implements AltosFlightDisplay { 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; @@ -176,6 +181,7 @@ public class AltosAscent extends JComponent implements AltosFlightDisplay { 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; @@ -215,22 +221,21 @@ public class AltosAscent extends JComponent implements AltosFlightDisplay { } public void units_changed(boolean imperial_units) { - show(v); + show(v, max); } - void show(double v) { + 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 (v > max || max == AltosLib.MISSING) - max = v; } if (max == AltosLib.MISSING) max_value.setText("Missing"); else - max_value.setText(units.show(8, v)); + max_value.setText(units.show(8, max)); } void hide() { @@ -256,6 +261,7 @@ public class AltosAscent extends JComponent implements AltosFlightDisplay { 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; @@ -266,6 +272,7 @@ public class AltosAscent extends JComponent implements AltosFlightDisplay { 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; @@ -279,7 +286,7 @@ public class AltosAscent extends JComponent implements AltosFlightDisplay { class Height extends AscentValueHold { void show (AltosState state, AltosListenerState listener_state) { - show(state.height()); + show(state.height(), state.max_height()); } public Height (GridBagLayout layout, int y) { super (layout, y, AltosConvert.height, "Height"); @@ -290,7 +297,7 @@ public class AltosAscent extends JComponent implements AltosFlightDisplay { class Speed extends AscentValueHold { void show (AltosState state, AltosListenerState listener_state) { - show(state.speed()); + show(state.speed(), state.max_speed()); } public Speed (GridBagLayout layout, int y) { super (layout, y, AltosConvert.speed, "Speed"); @@ -301,7 +308,7 @@ public class AltosAscent extends JComponent implements AltosFlightDisplay { class Accel extends AscentValueHold { void show (AltosState state, AltosListenerState listener_state) { - show(state.acceleration()); + show(state.acceleration(), state.max_acceleration()); } public Accel (GridBagLayout layout, int y) { super (layout, y, AltosConvert.accel, "Acceleration"); @@ -312,7 +319,7 @@ public class AltosAscent extends JComponent implements AltosFlightDisplay { class Orient extends AscentValueHold { void show (AltosState state, AltosListenerState listener_state) { - show(state.orient()); + show(state.orient(), state.max_orient()); } public Orient (GridBagLayout layout, int y) { super (layout, y, AltosConvert.orient, "Tilt Angle"); @@ -420,6 +427,12 @@ public class AltosAscent extends JComponent implements AltosFlightDisplay { } 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); @@ -466,6 +479,17 @@ public class AltosAscent extends JComponent implements AltosFlightDisplay { 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(); @@ -487,5 +511,6 @@ public class AltosAscent extends JComponent implements AltosFlightDisplay { lon = new Lon(layout, y++); apogee = new Apogee(layout, y++); main = new Main(layout, y++); + addHierarchyListener(this); } } diff --git a/altosui/AltosDescent.java b/altosui/AltosDescent.java index b56a08bc..11bd6dbf 100644 --- a/altosui/AltosDescent.java +++ b/altosui/AltosDescent.java @@ -18,13 +18,17 @@ package altosui; import java.awt.*; +import java.awt.event.*; import javax.swing.*; import org.altusmetrum.altoslib_4.*; import org.altusmetrum.altosuilib_2.*; -public class AltosDescent extends JComponent implements AltosFlightDisplay { +public class AltosDescent extends JComponent implements AltosFlightDisplay, HierarchyListener { GridBagLayout layout; + private AltosState last_state; + private AltosListenerState last_listener_state; + public abstract class DescentStatus implements AltosFontListener, AltosUnitsListener { JLabel label; JTextField value; @@ -91,6 +95,7 @@ public class AltosDescent extends JComponent implements AltosFlightDisplay { add(label); value = new JTextField(Altos.text_width); + value.setEditable(false); value.setFont(Altos.value_font); value.setHorizontalAlignment(SwingConstants.RIGHT); c.gridx = 4; c.gridy = y; @@ -109,6 +114,7 @@ public class AltosDescent extends JComponent implements AltosFlightDisplay { JTextField value; AltosUnits units; double v; + String last_value = ""; void reset() { value.setText(""); @@ -128,7 +134,11 @@ public class AltosDescent extends JComponent implements AltosFlightDisplay { void show(String v) { show(); - value.setText(v); + + if (!last_value.equals(v)) { + value.setText(v); + last_value = v; + } } void show(double v) { @@ -172,6 +182,7 @@ public class AltosDescent extends JComponent implements AltosFlightDisplay { add(label, c); value = new JTextField(Altos.text_width); + value.setEditable(false); value.setFont(Altos.value_font); value.setHorizontalAlignment(SwingConstants.RIGHT); c.gridx = x + 2; c.gridy = y; @@ -247,6 +258,7 @@ public class AltosDescent extends JComponent implements AltosFlightDisplay { add(label); value1 = new JTextField(Altos.text_width); + value1.setEditable(false); value1.setFont(Altos.value_font); value1.setHorizontalAlignment(SwingConstants.RIGHT); c.gridx = x + 2; c.gridy = y; @@ -257,6 +269,7 @@ public class AltosDescent extends JComponent implements AltosFlightDisplay { add(value1); value2 = new JTextField(Altos.text_width); + value2.setEditable(false); value2.setFont(Altos.value_font); value2.setHorizontalAlignment(SwingConstants.RIGHT); c.gridx = x + 4; c.gridy = y; @@ -449,6 +462,12 @@ public class AltosDescent extends JComponent implements AltosFlightDisplay { } public void show(AltosState state, AltosListenerState listener_state) { + if (!isShowing()) { + last_state = state; + last_listener_state = listener_state; + return; + } + height.show(state, listener_state); speed.show(state, listener_state); if (state.gps != null && state.gps.connected) { @@ -480,6 +499,17 @@ public class AltosDescent extends JComponent implements AltosFlightDisplay { return "Descent"; } + 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 AltosDescent() { layout = new GridBagLayout(); @@ -497,5 +527,6 @@ public class AltosDescent extends JComponent implements AltosFlightDisplay { apogee = new Apogee(layout, 5); main = new Main(layout, 6); + addHierarchyListener(this); } } diff --git a/altosui/AltosFlightStatus.java b/altosui/AltosFlightStatus.java index 459b0495..b27deba9 100644 --- a/altosui/AltosFlightStatus.java +++ b/altosui/AltosFlightStatus.java @@ -61,6 +61,7 @@ public class AltosFlightStatus extends JComponent implements AltosFlightDisplay add(label); value = new JTextField(""); + value.setEditable(false); value.setFont(Altos.status_font); value.setHorizontalAlignment(SwingConstants.CENTER); c.gridx = x; c.gridy = 1; @@ -70,12 +71,25 @@ public class AltosFlightStatus extends JComponent implements AltosFlightDisplay } class Call extends FlightValue { - void show(AltosState state, AltosListenerState listener_state) { - value.setText(state.callsign); - if (state.callsign == null) - setVisible(false); + + String last_call = ""; + + boolean same_call(String call) { + if (last_call == null) + return call == null; else - setVisible(true); + return last_call.equals(call); + } + + void show(AltosState state, AltosListenerState listener_state) { + if (!same_call(state.callsign)) { + value.setText(state.callsign); + if (state.callsign == null) + setVisible(false); + else + setVisible(true); + last_call = state.callsign; + } } public Call (GridBagLayout layout, int x) { super (layout, x, "Callsign"); @@ -85,11 +99,16 @@ public class AltosFlightStatus extends JComponent implements AltosFlightDisplay Call call; class Serial extends FlightValue { + + int last_serial = -1; void show(AltosState state, AltosListenerState listener_state) { - if (state.serial == AltosLib.MISSING) - value.setText("none"); - else - value.setText(String.format("%d", state.serial)); + if (state.serial != last_serial) { + if (state.serial == AltosLib.MISSING) + value.setText("none"); + else + value.setText(String.format("%d", state.serial)); + last_serial = state.serial; + } } public Serial (GridBagLayout layout, int x) { super (layout, x, "Serial"); @@ -99,11 +118,17 @@ public class AltosFlightStatus extends JComponent implements AltosFlightDisplay Serial serial; class Flight extends FlightValue { + + int last_flight = -1; + void show(AltosState state, AltosListenerState listener_state) { - if (state.flight == AltosLib.MISSING) - value.setText("none"); - else - value.setText(String.format("%d", state.flight)); + if (state.flight != last_flight) { + if (state.flight == AltosLib.MISSING) + value.setText("none"); + else + value.setText(String.format("%d", state.flight)); + last_flight = state.flight; + } } public Flight (GridBagLayout layout, int x) { super (layout, x, "Flight"); @@ -113,8 +138,14 @@ public class AltosFlightStatus extends JComponent implements AltosFlightDisplay Flight flight; class FlightState extends FlightValue { + + int last_state = -1; + void show(AltosState state, AltosListenerState listener_state) { - value.setText(state.state_name()); + if (state.state != last_state) { + value.setText(state.state_name()); + last_state = state.state; + } } public FlightState (GridBagLayout layout, int x) { super (layout, x, "State"); @@ -124,12 +155,18 @@ public class AltosFlightStatus extends JComponent implements AltosFlightDisplay FlightState flight_state; class RSSI extends FlightValue { + + int last_rssi = 10000; + void show(AltosState state, AltosListenerState listener_state) { - value.setText(String.format("%d", state.rssi())); - if (state.rssi == AltosLib.MISSING) - setVisible(false); - else - setVisible(true); + if (state.rssi() != last_rssi) { + value.setText(String.format("%d", state.rssi())); + if (state.rssi == AltosLib.MISSING) + setVisible(false); + else + setVisible(true); + last_rssi = state.rssi(); + } } public RSSI (GridBagLayout layout, int x) { super (layout, x, "RSSI"); @@ -139,9 +176,15 @@ public class AltosFlightStatus extends JComponent implements AltosFlightDisplay RSSI rssi; class LastPacket extends FlightValue { + + long last_secs = -1; + void show(AltosState state, AltosListenerState listener_state) { long secs = (System.currentTimeMillis() - state.received_time + 500) / 1000; - value.setText(String.format("%d", secs)); + if (secs != last_secs) { + value.setText(String.format("%d", secs)); + last_secs = secs; + } } public LastPacket(GridBagLayout layout, int x) { super (layout, x, "Age"); diff --git a/altosui/AltosIgnitor.java b/altosui/AltosIgnitor.java index 7f62938d..73318117 100644 --- a/altosui/AltosIgnitor.java +++ b/altosui/AltosIgnitor.java @@ -102,6 +102,7 @@ public class AltosIgnitor extends JComponent implements AltosFlightDisplay { 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; diff --git a/altosui/AltosLanded.java b/altosui/AltosLanded.java index d2628fb0..760b2d64 100644 --- a/altosui/AltosLanded.java +++ b/altosui/AltosLanded.java @@ -24,14 +24,18 @@ import java.io.*; import org.altusmetrum.altoslib_4.*; import org.altusmetrum.altosuilib_2.*; -public class AltosLanded extends JComponent implements AltosFlightDisplay, ActionListener { +public class AltosLanded extends JComponent implements AltosFlightDisplay, ActionListener, HierarchyListener { GridBagLayout layout; + private AltosState last_state; + private AltosListenerState last_listener_state; + public abstract class LandedValue implements AltosFontListener, AltosUnitsListener { JLabel label; JTextField value; AltosUnits units; double v; + String last_value = ""; abstract void show(AltosState state, AltosListenerState listener_state); @@ -46,7 +50,10 @@ public class AltosLanded extends JComponent implements AltosFlightDisplay, Actio void show(String s) { show(); - value.setText(s); + if (!last_value.equals(s)) { + value.setText(s); + last_value = s; + } } void show(double v) { @@ -94,6 +101,7 @@ public class AltosLanded extends JComponent implements AltosFlightDisplay, Actio add(label); value = new JTextField(Altos.text_width); + value.setEditable(false); value.setFont(Altos.value_font); value.setHorizontalAlignment(SwingConstants.RIGHT); c.gridx = 1; c.gridy = y; @@ -244,6 +252,12 @@ public class AltosLanded extends JComponent implements AltosFlightDisplay, Actio } 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) { bearing.show(state, listener_state); distance.show(state, listener_state); @@ -302,6 +316,17 @@ public class AltosLanded extends JComponent implements AltosFlightDisplay, Actio return "Landed"; } + 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 AltosLanded(AltosFlightReader in_reader) { layout = new GridBagLayout(); @@ -332,5 +357,6 @@ public class AltosLanded extends JComponent implements AltosFlightDisplay, Actio c.weighty = 0; c.fill = GridBagConstraints.VERTICAL; add(graph, c); + addHierarchyListener(this); } } diff --git a/altosui/AltosPad.java b/altosui/AltosPad.java index ce1f2ab7..3294949c 100644 --- a/altosui/AltosPad.java +++ b/altosui/AltosPad.java @@ -98,6 +98,7 @@ public class AltosPad extends JComponent implements AltosFlightDisplay { 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; @@ -178,6 +179,7 @@ public class AltosPad extends JComponent implements AltosFlightDisplay { 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; @@ -193,50 +195,56 @@ public class AltosPad extends JComponent implements AltosFlightDisplay { } } - class Battery extends LaunchStatus { + class Voltage extends LaunchStatus { + + double voltage(AltosState state) { return AltosLib.MISSING; }; + double good() { return 0; }; + + double last_voltage = -1; + void show (AltosState state, AltosListenerState listener_state) { - if (state == null || state.battery_voltage == AltosLib.MISSING) - hide(); - else { - show("%4.2f V", state.battery_voltage); - lights.set(state.battery_voltage >= AltosLib.ao_battery_good); + double voltage = AltosLib.MISSING; + if (state != null) + voltage = voltage(state); + + if (voltage != last_voltage) { + if (voltage == AltosLib.MISSING) + hide(); + else { + show("%4.2f V", voltage); + lights.set(voltage >= good()); + } + last_voltage = voltage; } } + public Voltage (GridBagLayout layout, int y, String name) { super(layout, y, name); } + } + + + class Battery extends Voltage { + double voltage(AltosState state) { return state.battery_voltage; } + double good() { return AltosLib.ao_battery_good; } + public Battery (GridBagLayout layout, int y) { super(layout, y, "Battery Voltage"); } + } Battery battery; - class Apogee extends LaunchStatus { - void show (AltosState state, AltosListenerState listener_state) { - if (state == null || state.apogee_voltage == AltosLib.MISSING) - hide(); - else { - 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, "Apogee Igniter Voltage"); - } + class Apogee extends Voltage { + double voltage(AltosState state) { return state.apogee_voltage; } + double good() { return AltosLib.ao_igniter_good; } + public Apogee (GridBagLayout layout, int y) { super(layout, y, "Apogee Igniter Voltage"); } } Apogee apogee; - class Main extends LaunchStatus { - void show (AltosState state, AltosListenerState listener_state) { - if (state == null || state.main_voltage == AltosLib.MISSING) - hide(); - else { - 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, "Main Igniter Voltage"); - } + class Main extends Voltage { + double voltage(AltosState state) { return state.main_voltage; } + double good() { return AltosLib.ao_igniter_good; } + public Main (GridBagLayout layout, int y) { super(layout, y, "Main Igniter Voltage"); } } Main main; @@ -328,6 +336,9 @@ public class AltosPad extends JComponent implements AltosFlightDisplay { } class PadLat extends LaunchValue { + + double last_lat = 1000; + void show (AltosState state, AltosListenerState listener_state) { double lat = AltosLib.MISSING; String label = null; @@ -341,11 +352,14 @@ public class AltosPad extends JComponent implements AltosFlightDisplay { label = "Pad Latitude"; } } - if (lat != AltosLib.MISSING) { - show(pos(lat,"N", "S")); - set_label(label); - } else - hide(); + if (lat != last_lat) { + if (lat != AltosLib.MISSING) { + show(pos(lat,"N", "S")); + set_label(label); + } else + hide(); + last_lat = lat; + } } public PadLat (GridBagLayout layout, int y) { super (layout, y, "Pad Latitude"); @@ -355,6 +369,9 @@ public class AltosPad extends JComponent implements AltosFlightDisplay { PadLat pad_lat; class PadLon extends LaunchValue { + + double last_lon = 1000; + void show (AltosState state, AltosListenerState listener_state) { double lon = AltosLib.MISSING; String label = null; @@ -368,11 +385,14 @@ public class AltosPad extends JComponent implements AltosFlightDisplay { label = "Pad Longitude"; } } - if (lon != AltosLib.MISSING) { - show(pos(lon,"E", "W")); - set_label(label); - } else - hide(); + if (lon != last_lon) { + if (lon != AltosLib.MISSING) { + show(pos(lon,"E", "W")); + set_label(label); + } else + hide(); + last_lon = lon; + } } public PadLon (GridBagLayout layout, int y) { super (layout, y, "Pad Longitude"); @@ -382,6 +402,9 @@ public class AltosPad extends JComponent implements AltosFlightDisplay { PadLon pad_lon; class PadAlt extends LaunchValue { + + double last_alt = -1000000; + void show (AltosState state, AltosListenerState listener_state) { double alt = AltosLib.MISSING; String label = null; @@ -395,11 +418,14 @@ public class AltosPad extends JComponent implements AltosFlightDisplay { label = "Pad Altitude"; } } - if (alt != AltosLib.MISSING) { - show(alt); - set_label(label); - } else - hide(); + if (alt != last_alt) { + if (alt != AltosLib.MISSING) { + show(alt); + set_label(label); + } else + hide(); + last_alt = alt; + } } public PadAlt (GridBagLayout layout, int y) { super (layout, y, AltosConvert.height, "Pad Altitude"); diff --git a/altosuilib/AltosInfoTable.java b/altosuilib/AltosInfoTable.java index 24a895eb..8ded1bea 100644 --- a/altosuilib/AltosInfoTable.java +++ b/altosuilib/AltosInfoTable.java @@ -18,16 +18,20 @@ package org.altusmetrum.altosuilib_2; import java.awt.*; +import java.awt.event.*; import javax.swing.*; import javax.swing.table.*; import org.altusmetrum.altoslib_4.*; -public class AltosInfoTable extends JTable implements AltosFlightDisplay { +public class AltosInfoTable extends JTable implements AltosFlightDisplay, HierarchyListener { private AltosFlightInfoTableModel model; static final int info_columns = 3; static final int info_rows = 17; + private AltosState last_state; + private AltosListenerState last_listener_state; + int desired_row_height() { FontMetrics infoValueMetrics = getFontMetrics(AltosUILib.table_value_font); return (infoValueMetrics.getHeight() + infoValueMetrics.getLeading()) * 18 / 10; @@ -56,6 +60,7 @@ public class AltosInfoTable extends JTable implements AltosFlightDisplay { super(new AltosFlightInfoTableModel(info_rows, info_columns)); model = (AltosFlightInfoTableModel) getModel(); setFont(AltosUILib.table_value_font); + addHierarchyListener(this); setAutoResizeMode(AUTO_RESIZE_ALL_COLUMNS); setShowGrid(true); set_layout(); @@ -71,6 +76,17 @@ public class AltosInfoTable extends JTable implements AltosFlightDisplay { public void units_changed(boolean imperial_units) { } + 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 Dimension getPreferredScrollableViewportSize() { return getPreferredSize(); } @@ -108,6 +124,13 @@ public class AltosInfoTable extends JTable implements AltosFlightDisplay { } public void show(AltosState state, AltosListenerState listener_state) { + + if (!isShowing()) { + last_state = state; + last_listener_state = listener_state; + return; + } + reset(); if (state != null) { if (state.device_type != AltosLib.MISSING) diff --git a/altosuilib/AltosUIMapView.java b/altosuilib/AltosUIMapView.java index 70198682..efae3767 100644 --- a/altosuilib/AltosUIMapView.java +++ b/altosuilib/AltosUIMapView.java @@ -28,7 +28,7 @@ import java.util.*; import java.util.concurrent.*; import org.altusmetrum.altoslib_4.*; -public class AltosUIMapView extends Canvas implements MouseMotionListener, MouseListener, MouseWheelListener, ComponentListener, AltosUIMapTileListener, AltosUIMapStoreListener { +public class AltosUIMapView extends Component implements MouseMotionListener, MouseListener, MouseWheelListener, ComponentListener, AltosUIMapTileListener, AltosUIMapStoreListener { AltosUIMapPath path = new AltosUIMapPath(); @@ -422,7 +422,6 @@ public class AltosUIMapView extends Canvas implements MouseMotionListener, Mouse } public void paint(Graphics g) { - VolatileImage back_buffer = create_back_buffer(); do { GraphicsConfiguration gc = getGraphicsConfiguration(); diff --git a/telegps/TeleGPSInfo.java b/telegps/TeleGPSInfo.java index 2765f5ab..15eb9b75 100644 --- a/telegps/TeleGPSInfo.java +++ b/telegps/TeleGPSInfo.java @@ -18,14 +18,18 @@ package org.altusmetrum.telegps; import java.awt.*; +import java.awt.event.*; import javax.swing.*; import org.altusmetrum.altoslib_4.*; import org.altusmetrum.altosuilib_2.*; -public class TeleGPSInfo extends JComponent implements AltosFlightDisplay { +public class TeleGPSInfo extends JComponent implements AltosFlightDisplay, HierarchyListener { GridBagLayout layout; JLabel cur, max; + private AltosState last_state; + private AltosListenerState last_listener_state; + public abstract class Info implements AltosFontListener, AltosUnitsListener { JLabel label; JTextField value; @@ -280,6 +284,7 @@ public class TeleGPSInfo extends JComponent implements AltosFlightDisplay { value1 = new JTextField(AltosUILib.text_width); value1.setFont(AltosUILib.value_font); value1.setHorizontalAlignment(SwingConstants.RIGHT); + value1.setEditable(false); c.gridx = 2; c.gridy = y; c.anchor = GridBagConstraints.WEST; c.fill = GridBagConstraints.BOTH; @@ -290,6 +295,7 @@ public class TeleGPSInfo extends JComponent implements AltosFlightDisplay { value2 = new JTextField(AltosUILib.text_width); value2.setFont(AltosUILib.value_font); value2.setHorizontalAlignment(SwingConstants.RIGHT); + value1.setEditable(false); c.gridx = 3; c.gridy = y; c.anchor = GridBagConstraints.WEST; c.fill = GridBagConstraints.BOTH; @@ -323,18 +329,16 @@ public class TeleGPSInfo extends JComponent implements AltosFlightDisplay { } public void units_changed(boolean imperial_units) { - show(v); + show(v, max); } - void show(double v) { + void show(double v, double max) { this.v = v; - if (v == AltosLib.MISSING) { + this.max = max; + if (v == AltosLib.MISSING) value.setText("Missing"); - } else { + else value.setText(units.show(8, v)); - if (v > max || max == AltosLib.MISSING) - max = v; - } if (max == AltosLib.MISSING) max_value.setText("Missing"); else @@ -364,6 +368,7 @@ public class TeleGPSInfo extends JComponent implements AltosFlightDisplay { add(label); value = new JTextField(AltosUILib.text_width); + value.setEditable(false); value.setFont(AltosUILib.value_font); value.setHorizontalAlignment(SwingConstants.RIGHT); c.gridx = 2; c.gridy = y; @@ -374,6 +379,7 @@ public class TeleGPSInfo extends JComponent implements AltosFlightDisplay { add(value); max_value = new JTextField(AltosUILib.text_width); + max_value.setEditable(false); max_value.setFont(AltosUILib.value_font); max_value.setHorizontalAlignment(SwingConstants.RIGHT); c.gridx = 3; c.gridy = y; @@ -388,7 +394,7 @@ public class TeleGPSInfo extends JComponent implements AltosFlightDisplay { class Altitude extends ValueHold { void show (AltosState state, AltosListenerState listener_state) { - show(state.altitude()); + show(state.altitude(), state.max_altitude()); } public Altitude (GridBagLayout layout, int y) { super (layout, y, AltosConvert.height, "Altitude"); @@ -399,7 +405,7 @@ public class TeleGPSInfo extends JComponent implements AltosFlightDisplay { class AscentRate extends ValueHold { void show (AltosState state, AltosListenerState listener_state) { - show(state.gps_ascent_rate()); + show(state.gps_ascent_rate(), state.max_gps_ascent_rate()); } public AscentRate (GridBagLayout layout, int y) { super (layout, y, AltosConvert.speed, "Ascent Rate"); @@ -410,7 +416,7 @@ public class TeleGPSInfo extends JComponent implements AltosFlightDisplay { class GroundSpeed extends ValueHold { void show (AltosState state, AltosListenerState listener_state) { - show(state.gps_ground_speed()); + show(state.gps_ground_speed(), state.max_gps_ground_speed()); } public GroundSpeed (GridBagLayout layout, int y) { super (layout, y, AltosConvert.speed, "Ground Speed"); @@ -526,6 +532,12 @@ public class TeleGPSInfo extends JComponent implements AltosFlightDisplay { } 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); @@ -562,6 +574,17 @@ public class TeleGPSInfo extends JComponent implements AltosFlightDisplay { return "Info"; } + 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 TeleGPSInfo() { layout = new GridBagLayout(); @@ -582,5 +605,6 @@ public class TeleGPSInfo extends JComponent implements AltosFlightDisplay { lat = new Lat(layout, y++); lon = new Lon(layout, y++); gps_locked = new GPSLocked(layout, y++); + addHierarchyListener(this); } } diff --git a/telegps/TeleGPSStatus.java b/telegps/TeleGPSStatus.java index 37cfae37..e6bb1ea5 100644 --- a/telegps/TeleGPSStatus.java +++ b/telegps/TeleGPSStatus.java @@ -61,6 +61,7 @@ public class TeleGPSStatus extends JComponent implements AltosFlightDisplay { add(label); value = new JTextField(""); + value.setEditable(false); value.setFont(AltosUILib.status_font); value.setHorizontalAlignment(SwingConstants.CENTER); c.gridx = x; c.gridy = 1; @@ -70,8 +71,13 @@ public class TeleGPSStatus extends JComponent implements AltosFlightDisplay { } class Call extends Value { + String call; + void show(AltosState state, AltosListenerState listener_state) { - value.setText(state.callsign); + if (state.callsign != call) { + value.setText(state.callsign); + call = state.callsign; + } if (state.callsign == null) setVisible(false); else @@ -85,11 +91,15 @@ public class TeleGPSStatus extends JComponent implements AltosFlightDisplay { Call call; class Serial extends Value { + int serial = -1; void show(AltosState state, AltosListenerState listener_state) { - if (state.serial == AltosLib.MISSING) - value.setText("none"); - else - value.setText(String.format("%d", state.serial)); + if (state.serial != serial) { + if (state.serial == AltosLib.MISSING) + value.setText("none"); + else + value.setText(String.format("%d", state.serial)); + serial = state.serial; + } } public Serial (GridBagLayout layout, int x) { super (layout, x, "Serial"); @@ -99,12 +109,19 @@ public class TeleGPSStatus extends JComponent implements AltosFlightDisplay { Serial serial; class RSSI extends Value { + int rssi = 10000; + void show(AltosState state, AltosListenerState listener_state) { - value.setText(String.format("%d", state.rssi())); - if (state.rssi == AltosLib.MISSING) - setVisible(false); - else - setVisible(true); + int new_rssi = state.rssi(); + + if (new_rssi != rssi) { + value.setText(String.format("%d", new_rssi)); + if (state.rssi == AltosLib.MISSING) + setVisible(false); + else + setVisible(true); + rssi = new_rssi; + } } public RSSI (GridBagLayout layout, int x) { super (layout, x, "RSSI"); @@ -114,9 +131,16 @@ public class TeleGPSStatus extends JComponent implements AltosFlightDisplay { RSSI rssi; class LastPacket extends Value { + + long last_secs = -1; + void show(AltosState state, AltosListenerState listener_state) { long secs = (System.currentTimeMillis() - state.received_time + 500) / 1000; - value.setText(String.format("%d", secs)); + + if (secs != last_secs) { + value.setText(String.format("%d", secs)); + last_secs = secs; + } } public LastPacket(GridBagLayout layout, int x) { super (layout, x, "Age"); -- cgit v1.2.3 From 9a6a3c34293eac6442f766e13ce148f595e891eb Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Fri, 13 Jun 2014 21:26:33 -0700 Subject: altosuilib: Make map-cache per-window instead of global This consumes more memory, but avoids cache conflicts between windows Signed-off-by: Keith Packard --- altosuilib/AltosUIMapCache.java | 22 ++++++++++++---------- altosuilib/AltosUIMapPreload.java | 3 +++ altosuilib/AltosUIMapTile.java | 4 +++- altosuilib/AltosUIMapTileListener.java | 2 ++ altosuilib/AltosUIMapView.java | 6 +++++- 5 files changed, 25 insertions(+), 12 deletions(-) (limited to 'altosuilib/AltosUIMapView.java') diff --git a/altosuilib/AltosUIMapCache.java b/altosuilib/AltosUIMapCache.java index e849da79..55311d8c 100644 --- a/altosuilib/AltosUIMapCache.java +++ b/altosuilib/AltosUIMapCache.java @@ -31,18 +31,19 @@ public class AltosUIMapCache { static final int bad_request = 3; static final int forbidden = 4; - static private Object fetch_lock = new Object(); + static final int min_cache_size = 9; + static final int max_cache_size = 24; - static final int min_cache_size = 9; - static final int max_cache_size = 24; + private Object fetch_lock = new Object(); + private Object cache_lock = new Object(); - static int cache_size = min_cache_size; + int cache_size = min_cache_size; - static AltosUIMapImage[] images = new AltosUIMapImage[cache_size]; + AltosUIMapImage[] images = new AltosUIMapImage[cache_size]; - static Object cache_lock = new Object(); + long used; - public static void set_cache_size(int new_size) { + public void set_cache_size(int new_size) { if (new_size < min_cache_size) new_size = min_cache_size; if (new_size > max_cache_size) @@ -64,9 +65,7 @@ public class AltosUIMapCache { } } - static long used; - - public static Image get(AltosUIMapTile tile, AltosUIMapStore store, int width, int height) { + public Image get(AltosUIMapTile tile, AltosUIMapStore store, int width, int height) { int oldest = -1; long age = used; @@ -109,4 +108,7 @@ public class AltosUIMapCache { } } } + + public AltosUIMapCache() { + } } diff --git a/altosuilib/AltosUIMapPreload.java b/altosuilib/AltosUIMapPreload.java index d702dddf..3bdba39e 100644 --- a/altosuilib/AltosUIMapPreload.java +++ b/altosuilib/AltosUIMapPreload.java @@ -209,6 +209,7 @@ class AltosUISites extends Thread { public class AltosUIMapPreload extends AltosUIFrame implements ActionListener, ItemListener, AltosUIMapTileListener { AltosUIFrame owner; AltosUIMap map; + AltosUIMapCache cache = new AltosUIMapCache(); AltosUIMapPos lat; AltosUIMapPos lon; @@ -353,6 +354,8 @@ public class AltosUIMapPreload extends AltosUIFrame implements ActionListener, I } } + public AltosUIMapCache cache() { return cache; } + public void set_sites() { int i = 1; for (AltosUISite site : sites.sites) { diff --git a/altosuilib/AltosUIMapTile.java b/altosuilib/AltosUIMapTile.java index 6fbcdb4b..7c823183 100644 --- a/altosuilib/AltosUIMapTile.java +++ b/altosuilib/AltosUIMapTile.java @@ -34,6 +34,7 @@ public class AltosUIMapTile { int zoom; int maptype; AltosUIMapStore store; + AltosUIMapCache cache; int status; private File map_file() { @@ -153,7 +154,7 @@ public class AltosUIMapTile { ++painting_serial; if (image == null && t.has_location()) - image = AltosUIMapCache.get(this, store, px_size, px_size); + image = cache.get(this, store, px_size, px_size); paint_graphics(g2d, t, painting_serial); } @@ -173,6 +174,7 @@ public class AltosUIMapTile { public AltosUIMapTile(AltosUIMapTileListener listener, AltosUILatLon upper_left, AltosUILatLon center, int zoom, int maptype, int px_size, Font font) { this.listener = listener; this.upper_left = upper_left; + cache = listener.cache(); while (center.lon < -180.0) center.lon += 360.0; diff --git a/altosuilib/AltosUIMapTileListener.java b/altosuilib/AltosUIMapTileListener.java index 4cc3ff2f..4ca13539 100644 --- a/altosuilib/AltosUIMapTileListener.java +++ b/altosuilib/AltosUIMapTileListener.java @@ -19,4 +19,6 @@ package org.altusmetrum.altosuilib_2; public interface AltosUIMapTileListener { abstract public void notify_tile(AltosUIMapTile tile, int status); + + abstract public AltosUIMapCache cache(); } diff --git a/altosuilib/AltosUIMapView.java b/altosuilib/AltosUIMapView.java index efae3767..4df178e2 100644 --- a/altosuilib/AltosUIMapView.java +++ b/altosuilib/AltosUIMapView.java @@ -34,6 +34,8 @@ public class AltosUIMapView extends Component implements MouseMotionListener, Mo AltosUIMapLine line = new AltosUIMapLine(); + AltosUIMapCache cache = new AltosUIMapCache(); + LinkedList marks = new LinkedList(); LinkedList zoom_listeners = new LinkedList(); @@ -368,7 +370,7 @@ public class AltosUIMapView extends Component implements MouseMotionListener, Mo for (Point point : to_remove) tiles.remove(point); - AltosUIMapCache.set_cache_size(((lower_right.y - upper_left.y) / px_size + 1) * ((lower_right.x - upper_left.x) / px_size + 1)); + cache.set_cache_size(((lower_right.y - upper_left.y) / px_size + 1) * ((lower_right.x - upper_left.x) / px_size + 1)); for (int y = upper_left.y; y <= lower_right.y; y += px_size) { for (int x = upper_left.x; x <= lower_right.x; x += px_size) { Point point = new Point(x, y); @@ -394,6 +396,8 @@ public class AltosUIMapView extends Component implements MouseMotionListener, Mo } } + public AltosUIMapCache cache() { return cache; } + /* AltosUIMapStoreListener methods */ public void notify_store(AltosUIMapStore store, int status) { if (load_listener != null) { -- cgit v1.2.3 From 5392ee3c5328f8384ed30a2d147e4be96075e064 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Sat, 14 Jun 2014 18:51:25 -0700 Subject: altosuilib: Serialize access to async tile notify function in preload This ensures that we see each tile getting downloaded and don't mis-count, which would result in wedging the process Signed-off-by: Keith Packard --- altosuilib/AltosUIMapPreload.java | 2 +- altosuilib/AltosUIMapView.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'altosuilib/AltosUIMapView.java') diff --git a/altosuilib/AltosUIMapPreload.java b/altosuilib/AltosUIMapPreload.java index 3bdba39e..c91b06b8 100644 --- a/altosuilib/AltosUIMapPreload.java +++ b/altosuilib/AltosUIMapPreload.java @@ -327,7 +327,7 @@ public class AltosUIMapPreload extends AltosUIFrame implements ActionListener, I /* AltosUIMapTileListener methods */ - public void notify_tile(AltosUIMapTile tile, int status) { + public synchronized void notify_tile(AltosUIMapTile tile, int status) { if (status == AltosUIMapStore.loading) return; diff --git a/altosuilib/AltosUIMapView.java b/altosuilib/AltosUIMapView.java index 4df178e2..a14fde65 100644 --- a/altosuilib/AltosUIMapView.java +++ b/altosuilib/AltosUIMapView.java @@ -387,7 +387,7 @@ public class AltosUIMapView extends Component implements MouseMotionListener, Mo } /* AltosUIMapTileListener methods */ - public void notify_tile(AltosUIMapTile tile, int status) { + public synchronized void notify_tile(AltosUIMapTile tile, int status) { for (Point point : tiles.keySet()) { if (tile == tiles.get(point)) { Point screen = transform.screen(point); @@ -399,7 +399,7 @@ public class AltosUIMapView extends Component implements MouseMotionListener, Mo public AltosUIMapCache cache() { return cache; } /* AltosUIMapStoreListener methods */ - public void notify_store(AltosUIMapStore store, int status) { + public synchronized void notify_store(AltosUIMapStore store, int status) { if (load_listener != null) { for (AltosUIMapTile tile : tiles.values()) if (store.equals(tile.store)) -- cgit v1.2.3