source: josm/trunk/src/org/openstreetmap/josm/gui/layer/MapViewPaintable.java@ 13221

Last change on this file since 13221 was 12107, checked in by michael2402, 7 years ago

Allow invalidation listeners for all MapViewPaintables.

  • Property svn:eol-style set to native
File size: 5.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.layer;
3
4import java.awt.Graphics2D;
5
6import org.openstreetmap.josm.data.Bounds;
7import org.openstreetmap.josm.gui.MapView;
8
9/**
10 * This is a component that can be painted on the map view.
11 * <p>
12 * You might want to extend {@link AbstractMapViewPaintable} to ease implementation of this.
13 * <p>
14 * That class allows you to listen to paintable change events. Those methods may be moved here some time in the future.
15 * @since 608 (creation)
16 * @since 10600 (functional interface)
17 */
18@FunctionalInterface
19public interface MapViewPaintable {
20
21 /**
22 * This event is fired whenever the paintable got invalidated and needs repainting some time in the future.
23 * <p>
24 * Note: We might add an area in the future.
25 *
26 * @author Michael Zangl
27 */
28 class PaintableInvalidationEvent {
29 private final MapViewPaintable paintable;
30
31 /**
32 * Creates a new {@link PaintableInvalidationEvent}
33 * @param paintable The paintable that is invalidated.
34 */
35 public PaintableInvalidationEvent(MapViewPaintable paintable) {
36 this.paintable = paintable;
37 }
38
39 /**
40 * Gets the layer that was invalidated.
41 * @return The layer.
42 */
43 public MapViewPaintable getLayer() {
44 return paintable;
45 }
46
47 @Override
48 public String toString() {
49 return "LayerInvalidationEvent [layer=" + paintable + ']';
50 }
51 }
52
53 /**
54 * This is a listener that listens to {@link PaintableInvalidationEvent}s
55 * @author Michael Zangl
56 * @since 10600 (functional interface)
57 */
58 @FunctionalInterface
59 interface PaintableInvalidationListener {
60 /**
61 * Called whenever a {@link PaintableInvalidationEvent} is fired. This might be called from any thread.
62 * @param event The event
63 * @since 10600 (renamed)
64 */
65 void paintableInvalidated(PaintableInvalidationEvent event);
66 }
67
68 /**
69 * Gets a new LayerPainter that paints this {@link MapViewPaintable} to the given map view.
70 *
71 * @author Michael Zangl
72 * @since 10458
73 */
74 interface LayerPainter {
75
76 /**
77 * Paints the given layer.
78 * <p>
79 * This can be called in any thread at any time. You will not receive parallel calls for the same map view but you can receive parallel
80 * calls if you use the same {@link LayerPainter} for different map views.
81 * @param graphics The graphics object of the map view you should use.
82 * It provides you with a content pane, the bounds and the view state.
83 */
84 void paint(MapViewGraphics graphics);
85
86 /**
87 * Called when the layer is removed from the map view and this painter is not used any more.
88 * <p>
89 * This method is called once on the painter returned by {@link Layer#attachToMapView}
90 * @param event The event.
91 */
92 void detachFromMapView(MapViewEvent event);
93 }
94
95 /**
96 * A event that is fired whenever the map view is attached or detached from any layer.
97 * @author Michael Zangl
98 * @see Layer#attachToMapView
99 * @since 10458
100 */
101 class MapViewEvent {
102 private final MapView mapView;
103 private final boolean temporaryLayer;
104
105 /**
106 * Create a new {@link MapViewEvent}
107 * @param mapView The map view
108 * @param temporaryLayer <code>true</code> if this layer is in the temporary layer list of the view.
109 */
110 public MapViewEvent(MapView mapView, boolean temporaryLayer) {
111 super();
112 this.mapView = mapView;
113 this.temporaryLayer = temporaryLayer;
114 }
115
116 /**
117 * Gets the map view.
118 * @return The map view.
119 */
120 public MapView getMapView() {
121 return mapView;
122 }
123
124 /**
125 * @return true if this {@link MapViewPaintable} is used as a temporary layer.
126 */
127 public boolean isTemporaryLayer() {
128 return temporaryLayer;
129 }
130
131 @Override
132 public String toString() {
133 return "AttachToMapViewEvent [mapView=" + mapView + ", temporaryLayer=" + temporaryLayer + "]";
134 }
135 }
136
137 /**
138 * Paint the dataset using the engine set.
139 * @param g Graphics
140 * @param mv The object that can translate GeoPoints to screen coordinates.
141 * @param bbox Bounding box
142 */
143 void paint(Graphics2D g, MapView mv, Bounds bbox);
144
145 /**
146 * Adds a new paintable invalidation listener.
147 * @param l The listener to add.
148 * @since 12107
149 */
150 default void addInvalidationListener(PaintableInvalidationListener l) {
151 }
152
153 /**
154 * Removes an added paintable invalidation listener. May throw an exception if the listener is added twice.
155 * @param l The listener to remove.
156 * @since 12107
157 */
158 default void removeInvalidationListener(PaintableInvalidationListener l) {
159 }
160
161}
Note: See TracBrowser for help on using the repository browser.