1 | // License: GPL. See LICENSE file for details.
|
---|
2 |
|
---|
3 | package org.openstreetmap.josm.gui;
|
---|
4 |
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 |
|
---|
7 | import java.awt.Color;
|
---|
8 | import java.awt.Graphics;
|
---|
9 | import java.awt.Graphics2D;
|
---|
10 | import java.awt.Point;
|
---|
11 | import java.awt.Rectangle;
|
---|
12 | import java.awt.event.ComponentAdapter;
|
---|
13 | import java.awt.event.ComponentEvent;
|
---|
14 | import java.awt.event.MouseEvent;
|
---|
15 | import java.awt.event.MouseMotionListener;
|
---|
16 | import java.awt.geom.Area;
|
---|
17 | import java.awt.geom.GeneralPath;
|
---|
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.Collection;
|
---|
23 | import java.util.Collections;
|
---|
24 | import java.util.Comparator;
|
---|
25 | import java.util.Enumeration;
|
---|
26 | import java.util.LinkedList;
|
---|
27 | import java.util.List;
|
---|
28 | import java.util.concurrent.CopyOnWriteArrayList;
|
---|
29 |
|
---|
30 | import javax.swing.AbstractButton;
|
---|
31 | import javax.swing.JComponent;
|
---|
32 | import javax.swing.JOptionPane;
|
---|
33 |
|
---|
34 | import org.openstreetmap.josm.Main;
|
---|
35 | import org.openstreetmap.josm.actions.AutoScaleAction;
|
---|
36 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
37 | import org.openstreetmap.josm.actions.MoveAction;
|
---|
38 | import org.openstreetmap.josm.actions.mapmode.MapMode;
|
---|
39 | import org.openstreetmap.josm.data.Bounds;
|
---|
40 | import org.openstreetmap.josm.data.SelectionChangedListener;
|
---|
41 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
42 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
43 | import org.openstreetmap.josm.data.osm.DataSource;
|
---|
44 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
45 | import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
|
---|
46 | import org.openstreetmap.josm.data.osm.visitor.paint.PaintColors;
|
---|
47 | import org.openstreetmap.josm.gui.layer.GpxLayer;
|
---|
48 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
49 | import org.openstreetmap.josm.gui.layer.MapViewPaintable;
|
---|
50 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
51 | import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
|
---|
52 | import org.openstreetmap.josm.gui.layer.markerlayer.PlayHeadMarker;
|
---|
53 | import org.openstreetmap.josm.tools.AudioPlayer;
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * This is a component used in the MapFrame for browsing the map. It use is to
|
---|
57 | * provide the MapMode's enough capabilities to operate.
|
---|
58 | *
|
---|
59 | * MapView hold meta-data about the data set currently displayed, as scale level,
|
---|
60 | * center point viewed, what scrolling mode or editing mode is selected or with
|
---|
61 | * what projection the map is viewed etc..
|
---|
62 | *
|
---|
63 | * MapView is able to administrate several layers.
|
---|
64 | *
|
---|
65 | * @author imi
|
---|
66 | */
|
---|
67 | public class MapView extends NavigatableComponent implements PropertyChangeListener {
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Interface to notify listeners of the change of the active layer.
|
---|
71 | * @author imi
|
---|
72 | */
|
---|
73 | public interface LayerChangeListener {
|
---|
74 | void activeLayerChange(Layer oldLayer, Layer newLayer);
|
---|
75 | void layerAdded(Layer newLayer);
|
---|
76 | void layerRemoved(Layer oldLayer);
|
---|
77 | }
|
---|
78 |
|
---|
79 | public interface EditLayerChangeListener {
|
---|
80 | void editLayerChanged(OsmDataLayer oldLayer, OsmDataLayer newLayer);
|
---|
81 | }
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * the layer listeners
|
---|
85 | */
|
---|
86 | private static final CopyOnWriteArrayList<MapView.LayerChangeListener> layerChangeListeners = new CopyOnWriteArrayList<MapView.LayerChangeListener>();
|
---|
87 | private static final CopyOnWriteArrayList<EditLayerChangeListener> editLayerChangeListeners = new CopyOnWriteArrayList<EditLayerChangeListener>();
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * Removes a layer change listener
|
---|
91 | *
|
---|
92 | * @param listener the listener. Ignored if null or already registered.
|
---|
93 | */
|
---|
94 | public static void removeLayerChangeListener(MapView.LayerChangeListener listener) {
|
---|
95 | layerChangeListeners.remove(listener);
|
---|
96 | }
|
---|
97 |
|
---|
98 | public static void removeEditLayerChangeListener(EditLayerChangeListener listener) {
|
---|
99 | editLayerChangeListeners.remove(listener);
|
---|
100 | }
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * Adds a layer change listener
|
---|
104 | *
|
---|
105 | * @param listener the listener. Ignored if null or already registered.
|
---|
106 | */
|
---|
107 | public static void addLayerChangeListener(MapView.LayerChangeListener listener) {
|
---|
108 | if (listener != null) {
|
---|
109 | layerChangeListeners.addIfAbsent(listener);
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 | /**
|
---|
114 | * Adds a edit layer change listener
|
---|
115 | *
|
---|
116 | * @param listener the listener. Ignored if null or already registered.
|
---|
117 | * @param initialFire Fire an edit-layer-changed-event right after adding the listener.
|
---|
118 | */
|
---|
119 | public static void addEditLayerChangeListener(EditLayerChangeListener listener, boolean initialFire) {
|
---|
120 | addEditLayerChangeListener(listener);
|
---|
121 | if (initialFire) {
|
---|
122 | if (Main.map != null && Main.map.mapView != null && Main.map.mapView.getEditLayer() != null) {
|
---|
123 | fireEditLayerChanged(null, Main.map.mapView.getEditLayer());
|
---|
124 | }
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | public static void addEditLayerChangeListener(EditLayerChangeListener listener) {
|
---|
129 | if (listener != null) {
|
---|
130 | editLayerChangeListeners.addIfAbsent(listener);
|
---|
131 | }
|
---|
132 | }
|
---|
133 |
|
---|
134 | protected static void fireActiveLayerChanged(Layer oldLayer, Layer newLayer) {
|
---|
135 | for (LayerChangeListener l : layerChangeListeners) {
|
---|
136 | l.activeLayerChange(oldLayer, newLayer);
|
---|
137 | }
|
---|
138 | }
|
---|
139 |
|
---|
140 | protected static void fireLayerAdded(Layer newLayer) {
|
---|
141 | for (MapView.LayerChangeListener l : MapView.layerChangeListeners) {
|
---|
142 | l.layerAdded(newLayer);
|
---|
143 | }
|
---|
144 | }
|
---|
145 |
|
---|
146 | protected static void fireLayerRemoved(Layer layer) {
|
---|
147 | for (MapView.LayerChangeListener l : MapView.layerChangeListeners) {
|
---|
148 | l.layerRemoved(layer);
|
---|
149 | }
|
---|
150 | }
|
---|
151 |
|
---|
152 | protected static void fireEditLayerChanged(OsmDataLayer oldLayer, OsmDataLayer newLayer) {
|
---|
153 | for (EditLayerChangeListener l : editLayerChangeListeners) {
|
---|
154 | l.editLayerChanged(oldLayer, newLayer);
|
---|
155 | }
|
---|
156 | }
|
---|
157 |
|
---|
158 | /**
|
---|
159 | * A list of all layers currently loaded.
|
---|
160 | */
|
---|
161 | private ArrayList<Layer> layers = new ArrayList<Layer>();
|
---|
162 | /**
|
---|
163 | * The play head marker: there is only one of these so it isn't in any specific layer
|
---|
164 | */
|
---|
165 | public PlayHeadMarker playHeadMarker = null;
|
---|
166 |
|
---|
167 | /**
|
---|
168 | * The layer from the layers list that is currently active.
|
---|
169 | */
|
---|
170 | private Layer activeLayer;
|
---|
171 |
|
---|
172 | private OsmDataLayer editLayer;
|
---|
173 |
|
---|
174 | /**
|
---|
175 | * The last event performed by mouse.
|
---|
176 | */
|
---|
177 | public MouseEvent lastMEvent;
|
---|
178 |
|
---|
179 | private LinkedList<MapViewPaintable> temporaryLayers = new LinkedList<MapViewPaintable>();
|
---|
180 |
|
---|
181 | private BufferedImage offscreenBuffer;
|
---|
182 |
|
---|
183 | public MapView() {
|
---|
184 | addComponentListener(new ComponentAdapter(){
|
---|
185 | @Override public void componentResized(ComponentEvent e) {
|
---|
186 | removeComponentListener(this);
|
---|
187 |
|
---|
188 | MapSlider zoomSlider = new MapSlider(MapView.this);
|
---|
189 | add(zoomSlider);
|
---|
190 | zoomSlider.setBounds(3, 0, 114, 30);
|
---|
191 |
|
---|
192 | MapScaler scaler = new MapScaler(MapView.this);
|
---|
193 | add(scaler);
|
---|
194 | scaler.setLocation(10,30);
|
---|
195 |
|
---|
196 | OsmDataLayer layer = getEditLayer();
|
---|
197 | if (layer != null) {
|
---|
198 | if (!zoomToDataSetBoundingBox(layer.data)) {
|
---|
199 | // no bounding box defined
|
---|
200 | new AutoScaleAction("data").actionPerformed(null);
|
---|
201 | }
|
---|
202 | } else {
|
---|
203 | new AutoScaleAction("layer").actionPerformed(null);
|
---|
204 | }
|
---|
205 |
|
---|
206 | new MapMover(MapView.this, Main.contentPane);
|
---|
207 | JosmAction mv;
|
---|
208 | mv = new MoveAction(MoveAction.Direction.UP);
|
---|
209 | if (mv.getShortcut() != null) {
|
---|
210 | Main.contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(mv.getShortcut().getKeyStroke(), "UP");
|
---|
211 | Main.contentPane.getActionMap().put("UP", mv);
|
---|
212 | }
|
---|
213 | mv = new MoveAction(MoveAction.Direction.DOWN);
|
---|
214 | if (mv.getShortcut() != null) {
|
---|
215 | Main.contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(mv.getShortcut().getKeyStroke(), "DOWN");
|
---|
216 | Main.contentPane.getActionMap().put("DOWN", mv);
|
---|
217 | }
|
---|
218 | mv = new MoveAction(MoveAction.Direction.LEFT);
|
---|
219 | if (mv.getShortcut() != null) {
|
---|
220 | Main.contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(mv.getShortcut().getKeyStroke(), "LEFT");
|
---|
221 | Main.contentPane.getActionMap().put("LEFT", mv);
|
---|
222 | }
|
---|
223 | mv = new MoveAction(MoveAction.Direction.RIGHT);
|
---|
224 | if (mv.getShortcut() != null) {
|
---|
225 | Main.contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(mv.getShortcut().getKeyStroke(), "RIGHT");
|
---|
226 | Main.contentPane.getActionMap().put("RIGHT", mv);
|
---|
227 | }
|
---|
228 | }
|
---|
229 | });
|
---|
230 |
|
---|
231 | // listend to selection changes to redraw the map
|
---|
232 | DataSet.selListeners.add(new SelectionChangedListener(){
|
---|
233 | public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) {
|
---|
234 | repaint();
|
---|
235 | }
|
---|
236 | });
|
---|
237 |
|
---|
238 | //store the last mouse action
|
---|
239 | this.addMouseMotionListener(new MouseMotionListener() {
|
---|
240 | public void mouseDragged(MouseEvent e) {
|
---|
241 | mouseMoved(e);
|
---|
242 | }
|
---|
243 | public void mouseMoved(MouseEvent e) {
|
---|
244 | lastMEvent = e;
|
---|
245 | }
|
---|
246 | });
|
---|
247 | }
|
---|
248 |
|
---|
249 | /**
|
---|
250 | * Adds a GPX layer. A GPX layer is added below the lowest data layer.
|
---|
251 | *
|
---|
252 | * @param layer the GPX layer
|
---|
253 | */
|
---|
254 | protected void addGpxLayer(GpxLayer layer) {
|
---|
255 | if (layers.isEmpty()) {
|
---|
256 | layers.add(layer);
|
---|
257 | return;
|
---|
258 | }
|
---|
259 | for (int i=layers.size()-1; i>= 0; i--) {
|
---|
260 | if (layers.get(i) instanceof OsmDataLayer) {
|
---|
261 | if (i == layers.size()-1) {
|
---|
262 | layers.add(layer);
|
---|
263 | } else {
|
---|
264 | layers.add(i+1, layer);
|
---|
265 | }
|
---|
266 | return;
|
---|
267 | }
|
---|
268 | }
|
---|
269 | layers.add(layer);
|
---|
270 | }
|
---|
271 |
|
---|
272 | /**
|
---|
273 | * Add a layer to the current MapView. The layer will be added at topmost
|
---|
274 | * position.
|
---|
275 | */
|
---|
276 | public void addLayer(Layer layer) {
|
---|
277 | if (layer instanceof MarkerLayer && playHeadMarker == null) {
|
---|
278 | playHeadMarker = PlayHeadMarker.create();
|
---|
279 | }
|
---|
280 |
|
---|
281 | if (layer instanceof GpxLayer) {
|
---|
282 | addGpxLayer((GpxLayer)layer);
|
---|
283 | } else if (layer.isBackgroundLayer() || layers.isEmpty()) {
|
---|
284 | layers.add(layer);
|
---|
285 | } else {
|
---|
286 | layers.add(0, layer);
|
---|
287 | }
|
---|
288 | fireLayerAdded(layer);
|
---|
289 | if (layer instanceof OsmDataLayer || activeLayer == null) {
|
---|
290 | // autoselect the new layer
|
---|
291 | setActiveLayer(layer);
|
---|
292 | }
|
---|
293 | layer.addPropertyChangeListener(this);
|
---|
294 | AudioPlayer.reset();
|
---|
295 | repaint();
|
---|
296 | }
|
---|
297 |
|
---|
298 | @Override
|
---|
299 | protected DataSet getCurrentDataSet() {
|
---|
300 | if (editLayer != null)
|
---|
301 | return editLayer.data;
|
---|
302 | else
|
---|
303 | return null;
|
---|
304 | }
|
---|
305 |
|
---|
306 | /**
|
---|
307 | * Replies true if the active layer is drawable.
|
---|
308 | *
|
---|
309 | * @return true if the active layer is drawable, false otherwise
|
---|
310 | */
|
---|
311 | public boolean isActiveLayerDrawable() {
|
---|
312 | return editLayer != null;
|
---|
313 | }
|
---|
314 |
|
---|
315 | /**
|
---|
316 | * Replies true if the active layer is visible.
|
---|
317 | *
|
---|
318 | * @return true if the active layer is visible, false otherwise
|
---|
319 | */
|
---|
320 | public boolean isActiveLayerVisible() {
|
---|
321 | return isActiveLayerDrawable() && editLayer.isVisible();
|
---|
322 | }
|
---|
323 |
|
---|
324 | /**
|
---|
325 | * Determines the next active data layer according to the following
|
---|
326 | * rules:
|
---|
327 | * <ul>
|
---|
328 | * <li>if there is at least one {@see OsmDataLayer} the first one
|
---|
329 | * becomes active</li>
|
---|
330 | * <li>otherwise, the top most layer of any type becomes active</li>
|
---|
331 | * </ul>
|
---|
332 | *
|
---|
333 | * @return the next active data layer
|
---|
334 | */
|
---|
335 | protected Layer determineNextActiveLayer(List<Layer> layersList) {
|
---|
336 | // First look for data layer
|
---|
337 | for (Layer layer:layersList) {
|
---|
338 | if (layer instanceof OsmDataLayer)
|
---|
339 | return layer;
|
---|
340 | }
|
---|
341 |
|
---|
342 | // Then any layer
|
---|
343 | if (!layersList.isEmpty())
|
---|
344 | return layersList.get(0);
|
---|
345 |
|
---|
346 | // and then give up
|
---|
347 | return null;
|
---|
348 |
|
---|
349 | }
|
---|
350 |
|
---|
351 | /**
|
---|
352 | * Remove the layer from the mapview. If the layer was in the list before,
|
---|
353 | * an LayerChange event is fired.
|
---|
354 | */
|
---|
355 | public void removeLayer(Layer layer) {
|
---|
356 | List<Layer> layersList = new ArrayList<Layer>(layers);
|
---|
357 |
|
---|
358 | if (!layersList.remove(layer))
|
---|
359 | return;
|
---|
360 |
|
---|
361 | setEditLayer(layersList);
|
---|
362 |
|
---|
363 | if (layer == activeLayer) {
|
---|
364 | setActiveLayer(determineNextActiveLayer(layersList), false);
|
---|
365 | }
|
---|
366 |
|
---|
367 | layers.remove(layer);
|
---|
368 | fireLayerRemoved(layer);
|
---|
369 | layer.removePropertyChangeListener(this);
|
---|
370 | layer.destroy();
|
---|
371 | AudioPlayer.reset();
|
---|
372 | repaint();
|
---|
373 | }
|
---|
374 |
|
---|
375 | private boolean virtualNodesEnabled = false;
|
---|
376 |
|
---|
377 | public void setVirtualNodesEnabled(boolean enabled) {
|
---|
378 | if(virtualNodesEnabled != enabled) {
|
---|
379 | virtualNodesEnabled = enabled;
|
---|
380 | repaint();
|
---|
381 | }
|
---|
382 | }
|
---|
383 | public boolean isVirtualNodesEnabled() {
|
---|
384 | return virtualNodesEnabled;
|
---|
385 | }
|
---|
386 |
|
---|
387 | /**
|
---|
388 | * Moves the layer to the given new position. No event is fired, but repaints
|
---|
389 | * according to the new Z-Order of the layers.
|
---|
390 | *
|
---|
391 | * @param layer The layer to move
|
---|
392 | * @param pos The new position of the layer
|
---|
393 | */
|
---|
394 | public void moveLayer(Layer layer, int pos) {
|
---|
395 | int curLayerPos = layers.indexOf(layer);
|
---|
396 | if (curLayerPos == -1)
|
---|
397 | throw new IllegalArgumentException(tr("Layer not in list."));
|
---|
398 | if (pos == curLayerPos)
|
---|
399 | return; // already in place.
|
---|
400 | layers.remove(curLayerPos);
|
---|
401 | if (pos >= layers.size()) {
|
---|
402 | layers.add(layer);
|
---|
403 | } else {
|
---|
404 | layers.add(pos, layer);
|
---|
405 | }
|
---|
406 | setEditLayer(layers);
|
---|
407 | AudioPlayer.reset();
|
---|
408 | repaint();
|
---|
409 | }
|
---|
410 |
|
---|
411 | public int getLayerPos(Layer layer) {
|
---|
412 | int curLayerPos = layers.indexOf(layer);
|
---|
413 | if (curLayerPos == -1)
|
---|
414 | throw new IllegalArgumentException(tr("Layer not in list."));
|
---|
415 | return curLayerPos;
|
---|
416 | }
|
---|
417 |
|
---|
418 | /**
|
---|
419 | * Creates a list of the visible layers in Z-Order, the layer with the lowest Z-Order
|
---|
420 | * first, layer with the highest Z-Order last.
|
---|
421 | *
|
---|
422 | * @return a list of the visible in Z-Order, the layer with the lowest Z-Order
|
---|
423 | * first, layer with the highest Z-Order last.
|
---|
424 | */
|
---|
425 | protected List<Layer> getVisibleLayersInZOrder() {
|
---|
426 | ArrayList<Layer> ret = new ArrayList<Layer>();
|
---|
427 | for (Layer l: layers) {
|
---|
428 | if (l.isVisible()) {
|
---|
429 | ret.add(l);
|
---|
430 | }
|
---|
431 | }
|
---|
432 | // sort according to position in the list of layers, with one exception:
|
---|
433 | // an active data layer always becomes a higher Z-Order than all other
|
---|
434 | // data layers
|
---|
435 | //
|
---|
436 | Collections.sort(
|
---|
437 | ret,
|
---|
438 | new Comparator<Layer>() {
|
---|
439 | public int compare(Layer l1, Layer l2) {
|
---|
440 | if (l1 instanceof OsmDataLayer && l2 instanceof OsmDataLayer) {
|
---|
441 | if (l1 == getActiveLayer()) return -1;
|
---|
442 | if (l2 == getActiveLayer()) return 1;
|
---|
443 | return Integer.valueOf(layers.indexOf(l1)).compareTo(layers.indexOf(l2));
|
---|
444 | } else
|
---|
445 | return Integer.valueOf(layers.indexOf(l1)).compareTo(layers.indexOf(l2));
|
---|
446 | }
|
---|
447 | }
|
---|
448 | );
|
---|
449 | Collections.reverse(ret);
|
---|
450 | return ret;
|
---|
451 | }
|
---|
452 |
|
---|
453 | /**
|
---|
454 | * Draw the component.
|
---|
455 | */
|
---|
456 | @Override public void paint(Graphics g) {
|
---|
457 | if (center == null)
|
---|
458 | return; // no data loaded yet.
|
---|
459 |
|
---|
460 | // re-create offscreen-buffer if we've been resized, otherwise
|
---|
461 | // just re-use it.
|
---|
462 | if (null == offscreenBuffer || offscreenBuffer.getWidth() != getWidth()
|
---|
463 | || offscreenBuffer.getHeight() != getHeight()) {
|
---|
464 | offscreenBuffer = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_3BYTE_BGR);
|
---|
465 | }
|
---|
466 |
|
---|
467 | Graphics2D tempG = offscreenBuffer.createGraphics();
|
---|
468 | tempG.setClip(g.getClip());
|
---|
469 | tempG.setColor(PaintColors.BACKGROUND.get());
|
---|
470 | tempG.fillRect(0, 0, getWidth(), getHeight());
|
---|
471 |
|
---|
472 | Bounds box = getLatLonBounds(g.getClipBounds());
|
---|
473 |
|
---|
474 | for (Layer l: getVisibleLayersInZOrder()) {
|
---|
475 | l.paint(tempG, this, box);
|
---|
476 | }
|
---|
477 | for (MapViewPaintable mvp : temporaryLayers) {
|
---|
478 | mvp.paint(tempG, this, box);
|
---|
479 | }
|
---|
480 |
|
---|
481 | // draw world borders
|
---|
482 | tempG.setColor(Color.WHITE);
|
---|
483 | Bounds b = getProjection().getWorldBoundsLatLon();
|
---|
484 | double lat = b.getMin().lat();
|
---|
485 | double lon = b.getMin().lon();
|
---|
486 |
|
---|
487 | Point p = getPoint(b.getMin());
|
---|
488 |
|
---|
489 | GeneralPath path = new GeneralPath();
|
---|
490 |
|
---|
491 | path.moveTo(p.x, p.y);
|
---|
492 | double max = b.getMax().lat();
|
---|
493 | for(; lat <= max; lat += 1.0)
|
---|
494 | {
|
---|
495 | p = getPoint(new LatLon(lat >= max ? max : lat, lon));
|
---|
496 | path.lineTo(p.x, p.y);
|
---|
497 | }
|
---|
498 | lat = max; max = b.getMax().lon();
|
---|
499 | for(; lon <= max; lon += 1.0)
|
---|
500 | {
|
---|
501 | p = getPoint(new LatLon(lat, lon >= max ? max : lon));
|
---|
502 | path.lineTo(p.x, p.y);
|
---|
503 | }
|
---|
504 | lon = max; max = b.getMin().lat();
|
---|
505 | for(; lat >= max; lat -= 1.0)
|
---|
506 | {
|
---|
507 | p = getPoint(new LatLon(lat <= max ? max : lat, lon));
|
---|
508 | path.lineTo(p.x, p.y);
|
---|
509 | }
|
---|
510 | lat = max; max = b.getMin().lon();
|
---|
511 | for(; lon >= max; lon -= 1.0)
|
---|
512 | {
|
---|
513 | p = getPoint(new LatLon(lat, lon <= max ? max : lon));
|
---|
514 | path.lineTo(p.x, p.y);
|
---|
515 | }
|
---|
516 |
|
---|
517 | int w = offscreenBuffer.getWidth();
|
---|
518 | int h = offscreenBuffer.getHeight();
|
---|
519 |
|
---|
520 | // Work around OpenJDK having problems when drawing out of bounds
|
---|
521 | final Area border = new Area(path);
|
---|
522 | // Make the viewport 1px larger in every direction to prevent an
|
---|
523 | // additional 1px border when zooming in
|
---|
524 | final Area viewport = new Area(new Rectangle(-1, -1, w + 2, h + 2));
|
---|
525 | border.intersect(viewport);
|
---|
526 | tempG.draw(border);
|
---|
527 |
|
---|
528 | if (playHeadMarker != null) {
|
---|
529 | playHeadMarker.paint(tempG, this);
|
---|
530 | }
|
---|
531 |
|
---|
532 | g.drawImage(offscreenBuffer, 0, 0, null);
|
---|
533 | super.paint(g);
|
---|
534 | }
|
---|
535 |
|
---|
536 | /**
|
---|
537 | * Set the new dimension to the view.
|
---|
538 | */
|
---|
539 | public void recalculateCenterScale(BoundingXYVisitor box) {
|
---|
540 | if (box == null) {
|
---|
541 | box = new BoundingXYVisitor();
|
---|
542 | }
|
---|
543 | if (box.getBounds() == null) {
|
---|
544 | box.visit(getProjection().getWorldBoundsLatLon());
|
---|
545 | }
|
---|
546 | if (!box.hasExtend()) {
|
---|
547 | box.enlargeBoundingBox();
|
---|
548 | }
|
---|
549 |
|
---|
550 | zoomTo(box.getBounds());
|
---|
551 | }
|
---|
552 |
|
---|
553 | /**
|
---|
554 | * @return An unmodifiable collection of all layers
|
---|
555 | */
|
---|
556 | public Collection<Layer> getAllLayers() {
|
---|
557 | return Collections.unmodifiableCollection(layers);
|
---|
558 | }
|
---|
559 |
|
---|
560 | /**
|
---|
561 | * @return An unmodifiable ordered list of all layers
|
---|
562 | */
|
---|
563 | public List<Layer> getAllLayersAsList() {
|
---|
564 | return Collections.unmodifiableList(layers);
|
---|
565 | }
|
---|
566 |
|
---|
567 | /**
|
---|
568 | * Replies an unmodifiable list of layers of a certain type.
|
---|
569 | *
|
---|
570 | * Example:
|
---|
571 | * <pre>
|
---|
572 | * List<WMSLayer> wmsLayers = getLayersOfType(WMSLayer.class);
|
---|
573 | * </pre>
|
---|
574 | *
|
---|
575 | * @return an unmodifiable list of layers of a certain type.
|
---|
576 | */
|
---|
577 | public <T> List<T> getLayersOfType(Class<T> ofType) {
|
---|
578 | ArrayList<T> ret = new ArrayList<T>();
|
---|
579 | for (Layer layer : getAllLayersAsList()) {
|
---|
580 | if (ofType.isInstance(layer)) {
|
---|
581 | ret.add(ofType.cast(layer));
|
---|
582 | }
|
---|
583 | }
|
---|
584 | return ret;
|
---|
585 | }
|
---|
586 |
|
---|
587 | /**
|
---|
588 | * Replies the number of layers managed by this mav view
|
---|
589 | *
|
---|
590 | * @return the number of layers managed by this mav view
|
---|
591 | */
|
---|
592 | public int getNumLayers() {
|
---|
593 | return layers.size();
|
---|
594 | }
|
---|
595 |
|
---|
596 | /**
|
---|
597 | * Replies true if there is at least one layer in this map view
|
---|
598 | *
|
---|
599 | * @return true if there is at least one layer in this map view
|
---|
600 | */
|
---|
601 | public boolean hasLayers() {
|
---|
602 | return getNumLayers() > 0;
|
---|
603 | }
|
---|
604 |
|
---|
605 | private void setEditLayer(List<Layer> layersList) {
|
---|
606 | OsmDataLayer newEditLayer = layersList.contains(editLayer)?editLayer:null;
|
---|
607 | OsmDataLayer oldEditLayer = editLayer;
|
---|
608 |
|
---|
609 | // Find new edit layer
|
---|
610 | if (activeLayer != editLayer || !layersList.contains(editLayer)) {
|
---|
611 | if (activeLayer instanceof OsmDataLayer && layersList.contains(activeLayer)) {
|
---|
612 | newEditLayer = (OsmDataLayer) activeLayer;
|
---|
613 | } else {
|
---|
614 | for (Layer layer:layersList) {
|
---|
615 | if (layer instanceof OsmDataLayer) {
|
---|
616 | newEditLayer = (OsmDataLayer) layer;
|
---|
617 | break;
|
---|
618 | }
|
---|
619 | }
|
---|
620 | }
|
---|
621 | }
|
---|
622 |
|
---|
623 | // Set new edit layer
|
---|
624 | if (newEditLayer != editLayer) {
|
---|
625 | if (newEditLayer == null) {
|
---|
626 | getCurrentDataSet().setSelected();
|
---|
627 | }
|
---|
628 |
|
---|
629 | editLayer = newEditLayer;
|
---|
630 | fireEditLayerChanged(oldEditLayer, newEditLayer);
|
---|
631 | refreshTitle();
|
---|
632 | }
|
---|
633 |
|
---|
634 | }
|
---|
635 |
|
---|
636 | /**
|
---|
637 | * Sets the active layer to <code>layer</code>. If <code>layer</code> is an instance
|
---|
638 | * of {@see OsmDataLayer} also sets {@see #editLayer} to <code>layer</code>.
|
---|
639 | *
|
---|
640 | * @param layer the layer to be activate; must be one of the layers in the list of layers
|
---|
641 | * @exception IllegalArgumentException thrown if layer is not in the lis of layers
|
---|
642 | */
|
---|
643 | public void setActiveLayer(Layer layer) {
|
---|
644 | setActiveLayer(layer, true);
|
---|
645 | }
|
---|
646 |
|
---|
647 | private void setActiveLayer(Layer layer, boolean setEditLayer) {
|
---|
648 | if (layer != null && !layers.contains(layer))
|
---|
649 | throw new IllegalArgumentException(tr("Layer ''{0}'' must be in list of layers", layer.toString()));
|
---|
650 |
|
---|
651 | if (layer == activeLayer)
|
---|
652 | return;
|
---|
653 |
|
---|
654 | Layer old = activeLayer;
|
---|
655 | activeLayer = layer;
|
---|
656 | if (setEditLayer) {
|
---|
657 | setEditLayer(layers);
|
---|
658 | }
|
---|
659 | fireActiveLayerChanged(old, layer);
|
---|
660 |
|
---|
661 | /* This only makes the buttons look disabled. Disabling the actions as well requires
|
---|
662 | * the user to re-select the tool after i.e. moving a layer. While testing I found
|
---|
663 | * that I switch layers and actions at the same time and it was annoying to mind the
|
---|
664 | * order. This way it works as visual clue for new users */
|
---|
665 | for (Enumeration<AbstractButton> e = Main.map.toolGroup.getElements() ; e.hasMoreElements() ;) {
|
---|
666 | AbstractButton x=e.nextElement();
|
---|
667 | x.setEnabled(((MapMode)x.getAction()).layerIsSupported(layer));
|
---|
668 | }
|
---|
669 | AudioPlayer.reset();
|
---|
670 | repaint();
|
---|
671 | }
|
---|
672 |
|
---|
673 | /**
|
---|
674 | * Replies the currently active layer
|
---|
675 | *
|
---|
676 | * @return the currently active layer (may be null)
|
---|
677 | */
|
---|
678 | public Layer getActiveLayer() {
|
---|
679 | return activeLayer;
|
---|
680 | }
|
---|
681 |
|
---|
682 | /**
|
---|
683 | * Replies the current edit layer, if any
|
---|
684 | *
|
---|
685 | * @return the current edit layer. May be null.
|
---|
686 | */
|
---|
687 | public OsmDataLayer getEditLayer() {
|
---|
688 | return editLayer;
|
---|
689 | }
|
---|
690 |
|
---|
691 | /**
|
---|
692 | * replies true if the list of layers managed by this map view contain layer
|
---|
693 | *
|
---|
694 | * @param layer the layer
|
---|
695 | * @return true if the list of layers managed by this map view contain layer
|
---|
696 | */
|
---|
697 | public boolean hasLayer(Layer layer) {
|
---|
698 | return layers.contains(layer);
|
---|
699 | }
|
---|
700 |
|
---|
701 | /**
|
---|
702 | * Tries to zoom to the download boundingbox[es] of the current edit layer
|
---|
703 | * (aka {@link OsmDataLayer}). If the edit layer has multiple download bounding
|
---|
704 | * boxes it zooms to a large virtual bounding box containing all smaller ones.
|
---|
705 | *
|
---|
706 | * @return <code>true</code> if a zoom operation has been performed
|
---|
707 | */
|
---|
708 | public boolean zoomToDataSetBoundingBox(DataSet ds) {
|
---|
709 | // In case we already have an existing data layer ...
|
---|
710 | OsmDataLayer layer= getEditLayer();
|
---|
711 | if (layer == null)
|
---|
712 | return false;
|
---|
713 | Collection<DataSource> dataSources = ds.dataSources;
|
---|
714 | // ... with bounding box[es] of data loaded from OSM or a file...
|
---|
715 | BoundingXYVisitor bbox = new BoundingXYVisitor();
|
---|
716 | for (DataSource source : dataSources) {
|
---|
717 | bbox.visit(source.bounds);
|
---|
718 | }
|
---|
719 | if (bbox.hasExtend()) {
|
---|
720 | // ... we zoom to it's bounding box
|
---|
721 | recalculateCenterScale(bbox);
|
---|
722 | return true;
|
---|
723 | }
|
---|
724 | return false;
|
---|
725 | }
|
---|
726 |
|
---|
727 | public boolean addTemporaryLayer(MapViewPaintable mvp) {
|
---|
728 | if (temporaryLayers.contains(mvp)) return false;
|
---|
729 | return temporaryLayers.add(mvp);
|
---|
730 | }
|
---|
731 |
|
---|
732 | public boolean removeTemporaryLayer(MapViewPaintable mvp) {
|
---|
733 | return temporaryLayers.remove(mvp);
|
---|
734 | }
|
---|
735 |
|
---|
736 | public void propertyChange(PropertyChangeEvent evt) {
|
---|
737 | if (evt.getPropertyName().equals(Layer.VISIBLE_PROP)) {
|
---|
738 | repaint();
|
---|
739 | } else if (evt.getPropertyName().equals(OsmDataLayer.REQUIRES_SAVE_TO_DISK_PROP)
|
---|
740 | || evt.getPropertyName().equals(OsmDataLayer.REQUIRES_UPLOAD_TO_SERVER_PROP)) {
|
---|
741 | OsmDataLayer layer = (OsmDataLayer)evt.getSource();
|
---|
742 | if (layer == getEditLayer()) {
|
---|
743 | refreshTitle();
|
---|
744 | }
|
---|
745 | }
|
---|
746 | }
|
---|
747 |
|
---|
748 | protected void refreshTitle() {
|
---|
749 | boolean dirty = editLayer != null && (editLayer.requiresSaveToFile() || editLayer.requiresUploadToServer());
|
---|
750 | if (dirty) {
|
---|
751 | JOptionPane.getFrameForComponent(Main.parent).setTitle("* " + tr("Java OpenStreetMap Editor"));
|
---|
752 | } else {
|
---|
753 | JOptionPane.getFrameForComponent(Main.parent).setTitle(tr("Java OpenStreetMap Editor"));
|
---|
754 | }
|
---|
755 | }
|
---|
756 |
|
---|
757 | }
|
---|