1 | // License: GPL. See LICENSE file for details.
|
---|
2 |
|
---|
3 | package org.openstreetmap.josm.gui.layer;
|
---|
4 |
|
---|
5 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
6 | import static org.openstreetmap.josm.tools.I18n.marktr;
|
---|
7 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
8 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
9 |
|
---|
10 | import java.awt.AlphaComposite;
|
---|
11 | import java.awt.Color;
|
---|
12 | import java.awt.Component;
|
---|
13 | import java.awt.Composite;
|
---|
14 | import java.awt.Graphics2D;
|
---|
15 | import java.awt.GridBagLayout;
|
---|
16 | import java.awt.Point;
|
---|
17 | import java.awt.Rectangle;
|
---|
18 | import java.awt.TexturePaint;
|
---|
19 | import java.awt.event.ActionEvent;
|
---|
20 | import java.awt.geom.Area;
|
---|
21 | import java.awt.image.BufferedImage;
|
---|
22 | import java.io.File;
|
---|
23 | import java.util.ArrayList;
|
---|
24 | import java.util.Collection;
|
---|
25 | import java.util.HashMap;
|
---|
26 | import java.util.HashSet;
|
---|
27 | import java.util.Iterator;
|
---|
28 | import java.util.LinkedList;
|
---|
29 | import java.util.Map;
|
---|
30 | import java.util.Set;
|
---|
31 |
|
---|
32 | import javax.swing.AbstractAction;
|
---|
33 | import javax.swing.Icon;
|
---|
34 | import javax.swing.JLabel;
|
---|
35 | import javax.swing.JMenuItem;
|
---|
36 | import javax.swing.JOptionPane;
|
---|
37 | import javax.swing.JPanel;
|
---|
38 | import javax.swing.JScrollPane;
|
---|
39 | import javax.swing.JSeparator;
|
---|
40 | import javax.swing.JTextArea;
|
---|
41 |
|
---|
42 | import org.openstreetmap.josm.Main;
|
---|
43 | import org.openstreetmap.josm.actions.RenameLayerAction;
|
---|
44 | import org.openstreetmap.josm.command.PurgePrimitivesCommand;
|
---|
45 | import org.openstreetmap.josm.data.Bounds;
|
---|
46 | import org.openstreetmap.josm.data.conflict.Conflict;
|
---|
47 | import org.openstreetmap.josm.data.conflict.ConflictCollection;
|
---|
48 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
49 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
50 | import org.openstreetmap.josm.data.gpx.GpxData;
|
---|
51 | import org.openstreetmap.josm.data.gpx.ImmutableGpxTrack;
|
---|
52 | import org.openstreetmap.josm.data.gpx.WayPoint;
|
---|
53 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
54 | import org.openstreetmap.josm.data.osm.DataSetMerger;
|
---|
55 | import org.openstreetmap.josm.data.osm.DataSource;
|
---|
56 | import org.openstreetmap.josm.data.osm.DatasetConsistencyTest;
|
---|
57 | import org.openstreetmap.josm.data.osm.Node;
|
---|
58 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
59 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
60 | import org.openstreetmap.josm.data.osm.Way;
|
---|
61 | import org.openstreetmap.josm.data.osm.visitor.AbstractVisitor;
|
---|
62 | import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
|
---|
63 | import org.openstreetmap.josm.data.osm.visitor.paint.MapPaintVisitor;
|
---|
64 | import org.openstreetmap.josm.data.osm.visitor.paint.PaintVisitor;
|
---|
65 | import org.openstreetmap.josm.data.osm.visitor.paint.SimplePaintVisitor;
|
---|
66 | import org.openstreetmap.josm.gui.HelpAwareOptionPane;
|
---|
67 | import org.openstreetmap.josm.gui.MapView;
|
---|
68 | import org.openstreetmap.josm.gui.HelpAwareOptionPane.ButtonSpec;
|
---|
69 | import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
|
---|
70 | import org.openstreetmap.josm.gui.dialogs.LayerListPopup;
|
---|
71 | import org.openstreetmap.josm.tools.DateUtils;
|
---|
72 | import org.openstreetmap.josm.tools.GBC;
|
---|
73 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * A layer holding data from a specific dataset.
|
---|
77 | * The data can be fully edited.
|
---|
78 | *
|
---|
79 | * @author imi
|
---|
80 | */
|
---|
81 | public class OsmDataLayer extends Layer {
|
---|
82 | static public final String REQUIRES_SAVE_TO_DISK_PROP = OsmDataLayer.class.getName() + ".requiresSaveToDisk";
|
---|
83 | static public final String REQUIRES_UPLOAD_TO_SERVER_PROP = OsmDataLayer.class.getName() + ".requiresUploadToServer";
|
---|
84 |
|
---|
85 | private boolean requiresSaveToFile = false;
|
---|
86 | private boolean requiresUploadToServer = false;
|
---|
87 |
|
---|
88 | protected void setRequiresSaveToFile(boolean newValue) {
|
---|
89 | boolean oldValue = requiresSaveToFile;
|
---|
90 | requiresSaveToFile = newValue;
|
---|
91 | if (oldValue != newValue) {
|
---|
92 | propertyChangeSupport.firePropertyChange(REQUIRES_SAVE_TO_DISK_PROP, oldValue, newValue);
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | protected void setRequiresUploadToServer(boolean newValue) {
|
---|
97 | boolean oldValue = requiresUploadToServer;
|
---|
98 | requiresUploadToServer = newValue;
|
---|
99 | if (oldValue != newValue) {
|
---|
100 | propertyChangeSupport.firePropertyChange(REQUIRES_UPLOAD_TO_SERVER_PROP, oldValue, newValue);
|
---|
101 | }
|
---|
102 | }
|
---|
103 |
|
---|
104 | /** the global counter for created data layers */
|
---|
105 | static private int dataLayerCounter = 0;
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * Replies a new unique name for a data layer
|
---|
109 | *
|
---|
110 | * @return a new unique name for a data layer
|
---|
111 | */
|
---|
112 | static public String createNewName() {
|
---|
113 | dataLayerCounter++;
|
---|
114 | return tr("Data Layer {0}", dataLayerCounter);
|
---|
115 | }
|
---|
116 |
|
---|
117 | public final static class DataCountVisitor extends AbstractVisitor {
|
---|
118 | public int nodes;
|
---|
119 | public int ways;
|
---|
120 | public int relations;
|
---|
121 | public int deletedNodes;
|
---|
122 | public int deletedWays;
|
---|
123 | public int deletedRelations;
|
---|
124 |
|
---|
125 | public void visit(final Node n) {
|
---|
126 | nodes++;
|
---|
127 | if (n.isDeleted()) {
|
---|
128 | deletedNodes++;
|
---|
129 | }
|
---|
130 | }
|
---|
131 |
|
---|
132 | public void visit(final Way w) {
|
---|
133 | ways++;
|
---|
134 | if (w.isDeleted()) {
|
---|
135 | deletedWays++;
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 | public void visit(final Relation r) {
|
---|
140 | relations++;
|
---|
141 | if (r.isDeleted()) {
|
---|
142 | deletedRelations++;
|
---|
143 | }
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 | public interface CommandQueueListener {
|
---|
148 | void commandChanged(int queueSize, int redoSize);
|
---|
149 | }
|
---|
150 |
|
---|
151 | /**
|
---|
152 | * The data behind this layer.
|
---|
153 | */
|
---|
154 | public final DataSet data;
|
---|
155 |
|
---|
156 | /**
|
---|
157 | * the collection of conflicts detected in this layer
|
---|
158 | */
|
---|
159 | private ConflictCollection conflicts;
|
---|
160 |
|
---|
161 | public final LinkedList<DataChangeListener> listenerDataChanged = new LinkedList<DataChangeListener>();
|
---|
162 |
|
---|
163 | /**
|
---|
164 | * a paint texture for non-downloaded area
|
---|
165 | */
|
---|
166 | private static TexturePaint hatched;
|
---|
167 |
|
---|
168 | static {
|
---|
169 | createHatchTexture();
|
---|
170 | }
|
---|
171 |
|
---|
172 | /**
|
---|
173 | * Initialize the hatch pattern used to paint the non-downloaded area
|
---|
174 | */
|
---|
175 | public static void createHatchTexture() {
|
---|
176 | BufferedImage bi = new BufferedImage(15, 15, BufferedImage.TYPE_INT_ARGB);
|
---|
177 | Graphics2D big = bi.createGraphics();
|
---|
178 | big.setColor(Main.pref.getColor(marktr("background"), Color.BLACK));
|
---|
179 | Composite comp = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f);
|
---|
180 | big.setComposite(comp);
|
---|
181 | big.fillRect(0,0,15,15);
|
---|
182 | big.setColor(Main.pref.getColor(marktr("outside downloaded area"), Color.YELLOW));
|
---|
183 | big.drawLine(0,15,15,0);
|
---|
184 | Rectangle r = new Rectangle(0, 0, 15,15);
|
---|
185 | hatched = new TexturePaint(bi, r);
|
---|
186 | }
|
---|
187 |
|
---|
188 | /**
|
---|
189 | * Construct a OsmDataLayer.
|
---|
190 | */
|
---|
191 | public OsmDataLayer(final DataSet data, final String name, final File associatedFile) {
|
---|
192 | super(name);
|
---|
193 | this.data = data;
|
---|
194 | this.setAssociatedFile(associatedFile);
|
---|
195 | conflicts = new ConflictCollection();
|
---|
196 | }
|
---|
197 |
|
---|
198 | /**
|
---|
199 | * TODO: @return Return a dynamic drawn icon of the map data. The icon is
|
---|
200 | * updated by a background thread to not disturb the running programm.
|
---|
201 | */
|
---|
202 | @Override public Icon getIcon() {
|
---|
203 | return ImageProvider.get("layer", "osmdata_small");
|
---|
204 | }
|
---|
205 |
|
---|
206 | /**
|
---|
207 | * Draw all primitives in this layer but do not draw modified ones (they
|
---|
208 | * are drawn by the edit layer).
|
---|
209 | * Draw nodes last to overlap the ways they belong to.
|
---|
210 | */
|
---|
211 | @Override public void paint(final Graphics2D g, final MapView mv, Bounds box) {
|
---|
212 | boolean active = mv.getActiveLayer() == this;
|
---|
213 | boolean inactive = !active && Main.pref.getBoolean("draw.data.inactive_color", true);
|
---|
214 | boolean virtual = !inactive && mv.isVirtualNodesEnabled();
|
---|
215 |
|
---|
216 | // draw the hatched area for non-downloaded region. only draw if we're the active
|
---|
217 | // and bounds are defined; don't draw for inactive layers or loaded GPX files etc
|
---|
218 | if (active && Main.pref.getBoolean("draw.data.downloaded_area", true) && !data.dataSources.isEmpty()) {
|
---|
219 | // initialize area with current viewport
|
---|
220 | Rectangle b = mv.getBounds();
|
---|
221 | // on some platforms viewport bounds seem to be offset from the left,
|
---|
222 | // over-grow it just to be sure
|
---|
223 | b.grow(100, 100);
|
---|
224 | Area a = new Area(b);
|
---|
225 |
|
---|
226 | // now succesively subtract downloaded areas
|
---|
227 | for (DataSource src : data.dataSources) {
|
---|
228 | if (src.bounds != null && !src.bounds.getMin().equals(src.bounds.getMax())) {
|
---|
229 | EastNorth en1 = mv.getProjection().latlon2eastNorth(src.bounds.getMin());
|
---|
230 | EastNorth en2 = mv.getProjection().latlon2eastNorth(src.bounds.getMax());
|
---|
231 | Point p1 = mv.getPoint(en1);
|
---|
232 | Point p2 = mv.getPoint(en2);
|
---|
233 | Rectangle r = new Rectangle(Math.min(p1.x, p2.x),Math.min(p1.y, p2.y),Math.abs(p2.x-p1.x),Math.abs(p2.y-p1.y));
|
---|
234 | a.subtract(new Area(r));
|
---|
235 | }
|
---|
236 | }
|
---|
237 |
|
---|
238 | // paint remainder
|
---|
239 | g.setPaint(hatched);
|
---|
240 | g.fill(a);
|
---|
241 | }
|
---|
242 |
|
---|
243 | PaintVisitor painter;
|
---|
244 | if (Main.pref.getBoolean("draw.wireframe")) {
|
---|
245 | painter = new SimplePaintVisitor();
|
---|
246 | } else {
|
---|
247 | painter = new MapPaintVisitor();
|
---|
248 | }
|
---|
249 | painter.setGraphics(g);
|
---|
250 | painter.setNavigatableComponent(mv);
|
---|
251 | painter.setInactive(inactive);
|
---|
252 | painter.visitAll(data, virtual, box);
|
---|
253 | Main.map.conflictDialog.paintConflicts(g, mv);
|
---|
254 | }
|
---|
255 |
|
---|
256 | @Override public String getToolTipText() {
|
---|
257 | int nodes = undeletedSize(data.getNodes());
|
---|
258 | int ways = undeletedSize(data.getWays());
|
---|
259 |
|
---|
260 | String tool = trn("{0} node", "{0} nodes", nodes, nodes)+", ";
|
---|
261 | tool += trn("{0} way", "{0} ways", ways, ways);
|
---|
262 |
|
---|
263 | if (data.getVersion() != null) {
|
---|
264 | tool += ", " + tr("version {0}", data.getVersion());
|
---|
265 | }
|
---|
266 | File f = getAssociatedFile();
|
---|
267 | if (f != null) {
|
---|
268 | tool = "<html>"+tool+"<br>"+f.getPath()+"</html>";
|
---|
269 | }
|
---|
270 | return tool;
|
---|
271 | }
|
---|
272 |
|
---|
273 | @Override public void mergeFrom(final Layer from) {
|
---|
274 | mergeFrom(((OsmDataLayer)from).data);
|
---|
275 | }
|
---|
276 |
|
---|
277 | /**
|
---|
278 | * merges the primitives in dataset <code>from</code> into the dataset of
|
---|
279 | * this layer
|
---|
280 | *
|
---|
281 | * @param from the source data set
|
---|
282 | */
|
---|
283 | public void mergeFrom(final DataSet from) {
|
---|
284 | final DataSetMerger visitor = new DataSetMerger(data,from);
|
---|
285 | visitor.merge();
|
---|
286 |
|
---|
287 | Area a = data.getDataSourceArea();
|
---|
288 |
|
---|
289 | // copy the merged layer's data source info;
|
---|
290 | // only add source rectangles if they are not contained in the
|
---|
291 | // layer already.
|
---|
292 | for (DataSource src : from.dataSources) {
|
---|
293 | if (a == null || !a.contains(src.bounds.asRect())) {
|
---|
294 | data.dataSources.add(src);
|
---|
295 | }
|
---|
296 | }
|
---|
297 |
|
---|
298 | // copy the merged layer's API version, downgrade if required
|
---|
299 | if (data.getVersion() == null) {
|
---|
300 | data.setVersion(from.getVersion());
|
---|
301 | } else if ("0.5".equals(data.getVersion()) ^ "0.5".equals(from.getVersion())) {
|
---|
302 | System.err.println(tr("Warning: mixing 0.6 and 0.5 data results in version 0.5"));
|
---|
303 | data.setVersion("0.5");
|
---|
304 | }
|
---|
305 |
|
---|
306 | int numNewConflicts = 0;
|
---|
307 | for (Conflict<?> c : visitor.getConflicts()) {
|
---|
308 | if (!conflicts.hasConflict(c)) {
|
---|
309 | numNewConflicts++;
|
---|
310 | conflicts.add(c);
|
---|
311 | }
|
---|
312 | }
|
---|
313 | PurgePrimitivesCommand cmd = buildPurgeCommand();
|
---|
314 | if (cmd != null) {
|
---|
315 | Main.main.undoRedo.add(cmd);
|
---|
316 | }
|
---|
317 | fireDataChange();
|
---|
318 | // repaint to make sure new data is displayed properly.
|
---|
319 | Main.map.mapView.repaint();
|
---|
320 | warnNumNewConflicts(
|
---|
321 | numNewConflicts,
|
---|
322 | cmd == null ? 0 : cmd.getPurgedPrimitives().size()
|
---|
323 | );
|
---|
324 | }
|
---|
325 |
|
---|
326 | /**
|
---|
327 | * Warns the user about the number of detected conflicts
|
---|
328 | *
|
---|
329 | * @param numNewConflicts the number of detected conflicts
|
---|
330 | * @param numPurgedPrimitives the number of automatically purged objects
|
---|
331 | */
|
---|
332 | protected void warnNumNewConflicts(int numNewConflicts, int numPurgedPrimitives) {
|
---|
333 | if (numNewConflicts == 0 && numPurgedPrimitives == 0) return;
|
---|
334 |
|
---|
335 | String msg1 = trn(
|
---|
336 | "There was {0} conflict detected.",
|
---|
337 | "There were {0} conflicts detected.",
|
---|
338 | numNewConflicts,
|
---|
339 | numNewConflicts
|
---|
340 | );
|
---|
341 | String msg2 = trn(
|
---|
342 | "{0} conflict has been <strong>resolved automatically</strong> by purging {0} object<br>from the local dataset because it is deleted on the server.",
|
---|
343 | "{0} conflicts have been <strong>resolved automatically</strong> by purging {0} objects<br> from the local dataset because they are deleted on the server.",
|
---|
344 | numPurgedPrimitives,
|
---|
345 | numPurgedPrimitives
|
---|
346 | );
|
---|
347 | int numRemainingConflicts = numNewConflicts - numPurgedPrimitives;
|
---|
348 | String msg3 = "";
|
---|
349 | if (numRemainingConflicts >0) {
|
---|
350 | msg3 = trn(
|
---|
351 | "{0} conflict remains to be resolved.<br><br>Please open the Conflict List Dialog and manually resolve it.",
|
---|
352 | "{0} conflicts remain to be resolved.<br><br>Please open the Conflict List Dialog and manually resolve them.",
|
---|
353 | numRemainingConflicts,
|
---|
354 | numRemainingConflicts
|
---|
355 | );
|
---|
356 | }
|
---|
357 |
|
---|
358 | StringBuffer sb = new StringBuffer();
|
---|
359 | sb.append("<html>").append(msg1);
|
---|
360 | if (numPurgedPrimitives > 0) {
|
---|
361 | sb.append("<br>").append(msg2);
|
---|
362 | }
|
---|
363 | if (numRemainingConflicts > 0) {
|
---|
364 | sb.append("<br>").append(msg3);
|
---|
365 | }
|
---|
366 | sb.append("</html>");
|
---|
367 | if (numNewConflicts > 0) {
|
---|
368 | ButtonSpec[] options = new ButtonSpec[] {
|
---|
369 | new ButtonSpec(
|
---|
370 | tr("OK"),
|
---|
371 | ImageProvider.get("ok"),
|
---|
372 | tr("Click to close this dialog and continue editing"),
|
---|
373 | null /* no specific help */
|
---|
374 | )
|
---|
375 | };
|
---|
376 | HelpAwareOptionPane.showOptionDialog(
|
---|
377 | Main.parent,
|
---|
378 | sb.toString(),
|
---|
379 | tr("Conflicts detected"),
|
---|
380 | JOptionPane.WARNING_MESSAGE,
|
---|
381 | null, /* no icon */
|
---|
382 | options,
|
---|
383 | options[0],
|
---|
384 | ht("/Concepts/Conflict#WarningAboutDetectedConflicts")
|
---|
385 | );
|
---|
386 | }
|
---|
387 | }
|
---|
388 |
|
---|
389 | /**
|
---|
390 | * Builds the purge command for primitives which can be purged automatically
|
---|
391 | * from the local dataset because they've been deleted on the
|
---|
392 | * server.
|
---|
393 | *
|
---|
394 | * @return the purge command. <code>null</code> if no primitives have to
|
---|
395 | * be purged
|
---|
396 | */
|
---|
397 | protected PurgePrimitivesCommand buildPurgeCommand() {
|
---|
398 | ArrayList<OsmPrimitive> toPurge = new ArrayList<OsmPrimitive>();
|
---|
399 | conflictLoop:
|
---|
400 | for (Conflict<?> c: conflicts) {
|
---|
401 | if (c.getMy().isDeleted() && !c.getTheir().isVisible()) {
|
---|
402 | // Local and server version of the primitive are deleted. We
|
---|
403 | // can purge it from the local dataset.
|
---|
404 | //
|
---|
405 | toPurge.add(c.getMy());
|
---|
406 | } else if (!c.getMy().isModified() && ! c.getTheir().isVisible()) {
|
---|
407 | // We purge deleted *ways* and *relations* automatically if they are
|
---|
408 | // deleted on the server and if they aren't modified in the local
|
---|
409 | // dataset.
|
---|
410 | //
|
---|
411 | if (c.getMy() instanceof Way || c.getMy() instanceof Relation) {
|
---|
412 | toPurge.add(c.getMy());
|
---|
413 | continue conflictLoop;
|
---|
414 | }
|
---|
415 | // We only purge nodes if they aren't part of a modified way.
|
---|
416 | // Otherwise the number of nodes of a modified way could drop
|
---|
417 | // below 2 and we would lose the modified data when the way
|
---|
418 | // gets purged.
|
---|
419 | //
|
---|
420 | for (OsmPrimitive parent: c.getMy().getReferrers()) {
|
---|
421 | if (parent.isModified() && parent instanceof Way) {
|
---|
422 | continue conflictLoop;
|
---|
423 | }
|
---|
424 | }
|
---|
425 | toPurge.add(c.getMy());
|
---|
426 | }
|
---|
427 | }
|
---|
428 | if (toPurge.isEmpty()) return null;
|
---|
429 | PurgePrimitivesCommand cmd = new PurgePrimitivesCommand(this, toPurge);
|
---|
430 | return cmd;
|
---|
431 | }
|
---|
432 |
|
---|
433 | @Override public boolean isMergable(final Layer other) {
|
---|
434 | return other instanceof OsmDataLayer;
|
---|
435 | }
|
---|
436 |
|
---|
437 | @Override public void visitBoundingBox(final BoundingXYVisitor v) {
|
---|
438 | for (final Node n: data.getNodes()) {
|
---|
439 | if (n.isUsable()) {
|
---|
440 | v.visit(n);
|
---|
441 | }
|
---|
442 | }
|
---|
443 | }
|
---|
444 |
|
---|
445 | /**
|
---|
446 | * Clean out the data behind the layer. This means clearing the redo/undo lists,
|
---|
447 | * really deleting all deleted objects and reset the modified flags. This should
|
---|
448 | * be done after an upload, even after a partial upload.
|
---|
449 | *
|
---|
450 | * @param processed A list of all objects that were actually uploaded.
|
---|
451 | * May be <code>null</code>, which means nothing has been uploaded
|
---|
452 | */
|
---|
453 | public void cleanupAfterUpload(final Collection<OsmPrimitive> processed) {
|
---|
454 | // return immediately if an upload attempt failed
|
---|
455 | if (processed == null || processed.isEmpty())
|
---|
456 | return;
|
---|
457 |
|
---|
458 | Main.main.undoRedo.clean(this);
|
---|
459 |
|
---|
460 | // if uploaded, clean the modified flags as well
|
---|
461 | final Set<OsmPrimitive> processedSet = new HashSet<OsmPrimitive>(processed);
|
---|
462 | data.clenupDeletedPrimitives();
|
---|
463 | for (final Iterator<Node> it = data.getNodes().iterator(); it.hasNext();) {
|
---|
464 | cleanIterator(it, processedSet);
|
---|
465 | }
|
---|
466 | for (final Iterator<Way> it = data.getWays().iterator(); it.hasNext();) {
|
---|
467 | cleanIterator(it, processedSet);
|
---|
468 | }
|
---|
469 | for (final Iterator<Relation> it = data.getRelations().iterator(); it.hasNext();) {
|
---|
470 | cleanIterator(it, processedSet);
|
---|
471 | }
|
---|
472 | }
|
---|
473 |
|
---|
474 | /**
|
---|
475 | * Clean the modified flag for the given iterator over a collection if it is in the
|
---|
476 | * list of processed entries.
|
---|
477 | *
|
---|
478 | * @param it The iterator to change the modified and remove the items if deleted.
|
---|
479 | * @param processed A list of all objects that have been successfully progressed.
|
---|
480 | * If the object in the iterator is not in the list, nothing will be changed on it.
|
---|
481 | */
|
---|
482 | private void cleanIterator(final Iterator<? extends OsmPrimitive> it, final Collection<OsmPrimitive> processed) {
|
---|
483 | final OsmPrimitive osm = it.next();
|
---|
484 | if (!processed.remove(osm))
|
---|
485 | return;
|
---|
486 | osm.setModified(false);
|
---|
487 | }
|
---|
488 |
|
---|
489 | /**
|
---|
490 | * @return The number of not-deleted and visible primitives in the list.
|
---|
491 | */
|
---|
492 | private int undeletedSize(final Collection<? extends OsmPrimitive> list) {
|
---|
493 | int size = 0;
|
---|
494 | for (final OsmPrimitive osm : list)
|
---|
495 | if (!osm.isDeleted() && osm.isVisible()) {
|
---|
496 | size++;
|
---|
497 | }
|
---|
498 | return size;
|
---|
499 | }
|
---|
500 |
|
---|
501 | @Override public Object getInfoComponent() {
|
---|
502 | final DataCountVisitor counter = new DataCountVisitor();
|
---|
503 | for (final OsmPrimitive osm : data.allPrimitives()) {
|
---|
504 | osm.visit(counter);
|
---|
505 | }
|
---|
506 | final JPanel p = new JPanel(new GridBagLayout());
|
---|
507 |
|
---|
508 | String nodeText = trn("{0} node", "{0} nodes", counter.nodes, counter.nodes);
|
---|
509 | if (counter.deletedNodes > 0) {
|
---|
510 | nodeText += " ("+trn("{0} deleted", "{0} deleted", counter.deletedNodes, counter.deletedNodes)+")";
|
---|
511 | }
|
---|
512 |
|
---|
513 | String wayText = trn("{0} way", "{0} ways", counter.ways, counter.ways);
|
---|
514 | if (counter.deletedWays > 0) {
|
---|
515 | wayText += " ("+trn("{0} deleted", "{0} deleted", counter.deletedWays, counter.deletedWays)+")";
|
---|
516 | }
|
---|
517 |
|
---|
518 | String relationText = trn("{0} relation", "{0} relations", counter.relations, counter.relations);
|
---|
519 | if (counter.deletedRelations > 0) {
|
---|
520 | relationText += " ("+trn("{0} deleted", "{0} deleted", counter.deletedRelations, counter.deletedRelations)+")";
|
---|
521 | }
|
---|
522 |
|
---|
523 | p.add(new JLabel(tr("{0} consists of:", getName())), GBC.eol());
|
---|
524 | p.add(new JLabel(nodeText, ImageProvider.get("data", "node"), JLabel.HORIZONTAL), GBC.eop().insets(15,0,0,0));
|
---|
525 | p.add(new JLabel(wayText, ImageProvider.get("data", "way"), JLabel.HORIZONTAL), GBC.eop().insets(15,0,0,0));
|
---|
526 | p.add(new JLabel(relationText, ImageProvider.get("data", "relation"), JLabel.HORIZONTAL), GBC.eop().insets(15,0,0,0));
|
---|
527 | p.add(new JLabel(tr("API version: {0}", (data.getVersion() != null) ? data.getVersion() : tr("unset"))));
|
---|
528 |
|
---|
529 | return p;
|
---|
530 | }
|
---|
531 |
|
---|
532 | @Override public Component[] getMenuEntries() {
|
---|
533 | if (Main.applet)
|
---|
534 | return new Component[]{
|
---|
535 | new JMenuItem(LayerListDialog.getInstance().createActivateLayerAction(this)),
|
---|
536 | new JMenuItem(LayerListDialog.getInstance().createShowHideLayerAction(this)),
|
---|
537 | new JMenuItem(LayerListDialog.getInstance().createDeleteLayerAction(this)),
|
---|
538 | new JSeparator(),
|
---|
539 | new JMenuItem(LayerListDialog.getInstance().createMergeLayerAction(this)),
|
---|
540 | new JSeparator(),
|
---|
541 | new JMenuItem(new RenameLayerAction(getAssociatedFile(), this)),
|
---|
542 | new JMenuItem(new ConsistencyTestAction()),
|
---|
543 | new JSeparator(),
|
---|
544 | new JMenuItem(new LayerListPopup.InfoAction(this))};
|
---|
545 | return new Component[]{
|
---|
546 | new JMenuItem(LayerListDialog.getInstance().createActivateLayerAction(this)),
|
---|
547 | new JMenuItem(LayerListDialog.getInstance().createShowHideLayerAction(this)),
|
---|
548 | new JMenuItem(LayerListDialog.getInstance().createDeleteLayerAction(this)),
|
---|
549 | new JSeparator(),
|
---|
550 | new JMenuItem(LayerListDialog.getInstance().createMergeLayerAction(this)),
|
---|
551 | new JMenuItem(new LayerSaveAction(this)),
|
---|
552 | new JMenuItem(new LayerSaveAsAction(this)),
|
---|
553 | new JMenuItem(new LayerGpxExportAction(this)),
|
---|
554 | new JMenuItem(new ConvertToGpxLayerAction()),
|
---|
555 | new JSeparator(),
|
---|
556 | new JMenuItem(new RenameLayerAction(getAssociatedFile(), this)),
|
---|
557 | new JMenuItem(new ConsistencyTestAction()),
|
---|
558 | new JSeparator(),
|
---|
559 | new JMenuItem(new LayerListPopup.InfoAction(this))};
|
---|
560 | }
|
---|
561 |
|
---|
562 | public void fireDataChange() {
|
---|
563 | setRequiresSaveToFile(true);
|
---|
564 | setRequiresUploadToServer(true);
|
---|
565 | for (DataChangeListener dcl : listenerDataChanged) {
|
---|
566 | dcl.dataChanged(this);
|
---|
567 | }
|
---|
568 | }
|
---|
569 |
|
---|
570 | public static GpxData toGpxData(DataSet data, File file) {
|
---|
571 | GpxData gpxData = new GpxData();
|
---|
572 | gpxData.storageFile = file;
|
---|
573 | HashSet<Node> doneNodes = new HashSet<Node>();
|
---|
574 | for (Way w : data.getWays()) {
|
---|
575 | if (!w.isUsable()) {
|
---|
576 | continue;
|
---|
577 | }
|
---|
578 | Collection<Collection<WayPoint>> trk = new ArrayList<Collection<WayPoint>>();
|
---|
579 | Map<String, Object> trkAttr = new HashMap<String, Object>();
|
---|
580 |
|
---|
581 | if (w.get("name") != null) {
|
---|
582 | trkAttr.put("name", w.get("name"));
|
---|
583 | }
|
---|
584 |
|
---|
585 | ArrayList<WayPoint> trkseg = null;
|
---|
586 | for (Node n : w.getNodes()) {
|
---|
587 | if (!n.isUsable()) {
|
---|
588 | trkseg = null;
|
---|
589 | continue;
|
---|
590 | }
|
---|
591 | if (trkseg == null) {
|
---|
592 | trkseg = new ArrayList<WayPoint>();
|
---|
593 | trk.add(trkseg);
|
---|
594 | }
|
---|
595 | if (!n.isTagged()) {
|
---|
596 | doneNodes.add(n);
|
---|
597 | }
|
---|
598 | WayPoint wpt = new WayPoint(n.getCoor());
|
---|
599 | if (!n.isTimestampEmpty()) {
|
---|
600 | wpt.attr.put("time", DateUtils.fromDate(n.getTimestamp()));
|
---|
601 | wpt.setTime();
|
---|
602 | }
|
---|
603 | trkseg.add(wpt);
|
---|
604 | }
|
---|
605 |
|
---|
606 | gpxData.tracks.add(new ImmutableGpxTrack(trk, trkAttr));
|
---|
607 | }
|
---|
608 |
|
---|
609 | // what is this loop meant to do? it creates waypoints but never
|
---|
610 | // records them?
|
---|
611 | for (Node n : data.getNodes()) {
|
---|
612 | if (n.isIncomplete() || n.isDeleted() || doneNodes.contains(n)) {
|
---|
613 | continue;
|
---|
614 | }
|
---|
615 | WayPoint wpt = new WayPoint(n.getCoor());
|
---|
616 | if (!n.isTimestampEmpty()) {
|
---|
617 | wpt.attr.put("time", DateUtils.fromDate(n.getTimestamp()));
|
---|
618 | wpt.setTime();
|
---|
619 | }
|
---|
620 | String name = n.get("name");
|
---|
621 | if (name != null) {
|
---|
622 | wpt.attr.put("name", name);
|
---|
623 | }
|
---|
624 | }
|
---|
625 | return gpxData;
|
---|
626 | }
|
---|
627 |
|
---|
628 | public GpxData toGpxData() {
|
---|
629 | return toGpxData(data, getAssociatedFile());
|
---|
630 | }
|
---|
631 |
|
---|
632 | public class ConvertToGpxLayerAction extends AbstractAction {
|
---|
633 | public ConvertToGpxLayerAction() {
|
---|
634 | super(tr("Convert to GPX layer"), ImageProvider.get("converttogpx"));
|
---|
635 | }
|
---|
636 | public void actionPerformed(ActionEvent e) {
|
---|
637 | Main.main.addLayer(new GpxLayer(toGpxData(), tr("Converted from: {0}", getName())));
|
---|
638 | Main.main.removeLayer(OsmDataLayer.this);
|
---|
639 | }
|
---|
640 | }
|
---|
641 |
|
---|
642 | public boolean containsPoint(LatLon coor) {
|
---|
643 | // we'll assume that if this has no data sources
|
---|
644 | // that it also has no borders
|
---|
645 | if (this.data.dataSources.isEmpty())
|
---|
646 | return true;
|
---|
647 |
|
---|
648 | boolean layer_bounds_point = false;
|
---|
649 | for (DataSource src : this.data.dataSources) {
|
---|
650 | if (src.bounds.contains(coor)) {
|
---|
651 | layer_bounds_point = true;
|
---|
652 | break;
|
---|
653 | }
|
---|
654 | }
|
---|
655 | return layer_bounds_point;
|
---|
656 | }
|
---|
657 |
|
---|
658 | /**
|
---|
659 | * replies the set of conflicts currently managed in this layer
|
---|
660 | *
|
---|
661 | * @return the set of conflicts currently managed in this layer
|
---|
662 | */
|
---|
663 | public ConflictCollection getConflicts() {
|
---|
664 | return conflicts;
|
---|
665 | }
|
---|
666 |
|
---|
667 | /**
|
---|
668 | * Replies true if the data managed by this layer needs to be uploaded to
|
---|
669 | * the server because it contains at least one modified primitive.
|
---|
670 | *
|
---|
671 | * @return true if the data managed by this layer needs to be uploaded to
|
---|
672 | * the server because it contains at least one modified primitive; false,
|
---|
673 | * otherwise
|
---|
674 | */
|
---|
675 | public boolean requiresUploadToServer() {
|
---|
676 | return requiresUploadToServer;
|
---|
677 | }
|
---|
678 |
|
---|
679 | /**
|
---|
680 | * Replies true if the data managed by this layer needs to be saved to
|
---|
681 | * a file. Only replies true if a file is assigned to this layer and
|
---|
682 | * if the data managed by this layer has been modified since the last
|
---|
683 | * save operation to the file.
|
---|
684 | *
|
---|
685 | * @return true if the data managed by this layer needs to be saved to
|
---|
686 | * a file
|
---|
687 | */
|
---|
688 | public boolean requiresSaveToFile() {
|
---|
689 | return getAssociatedFile() != null && requiresSaveToFile;
|
---|
690 | }
|
---|
691 |
|
---|
692 | /**
|
---|
693 | * Initializes the layer after a successful load of OSM data from a file
|
---|
694 | *
|
---|
695 | */
|
---|
696 | public void onPostLoadFromFile() {
|
---|
697 | setRequiresSaveToFile(false);
|
---|
698 | setRequiresUploadToServer(data.isModified());
|
---|
699 | }
|
---|
700 |
|
---|
701 | public void onPostDownloadFromServer() {
|
---|
702 | setRequiresSaveToFile(true);
|
---|
703 | setRequiresUploadToServer(data.isModified());
|
---|
704 | }
|
---|
705 |
|
---|
706 | /**
|
---|
707 | * Initializes the layer after a successful save of OSM data to a file
|
---|
708 | *
|
---|
709 | */
|
---|
710 | public void onPostSaveToFile() {
|
---|
711 | setRequiresSaveToFile(false);
|
---|
712 | setRequiresUploadToServer(data.isModified());
|
---|
713 | }
|
---|
714 |
|
---|
715 | /**
|
---|
716 | * Initializes the layer after a successful upload to the server
|
---|
717 | *
|
---|
718 | */
|
---|
719 | public void onPostUploadToServer() {
|
---|
720 | setRequiresUploadToServer(data.isModified());
|
---|
721 | // keep requiresSaveToDisk unchanged
|
---|
722 | }
|
---|
723 |
|
---|
724 | private class ConsistencyTestAction extends AbstractAction {
|
---|
725 |
|
---|
726 | public ConsistencyTestAction() {
|
---|
727 | super(tr("Dataset consistency test"));
|
---|
728 | }
|
---|
729 |
|
---|
730 | public void actionPerformed(ActionEvent e) {
|
---|
731 | String result = DatasetConsistencyTest.runTests(data);
|
---|
732 | if (result.length() == 0) {
|
---|
733 | JOptionPane.showMessageDialog(Main.parent, tr("No problems found"));
|
---|
734 | } else {
|
---|
735 | JPanel p = new JPanel(new GridBagLayout());
|
---|
736 | p.add(new JLabel(tr("Following problems found:")), GBC.eol());
|
---|
737 | JTextArea info = new JTextArea(result, 20, 60);
|
---|
738 | info.setCaretPosition(0);
|
---|
739 | info.setEditable(false);
|
---|
740 | p.add(new JScrollPane(info), GBC.eop());
|
---|
741 |
|
---|
742 | JOptionPane.showMessageDialog(Main.parent, p, tr("Warning"), JOptionPane.WARNING_MESSAGE);
|
---|
743 | }
|
---|
744 | }
|
---|
745 |
|
---|
746 | }
|
---|
747 | }
|
---|