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