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