Ignore:
Timestamp:
2016-03-23T21:30:27+01:00 (8 years ago)
Author:
Don-vip
Message:

fix #12654 - Add layer invalidation listener (patch by michael2402)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/MapView.java

    r10008 r10031  
    5555import org.openstreetmap.josm.data.osm.visitor.paint.Rendering;
    5656import org.openstreetmap.josm.data.osm.visitor.paint.relations.MultipolygonCache;
     57import org.openstreetmap.josm.gui.layer.AbstractMapViewPaintable;
    5758import org.openstreetmap.josm.gui.layer.GpxLayer;
    5859import org.openstreetmap.josm.gui.layer.ImageryLayer;
     
    6061import org.openstreetmap.josm.gui.layer.LayerPositionStrategy;
    6162import org.openstreetmap.josm.gui.layer.MapViewPaintable;
     63import org.openstreetmap.josm.gui.layer.MapViewPaintable.PaintableInvalidationEvent;
     64import org.openstreetmap.josm.gui.layer.MapViewPaintable.PaintableInvalidationListener;
    6265import org.openstreetmap.josm.gui.layer.NativeScaleLayer;
    6366import org.openstreetmap.josm.gui.layer.OsmDataLayer;
     
    125128    }
    126129
     130    /**
     131     * An invalidation listener that simply calls repaint() for now.
     132     * @author Michael Zangl
     133     */
     134    private class LayerInvalidatedListener implements PaintableInvalidationListener {
     135        private boolean ignoreRepaint;
     136        @Override
     137        public void paintablInvalidated(PaintableInvalidationEvent event) {
     138            ignoreRepaint = true;
     139            repaint();
     140        }
     141
     142        /**
     143         * Temporary until all {@link MapViewPaintable}s support this.
     144         * @param p The paintable.
     145         */
     146        public void addTo(MapViewPaintable p) {
     147            if (p instanceof AbstractMapViewPaintable) {
     148                ((AbstractMapViewPaintable) p).addInvalidationListener(this);
     149            }
     150        }
     151        /**
     152         * Temporary until all {@link MapViewPaintable}s support this.
     153         * @param p The paintable.
     154         */
     155        public void removeFrom(MapViewPaintable p) {
     156            if (p instanceof AbstractMapViewPaintable) {
     157                ((AbstractMapViewPaintable) p).removeInvalidationListener(this);
     158            }
     159        }
     160
     161        /**
     162         * Attempts to trace repaints that did not originate from this listener. Good to find missed {@link MapView#repaint()}s in code.
     163         */
     164        protected synchronized void traceRandomRepaint() {
     165            if (!ignoreRepaint) {
     166                System.err.println("Repaint:");
     167                Thread.dumpStack();
     168            }
     169            ignoreRepaint = false;
     170        }
     171    }
     172
    127173    public boolean viewportFollowing;
    128174
     
    269315
    270316    /**
     317     * The listener that listens to invalidations of all layers.
     318     */
     319    private final LayerInvalidatedListener invalidatedListener = new LayerInvalidatedListener();
     320
     321    /**
    271322     * Constructs a new {@code MapView}.
    272323     * @param contentPane The content pane used to register shortcuts in its
     
    382433
    383434            layer.addPropertyChangeListener(this);
     435            invalidatedListener.addTo(layer);
    384436            Main.addProjectionChangeListener(layer);
    385437            AudioPlayer.reset();
     
    506558            Main.removeProjectionChangeListener(layer);
    507559            layer.removePropertyChangeListener(this);
     560            invalidatedListener.removeFrom(layer);
    508561            layer.destroy();
    509562            AudioPlayer.reset();
     
    10351088    public boolean addTemporaryLayer(MapViewPaintable mvp) {
    10361089        synchronized (temporaryLayers) {
    1037             return temporaryLayers.add(mvp);
     1090            boolean added = temporaryLayers.add(mvp);
     1091            if (added) {
     1092                invalidatedListener.addTo(mvp);
     1093            }
     1094            return added;
    10381095        }
    10391096    }
     
    10461103    public boolean removeTemporaryLayer(MapViewPaintable mvp) {
    10471104        synchronized (temporaryLayers) {
    1048             return temporaryLayers.remove(mvp);
     1105            boolean removed = temporaryLayers.remove(mvp);
     1106            if (removed) {
     1107                invalidatedListener.removeFrom(mvp);
     1108            }
     1109            return removed;
    10491110        }
    10501111    }
     
    12061267        super.repaint(tm, x, y, width, height);
    12071268    }
     1269
     1270    @Override
     1271    public void repaint() {
     1272        if (Main.isTraceEnabled()) {
     1273            invalidatedListener.traceRandomRepaint();
     1274        }
     1275        super.repaint();
     1276    }
    12081277}
Note: See TracChangeset for help on using the changeset viewer.