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.event.ComponentAdapter;
|
---|
12 | import java.awt.event.ComponentEvent;
|
---|
13 | import java.awt.event.MouseEvent;
|
---|
14 | import java.awt.event.MouseMotionListener;
|
---|
15 | import java.awt.image.BufferedImage;
|
---|
16 | import java.util.ArrayList;
|
---|
17 | import java.util.Collection;
|
---|
18 | import java.util.Collections;
|
---|
19 | import java.util.Enumeration;
|
---|
20 | import java.util.LinkedList;
|
---|
21 |
|
---|
22 | import javax.swing.AbstractButton;
|
---|
23 | import javax.swing.JComponent;
|
---|
24 | import javax.swing.JOptionPane;
|
---|
25 |
|
---|
26 | import org.openstreetmap.josm.Main;
|
---|
27 | import org.openstreetmap.josm.actions.AutoScaleAction;
|
---|
28 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
29 | import org.openstreetmap.josm.actions.MoveAction;
|
---|
30 | import org.openstreetmap.josm.actions.mapmode.MapMode;
|
---|
31 | import org.openstreetmap.josm.data.ProjectionBounds;
|
---|
32 | import org.openstreetmap.josm.data.SelectionChangedListener;
|
---|
33 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
34 | import org.openstreetmap.josm.data.osm.DataSource;
|
---|
35 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
36 | import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
|
---|
37 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
38 | import org.openstreetmap.josm.gui.layer.MapViewPaintable;
|
---|
39 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
40 | import org.openstreetmap.josm.gui.layer.OsmDataLayer.ModifiedChangedListener;
|
---|
41 | import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
|
---|
42 | import org.openstreetmap.josm.gui.layer.markerlayer.PlayHeadMarker;
|
---|
43 | import org.openstreetmap.josm.tools.AudioPlayer;
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * This is a component used in the MapFrame for browsing the map. It use is to
|
---|
47 | * provide the MapMode's enough capabilities to operate.
|
---|
48 | *
|
---|
49 | * MapView hold meta-data about the data set currently displayed, as scale level,
|
---|
50 | * center point viewed, what scrolling mode or editing mode is selected or with
|
---|
51 | * what projection the map is viewed etc..
|
---|
52 | *
|
---|
53 | * MapView is able to administrate several layers.
|
---|
54 | *
|
---|
55 | * @author imi
|
---|
56 | */
|
---|
57 | public class MapView extends NavigatableComponent {
|
---|
58 |
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * A list of all layers currently loaded.
|
---|
62 | */
|
---|
63 | private ArrayList<Layer> layers = new ArrayList<Layer>();
|
---|
64 | /**
|
---|
65 | * The play head marker: there is only one of these so it isn't in any specific layer
|
---|
66 | */
|
---|
67 | public PlayHeadMarker playHeadMarker = null;
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * The layer from the layers list that is currently active.
|
---|
71 | */
|
---|
72 | private Layer activeLayer;
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * The last event performed by mouse.
|
---|
76 | */
|
---|
77 | public MouseEvent lastMEvent;
|
---|
78 |
|
---|
79 | private LinkedList<MapViewPaintable> temporaryLayers = new LinkedList<MapViewPaintable>();
|
---|
80 |
|
---|
81 | private BufferedImage offscreenBuffer;
|
---|
82 |
|
---|
83 | public MapView() {
|
---|
84 | addComponentListener(new ComponentAdapter(){
|
---|
85 | @Override public void componentResized(ComponentEvent e) {
|
---|
86 | removeComponentListener(this);
|
---|
87 |
|
---|
88 | MapSlider zoomSlider = new MapSlider(MapView.this);
|
---|
89 | add(zoomSlider);
|
---|
90 | zoomSlider.setBounds(3, 0, 114, 30);
|
---|
91 |
|
---|
92 | MapScaler scaler = new MapScaler(MapView.this);
|
---|
93 | add(scaler);
|
---|
94 | scaler.setLocation(10,30);
|
---|
95 |
|
---|
96 | if (!zoomToEditLayerBoundingBox()) {
|
---|
97 | new AutoScaleAction("data").actionPerformed(null);
|
---|
98 | }
|
---|
99 |
|
---|
100 | new MapMover(MapView.this, Main.contentPane);
|
---|
101 | JosmAction mv;
|
---|
102 | mv = new MoveAction(MoveAction.Direction.UP);
|
---|
103 | if (mv.getShortcut() != null) {
|
---|
104 | Main.contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(mv.getShortcut().getKeyStroke(), "UP");
|
---|
105 | Main.contentPane.getActionMap().put("UP", mv);
|
---|
106 | }
|
---|
107 | mv = new MoveAction(MoveAction.Direction.DOWN);
|
---|
108 | if (mv.getShortcut() != null) {
|
---|
109 | Main.contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(mv.getShortcut().getKeyStroke(), "DOWN");
|
---|
110 | Main.contentPane.getActionMap().put("DOWN", mv);
|
---|
111 | }
|
---|
112 | mv = new MoveAction(MoveAction.Direction.LEFT);
|
---|
113 | if (mv.getShortcut() != null) {
|
---|
114 | Main.contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(mv.getShortcut().getKeyStroke(), "LEFT");
|
---|
115 | Main.contentPane.getActionMap().put("LEFT", mv);
|
---|
116 | }
|
---|
117 | mv = new MoveAction(MoveAction.Direction.RIGHT);
|
---|
118 | if (mv.getShortcut() != null) {
|
---|
119 | Main.contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(mv.getShortcut().getKeyStroke(), "RIGHT");
|
---|
120 | Main.contentPane.getActionMap().put("RIGHT", mv);
|
---|
121 | }
|
---|
122 | }
|
---|
123 | });
|
---|
124 |
|
---|
125 | // listend to selection changes to redraw the map
|
---|
126 | DataSet.selListeners.add(new SelectionChangedListener(){
|
---|
127 | public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) {
|
---|
128 | repaint();
|
---|
129 | }
|
---|
130 | });
|
---|
131 |
|
---|
132 | //store the last mouse action
|
---|
133 | this.addMouseMotionListener(new MouseMotionListener() {
|
---|
134 | public void mouseDragged(MouseEvent e) {
|
---|
135 | mouseMoved(e);
|
---|
136 | }
|
---|
137 | public void mouseMoved(MouseEvent e) {
|
---|
138 | lastMEvent = e;
|
---|
139 | }
|
---|
140 | });
|
---|
141 | }
|
---|
142 |
|
---|
143 | /**
|
---|
144 | * Add a layer to the current MapView. The layer will be added at topmost
|
---|
145 | * position.
|
---|
146 | */
|
---|
147 | public void addLayer(Layer layer) {
|
---|
148 | if (layer instanceof OsmDataLayer) {
|
---|
149 | OsmDataLayer editLayer = (OsmDataLayer)layer;
|
---|
150 | Main.ds = editLayer.data;
|
---|
151 | editLayer.listenerModified.add(new ModifiedChangedListener(){
|
---|
152 | public void modifiedChanged(boolean value, OsmDataLayer source) {
|
---|
153 | JOptionPane.getFrameForComponent(Main.parent).setTitle((value?"*":"")
|
---|
154 | +tr("Java OpenStreetMap Editor"));
|
---|
155 | }
|
---|
156 | });
|
---|
157 | }
|
---|
158 | if (layer instanceof MarkerLayer && playHeadMarker == null) {
|
---|
159 | playHeadMarker = PlayHeadMarker.create();
|
---|
160 | }
|
---|
161 | int pos = layers.size();
|
---|
162 | while(pos > 0 && layers.get(pos-1).background) {
|
---|
163 | --pos;
|
---|
164 | }
|
---|
165 | layers.add(pos, layer);
|
---|
166 |
|
---|
167 | for (Layer.LayerChangeListener l : Layer.listeners) {
|
---|
168 | l.layerAdded(layer);
|
---|
169 | }
|
---|
170 | if (layer instanceof OsmDataLayer || activeLayer == null) {
|
---|
171 | // autoselect the new layer
|
---|
172 | Layer old = activeLayer;
|
---|
173 | setActiveLayer(layer);
|
---|
174 | for (Layer.LayerChangeListener l : Layer.listeners) {
|
---|
175 | l.activeLayerChange(old, layer);
|
---|
176 | }
|
---|
177 | }
|
---|
178 | AudioPlayer.reset();
|
---|
179 | repaint();
|
---|
180 | }
|
---|
181 |
|
---|
182 | @Override
|
---|
183 | protected DataSet getData() {
|
---|
184 | if(activeLayer != null && activeLayer instanceof OsmDataLayer)
|
---|
185 | return ((OsmDataLayer)activeLayer).data;
|
---|
186 | return new DataSet();
|
---|
187 | }
|
---|
188 |
|
---|
189 | /**
|
---|
190 | * Replies true if the active layer is drawable.
|
---|
191 | *
|
---|
192 | * @return true if the active layer is drawable, false otherwise
|
---|
193 | */
|
---|
194 | public boolean isActiveLayerDrawable() {
|
---|
195 | return activeLayer != null && activeLayer instanceof OsmDataLayer;
|
---|
196 | }
|
---|
197 |
|
---|
198 | /**
|
---|
199 | * Replies true if the active layer is visible.
|
---|
200 | *
|
---|
201 | * @return true if the active layer is visible, false otherwise
|
---|
202 | */
|
---|
203 | public boolean isActiveLayerVisible() {
|
---|
204 | return isActiveLayerDrawable() && activeLayer.visible;
|
---|
205 | }
|
---|
206 |
|
---|
207 | /**
|
---|
208 | * Remove the layer from the mapview. If the layer was in the list before,
|
---|
209 | * an LayerChange event is fired.
|
---|
210 | */
|
---|
211 | public void removeLayer(Layer layer) {
|
---|
212 | if (layer == activeLayer) {
|
---|
213 | if (layer instanceof OsmDataLayer) {
|
---|
214 | Main.ds = null;
|
---|
215 | }
|
---|
216 | activeLayer = null;
|
---|
217 | }
|
---|
218 | if (layers.remove(layer)) {
|
---|
219 | for (Layer.LayerChangeListener l : Layer.listeners) {
|
---|
220 | l.layerRemoved(layer);
|
---|
221 | }
|
---|
222 | }
|
---|
223 | layer.destroy();
|
---|
224 | AudioPlayer.reset();
|
---|
225 | }
|
---|
226 |
|
---|
227 | private boolean virtualNodesEnabled = false;
|
---|
228 | public void setVirtualNodesEnabled(boolean enabled) {
|
---|
229 | if(virtualNodesEnabled != enabled) {
|
---|
230 | virtualNodesEnabled = enabled;
|
---|
231 | repaint();
|
---|
232 | }
|
---|
233 | }
|
---|
234 | public boolean isVirtualNodesEnabled() {
|
---|
235 | return virtualNodesEnabled;
|
---|
236 | }
|
---|
237 |
|
---|
238 | /**
|
---|
239 | * Moves the layer to the given new position. No event is fired.
|
---|
240 | * @param layer The layer to move
|
---|
241 | * @param pos The new position of the layer
|
---|
242 | */
|
---|
243 | public void moveLayer(Layer layer, int pos) {
|
---|
244 | int curLayerPos = layers.indexOf(layer);
|
---|
245 | if (curLayerPos == -1)
|
---|
246 | throw new IllegalArgumentException(tr("layer not in list."));
|
---|
247 | if (pos == curLayerPos)
|
---|
248 | return; // already in place.
|
---|
249 | layers.remove(curLayerPos);
|
---|
250 | if (pos >= layers.size()) {
|
---|
251 | layers.add(layer);
|
---|
252 | } else {
|
---|
253 | layers.add(pos, layer);
|
---|
254 | }
|
---|
255 | AudioPlayer.reset();
|
---|
256 | }
|
---|
257 |
|
---|
258 |
|
---|
259 | public int getLayerPos(Layer layer) {
|
---|
260 | int curLayerPos = layers.indexOf(layer);
|
---|
261 | if (curLayerPos == -1)
|
---|
262 | throw new IllegalArgumentException(tr("layer not in list."));
|
---|
263 | return curLayerPos;
|
---|
264 | }
|
---|
265 |
|
---|
266 | /**
|
---|
267 | * Draw the component.
|
---|
268 | */
|
---|
269 | @Override public void paint(Graphics g) {
|
---|
270 | if (center == null)
|
---|
271 | return; // no data loaded yet.
|
---|
272 |
|
---|
273 | // re-create offscreen-buffer if we've been resized, otherwise
|
---|
274 | // just re-use it.
|
---|
275 | if (null == offscreenBuffer || offscreenBuffer.getWidth() != getWidth()
|
---|
276 | || offscreenBuffer.getHeight() != getHeight()) {
|
---|
277 | offscreenBuffer = new BufferedImage(getWidth(), getHeight(),
|
---|
278 | BufferedImage.TYPE_INT_ARGB);
|
---|
279 | }
|
---|
280 |
|
---|
281 | Graphics2D tempG = offscreenBuffer.createGraphics();
|
---|
282 | tempG.setColor(Main.pref.getColor("background", Color.BLACK));
|
---|
283 | tempG.fillRect(0, 0, getWidth(), getHeight());
|
---|
284 |
|
---|
285 | for (int i = layers.size()-1; i >= 0; --i) {
|
---|
286 | Layer l = layers.get(i);
|
---|
287 | if (l.visible/* && l != getActiveLayer()*/) {
|
---|
288 | l.paint(tempG, this);
|
---|
289 | }
|
---|
290 | }
|
---|
291 |
|
---|
292 | /*if (getActiveLayer() != null && getActiveLayer().visible)
|
---|
293 | getActiveLayer().paint(tempG, this);*/
|
---|
294 |
|
---|
295 | for (MapViewPaintable mvp : temporaryLayers) {
|
---|
296 | mvp.paint(tempG, this);
|
---|
297 | }
|
---|
298 |
|
---|
299 | // draw world borders
|
---|
300 | tempG.setColor(Color.WHITE);
|
---|
301 | ProjectionBounds b = getProjection().getWorldBounds();
|
---|
302 | Point min = getPoint(b.min);
|
---|
303 | Point max = getPoint(b.max);
|
---|
304 | int x1 = Math.min(min.x, max.x);
|
---|
305 | int y1 = Math.min(min.y, max.y);
|
---|
306 | int x2 = Math.max(min.x, max.x);
|
---|
307 | int y2 = Math.max(min.y, max.y);
|
---|
308 | if (x1 > 0 || y1 > 0 || x2 < getWidth() || y2 < getHeight()) {
|
---|
309 | tempG.drawRect(x1, y1, x2-x1+1, y2-y1+1);
|
---|
310 | }
|
---|
311 |
|
---|
312 | if (playHeadMarker != null) {
|
---|
313 | playHeadMarker.paint(tempG, this);
|
---|
314 | }
|
---|
315 |
|
---|
316 | g.drawImage(offscreenBuffer, 0, 0, null);
|
---|
317 | super.paint(g);
|
---|
318 | }
|
---|
319 |
|
---|
320 | /**
|
---|
321 | * Set the new dimension to the view.
|
---|
322 | */
|
---|
323 | public void recalculateCenterScale(BoundingXYVisitor box) {
|
---|
324 | if (box == null) {
|
---|
325 | box = new BoundingXYVisitor();
|
---|
326 | }
|
---|
327 | if (box.getBounds() == null) {
|
---|
328 | box.visit(getProjection().getWorldBounds());
|
---|
329 | }
|
---|
330 | if (!box.hasExtend()) {
|
---|
331 | box.enlargeBoundingBox();
|
---|
332 | }
|
---|
333 |
|
---|
334 | zoomTo(box.getBounds());
|
---|
335 | }
|
---|
336 |
|
---|
337 | /**
|
---|
338 | * @return An unmodifiable collection of all layers
|
---|
339 | */
|
---|
340 | public Collection<Layer> getAllLayers() {
|
---|
341 | return Collections.unmodifiableCollection(layers);
|
---|
342 | }
|
---|
343 |
|
---|
344 | /**
|
---|
345 | * Sets the active layer to <code>layer</code>. If <code>layer</code> is an instance
|
---|
346 | * of {@see OsmDataLayer} also sets {@see #editLayer} to <code>layer</code>.
|
---|
347 | *
|
---|
348 | * @param layer the layer to be activate; must be one of the layers in the list of layers
|
---|
349 | * @exception IllegalArgumentException thrown if layer is not in the lis of layers
|
---|
350 | */
|
---|
351 | public void setActiveLayer(Layer layer) {
|
---|
352 | if (!layers.contains(layer))
|
---|
353 | throw new IllegalArgumentException(tr("Layer {0} must be in list of layers", layer.toString()));
|
---|
354 | if (layer instanceof OsmDataLayer) {
|
---|
355 | OsmDataLayer editLayer = (OsmDataLayer)layer;
|
---|
356 | Main.ds = editLayer.data;
|
---|
357 | } else {
|
---|
358 | Main.ds.setSelected();
|
---|
359 | }
|
---|
360 | DataSet.fireSelectionChanged(Main.ds.getSelected());
|
---|
361 | Layer old = activeLayer;
|
---|
362 | activeLayer = layer;
|
---|
363 | if (old != layer) {
|
---|
364 | for (Layer.LayerChangeListener l : Layer.listeners) {
|
---|
365 | l.activeLayerChange(old, layer);
|
---|
366 | }
|
---|
367 | }
|
---|
368 |
|
---|
369 | /* This only makes the buttons look disabled. Disabling the actions as well requires
|
---|
370 | * the user to re-select the tool after i.e. moving a layer. While testing I found
|
---|
371 | * that I switch layers and actions at the same time and it was annoying to mind the
|
---|
372 | * order. This way it works as visual clue for new users */
|
---|
373 | for (Enumeration<AbstractButton> e = Main.map.toolGroup.getElements() ; e.hasMoreElements() ;) {
|
---|
374 | AbstractButton x=e.nextElement();
|
---|
375 | x.setEnabled(((MapMode)x.getAction()).layerIsSupported(layer));
|
---|
376 | }
|
---|
377 | AudioPlayer.reset();
|
---|
378 | repaint();
|
---|
379 | }
|
---|
380 |
|
---|
381 | /**
|
---|
382 | * Replies the currently active layer
|
---|
383 | *
|
---|
384 | * @return the currently active layer (may be null)
|
---|
385 | */
|
---|
386 | public Layer getActiveLayer() {
|
---|
387 | return activeLayer;
|
---|
388 | }
|
---|
389 |
|
---|
390 | /**
|
---|
391 | * Replies the current edit layer, if any
|
---|
392 | *
|
---|
393 | * @return the current edit layer. May be null.
|
---|
394 | */
|
---|
395 | public OsmDataLayer getEditLayer() {
|
---|
396 | if (activeLayer instanceof OsmDataLayer)
|
---|
397 | return (OsmDataLayer)activeLayer;
|
---|
398 | return null;
|
---|
399 | }
|
---|
400 |
|
---|
401 | /**
|
---|
402 | * replies true if the list of layers managed by this map view contain layer
|
---|
403 | *
|
---|
404 | * @param layer the layer
|
---|
405 | * @return true if the list of layers managed by this map view contain layer
|
---|
406 | */
|
---|
407 | public boolean hasLayer(Layer layer) {
|
---|
408 | return layers.contains(layer);
|
---|
409 | }
|
---|
410 |
|
---|
411 | /**
|
---|
412 | * Tries to zoom to the download boundingbox[es] of the current edit layer
|
---|
413 | * (aka {@link OsmDataLayer}). If the edit layer has multiple download bounding
|
---|
414 | * boxes it zooms to a large virtual bounding box containing all smaller ones.
|
---|
415 | * This implementation can be used for resolving ticket #1461.
|
---|
416 | *
|
---|
417 | * @return <code>true</code> if a zoom operation has been performed
|
---|
418 | */
|
---|
419 | public boolean zoomToEditLayerBoundingBox() {
|
---|
420 | // workaround for #1461 (zoom to download bounding box instead of all data)
|
---|
421 | // In case we already have an existing data layer ...
|
---|
422 | OsmDataLayer layer= getEditLayer();
|
---|
423 | if (layer == null)
|
---|
424 | return false;
|
---|
425 | Collection<DataSource> dataSources = layer.data.dataSources;
|
---|
426 | // ... with bounding box[es] of data loaded from OSM or a file...
|
---|
427 | BoundingXYVisitor bbox = new BoundingXYVisitor();
|
---|
428 | for (DataSource ds : dataSources) {
|
---|
429 | bbox.visit(ds.bounds);
|
---|
430 | if (bbox.hasExtend()) {
|
---|
431 | // ... we zoom to it's bounding box
|
---|
432 | recalculateCenterScale(bbox);
|
---|
433 | return true;
|
---|
434 | }
|
---|
435 | }
|
---|
436 | return false;
|
---|
437 | }
|
---|
438 |
|
---|
439 | public boolean addTemporaryLayer(MapViewPaintable mvp) {
|
---|
440 | if (temporaryLayers.contains(mvp)) return false;
|
---|
441 | return temporaryLayers.add(mvp);
|
---|
442 | }
|
---|
443 |
|
---|
444 | public boolean removeTemporaryLayer(MapViewPaintable mvp) {
|
---|
445 | return temporaryLayers.remove(mvp);
|
---|
446 | }
|
---|
447 | }
|
---|