1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui;
|
---|
3 |
|
---|
4 | import java.awt.AlphaComposite;
|
---|
5 | import java.awt.Color;
|
---|
6 | import java.awt.Dimension;
|
---|
7 | import java.awt.Graphics;
|
---|
8 | import java.awt.Graphics2D;
|
---|
9 | import java.awt.Point;
|
---|
10 | import java.awt.Rectangle;
|
---|
11 | import java.awt.event.ComponentAdapter;
|
---|
12 | import java.awt.event.ComponentEvent;
|
---|
13 | import java.awt.event.KeyEvent;
|
---|
14 | import java.awt.event.MouseAdapter;
|
---|
15 | import java.awt.event.MouseEvent;
|
---|
16 | import java.awt.event.MouseMotionListener;
|
---|
17 | import java.awt.geom.Area;
|
---|
18 | import java.awt.image.BufferedImage;
|
---|
19 | import java.beans.PropertyChangeEvent;
|
---|
20 | import java.beans.PropertyChangeListener;
|
---|
21 | import java.util.ArrayList;
|
---|
22 | import java.util.Arrays;
|
---|
23 | import java.util.Collections;
|
---|
24 | import java.util.HashMap;
|
---|
25 | import java.util.IdentityHashMap;
|
---|
26 | import java.util.LinkedHashSet;
|
---|
27 | import java.util.List;
|
---|
28 | import java.util.Set;
|
---|
29 | import java.util.TreeSet;
|
---|
30 | import java.util.concurrent.CopyOnWriteArrayList;
|
---|
31 | import java.util.concurrent.atomic.AtomicBoolean;
|
---|
32 |
|
---|
33 | import javax.swing.AbstractButton;
|
---|
34 | import javax.swing.JComponent;
|
---|
35 | import javax.swing.JPanel;
|
---|
36 | import javax.swing.SwingUtilities;
|
---|
37 |
|
---|
38 | import org.openstreetmap.josm.Main;
|
---|
39 | import org.openstreetmap.josm.actions.mapmode.MapMode;
|
---|
40 | import org.openstreetmap.josm.data.Bounds;
|
---|
41 | import org.openstreetmap.josm.data.Preferences.PreferenceChangeEvent;
|
---|
42 | import org.openstreetmap.josm.data.Preferences.PreferenceChangedListener;
|
---|
43 | import org.openstreetmap.josm.data.ProjectionBounds;
|
---|
44 | import org.openstreetmap.josm.data.ViewportData;
|
---|
45 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
46 | import org.openstreetmap.josm.data.imagery.ImageryInfo;
|
---|
47 | import org.openstreetmap.josm.data.osm.DataSelectionListener;
|
---|
48 | import org.openstreetmap.josm.data.osm.event.SelectionEventManager;
|
---|
49 | import org.openstreetmap.josm.data.osm.visitor.paint.PaintColors;
|
---|
50 | import org.openstreetmap.josm.data.osm.visitor.paint.Rendering;
|
---|
51 | import org.openstreetmap.josm.data.osm.visitor.paint.relations.MultipolygonCache;
|
---|
52 | import org.openstreetmap.josm.gui.MapViewState.MapViewRectangle;
|
---|
53 | import org.openstreetmap.josm.gui.autofilter.AutoFilterManager;
|
---|
54 | import org.openstreetmap.josm.gui.datatransfer.OsmTransferHandler;
|
---|
55 | import org.openstreetmap.josm.gui.layer.GpxLayer;
|
---|
56 | import org.openstreetmap.josm.gui.layer.ImageryLayer;
|
---|
57 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
58 | import org.openstreetmap.josm.gui.layer.LayerManager;
|
---|
59 | import org.openstreetmap.josm.gui.layer.LayerManager.LayerAddEvent;
|
---|
60 | import org.openstreetmap.josm.gui.layer.LayerManager.LayerOrderChangeEvent;
|
---|
61 | import org.openstreetmap.josm.gui.layer.LayerManager.LayerRemoveEvent;
|
---|
62 | import org.openstreetmap.josm.gui.layer.MainLayerManager;
|
---|
63 | import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeEvent;
|
---|
64 | import org.openstreetmap.josm.gui.layer.MapViewGraphics;
|
---|
65 | import org.openstreetmap.josm.gui.layer.MapViewPaintable;
|
---|
66 | import org.openstreetmap.josm.gui.layer.MapViewPaintable.LayerPainter;
|
---|
67 | import org.openstreetmap.josm.gui.layer.MapViewPaintable.MapViewEvent;
|
---|
68 | import org.openstreetmap.josm.gui.layer.MapViewPaintable.PaintableInvalidationEvent;
|
---|
69 | import org.openstreetmap.josm.gui.layer.MapViewPaintable.PaintableInvalidationListener;
|
---|
70 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
71 | import org.openstreetmap.josm.gui.layer.geoimage.GeoImageLayer;
|
---|
72 | import org.openstreetmap.josm.gui.layer.markerlayer.PlayHeadMarker;
|
---|
73 | import org.openstreetmap.josm.gui.mappaint.MapPaintStyles;
|
---|
74 | import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.MapPaintSylesUpdateListener;
|
---|
75 | import org.openstreetmap.josm.io.audio.AudioPlayer;
|
---|
76 | import org.openstreetmap.josm.tools.JosmRuntimeException;
|
---|
77 | import org.openstreetmap.josm.tools.Logging;
|
---|
78 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
79 | import org.openstreetmap.josm.tools.Utils;
|
---|
80 | import org.openstreetmap.josm.tools.bugreport.BugReport;
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * This is a component used in the {@link MapFrame} for browsing the map. It use is to
|
---|
84 | * provide the MapMode's enough capabilities to operate.<br><br>
|
---|
85 | *
|
---|
86 | * {@code MapView} holds meta-data about the data set currently displayed, as scale level,
|
---|
87 | * center point viewed, what scrolling mode or editing mode is selected or with
|
---|
88 | * what projection the map is viewed etc..<br><br>
|
---|
89 | *
|
---|
90 | * {@code MapView} is able to administrate several layers.
|
---|
91 | *
|
---|
92 | * @author imi
|
---|
93 | */
|
---|
94 | public class MapView extends NavigatableComponent
|
---|
95 | implements PropertyChangeListener, PreferenceChangedListener,
|
---|
96 | LayerManager.LayerChangeListener, MainLayerManager.ActiveLayerChangeListener {
|
---|
97 |
|
---|
98 | static {
|
---|
99 | MapPaintStyles.addMapPaintSylesUpdateListener(new MapPaintSylesUpdateListener() {
|
---|
100 | @Override
|
---|
101 | public void mapPaintStylesUpdated() {
|
---|
102 | SwingUtilities.invokeLater(() -> {
|
---|
103 | // Trigger a repaint of all data layers
|
---|
104 | MainApplication.getLayerManager().getLayers()
|
---|
105 | .stream()
|
---|
106 | .filter(layer -> layer instanceof OsmDataLayer)
|
---|
107 | .forEach(Layer::invalidate);
|
---|
108 | });
|
---|
109 | }
|
---|
110 |
|
---|
111 | @Override
|
---|
112 | public void mapPaintStyleEntryUpdated(int index) {
|
---|
113 | mapPaintStylesUpdated();
|
---|
114 | }
|
---|
115 | });
|
---|
116 | }
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * An invalidation listener that simply calls repaint() for now.
|
---|
120 | * @author Michael Zangl
|
---|
121 | * @since 10271
|
---|
122 | */
|
---|
123 | private class LayerInvalidatedListener implements PaintableInvalidationListener {
|
---|
124 | private boolean ignoreRepaint;
|
---|
125 |
|
---|
126 | private final Set<MapViewPaintable> invalidatedLayers = Collections.newSetFromMap(new IdentityHashMap<MapViewPaintable, Boolean>());
|
---|
127 |
|
---|
128 | @Override
|
---|
129 | public void paintableInvalidated(PaintableInvalidationEvent event) {
|
---|
130 | invalidate(event.getLayer());
|
---|
131 | }
|
---|
132 |
|
---|
133 | /**
|
---|
134 | * Invalidate contents and repaint map view
|
---|
135 | * @param mapViewPaintable invalidated layer
|
---|
136 | */
|
---|
137 | public synchronized void invalidate(MapViewPaintable mapViewPaintable) {
|
---|
138 | ignoreRepaint = true;
|
---|
139 | invalidatedLayers.add(mapViewPaintable);
|
---|
140 | repaint();
|
---|
141 | }
|
---|
142 |
|
---|
143 | /**
|
---|
144 | * Temporary until all {@link MapViewPaintable}s support this.
|
---|
145 | * @param p The paintable.
|
---|
146 | */
|
---|
147 | public synchronized void addTo(MapViewPaintable p) {
|
---|
148 | p.addInvalidationListener(this);
|
---|
149 | }
|
---|
150 |
|
---|
151 | /**
|
---|
152 | * Temporary until all {@link MapViewPaintable}s support this.
|
---|
153 | * @param p The paintable.
|
---|
154 | */
|
---|
155 | public synchronized void removeFrom(MapViewPaintable p) {
|
---|
156 | p.removeInvalidationListener(this);
|
---|
157 | invalidatedLayers.remove(p);
|
---|
158 | }
|
---|
159 |
|
---|
160 | /**
|
---|
161 | * Attempts to trace repaints that did not originate from this listener. Good to find missed {@link MapView#repaint()}s in code.
|
---|
162 | */
|
---|
163 | protected synchronized void traceRandomRepaint() {
|
---|
164 | if (!ignoreRepaint) {
|
---|
165 | System.err.println("Repaint:");
|
---|
166 | Thread.dumpStack();
|
---|
167 | }
|
---|
168 | ignoreRepaint = false;
|
---|
169 | }
|
---|
170 |
|
---|
171 | /**
|
---|
172 | * Retrieves a set of all layers that have been marked as invalid since the last call to this method.
|
---|
173 | * @return The layers
|
---|
174 | */
|
---|
175 | protected synchronized Set<MapViewPaintable> collectInvalidatedLayers() {
|
---|
176 | Set<MapViewPaintable> layers = Collections.newSetFromMap(new IdentityHashMap<MapViewPaintable, Boolean>());
|
---|
177 | layers.addAll(invalidatedLayers);
|
---|
178 | invalidatedLayers.clear();
|
---|
179 | return layers;
|
---|
180 | }
|
---|
181 | }
|
---|
182 |
|
---|
183 | /**
|
---|
184 | * A layer painter that issues a warning when being called.
|
---|
185 | * @author Michael Zangl
|
---|
186 | * @since 10474
|
---|
187 | */
|
---|
188 | private static class WarningLayerPainter implements LayerPainter {
|
---|
189 | boolean warningPrinted;
|
---|
190 | private final Layer layer;
|
---|
191 |
|
---|
192 | WarningLayerPainter(Layer layer) {
|
---|
193 | this.layer = layer;
|
---|
194 | }
|
---|
195 |
|
---|
196 | @Override
|
---|
197 | public void paint(MapViewGraphics graphics) {
|
---|
198 | if (!warningPrinted) {
|
---|
199 | Logging.debug("A layer triggered a repaint while being added: " + layer);
|
---|
200 | warningPrinted = true;
|
---|
201 | }
|
---|
202 | }
|
---|
203 |
|
---|
204 | @Override
|
---|
205 | public void detachFromMapView(MapViewEvent event) {
|
---|
206 | // ignored
|
---|
207 | }
|
---|
208 | }
|
---|
209 |
|
---|
210 | /**
|
---|
211 | * A list of all layers currently loaded. If we support multiple map views, this list may be different for each of them.
|
---|
212 | */
|
---|
213 | private final MainLayerManager layerManager;
|
---|
214 |
|
---|
215 | /**
|
---|
216 | * The play head marker: there is only one of these so it isn't in any specific layer
|
---|
217 | */
|
---|
218 | public transient PlayHeadMarker playHeadMarker;
|
---|
219 |
|
---|
220 | /**
|
---|
221 | * The last event performed by mouse.
|
---|
222 | */
|
---|
223 | public MouseEvent lastMEvent = new MouseEvent(this, 0, 0, 0, 0, 0, 0, false); // In case somebody reads it before first mouse move
|
---|
224 |
|
---|
225 | /**
|
---|
226 | * Temporary layers (selection rectangle, etc.) that are never cached and
|
---|
227 | * drawn on top of regular layers.
|
---|
228 | * Access must be synchronized.
|
---|
229 | */
|
---|
230 | private final transient Set<MapViewPaintable> temporaryLayers = new LinkedHashSet<>();
|
---|
231 |
|
---|
232 | private transient BufferedImage nonChangedLayersBuffer;
|
---|
233 | private transient BufferedImage offscreenBuffer;
|
---|
234 | // Layers that wasn't changed since last paint
|
---|
235 | private final transient List<Layer> nonChangedLayers = new ArrayList<>();
|
---|
236 | private int lastViewID;
|
---|
237 | private final AtomicBoolean paintPreferencesChanged = new AtomicBoolean(true);
|
---|
238 | private Rectangle lastClipBounds = new Rectangle();
|
---|
239 | private transient MapMover mapMover;
|
---|
240 |
|
---|
241 | /**
|
---|
242 | * The listener that listens to invalidations of all layers.
|
---|
243 | */
|
---|
244 | private final LayerInvalidatedListener invalidatedListener = new LayerInvalidatedListener();
|
---|
245 |
|
---|
246 | /**
|
---|
247 | * This is a map of all Layers that have been added to this view.
|
---|
248 | */
|
---|
249 | private final HashMap<Layer, LayerPainter> registeredLayers = new HashMap<>();
|
---|
250 |
|
---|
251 | /**
|
---|
252 | * Constructs a new {@code MapView}.
|
---|
253 | * @param layerManager The layers to display.
|
---|
254 | * @param contentPane Ignored. Main content pane is used.
|
---|
255 | * @param viewportData the initial viewport of the map. Can be null, then
|
---|
256 | * the viewport is derived from the layer data.
|
---|
257 | * @since 10279
|
---|
258 | * @deprecated use {@link #MapView(MainLayerManager, ViewportData)} instead
|
---|
259 | */
|
---|
260 | @Deprecated
|
---|
261 | public MapView(MainLayerManager layerManager, final JPanel contentPane, final ViewportData viewportData) {
|
---|
262 | this(layerManager, viewportData);
|
---|
263 | }
|
---|
264 |
|
---|
265 | /**
|
---|
266 | * Constructs a new {@code MapView}.
|
---|
267 | * @param layerManager The layers to display.
|
---|
268 | * @param viewportData the initial viewport of the map. Can be null, then
|
---|
269 | * the viewport is derived from the layer data.
|
---|
270 | * @since 11713
|
---|
271 | */
|
---|
272 | public MapView(MainLayerManager layerManager, final ViewportData viewportData) {
|
---|
273 | this.layerManager = layerManager;
|
---|
274 | initialViewport = viewportData;
|
---|
275 | layerManager.addAndFireLayerChangeListener(this);
|
---|
276 | layerManager.addActiveLayerChangeListener(this);
|
---|
277 | Main.pref.addPreferenceChangeListener(this);
|
---|
278 |
|
---|
279 | addComponentListener(new ComponentAdapter() {
|
---|
280 | @Override
|
---|
281 | public void componentResized(ComponentEvent e) {
|
---|
282 | removeComponentListener(this);
|
---|
283 | mapMover = new MapMover(MapView.this);
|
---|
284 | }
|
---|
285 | });
|
---|
286 |
|
---|
287 | // listens to selection changes to redraw the map
|
---|
288 | SelectionEventManager.getInstance().addSelectionListenerForEdt(repaintSelectionChangedListener);
|
---|
289 |
|
---|
290 | //store the last mouse action
|
---|
291 | this.addMouseMotionListener(new MouseMotionListener() {
|
---|
292 | @Override
|
---|
293 | public void mouseDragged(MouseEvent e) {
|
---|
294 | mouseMoved(e);
|
---|
295 | }
|
---|
296 |
|
---|
297 | @Override
|
---|
298 | public void mouseMoved(MouseEvent e) {
|
---|
299 | lastMEvent = e;
|
---|
300 | }
|
---|
301 | });
|
---|
302 | this.addMouseListener(new MouseAdapter() {
|
---|
303 | @Override
|
---|
304 | public void mousePressed(MouseEvent me) {
|
---|
305 | // focus the MapView component when mouse is pressed inside it
|
---|
306 | requestFocus();
|
---|
307 | }
|
---|
308 | });
|
---|
309 |
|
---|
310 | setFocusTraversalKeysEnabled(!Shortcut.findShortcut(KeyEvent.VK_TAB, 0).isPresent());
|
---|
311 |
|
---|
312 | for (JComponent c : getMapNavigationComponents(this)) {
|
---|
313 | add(c);
|
---|
314 | }
|
---|
315 | if (AutoFilterManager.PROP_AUTO_FILTER_ENABLED.get()) {
|
---|
316 | AutoFilterManager.getInstance().enableAutoFilterRule(AutoFilterManager.PROP_AUTO_FILTER_RULE.get());
|
---|
317 | }
|
---|
318 | setTransferHandler(new OsmTransferHandler());
|
---|
319 | }
|
---|
320 |
|
---|
321 | /**
|
---|
322 | * Adds the map navigation components to a
|
---|
323 | * @param forMapView The map view to get the components for.
|
---|
324 | * @return A list containing the correctly positioned map navigation components.
|
---|
325 | */
|
---|
326 | public static List<? extends JComponent> getMapNavigationComponents(MapView forMapView) {
|
---|
327 | MapSlider zoomSlider = new MapSlider(forMapView);
|
---|
328 | Dimension size = zoomSlider.getPreferredSize();
|
---|
329 | zoomSlider.setSize(size);
|
---|
330 | zoomSlider.setLocation(3, 0);
|
---|
331 | zoomSlider.setFocusTraversalKeysEnabled(!Shortcut.findShortcut(KeyEvent.VK_TAB, 0).isPresent());
|
---|
332 |
|
---|
333 | MapScaler scaler = new MapScaler(forMapView);
|
---|
334 | scaler.setPreferredLineLength(size.width - 10);
|
---|
335 | scaler.setSize(scaler.getPreferredSize());
|
---|
336 | scaler.setLocation(3, size.height);
|
---|
337 |
|
---|
338 | return Arrays.asList(zoomSlider, scaler);
|
---|
339 | }
|
---|
340 |
|
---|
341 | // remebered geometry of the component
|
---|
342 | private Dimension oldSize;
|
---|
343 | private Point oldLoc;
|
---|
344 |
|
---|
345 | /**
|
---|
346 | * Call this method to keep map position on screen during next repaint
|
---|
347 | */
|
---|
348 | public void rememberLastPositionOnScreen() {
|
---|
349 | oldSize = getSize();
|
---|
350 | oldLoc = getLocationOnScreen();
|
---|
351 | }
|
---|
352 |
|
---|
353 | @Override
|
---|
354 | public void layerAdded(LayerAddEvent e) {
|
---|
355 | try {
|
---|
356 | Layer layer = e.getAddedLayer();
|
---|
357 | registeredLayers.put(layer, new WarningLayerPainter(layer));
|
---|
358 | // Layers may trigger a redraw during this call if they open dialogs.
|
---|
359 | LayerPainter painter = layer.attachToMapView(new MapViewEvent(this, false));
|
---|
360 | if (!registeredLayers.containsKey(layer)) {
|
---|
361 | // The layer may have removed itself during attachToMapView()
|
---|
362 | Logging.warn("Layer was removed during attachToMapView()");
|
---|
363 | } else {
|
---|
364 | registeredLayers.put(layer, painter);
|
---|
365 |
|
---|
366 | if (e.isZoomRequired()) {
|
---|
367 | ProjectionBounds viewProjectionBounds = layer.getViewProjectionBounds();
|
---|
368 | if (viewProjectionBounds != null) {
|
---|
369 | scheduleZoomTo(new ViewportData(viewProjectionBounds));
|
---|
370 | }
|
---|
371 | }
|
---|
372 |
|
---|
373 | layer.addPropertyChangeListener(this);
|
---|
374 | Main.addProjectionChangeListener(layer);
|
---|
375 | invalidatedListener.addTo(layer);
|
---|
376 | AudioPlayer.reset();
|
---|
377 |
|
---|
378 | repaint();
|
---|
379 | }
|
---|
380 | } catch (JosmRuntimeException | IllegalArgumentException | IllegalStateException t) {
|
---|
381 | throw BugReport.intercept(t).put("layer", e.getAddedLayer());
|
---|
382 | }
|
---|
383 | }
|
---|
384 |
|
---|
385 | /**
|
---|
386 | * Replies true if the active data layer (edit layer) is drawable.
|
---|
387 | *
|
---|
388 | * @return true if the active data layer (edit layer) is drawable, false otherwise
|
---|
389 | */
|
---|
390 | public boolean isActiveLayerDrawable() {
|
---|
391 | return layerManager.getEditLayer() != null;
|
---|
392 | }
|
---|
393 |
|
---|
394 | /**
|
---|
395 | * Replies true if the active data layer (edit layer) is visible.
|
---|
396 | *
|
---|
397 | * @return true if the active data layer (edit layer) is visible, false otherwise
|
---|
398 | */
|
---|
399 | public boolean isActiveLayerVisible() {
|
---|
400 | OsmDataLayer e = layerManager.getEditLayer();
|
---|
401 | return e != null && e.isVisible();
|
---|
402 | }
|
---|
403 |
|
---|
404 | @Override
|
---|
405 | public void layerRemoving(LayerRemoveEvent e) {
|
---|
406 | Layer layer = e.getRemovedLayer();
|
---|
407 |
|
---|
408 | LayerPainter painter = registeredLayers.remove(layer);
|
---|
409 | if (painter == null) {
|
---|
410 | Logging.error("The painter for layer " + layer + " was not registered.");
|
---|
411 | return;
|
---|
412 | }
|
---|
413 | painter.detachFromMapView(new MapViewEvent(this, false));
|
---|
414 | Main.removeProjectionChangeListener(layer);
|
---|
415 | layer.removePropertyChangeListener(this);
|
---|
416 | invalidatedListener.removeFrom(layer);
|
---|
417 | layer.destroy();
|
---|
418 | AudioPlayer.reset();
|
---|
419 |
|
---|
420 | repaint();
|
---|
421 | }
|
---|
422 |
|
---|
423 | private boolean virtualNodesEnabled;
|
---|
424 |
|
---|
425 | /**
|
---|
426 | * Enables or disables drawing of the virtual nodes.
|
---|
427 | * @param enabled if virtual nodes are enabled
|
---|
428 | */
|
---|
429 | public void setVirtualNodesEnabled(boolean enabled) {
|
---|
430 | if (virtualNodesEnabled != enabled) {
|
---|
431 | virtualNodesEnabled = enabled;
|
---|
432 | repaint();
|
---|
433 | }
|
---|
434 | }
|
---|
435 |
|
---|
436 | /**
|
---|
437 | * Checks if virtual nodes should be drawn. Default is <code>false</code>
|
---|
438 | * @return The virtual nodes property.
|
---|
439 | * @see Rendering#render
|
---|
440 | */
|
---|
441 | public boolean isVirtualNodesEnabled() {
|
---|
442 | return virtualNodesEnabled;
|
---|
443 | }
|
---|
444 |
|
---|
445 | /**
|
---|
446 | * Moves the layer to the given new position. No event is fired, but repaints
|
---|
447 | * according to the new Z-Order of the layers.
|
---|
448 | *
|
---|
449 | * @param layer The layer to move
|
---|
450 | * @param pos The new position of the layer
|
---|
451 | */
|
---|
452 | public void moveLayer(Layer layer, int pos) {
|
---|
453 | layerManager.moveLayer(layer, pos);
|
---|
454 | }
|
---|
455 |
|
---|
456 | @Override
|
---|
457 | public void layerOrderChanged(LayerOrderChangeEvent e) {
|
---|
458 | AudioPlayer.reset();
|
---|
459 | repaint();
|
---|
460 | }
|
---|
461 |
|
---|
462 | /**
|
---|
463 | * Paints the given layer to the graphics object, using the current state of this map view.
|
---|
464 | * @param layer The layer to draw.
|
---|
465 | * @param g A graphics object. It should have the width and height of this component
|
---|
466 | * @throws IllegalArgumentException If the layer is not part of this map view.
|
---|
467 | * @since 11226
|
---|
468 | */
|
---|
469 | public void paintLayer(Layer layer, Graphics2D g) {
|
---|
470 | try {
|
---|
471 | LayerPainter painter = registeredLayers.get(layer);
|
---|
472 | if (painter == null) {
|
---|
473 | Logging.warn("Cannot paint layer, it is not registered: {0}", layer);
|
---|
474 | return;
|
---|
475 | }
|
---|
476 | MapViewRectangle clipBounds = getState().getViewArea(g.getClipBounds());
|
---|
477 | MapViewGraphics paintGraphics = new MapViewGraphics(this, g, clipBounds);
|
---|
478 |
|
---|
479 | if (layer.getOpacity() < 1) {
|
---|
480 | g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float) layer.getOpacity()));
|
---|
481 | }
|
---|
482 | painter.paint(paintGraphics);
|
---|
483 | g.setPaintMode();
|
---|
484 | } catch (JosmRuntimeException | IllegalArgumentException | IllegalStateException t) {
|
---|
485 | BugReport.intercept(t).put("layer", layer).warn();
|
---|
486 | }
|
---|
487 | }
|
---|
488 |
|
---|
489 | /**
|
---|
490 | * Draw the component.
|
---|
491 | */
|
---|
492 | @Override
|
---|
493 | public void paint(Graphics g) {
|
---|
494 | try {
|
---|
495 | if (!prepareToDraw()) {
|
---|
496 | return;
|
---|
497 | }
|
---|
498 | } catch (JosmRuntimeException | IllegalArgumentException | IllegalStateException e) {
|
---|
499 | BugReport.intercept(e).put("center", this::getCenter).warn();
|
---|
500 | return;
|
---|
501 | }
|
---|
502 |
|
---|
503 | try {
|
---|
504 | drawMapContent(g);
|
---|
505 | } catch (JosmRuntimeException | IllegalArgumentException | IllegalStateException e) {
|
---|
506 | throw BugReport.intercept(e).put("visibleLayers", layerManager::getVisibleLayersInZOrder)
|
---|
507 | .put("temporaryLayers", temporaryLayers);
|
---|
508 | }
|
---|
509 | super.paint(g);
|
---|
510 | }
|
---|
511 |
|
---|
512 | private void drawMapContent(Graphics g) {
|
---|
513 | List<Layer> visibleLayers = layerManager.getVisibleLayersInZOrder();
|
---|
514 |
|
---|
515 | int nonChangedLayersCount = 0;
|
---|
516 | Set<MapViewPaintable> invalidated = invalidatedListener.collectInvalidatedLayers();
|
---|
517 | for (Layer l: visibleLayers) {
|
---|
518 | if (invalidated.contains(l)) {
|
---|
519 | break;
|
---|
520 | } else {
|
---|
521 | nonChangedLayersCount++;
|
---|
522 | }
|
---|
523 | }
|
---|
524 |
|
---|
525 | boolean canUseBuffer = !paintPreferencesChanged.getAndSet(false)
|
---|
526 | && nonChangedLayers.size() <= nonChangedLayersCount
|
---|
527 | && lastViewID == getViewID()
|
---|
528 | && lastClipBounds.contains(g.getClipBounds())
|
---|
529 | && nonChangedLayers.equals(visibleLayers.subList(0, nonChangedLayers.size()));
|
---|
530 |
|
---|
531 | if (null == offscreenBuffer || offscreenBuffer.getWidth() != getWidth() || offscreenBuffer.getHeight() != getHeight()) {
|
---|
532 | offscreenBuffer = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_3BYTE_BGR);
|
---|
533 | }
|
---|
534 |
|
---|
535 | Graphics2D tempG = offscreenBuffer.createGraphics();
|
---|
536 | tempG.setClip(g.getClip());
|
---|
537 |
|
---|
538 | if (!canUseBuffer || nonChangedLayersBuffer == null) {
|
---|
539 | if (null == nonChangedLayersBuffer
|
---|
540 | || nonChangedLayersBuffer.getWidth() != getWidth() || nonChangedLayersBuffer.getHeight() != getHeight()) {
|
---|
541 | nonChangedLayersBuffer = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_3BYTE_BGR);
|
---|
542 | }
|
---|
543 | Graphics2D g2 = nonChangedLayersBuffer.createGraphics();
|
---|
544 | g2.setClip(g.getClip());
|
---|
545 | g2.setColor(PaintColors.getBackgroundColor());
|
---|
546 | g2.fillRect(0, 0, getWidth(), getHeight());
|
---|
547 |
|
---|
548 | for (int i = 0; i < nonChangedLayersCount; i++) {
|
---|
549 | paintLayer(visibleLayers.get(i), g2);
|
---|
550 | }
|
---|
551 | } else {
|
---|
552 | // Maybe there were more unchanged layers then last time - draw them to buffer
|
---|
553 | if (nonChangedLayers.size() != nonChangedLayersCount) {
|
---|
554 | Graphics2D g2 = nonChangedLayersBuffer.createGraphics();
|
---|
555 | g2.setClip(g.getClip());
|
---|
556 | for (int i = nonChangedLayers.size(); i < nonChangedLayersCount; i++) {
|
---|
557 | paintLayer(visibleLayers.get(i), g2);
|
---|
558 | }
|
---|
559 | }
|
---|
560 | }
|
---|
561 |
|
---|
562 | nonChangedLayers.clear();
|
---|
563 | nonChangedLayers.addAll(visibleLayers.subList(0, nonChangedLayersCount));
|
---|
564 | lastViewID = getViewID();
|
---|
565 | lastClipBounds = g.getClipBounds();
|
---|
566 |
|
---|
567 | tempG.drawImage(nonChangedLayersBuffer, 0, 0, null);
|
---|
568 |
|
---|
569 | for (int i = nonChangedLayersCount; i < visibleLayers.size(); i++) {
|
---|
570 | paintLayer(visibleLayers.get(i), tempG);
|
---|
571 | }
|
---|
572 |
|
---|
573 | try {
|
---|
574 | drawTemporaryLayers(tempG, getLatLonBounds(g.getClipBounds()));
|
---|
575 | } catch (JosmRuntimeException | IllegalArgumentException | IllegalStateException e) {
|
---|
576 | BugReport.intercept(e).put("temporaryLayers", temporaryLayers).warn();
|
---|
577 | }
|
---|
578 |
|
---|
579 | // draw world borders
|
---|
580 | try {
|
---|
581 | drawWorldBorders(tempG);
|
---|
582 | } catch (JosmRuntimeException | IllegalArgumentException | IllegalStateException e) {
|
---|
583 | // getProjection() needs to be inside lambda to catch errors.
|
---|
584 | BugReport.intercept(e).put("bounds", () -> getProjection().getWorldBoundsLatLon()).warn();
|
---|
585 | }
|
---|
586 |
|
---|
587 | MapFrame map = MainApplication.getMap();
|
---|
588 | if (AutoFilterManager.getInstance().getCurrentAutoFilter() != null) {
|
---|
589 | AutoFilterManager.getInstance().drawOSDText(tempG);
|
---|
590 | } else if (MainApplication.isDisplayingMapView() && map.filterDialog != null) {
|
---|
591 | map.filterDialog.drawOSDText(tempG);
|
---|
592 | }
|
---|
593 |
|
---|
594 | if (playHeadMarker != null) {
|
---|
595 | playHeadMarker.paint(tempG, this);
|
---|
596 | }
|
---|
597 |
|
---|
598 | try {
|
---|
599 | g.drawImage(offscreenBuffer, 0, 0, null);
|
---|
600 | } catch (ClassCastException e) {
|
---|
601 | // See #11002 and duplicate tickets. On Linux with Java >= 8 Many users face this error here:
|
---|
602 | //
|
---|
603 | // java.lang.ClassCastException: sun.awt.image.BufImgSurfaceData cannot be cast to sun.java2d.xr.XRSurfaceData
|
---|
604 | // at sun.java2d.xr.XRPMBlitLoops.cacheToTmpSurface(XRPMBlitLoops.java:145)
|
---|
605 | // at sun.java2d.xr.XrSwToPMBlit.Blit(XRPMBlitLoops.java:353)
|
---|
606 | // at sun.java2d.pipe.DrawImage.blitSurfaceData(DrawImage.java:959)
|
---|
607 | // at sun.java2d.pipe.DrawImage.renderImageCopy(DrawImage.java:577)
|
---|
608 | // at sun.java2d.pipe.DrawImage.copyImage(DrawImage.java:67)
|
---|
609 | // at sun.java2d.pipe.DrawImage.copyImage(DrawImage.java:1014)
|
---|
610 | // at sun.java2d.pipe.ValidatePipe.copyImage(ValidatePipe.java:186)
|
---|
611 | // at sun.java2d.SunGraphics2D.drawImage(SunGraphics2D.java:3318)
|
---|
612 | // at sun.java2d.SunGraphics2D.drawImage(SunGraphics2D.java:3296)
|
---|
613 | // at org.openstreetmap.josm.gui.MapView.paint(MapView.java:834)
|
---|
614 | //
|
---|
615 | // It seems to be this JDK bug, but Oracle does not seem to be fixing it:
|
---|
616 | // https://bugs.openjdk.java.net/browse/JDK-7172749
|
---|
617 | //
|
---|
618 | // According to bug reports it can happen for a variety of reasons such as:
|
---|
619 | // - long period of time
|
---|
620 | // - change of screen resolution
|
---|
621 | // - addition/removal of a secondary monitor
|
---|
622 | //
|
---|
623 | // But the application seems to work fine after, so let's just log the error
|
---|
624 | Logging.error(e);
|
---|
625 | }
|
---|
626 | }
|
---|
627 |
|
---|
628 | private void drawTemporaryLayers(Graphics2D tempG, Bounds box) {
|
---|
629 | synchronized (temporaryLayers) {
|
---|
630 | for (MapViewPaintable mvp : temporaryLayers) {
|
---|
631 | try {
|
---|
632 | mvp.paint(tempG, this, box);
|
---|
633 | } catch (JosmRuntimeException | IllegalArgumentException | IllegalStateException e) {
|
---|
634 | throw BugReport.intercept(e).put("mvp", mvp);
|
---|
635 | }
|
---|
636 | }
|
---|
637 | }
|
---|
638 | }
|
---|
639 |
|
---|
640 | private void drawWorldBorders(Graphics2D tempG) {
|
---|
641 | tempG.setColor(Color.WHITE);
|
---|
642 | Bounds b = getProjection().getWorldBoundsLatLon();
|
---|
643 |
|
---|
644 | int w = getWidth();
|
---|
645 | int h = getHeight();
|
---|
646 |
|
---|
647 | // Work around OpenJDK having problems when drawing out of bounds
|
---|
648 | final Area border = getState().getArea(b);
|
---|
649 | // Make the viewport 1px larger in every direction to prevent an
|
---|
650 | // additional 1px border when zooming in
|
---|
651 | final Area viewport = new Area(new Rectangle(-1, -1, w + 2, h + 2));
|
---|
652 | border.intersect(viewport);
|
---|
653 | tempG.draw(border);
|
---|
654 | }
|
---|
655 |
|
---|
656 | /**
|
---|
657 | * Sets up the viewport to prepare for drawing the view.
|
---|
658 | * @return <code>true</code> if the view can be drawn, <code>false</code> otherwise.
|
---|
659 | */
|
---|
660 | public boolean prepareToDraw() {
|
---|
661 | updateLocationState();
|
---|
662 | if (initialViewport != null) {
|
---|
663 | zoomTo(initialViewport);
|
---|
664 | initialViewport = null;
|
---|
665 | }
|
---|
666 |
|
---|
667 | if (getCenter() == null)
|
---|
668 | return false; // no data loaded yet.
|
---|
669 |
|
---|
670 | // if the position was remembered, we need to adjust center once before repainting
|
---|
671 | if (oldLoc != null && oldSize != null) {
|
---|
672 | Point l1 = getLocationOnScreen();
|
---|
673 | final EastNorth newCenter = new EastNorth(
|
---|
674 | getCenter().getX()+ (l1.x-oldLoc.x - (oldSize.width-getWidth())/2.0)*getScale(),
|
---|
675 | getCenter().getY()+ (oldLoc.y-l1.y + (oldSize.height-getHeight())/2.0)*getScale()
|
---|
676 | );
|
---|
677 | oldLoc = null; oldSize = null;
|
---|
678 | zoomTo(newCenter);
|
---|
679 | }
|
---|
680 |
|
---|
681 | return true;
|
---|
682 | }
|
---|
683 |
|
---|
684 | @Override
|
---|
685 | public void activeOrEditLayerChanged(ActiveLayerChangeEvent e) {
|
---|
686 | MapFrame map = MainApplication.getMap();
|
---|
687 | if (map != null) {
|
---|
688 | /* This only makes the buttons look disabled. Disabling the actions as well requires
|
---|
689 | * the user to re-select the tool after i.e. moving a layer. While testing I found
|
---|
690 | * that I switch layers and actions at the same time and it was annoying to mind the
|
---|
691 | * order. This way it works as visual clue for new users */
|
---|
692 | // FIXME: This does not belong here.
|
---|
693 | for (final AbstractButton b: map.allMapModeButtons) {
|
---|
694 | MapMode mode = (MapMode) b.getAction();
|
---|
695 | final boolean activeLayerSupported = mode.layerIsSupported(layerManager.getActiveLayer());
|
---|
696 | if (activeLayerSupported) {
|
---|
697 | MainApplication.registerActionShortcut(mode, mode.getShortcut()); //fix #6876
|
---|
698 | } else {
|
---|
699 | MainApplication.unregisterShortcut(mode.getShortcut());
|
---|
700 | }
|
---|
701 | b.setEnabled(activeLayerSupported);
|
---|
702 | }
|
---|
703 | }
|
---|
704 | // invalidate repaint cache. The layer order may have changed by this, so we invalidate every layer
|
---|
705 | getLayerManager().getLayers().forEach(invalidatedListener::invalidate);
|
---|
706 | AudioPlayer.reset();
|
---|
707 | }
|
---|
708 |
|
---|
709 | /**
|
---|
710 | * Adds a new temporary layer.
|
---|
711 | * <p>
|
---|
712 | * A temporary layer is a layer that is painted above all normal layers. Layers are painted in the order they are added.
|
---|
713 | *
|
---|
714 | * @param mvp The layer to paint.
|
---|
715 | * @return <code>true</code> if the layer was added.
|
---|
716 | */
|
---|
717 | public boolean addTemporaryLayer(MapViewPaintable mvp) {
|
---|
718 | synchronized (temporaryLayers) {
|
---|
719 | boolean added = temporaryLayers.add(mvp);
|
---|
720 | if (added) {
|
---|
721 | invalidatedListener.addTo(mvp);
|
---|
722 | }
|
---|
723 | repaint();
|
---|
724 | return added;
|
---|
725 | }
|
---|
726 | }
|
---|
727 |
|
---|
728 | /**
|
---|
729 | * Removes a layer previously added as temporary layer.
|
---|
730 | * @param mvp The layer to remove.
|
---|
731 | * @return <code>true</code> if that layer was removed.
|
---|
732 | */
|
---|
733 | public boolean removeTemporaryLayer(MapViewPaintable mvp) {
|
---|
734 | synchronized (temporaryLayers) {
|
---|
735 | boolean removed = temporaryLayers.remove(mvp);
|
---|
736 | if (removed) {
|
---|
737 | invalidatedListener.removeFrom(mvp);
|
---|
738 | }
|
---|
739 | repaint();
|
---|
740 | return removed;
|
---|
741 | }
|
---|
742 | }
|
---|
743 |
|
---|
744 | /**
|
---|
745 | * Gets a list of temporary layers.
|
---|
746 | * @return The layers in the order they are added.
|
---|
747 | */
|
---|
748 | public List<MapViewPaintable> getTemporaryLayers() {
|
---|
749 | synchronized (temporaryLayers) {
|
---|
750 | return Collections.unmodifiableList(new ArrayList<>(temporaryLayers));
|
---|
751 | }
|
---|
752 | }
|
---|
753 |
|
---|
754 | @Override
|
---|
755 | public void propertyChange(PropertyChangeEvent evt) {
|
---|
756 | if (evt.getPropertyName().equals(Layer.VISIBLE_PROP)) {
|
---|
757 | repaint();
|
---|
758 | } else if (evt.getPropertyName().equals(Layer.OPACITY_PROP) ||
|
---|
759 | evt.getPropertyName().equals(Layer.FILTER_STATE_PROP)) {
|
---|
760 | Layer l = (Layer) evt.getSource();
|
---|
761 | if (l.isVisible()) {
|
---|
762 | invalidatedListener.invalidate(l);
|
---|
763 | }
|
---|
764 | }
|
---|
765 | }
|
---|
766 |
|
---|
767 | @Override
|
---|
768 | public void preferenceChanged(PreferenceChangeEvent e) {
|
---|
769 | paintPreferencesChanged.set(true);
|
---|
770 | }
|
---|
771 |
|
---|
772 | private final transient DataSelectionListener repaintSelectionChangedListener = event -> repaint();
|
---|
773 |
|
---|
774 | /**
|
---|
775 | * Destroy this map view panel. Should be called once when it is not needed any more.
|
---|
776 | */
|
---|
777 | public void destroy() {
|
---|
778 | layerManager.removeAndFireLayerChangeListener(this);
|
---|
779 | layerManager.removeActiveLayerChangeListener(this);
|
---|
780 | Main.pref.removePreferenceChangeListener(this);
|
---|
781 | SelectionEventManager.getInstance().removeSelectionListener(repaintSelectionChangedListener);
|
---|
782 | MultipolygonCache.getInstance().clear();
|
---|
783 | if (mapMover != null) {
|
---|
784 | mapMover.destroy();
|
---|
785 | }
|
---|
786 | nonChangedLayers.clear();
|
---|
787 | synchronized (temporaryLayers) {
|
---|
788 | temporaryLayers.clear();
|
---|
789 | }
|
---|
790 | nonChangedLayersBuffer = null;
|
---|
791 | offscreenBuffer = null;
|
---|
792 | }
|
---|
793 |
|
---|
794 | /**
|
---|
795 | * Get a string representation of all layers suitable for the {@code source} changeset tag.
|
---|
796 | * @return A String of sources separated by ';'
|
---|
797 | */
|
---|
798 | public String getLayerInformationForSourceTag() {
|
---|
799 | final Set<String> layerInfo = new TreeSet<>();
|
---|
800 | if (!layerManager.getLayersOfType(GpxLayer.class).isEmpty()) {
|
---|
801 | // no i18n for international values
|
---|
802 | layerInfo.add("survey");
|
---|
803 | }
|
---|
804 | for (final GeoImageLayer i : layerManager.getLayersOfType(GeoImageLayer.class)) {
|
---|
805 | if (i.isVisible()) {
|
---|
806 | layerInfo.add(i.getName());
|
---|
807 | }
|
---|
808 | }
|
---|
809 | for (final ImageryLayer i : layerManager.getLayersOfType(ImageryLayer.class)) {
|
---|
810 | if (i.isVisible()) {
|
---|
811 | layerInfo.add(ImageryInfo.ImageryType.BING.equals(i.getInfo().getImageryType()) ? "Bing" : i.getName());
|
---|
812 | }
|
---|
813 | }
|
---|
814 | return Utils.join("; ", layerInfo);
|
---|
815 | }
|
---|
816 |
|
---|
817 | /**
|
---|
818 | * This is a listener that gets informed whenever repaint is called for this MapView.
|
---|
819 | * <p>
|
---|
820 | * This is the only safe method to find changes to the map view, since many components call MapView.repaint() directly.
|
---|
821 | * @author Michael Zangl
|
---|
822 | * @since 10600 (functional interface)
|
---|
823 | */
|
---|
824 | @FunctionalInterface
|
---|
825 | public interface RepaintListener {
|
---|
826 | /**
|
---|
827 | * Called when any repaint method is called (using default arguments if required).
|
---|
828 | * @param tm see {@link JComponent#repaint(long, int, int, int, int)}
|
---|
829 | * @param x see {@link JComponent#repaint(long, int, int, int, int)}
|
---|
830 | * @param y see {@link JComponent#repaint(long, int, int, int, int)}
|
---|
831 | * @param width see {@link JComponent#repaint(long, int, int, int, int)}
|
---|
832 | * @param height see {@link JComponent#repaint(long, int, int, int, int)}
|
---|
833 | */
|
---|
834 | void repaint(long tm, int x, int y, int width, int height);
|
---|
835 | }
|
---|
836 |
|
---|
837 | private final transient CopyOnWriteArrayList<RepaintListener> repaintListeners = new CopyOnWriteArrayList<>();
|
---|
838 |
|
---|
839 | /**
|
---|
840 | * Adds a listener that gets informed whenever repaint() is called for this class.
|
---|
841 | * @param l The listener.
|
---|
842 | */
|
---|
843 | public void addRepaintListener(RepaintListener l) {
|
---|
844 | repaintListeners.add(l);
|
---|
845 | }
|
---|
846 |
|
---|
847 | /**
|
---|
848 | * Removes a registered repaint listener.
|
---|
849 | * @param l The listener.
|
---|
850 | */
|
---|
851 | public void removeRepaintListener(RepaintListener l) {
|
---|
852 | repaintListeners.remove(l);
|
---|
853 | }
|
---|
854 |
|
---|
855 | @Override
|
---|
856 | public void repaint(long tm, int x, int y, int width, int height) {
|
---|
857 | // This is the main repaint method, all other methods are convenience methods and simply call this method.
|
---|
858 | // This is just an observation, not a must, but seems to be true for all implementations I found so far.
|
---|
859 | if (repaintListeners != null) {
|
---|
860 | // Might get called early in super constructor
|
---|
861 | for (RepaintListener l : repaintListeners) {
|
---|
862 | l.repaint(tm, x, y, width, height);
|
---|
863 | }
|
---|
864 | }
|
---|
865 | super.repaint(tm, x, y, width, height);
|
---|
866 | }
|
---|
867 |
|
---|
868 | @Override
|
---|
869 | public void repaint() {
|
---|
870 | if (Logging.isTraceEnabled()) {
|
---|
871 | invalidatedListener.traceRandomRepaint();
|
---|
872 | }
|
---|
873 | super.repaint();
|
---|
874 | }
|
---|
875 |
|
---|
876 | /**
|
---|
877 | * Returns the layer manager.
|
---|
878 | * @return the layer manager
|
---|
879 | * @since 10282
|
---|
880 | */
|
---|
881 | public final MainLayerManager getLayerManager() {
|
---|
882 | return layerManager;
|
---|
883 | }
|
---|
884 |
|
---|
885 | /**
|
---|
886 | * Schedule a zoom to the given position on the next redraw.
|
---|
887 | * Temporary, may be removed without warning.
|
---|
888 | * @param viewportData the viewport to zoom to
|
---|
889 | * @since 10394
|
---|
890 | */
|
---|
891 | public void scheduleZoomTo(ViewportData viewportData) {
|
---|
892 | initialViewport = viewportData;
|
---|
893 | }
|
---|
894 | }
|
---|