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