Changeset 8548 in josm


Ignore:
Timestamp:
2015-07-01T19:31:36+02:00 (9 years ago)
Author:
bastiK
Message:

applied #11627 - Added Repaint listener to MapView (patch by michael2402)

File:
1 edited

Legend:

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

    r8510 r8548  
    946946        return Utils.join("; ", layerInfo);
    947947    }
     948
     949    /**
     950     * This is a listener that gets informed whenever repaint is called for this MapView.
     951     * <p>
     952     * This is the only safe method to find changes to the map view, since many components call MapView.repaint() directly.
     953     * @author Michael Zangl
     954     */
     955    public interface RepaintListener {
     956        /**
     957         * Called when any repaint method is called (using default arguments if required).
     958         * @param tm see {@link JComponent#repaint(long, int, int, int, int)}
     959         * @param x see {@link JComponent#repaint(long, int, int, int, int)}
     960         * @param y see {@link JComponent#repaint(long, int, int, int, int)}
     961         * @param width see {@link JComponent#repaint(long, int, int, int, int)}
     962         * @param height see {@link JComponent#repaint(long, int, int, int, int)}
     963         */
     964        void repaint(long tm, int x, int y, int width, int height);
     965    }
     966
     967    private final CopyOnWriteArrayList<RepaintListener> repaintListeners = new CopyOnWriteArrayList<>();
     968
     969    /**
     970     * Adds a listener that gets informed whenever repaint() is called for this class.
     971     * @param l The listener.
     972     */
     973    public void addRepaintListener(RepaintListener l) {
     974        repaintListeners.add(l);
     975    }
     976
     977    /**
     978     * Removes a registered repaint listener.
     979     * @param l The listener.
     980     */
     981    public void removeRepaintListener(RepaintListener l) {
     982        repaintListeners.remove(l);
     983    }
     984
     985    @Override
     986    public void repaint(long tm, int x, int y, int width, int height) {
     987        // This is the main repaint method, all other methods are convenience methods and simply call this method. This is just an observation, not a must, but seems to be true for all implementations I found so far.
     988        if (repaintListeners != null) {
     989            // Might get called early in super constructor
     990            for (RepaintListener l : repaintListeners) {
     991                l.repaint(tm, x, y, width, height);
     992            }
     993        }
     994        super.repaint(tm, x, y, width, height);
     995    }
    948996}
Note: See TracChangeset for help on using the changeset viewer.