1 | // License: GPL. See LICENSE file for details.
|
---|
2 |
|
---|
3 | package org.openstreetmap.josm.gui.layer;
|
---|
4 |
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.marktr;
|
---|
6 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
7 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
8 |
|
---|
9 | import java.awt.Color;
|
---|
10 | import java.awt.Component;
|
---|
11 | import java.awt.Graphics;
|
---|
12 | import java.awt.GridBagLayout;
|
---|
13 | import java.awt.Point;
|
---|
14 | import java.awt.event.ActionEvent;
|
---|
15 | import java.io.File;
|
---|
16 | import java.util.Collection;
|
---|
17 | import java.util.HashSet;
|
---|
18 | import java.util.Iterator;
|
---|
19 | import java.util.LinkedList;
|
---|
20 | import java.util.Set;
|
---|
21 | import java.util.ArrayList;
|
---|
22 |
|
---|
23 | import javax.swing.AbstractAction;
|
---|
24 | import javax.swing.Icon;
|
---|
25 | import javax.swing.JLabel;
|
---|
26 | import javax.swing.JMenuItem;
|
---|
27 | import javax.swing.JOptionPane;
|
---|
28 | import javax.swing.JPanel;
|
---|
29 | import javax.swing.JSeparator;
|
---|
30 |
|
---|
31 | import org.openstreetmap.josm.Main;
|
---|
32 | import org.openstreetmap.josm.actions.GpxExportAction;
|
---|
33 | import org.openstreetmap.josm.actions.RenameLayerAction;
|
---|
34 | import org.openstreetmap.josm.actions.SaveAction;
|
---|
35 | import org.openstreetmap.josm.actions.SaveAsAction;
|
---|
36 | import org.openstreetmap.josm.command.Command;
|
---|
37 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
38 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
39 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
40 | import org.openstreetmap.josm.data.osm.DataSource;
|
---|
41 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
42 | import org.openstreetmap.josm.data.osm.Node;
|
---|
43 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
44 | import org.openstreetmap.josm.data.osm.Way;
|
---|
45 | import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
|
---|
46 | import org.openstreetmap.josm.data.osm.visitor.MapPaintVisitor;
|
---|
47 | import org.openstreetmap.josm.data.osm.visitor.MergeVisitor;
|
---|
48 | import org.openstreetmap.josm.data.osm.visitor.SimplePaintVisitor;
|
---|
49 | import org.openstreetmap.josm.data.osm.visitor.Visitor;
|
---|
50 | import org.openstreetmap.josm.data.gpx.GpxData;
|
---|
51 | import org.openstreetmap.josm.data.gpx.GpxTrack;
|
---|
52 | import org.openstreetmap.josm.data.gpx.WayPoint;
|
---|
53 | import org.openstreetmap.josm.gui.MapView;
|
---|
54 | import org.openstreetmap.josm.gui.dialogs.ConflictDialog;
|
---|
55 | import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
|
---|
56 | import org.openstreetmap.josm.gui.dialogs.LayerListPopup;
|
---|
57 | import org.openstreetmap.josm.tools.GBC;
|
---|
58 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * A layer holding data from a specific dataset.
|
---|
62 | * The data can be fully edited.
|
---|
63 | *
|
---|
64 | * @author imi
|
---|
65 | */
|
---|
66 | public class OsmDataLayer extends Layer {
|
---|
67 |
|
---|
68 | public final static class DataCountVisitor implements Visitor {
|
---|
69 | public final int[] normal = new int[3];
|
---|
70 | public final int[] deleted = new int[3];
|
---|
71 | public final String[] names = {"node", "way", "relation"};
|
---|
72 |
|
---|
73 | private void inc(final OsmPrimitive osm, final int i) {
|
---|
74 | normal[i]++;
|
---|
75 | if (osm.deleted)
|
---|
76 | deleted[i]++;
|
---|
77 | }
|
---|
78 |
|
---|
79 | public void visit(final Node n) {
|
---|
80 | inc(n, 0);
|
---|
81 | }
|
---|
82 |
|
---|
83 | public void visit(final Way w) {
|
---|
84 | inc(w, 1);
|
---|
85 | }
|
---|
86 | public void visit(final Relation w) {
|
---|
87 | inc(w, 2);
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | public interface ModifiedChangedListener {
|
---|
92 | void modifiedChanged(boolean value, OsmDataLayer source);
|
---|
93 | }
|
---|
94 | public interface CommandQueueListener {
|
---|
95 | void commandChanged(int queueSize, int redoSize);
|
---|
96 | }
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * @deprecated Use Main.main.undoRedo.add(...) instead.
|
---|
100 | */
|
---|
101 | @Deprecated public void add(final Command c) {
|
---|
102 | Main.main.undoRedo.add(c);
|
---|
103 | }
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * The data behind this layer.
|
---|
107 | */
|
---|
108 | public final DataSet data;
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * Whether the data of this layer was modified during the session.
|
---|
112 | */
|
---|
113 | private boolean modified = false;
|
---|
114 | /**
|
---|
115 | * Whether the data was modified due an upload of the data to the server.
|
---|
116 | */
|
---|
117 | public boolean uploadedModified = false;
|
---|
118 |
|
---|
119 | public final LinkedList<ModifiedChangedListener> listenerModified = new LinkedList<ModifiedChangedListener>();
|
---|
120 | public final LinkedList<DataChangeListener> listenerDataChanged = new LinkedList<DataChangeListener>();
|
---|
121 |
|
---|
122 | /**
|
---|
123 | * Construct a OsmDataLayer.
|
---|
124 | */
|
---|
125 | public OsmDataLayer(final DataSet data, final String name, final File associatedFile) {
|
---|
126 | super(name);
|
---|
127 | this.data = data;
|
---|
128 | this.associatedFile = associatedFile;
|
---|
129 | }
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * TODO: @return Return a dynamic drawn icon of the map data. The icon is
|
---|
133 | * updated by a background thread to not disturb the running programm.
|
---|
134 | */
|
---|
135 | @Override public Icon getIcon() {
|
---|
136 | return ImageProvider.get("layer", "osmdata_small");
|
---|
137 | }
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * Draw all primitives in this layer but do not draw modified ones (they
|
---|
141 | * are drawn by the edit layer).
|
---|
142 | * Draw nodes last to overlap the ways they belong to.
|
---|
143 | */
|
---|
144 | @Override public void paint(final Graphics g, final MapView mv) {
|
---|
145 | boolean inactive = Main.map.mapView.getActiveLayer() != this && Main.pref.getBoolean("draw.data.inactive_color", true);
|
---|
146 | boolean virtual = !inactive && Main.map.mapView.useVirtualNodes();
|
---|
147 | if (Main.pref.getBoolean("draw.data.downloaded_area", true)) {
|
---|
148 | // FIXME this is inefficient; instead a proper polygon has to be built, and instead
|
---|
149 | // of drawing the outline, the outlying areas should perhaps be shaded.
|
---|
150 | for (DataSource src : data.dataSources) {
|
---|
151 | if (src.bounds != null && !src.bounds.min.equals(src.bounds.max)) {
|
---|
152 | EastNorth en1 = Main.proj.latlon2eastNorth(src.bounds.min);
|
---|
153 | EastNorth en2 = Main.proj.latlon2eastNorth(src.bounds.max);
|
---|
154 | Point p1 = mv.getPoint(en1);
|
---|
155 | Point p2 = mv.getPoint(en2);
|
---|
156 | Color color = inactive ? Main.pref.getColor(marktr("inactive"), Color.DARK_GRAY) :
|
---|
157 | Main.pref.getColor(marktr("downloaded Area"), Color.YELLOW);
|
---|
158 | g.setColor(color);
|
---|
159 | g.drawRect(Math.min(p1.x,p2.x), Math.min(p1.y, p2.y), Math.abs(p2.x-p1.x), Math.abs(p2.y-p1.y));
|
---|
160 | }
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 |
|
---|
165 | SimplePaintVisitor painter;
|
---|
166 | if (Main.pref.getBoolean("draw.wireframe"))
|
---|
167 | painter = new SimplePaintVisitor();
|
---|
168 | else
|
---|
169 | painter = new MapPaintVisitor();
|
---|
170 | painter.setGraphics(g);
|
---|
171 | painter.setNavigatableComponent(mv);
|
---|
172 | painter.inactive = inactive;
|
---|
173 | painter.visitAll(data, virtual);
|
---|
174 | Main.map.conflictDialog.paintConflicts(g, mv);
|
---|
175 | }
|
---|
176 |
|
---|
177 | @Override public String getToolTipText() {
|
---|
178 | String tool = "";
|
---|
179 | tool += undeletedSize(data.nodes)+" "+trn("node", "nodes", undeletedSize(data.nodes))+", ";
|
---|
180 | tool += undeletedSize(data.ways)+" "+trn("way", "ways", undeletedSize(data.ways));
|
---|
181 | if (associatedFile != null)
|
---|
182 | tool = "<html>"+tool+"<br>"+associatedFile.getPath()+"</html>";
|
---|
183 | return tool;
|
---|
184 | }
|
---|
185 |
|
---|
186 | @Override public void mergeFrom(final Layer from) {
|
---|
187 | final MergeVisitor visitor = new MergeVisitor(data,((OsmDataLayer)from).data);
|
---|
188 | for (final OsmPrimitive osm : ((OsmDataLayer)from).data.allPrimitives())
|
---|
189 | osm.visit(visitor);
|
---|
190 | visitor.fixReferences();
|
---|
191 |
|
---|
192 | // copy the merged layer's data source info
|
---|
193 | for (DataSource src : ((OsmDataLayer)from).data.dataSources)
|
---|
194 | data.dataSources.add(src);
|
---|
195 | fireDataChange();
|
---|
196 | // repaint to make sure new data is displayed properly.
|
---|
197 | Main.map.mapView.repaint();
|
---|
198 |
|
---|
199 | if (visitor.conflicts.isEmpty())
|
---|
200 | return;
|
---|
201 | final ConflictDialog dlg = Main.map.conflictDialog;
|
---|
202 | dlg.add(visitor.conflicts);
|
---|
203 | JOptionPane.showMessageDialog(Main.parent,tr("There were conflicts during import."));
|
---|
204 | if (!dlg.isVisible())
|
---|
205 | dlg.action.actionPerformed(new ActionEvent(this, 0, ""));
|
---|
206 | }
|
---|
207 |
|
---|
208 | @Override public boolean isMergable(final Layer other) {
|
---|
209 | return other instanceof OsmDataLayer;
|
---|
210 | }
|
---|
211 |
|
---|
212 | @Override public void visitBoundingBox(final BoundingXYVisitor v) {
|
---|
213 | for (final Node n : data.nodes)
|
---|
214 | if (!n.deleted && !n.incomplete)
|
---|
215 | v.visit(n);
|
---|
216 | }
|
---|
217 |
|
---|
218 | /**
|
---|
219 | * Clean out the data behind the layer. This means clearing the redo/undo lists,
|
---|
220 | * really deleting all deleted objects and reset the modified flags. This is done
|
---|
221 | * after a successfull upload.
|
---|
222 | *
|
---|
223 | * @param processed A list of all objects that were actually uploaded.
|
---|
224 | * May be <code>null</code>, which means nothing has been uploaded but
|
---|
225 | * saved to disk instead. Note that an empty collection for "processed"
|
---|
226 | * means that an upload has been attempted but failed.
|
---|
227 | */
|
---|
228 | public void cleanData(final Collection<OsmPrimitive> processed, boolean dataAdded) {
|
---|
229 |
|
---|
230 | // return immediately if an upload attempt failed
|
---|
231 | if (processed != null && processed.isEmpty() && !dataAdded)
|
---|
232 | return;
|
---|
233 |
|
---|
234 | Main.main.undoRedo.clean();
|
---|
235 |
|
---|
236 | // if uploaded, clean the modified flags as well
|
---|
237 | if (processed != null) {
|
---|
238 | final Set<OsmPrimitive> processedSet = new HashSet<OsmPrimitive>(processed);
|
---|
239 | for (final Iterator<Node> it = data.nodes.iterator(); it.hasNext();)
|
---|
240 | cleanIterator(it, processedSet);
|
---|
241 | for (final Iterator<Way> it = data.ways.iterator(); it.hasNext();)
|
---|
242 | cleanIterator(it, processedSet);
|
---|
243 | for (final Iterator<Relation> it = data.relations.iterator(); it.hasNext();)
|
---|
244 | cleanIterator(it, processedSet);
|
---|
245 | }
|
---|
246 |
|
---|
247 | // update the modified flag
|
---|
248 | if (associatedFile != null && processed != null && !dataAdded)
|
---|
249 | return; // do nothing when uploading non-harmful changes.
|
---|
250 |
|
---|
251 | // modified if server changed the data (esp. the id).
|
---|
252 | uploadedModified = associatedFile != null && processed != null && dataAdded;
|
---|
253 | setModified(uploadedModified);
|
---|
254 | }
|
---|
255 |
|
---|
256 | /**
|
---|
257 | * Clean the modified flag for the given iterator over a collection if it is in the
|
---|
258 | * list of processed entries.
|
---|
259 | *
|
---|
260 | * @param it The iterator to change the modified and remove the items if deleted.
|
---|
261 | * @param processed A list of all objects that have been successfully progressed.
|
---|
262 | * If the object in the iterator is not in the list, nothing will be changed on it.
|
---|
263 | */
|
---|
264 | private void cleanIterator(final Iterator<? extends OsmPrimitive> it, final Collection<OsmPrimitive> processed) {
|
---|
265 | final OsmPrimitive osm = it.next();
|
---|
266 | if (!processed.remove(osm))
|
---|
267 | return;
|
---|
268 | osm.modified = false;
|
---|
269 | if (osm.deleted)
|
---|
270 | it.remove();
|
---|
271 | }
|
---|
272 |
|
---|
273 | public boolean isModified() {
|
---|
274 | return modified;
|
---|
275 | }
|
---|
276 |
|
---|
277 | public void setModified(final boolean modified) {
|
---|
278 | if (modified == this.modified)
|
---|
279 | return;
|
---|
280 | this.modified = modified;
|
---|
281 | for (final ModifiedChangedListener l : listenerModified)
|
---|
282 | l.modifiedChanged(modified, this);
|
---|
283 | }
|
---|
284 |
|
---|
285 | /**
|
---|
286 | * @return The number of not-deleted primitives in the list.
|
---|
287 | */
|
---|
288 | private int undeletedSize(final Collection<? extends OsmPrimitive> list) {
|
---|
289 | int size = 0;
|
---|
290 | for (final OsmPrimitive osm : list)
|
---|
291 | if (!osm.deleted)
|
---|
292 | size++;
|
---|
293 | return size;
|
---|
294 | }
|
---|
295 |
|
---|
296 | @Override public Object getInfoComponent() {
|
---|
297 | final DataCountVisitor counter = new DataCountVisitor();
|
---|
298 | for (final OsmPrimitive osm : data.allPrimitives())
|
---|
299 | osm.visit(counter);
|
---|
300 | final JPanel p = new JPanel(new GridBagLayout());
|
---|
301 | p.add(new JLabel(tr("{0} consists of:", name)), GBC.eol());
|
---|
302 | for (int i = 0; i < counter.normal.length; ++i) {
|
---|
303 | String s = counter.normal[i]+" "+trn(counter.names[i],counter.names[i]+"s",counter.normal[i]);
|
---|
304 | if (counter.deleted[i] > 0)
|
---|
305 | s += tr(" ({0} deleted.)",counter.deleted[i]);
|
---|
306 | p.add(new JLabel(s, ImageProvider.get("data", counter.names[i]), JLabel.HORIZONTAL), GBC.eop().insets(15,0,0,0));
|
---|
307 | }
|
---|
308 | return p;
|
---|
309 | }
|
---|
310 |
|
---|
311 | @Override public Component[] getMenuEntries() {
|
---|
312 | if (Main.applet) {
|
---|
313 | return new Component[]{
|
---|
314 | new JMenuItem(new LayerListDialog.ShowHideLayerAction(this)),
|
---|
315 | new JMenuItem(new LayerListDialog.DeleteLayerAction(this)),
|
---|
316 | new JSeparator(),
|
---|
317 | new JMenuItem(new RenameLayerAction(associatedFile, this)),
|
---|
318 | new JSeparator(),
|
---|
319 | new JMenuItem(new LayerListPopup.InfoAction(this))};
|
---|
320 | }
|
---|
321 | return new Component[]{
|
---|
322 | new JMenuItem(new LayerListDialog.ShowHideLayerAction(this)),
|
---|
323 | new JMenuItem(new LayerListDialog.DeleteLayerAction(this)),
|
---|
324 | new JSeparator(),
|
---|
325 | new JMenuItem(new SaveAction(this)),
|
---|
326 | new JMenuItem(new SaveAsAction(this)),
|
---|
327 | new JMenuItem(new GpxExportAction(this)),
|
---|
328 | new JMenuItem(new ConvertToGpxLayerAction()),
|
---|
329 | new JSeparator(),
|
---|
330 | new JMenuItem(new RenameLayerAction(associatedFile, this)),
|
---|
331 | new JSeparator(),
|
---|
332 | new JMenuItem(new LayerListPopup.InfoAction(this))};
|
---|
333 | }
|
---|
334 |
|
---|
335 | public void fireDataChange() {
|
---|
336 | for (DataChangeListener dcl : listenerDataChanged) {
|
---|
337 | dcl.dataChanged(this);
|
---|
338 | }
|
---|
339 | }
|
---|
340 |
|
---|
341 | public static GpxData toGpxData(DataSet data) {
|
---|
342 | GpxData gpxData = new GpxData();
|
---|
343 | HashSet<Node> doneNodes = new HashSet<Node>();
|
---|
344 | for (Way w : data.ways) {
|
---|
345 | if (w.incomplete || w.deleted) continue;
|
---|
346 | GpxTrack trk = new GpxTrack();
|
---|
347 | gpxData.tracks.add(trk);
|
---|
348 |
|
---|
349 | if (w.get("name") != null)
|
---|
350 | trk.attr.put("name", w.get("name"));
|
---|
351 |
|
---|
352 | ArrayList<WayPoint> trkseg = null;
|
---|
353 | for (Node n : w.nodes) {
|
---|
354 | if (n.incomplete || n.deleted) {
|
---|
355 | trkseg = null;
|
---|
356 | continue;
|
---|
357 | }
|
---|
358 | if (trkseg == null) {
|
---|
359 | trkseg = new ArrayList<WayPoint>();
|
---|
360 | trk.trackSegs.add(trkseg);
|
---|
361 | }
|
---|
362 | if (!n.tagged) {
|
---|
363 | doneNodes.add(n);
|
---|
364 | }
|
---|
365 | WayPoint wpt = new WayPoint(n.coor);
|
---|
366 | if (n.timestamp != null)
|
---|
367 | {
|
---|
368 | wpt.attr.put("time", n.timestamp);
|
---|
369 | wpt.setTime();
|
---|
370 | }
|
---|
371 | trkseg.add(wpt);
|
---|
372 | }
|
---|
373 | }
|
---|
374 |
|
---|
375 | // what is this loop meant to do? it creates waypoints but never
|
---|
376 | // records them?
|
---|
377 | for (Node n : data.nodes) {
|
---|
378 | if (n.incomplete || n.deleted || doneNodes.contains(n)) continue;
|
---|
379 | WayPoint wpt = new WayPoint(n.coor);
|
---|
380 | if (n.timestamp != null) {
|
---|
381 | wpt.attr.put("time", n.timestamp);
|
---|
382 | wpt.setTime();
|
---|
383 | }
|
---|
384 | if (n.keys != null && n.keys.containsKey("name")) {
|
---|
385 | wpt.attr.put("name", n.keys.get("name"));
|
---|
386 | }
|
---|
387 | }
|
---|
388 | return gpxData;
|
---|
389 | }
|
---|
390 |
|
---|
391 | public GpxData toGpxData() {
|
---|
392 | return toGpxData(data);
|
---|
393 | }
|
---|
394 |
|
---|
395 | public class ConvertToGpxLayerAction extends AbstractAction {
|
---|
396 | public ConvertToGpxLayerAction() {
|
---|
397 | super(tr("Convert to GPX layer"), ImageProvider.get("converttogpx"));
|
---|
398 | }
|
---|
399 | public void actionPerformed(ActionEvent e) {
|
---|
400 | Main.main.addLayer(new GpxLayer(toGpxData(), tr("Converted from: {0}", name)));
|
---|
401 | Main.main.removeLayer(OsmDataLayer.this);
|
---|
402 | }
|
---|
403 | }
|
---|
404 |
|
---|
405 | public boolean containsPoint(LatLon coor)
|
---|
406 | {
|
---|
407 | // we'll assume that if this has no data sources
|
---|
408 | // that it also has no borders
|
---|
409 | if (this.data.dataSources.isEmpty())
|
---|
410 | return true;
|
---|
411 |
|
---|
412 | boolean layer_bounds_point = false;
|
---|
413 | for (DataSource src : this.data.dataSources) {
|
---|
414 | if (src.bounds.contains(coor)) {
|
---|
415 | layer_bounds_point = true;
|
---|
416 | break;
|
---|
417 | }
|
---|
418 | }
|
---|
419 | return layer_bounds_point;
|
---|
420 | }
|
---|
421 | }
|
---|