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