1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.layer;
|
---|
3 |
|
---|
4 | import java.util.concurrent.CopyOnWriteArrayList;
|
---|
5 |
|
---|
6 | /**
|
---|
7 | * This class implements the invalidation listener mechanism suggested by {@link MapViewPaintable} and a default #atta
|
---|
8 | *
|
---|
9 | * @author Michael Zangl
|
---|
10 | * @since 10031
|
---|
11 | */
|
---|
12 | public abstract class AbstractMapViewPaintable implements MapViewPaintable {
|
---|
13 |
|
---|
14 | /**
|
---|
15 | * This is the default implementation of the layer painter.
|
---|
16 | * <p>
|
---|
17 | * You should not use it. Write your own implementation and put your paint code into that class.
|
---|
18 | * <p>
|
---|
19 | * It propagates all calls to the
|
---|
20 | * {@link MapViewPaintable#paint(java.awt.Graphics2D, org.openstreetmap.josm.gui.MapView, org.openstreetmap.josm.data.Bounds)} method.
|
---|
21 | * @author Michael Zangl
|
---|
22 | * @since 10458
|
---|
23 | */
|
---|
24 | protected class CompatibilityModeLayerPainter implements LayerPainter {
|
---|
25 | @Override
|
---|
26 | public void paint(MapViewGraphics graphics) {
|
---|
27 | AbstractMapViewPaintable.this.paint(
|
---|
28 | graphics.getDefaultGraphics(),
|
---|
29 | graphics.getMapView(),
|
---|
30 | graphics.getClipBounds().getLatLonBoundsBox());
|
---|
31 | }
|
---|
32 |
|
---|
33 | @Override
|
---|
34 | public void detachFromMapView(MapViewEvent event) {
|
---|
35 | // ignored in old implementation
|
---|
36 | }
|
---|
37 | }
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * A list of invalidation listeners to call when this layer is invalidated.
|
---|
41 | */
|
---|
42 | private final CopyOnWriteArrayList<PaintableInvalidationListener> invalidationListeners = new CopyOnWriteArrayList<>();
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * This method is called whenever this layer is added to a map view.
|
---|
46 | * <p>
|
---|
47 | * You need to return a painter here.
|
---|
48 | * The {@link MapViewPaintable.LayerPainter#detachFromMapView} method is called when the layer is removed
|
---|
49 | * from that map view. You are free to reuse painters.
|
---|
50 | * <p>
|
---|
51 | * You should always call the super method. See {@link #createMapViewPainter} if you want to influence painter creation.
|
---|
52 | * <p>
|
---|
53 | * This replaces {@link Layer#hookUpMapView} in the long run.
|
---|
54 | * @param event the event.
|
---|
55 | * @return A layer painter.
|
---|
56 | * @since 10458
|
---|
57 | */
|
---|
58 | public LayerPainter attachToMapView(MapViewEvent event) {
|
---|
59 | return createMapViewPainter(event);
|
---|
60 | }
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Creates a new LayerPainter.
|
---|
64 | * @param event The event that triggered the creation.
|
---|
65 | * @return The painter.
|
---|
66 | * @since 10458
|
---|
67 | */
|
---|
68 | protected LayerPainter createMapViewPainter(MapViewEvent event) {
|
---|
69 | return new CompatibilityModeLayerPainter();
|
---|
70 | }
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Adds a new paintable invalidation listener.
|
---|
74 | * @param l The listener to add.
|
---|
75 | */
|
---|
76 | public void addInvalidationListener(PaintableInvalidationListener l) {
|
---|
77 | invalidationListeners.add(l);
|
---|
78 | }
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * Removes an added paintable invalidation listener.
|
---|
82 | * @param l The listener to remove.
|
---|
83 | */
|
---|
84 | public void removeInvalidationListener(PaintableInvalidationListener l) {
|
---|
85 | invalidationListeners.remove(l);
|
---|
86 | }
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * This needs to be called whenever the content of this view was invalidated.
|
---|
90 | */
|
---|
91 | public void invalidate() {
|
---|
92 | for (PaintableInvalidationListener l : invalidationListeners) {
|
---|
93 | l.paintableInvalidated(new PaintableInvalidationEvent(this));
|
---|
94 | }
|
---|
95 | }
|
---|
96 | }
|
---|