source: josm/trunk/src/org/openstreetmap/josm/gui/layer/OsmDataLayer.java@ 11167

Last change on this file since 11167 was 11033, checked in by simon04, 8 years ago

fix #13663 - Two data layers with the same name

Counting starts from 1 again in an saved session file.

  • Property svn:eol-style set to native
File size: 39.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.layer;
3
4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
5import static org.openstreetmap.josm.tools.I18n.marktr;
6import static org.openstreetmap.josm.tools.I18n.tr;
7import static org.openstreetmap.josm.tools.I18n.trn;
8
9import java.awt.AlphaComposite;
10import java.awt.Color;
11import java.awt.Composite;
12import java.awt.Graphics2D;
13import java.awt.GraphicsEnvironment;
14import java.awt.GridBagLayout;
15import java.awt.Rectangle;
16import java.awt.TexturePaint;
17import java.awt.event.ActionEvent;
18import java.awt.geom.Area;
19import java.awt.geom.Rectangle2D;
20import java.awt.image.BufferedImage;
21import java.io.File;
22import java.util.ArrayList;
23import java.util.Arrays;
24import java.util.Collection;
25import java.util.Collections;
26import java.util.HashMap;
27import java.util.HashSet;
28import java.util.LinkedHashMap;
29import java.util.List;
30import java.util.Map;
31import java.util.Set;
32import java.util.concurrent.CopyOnWriteArrayList;
33import java.util.concurrent.atomic.AtomicInteger;
34import java.util.regex.Pattern;
35
36import javax.swing.AbstractAction;
37import javax.swing.Action;
38import javax.swing.Icon;
39import javax.swing.JLabel;
40import javax.swing.JOptionPane;
41import javax.swing.JPanel;
42import javax.swing.JScrollPane;
43
44import org.openstreetmap.josm.Main;
45import org.openstreetmap.josm.actions.ExpertToggleAction;
46import org.openstreetmap.josm.actions.RenameLayerAction;
47import org.openstreetmap.josm.actions.ToggleUploadDiscouragedLayerAction;
48import org.openstreetmap.josm.data.APIDataSet;
49import org.openstreetmap.josm.data.Bounds;
50import org.openstreetmap.josm.data.DataSource;
51import org.openstreetmap.josm.data.ProjectionBounds;
52import org.openstreetmap.josm.data.SelectionChangedListener;
53import org.openstreetmap.josm.data.conflict.Conflict;
54import org.openstreetmap.josm.data.conflict.ConflictCollection;
55import org.openstreetmap.josm.data.coor.EastNorth;
56import org.openstreetmap.josm.data.coor.LatLon;
57import org.openstreetmap.josm.data.gpx.GpxConstants;
58import org.openstreetmap.josm.data.gpx.GpxData;
59import org.openstreetmap.josm.data.gpx.GpxLink;
60import org.openstreetmap.josm.data.gpx.ImmutableGpxTrack;
61import org.openstreetmap.josm.data.gpx.WayPoint;
62import org.openstreetmap.josm.data.osm.DataIntegrityProblemException;
63import org.openstreetmap.josm.data.osm.DataSet;
64import org.openstreetmap.josm.data.osm.DataSetMerger;
65import org.openstreetmap.josm.data.osm.DatasetConsistencyTest;
66import org.openstreetmap.josm.data.osm.IPrimitive;
67import org.openstreetmap.josm.data.osm.Node;
68import org.openstreetmap.josm.data.osm.OsmPrimitive;
69import org.openstreetmap.josm.data.osm.OsmPrimitiveComparator;
70import org.openstreetmap.josm.data.osm.Relation;
71import org.openstreetmap.josm.data.osm.Way;
72import org.openstreetmap.josm.data.osm.event.AbstractDatasetChangedEvent;
73import org.openstreetmap.josm.data.osm.event.DataSetListenerAdapter;
74import org.openstreetmap.josm.data.osm.event.DataSetListenerAdapter.Listener;
75import org.openstreetmap.josm.data.osm.visitor.AbstractVisitor;
76import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
77import org.openstreetmap.josm.data.osm.visitor.paint.MapRendererFactory;
78import org.openstreetmap.josm.data.osm.visitor.paint.Rendering;
79import org.openstreetmap.josm.data.osm.visitor.paint.relations.MultipolygonCache;
80import org.openstreetmap.josm.data.preferences.ColorProperty;
81import org.openstreetmap.josm.data.preferences.IntegerProperty;
82import org.openstreetmap.josm.data.preferences.StringProperty;
83import org.openstreetmap.josm.data.projection.Projection;
84import org.openstreetmap.josm.data.validation.TestError;
85import org.openstreetmap.josm.gui.ExtendedDialog;
86import org.openstreetmap.josm.gui.MapView;
87import org.openstreetmap.josm.gui.MapViewState.MapViewPoint;
88import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
89import org.openstreetmap.josm.gui.dialogs.LayerListPopup;
90import org.openstreetmap.josm.gui.io.AbstractIOTask;
91import org.openstreetmap.josm.gui.io.AbstractUploadDialog;
92import org.openstreetmap.josm.gui.io.UploadDialog;
93import org.openstreetmap.josm.gui.io.UploadLayerTask;
94import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
95import org.openstreetmap.josm.gui.progress.PleaseWaitProgressMonitor;
96import org.openstreetmap.josm.gui.progress.ProgressMonitor;
97import org.openstreetmap.josm.gui.util.GuiHelper;
98import org.openstreetmap.josm.gui.widgets.FileChooserManager;
99import org.openstreetmap.josm.gui.widgets.JosmTextArea;
100import org.openstreetmap.josm.io.OsmImporter;
101import org.openstreetmap.josm.tools.AlphanumComparator;
102import org.openstreetmap.josm.tools.CheckParameterUtil;
103import org.openstreetmap.josm.tools.GBC;
104import org.openstreetmap.josm.tools.ImageOverlay;
105import org.openstreetmap.josm.tools.ImageProvider;
106import org.openstreetmap.josm.tools.ImageProvider.ImageSizes;
107import org.openstreetmap.josm.tools.SubclassFilteredCollection;
108import org.openstreetmap.josm.tools.date.DateUtils;
109
110/**
111 * A layer that holds OSM data from a specific dataset.
112 * The data can be fully edited.
113 *
114 * @author imi
115 * @since 17
116 */
117public class OsmDataLayer extends AbstractModifiableLayer implements Listener, SelectionChangedListener {
118 private static final int HATCHED_SIZE = 15;
119 /** Property used to know if this layer has to be saved on disk */
120 public static final String REQUIRES_SAVE_TO_DISK_PROP = OsmDataLayer.class.getName() + ".requiresSaveToDisk";
121 /** Property used to know if this layer has to be uploaded */
122 public static final String REQUIRES_UPLOAD_TO_SERVER_PROP = OsmDataLayer.class.getName() + ".requiresUploadToServer";
123
124 private boolean requiresSaveToFile;
125 private boolean requiresUploadToServer;
126 private int highlightUpdateCount;
127
128 /**
129 * List of validation errors in this layer.
130 * @since 3669
131 */
132 public final List<TestError> validationErrors = new ArrayList<>();
133
134 public static final int DEFAULT_RECENT_RELATIONS_NUMBER = 20;
135 public static final IntegerProperty PROPERTY_RECENT_RELATIONS_NUMBER = new IntegerProperty("properties.last-closed-relations-size",
136 DEFAULT_RECENT_RELATIONS_NUMBER);
137 public static final StringProperty PROPERTY_SAVE_EXTENSION = new StringProperty("save.extension.osm", "osm");
138
139 private static final ColorProperty PROPERTY_BACKGROUND_COLOR = new ColorProperty(marktr("background"), Color.BLACK);
140 private static final ColorProperty PROPERTY_OUTSIDE_COLOR = new ColorProperty(marktr("outside downloaded area"), Color.YELLOW);
141
142 /** List of recent relations */
143 private final Map<Relation, Void> recentRelations = new LinkedHashMap<Relation, Void>(PROPERTY_RECENT_RELATIONS_NUMBER.get()+1, 1.1f, true) {
144 @Override
145 protected boolean removeEldestEntry(Map.Entry<Relation, Void> eldest) {
146 return size() > PROPERTY_RECENT_RELATIONS_NUMBER.get();
147 }
148 };
149
150 /**
151 * Returns list of recently closed relations or null if none.
152 * @return list of recently closed relations or <code>null</code> if none
153 * @since 9668
154 */
155 public ArrayList<Relation> getRecentRelations() {
156 ArrayList<Relation> list = new ArrayList<>(recentRelations.keySet());
157 Collections.reverse(list);
158 return list;
159 }
160
161 /**
162 * Adds recently closed relation.
163 * @param relation new entry for the list of recently closed relations
164 * @since 9668
165 */
166 public void setRecentRelation(Relation relation) {
167 recentRelations.put(relation, null);
168 if (Main.map != null && Main.map.relationListDialog != null) {
169 Main.map.relationListDialog.enableRecentRelations();
170 }
171 }
172
173 /**
174 * Remove relation from list of recent relations.
175 * @param relation relation to remove
176 * @since 9668
177 */
178 public void removeRecentRelation(Relation relation) {
179 recentRelations.remove(relation);
180 if (Main.map != null && Main.map.relationListDialog != null) {
181 Main.map.relationListDialog.enableRecentRelations();
182 }
183 }
184
185 protected void setRequiresSaveToFile(boolean newValue) {
186 boolean oldValue = requiresSaveToFile;
187 requiresSaveToFile = newValue;
188 if (oldValue != newValue) {
189 propertyChangeSupport.firePropertyChange(REQUIRES_SAVE_TO_DISK_PROP, oldValue, newValue);
190 }
191 }
192
193 protected void setRequiresUploadToServer(boolean newValue) {
194 boolean oldValue = requiresUploadToServer;
195 requiresUploadToServer = newValue;
196 if (oldValue != newValue) {
197 propertyChangeSupport.firePropertyChange(REQUIRES_UPLOAD_TO_SERVER_PROP, oldValue, newValue);
198 }
199 }
200
201 /** the global counter for created data layers */
202 private static final AtomicInteger dataLayerCounter = new AtomicInteger();
203
204 /**
205 * Replies a new unique name for a data layer
206 *
207 * @return a new unique name for a data layer
208 */
209 public static String createNewName() {
210 return createLayerName(dataLayerCounter.incrementAndGet());
211 }
212
213 static String createLayerName(Object arg) {
214 return tr("Data Layer {0}", arg);
215 }
216
217 public static final class DataCountVisitor extends AbstractVisitor {
218 public int nodes;
219 public int ways;
220 public int relations;
221 public int deletedNodes;
222 public int deletedWays;
223 public int deletedRelations;
224
225 @Override
226 public void visit(final Node n) {
227 nodes++;
228 if (n.isDeleted()) {
229 deletedNodes++;
230 }
231 }
232
233 @Override
234 public void visit(final Way w) {
235 ways++;
236 if (w.isDeleted()) {
237 deletedWays++;
238 }
239 }
240
241 @Override
242 public void visit(final Relation r) {
243 relations++;
244 if (r.isDeleted()) {
245 deletedRelations++;
246 }
247 }
248 }
249
250 @FunctionalInterface
251 public interface CommandQueueListener {
252 void commandChanged(int queueSize, int redoSize);
253 }
254
255 /**
256 * Listener called when a state of this layer has changed.
257 * @since 10600 (functional interface)
258 */
259 @FunctionalInterface
260 public interface LayerStateChangeListener {
261 /**
262 * Notifies that the "upload discouraged" (upload=no) state has changed.
263 * @param layer The layer that has been modified
264 * @param newValue The new value of the state
265 */
266 void uploadDiscouragedChanged(OsmDataLayer layer, boolean newValue);
267 }
268
269 private final CopyOnWriteArrayList<LayerStateChangeListener> layerStateChangeListeners = new CopyOnWriteArrayList<>();
270
271 /**
272 * Adds a layer state change listener
273 *
274 * @param listener the listener. Ignored if null or already registered.
275 * @since 5519
276 */
277 public void addLayerStateChangeListener(LayerStateChangeListener listener) {
278 if (listener != null) {
279 layerStateChangeListeners.addIfAbsent(listener);
280 }
281 }
282
283 /**
284 * Removes a layer state change listener
285 *
286 * @param listener the listener. Ignored if null or already registered.
287 * @since 10340
288 */
289 public void removeLayerStateChangeListener(LayerStateChangeListener listener) {
290 layerStateChangeListeners.remove(listener);
291 }
292
293 /**
294 * The data behind this layer.
295 */
296 public final DataSet data;
297
298 /**
299 * the collection of conflicts detected in this layer
300 */
301 private final ConflictCollection conflicts;
302
303 /**
304 * a texture for non-downloaded area
305 */
306 private static volatile BufferedImage hatched;
307
308 static {
309 createHatchTexture();
310 }
311
312 /**
313 * Replies background color for downloaded areas.
314 * @return background color for downloaded areas. Black by default
315 */
316 public static Color getBackgroundColor() {
317 return PROPERTY_BACKGROUND_COLOR.get();
318 }
319
320 /**
321 * Replies background color for non-downloaded areas.
322 * @return background color for non-downloaded areas. Yellow by default
323 */
324 public static Color getOutsideColor() {
325 return PROPERTY_OUTSIDE_COLOR.get();
326 }
327
328 /**
329 * Initialize the hatch pattern used to paint the non-downloaded area
330 */
331 public static void createHatchTexture() {
332 BufferedImage bi = new BufferedImage(HATCHED_SIZE, HATCHED_SIZE, BufferedImage.TYPE_INT_ARGB);
333 Graphics2D big = bi.createGraphics();
334 big.setColor(getBackgroundColor());
335 Composite comp = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f);
336 big.setComposite(comp);
337 big.fillRect(0, 0, HATCHED_SIZE, HATCHED_SIZE);
338 big.setColor(getOutsideColor());
339 big.drawLine(-1, 6, 6, -1);
340 big.drawLine(4, 16, 16, 4);
341 hatched = bi;
342 }
343
344 /**
345 * Construct a new {@code OsmDataLayer}.
346 * @param data OSM data
347 * @param name Layer name
348 * @param associatedFile Associated .osm file (can be null)
349 */
350 public OsmDataLayer(final DataSet data, final String name, final File associatedFile) {
351 super(name);
352 CheckParameterUtil.ensureParameterNotNull(data, "data");
353 this.data = data;
354 this.setAssociatedFile(associatedFile);
355 conflicts = new ConflictCollection();
356 data.addDataSetListener(new DataSetListenerAdapter(this));
357 data.addDataSetListener(MultipolygonCache.getInstance());
358 DataSet.addSelectionListener(this);
359 if (name != null && name.startsWith(createLayerName(""))) {
360 while (AlphanumComparator.getInstance().compare(createLayerName(dataLayerCounter), name) < 0) {
361 dataLayerCounter.incrementAndGet();
362 }
363 }
364 }
365
366 /**
367 * Return the image provider to get the base icon
368 * @return image provider class which can be modified
369 * @since 8323
370 */
371 protected ImageProvider getBaseIconProvider() {
372 return new ImageProvider("layer", "osmdata_small");
373 }
374
375 @Override
376 public Icon getIcon() {
377 ImageProvider base = getBaseIconProvider().setMaxSize(ImageSizes.LAYER);
378 if (isUploadDiscouraged()) {
379 base.addOverlay(new ImageOverlay(new ImageProvider("warning-small"), 0.5, 0.5, 1.0, 1.0));
380 }
381 return base.get();
382 }
383
384 /**
385 * Draw all primitives in this layer but do not draw modified ones (they
386 * are drawn by the edit layer).
387 * Draw nodes last to overlap the ways they belong to.
388 */
389 @Override public void paint(final Graphics2D g, final MapView mv, Bounds box) {
390 highlightUpdateCount = data.getHighlightUpdateCount();
391
392 boolean active = mv.getLayerManager().getActiveLayer() == this;
393 boolean inactive = !active && Main.pref.getBoolean("draw.data.inactive_color", true);
394 boolean virtual = !inactive && mv.isVirtualNodesEnabled();
395
396 // draw the hatched area for non-downloaded region. only draw if we're the active
397 // and bounds are defined; don't draw for inactive layers or loaded GPX files etc
398 if (active && Main.pref.getBoolean("draw.data.downloaded_area", true) && !data.dataSources.isEmpty()) {
399 // initialize area with current viewport
400 Rectangle b = mv.getBounds();
401 // on some platforms viewport bounds seem to be offset from the left,
402 // over-grow it just to be sure
403 b.grow(100, 100);
404 Area a = new Area(b);
405
406 // now successively subtract downloaded areas
407 for (Bounds bounds : data.getDataSourceBounds()) {
408 if (bounds.isCollapsed()) {
409 continue;
410 }
411 a.subtract(mv.getState().getArea(bounds));
412 }
413
414 // paint remainder
415 MapViewPoint anchor = mv.getState().getPointFor(new EastNorth(0, 0));
416 Rectangle2D anchorRect = new Rectangle2D.Double(anchor.getInView().getX() % HATCHED_SIZE,
417 anchor.getInView().getY() % HATCHED_SIZE, HATCHED_SIZE, HATCHED_SIZE);
418 g.setPaint(new TexturePaint(hatched, anchorRect));
419 g.fill(a);
420 }
421
422 Rendering painter = MapRendererFactory.getInstance().createActiveRenderer(g, mv, inactive);
423 painter.render(data, virtual, box);
424 Main.map.conflictDialog.paintConflicts(g, mv);
425 }
426
427 @Override public String getToolTipText() {
428 int nodes = new SubclassFilteredCollection<>(data.getNodes(), p -> !p.isDeleted()).size();
429 int ways = new SubclassFilteredCollection<>(data.getWays(), p -> !p.isDeleted()).size();
430 int rels = new SubclassFilteredCollection<>(data.getRelations(), p -> !p.isDeleted()).size();
431
432 String tool = trn("{0} node", "{0} nodes", nodes, nodes)+", ";
433 tool += trn("{0} way", "{0} ways", ways, ways)+", ";
434 tool += trn("{0} relation", "{0} relations", rels, rels);
435
436 File f = getAssociatedFile();
437 if (f != null) {
438 tool = "<html>"+tool+"<br>"+f.getPath()+"</html>";
439 }
440 return tool;
441 }
442
443 @Override public void mergeFrom(final Layer from) {
444 final PleaseWaitProgressMonitor monitor = new PleaseWaitProgressMonitor(tr("Merging layers"));
445 monitor.setCancelable(false);
446 if (from instanceof OsmDataLayer && ((OsmDataLayer) from).isUploadDiscouraged()) {
447 setUploadDiscouraged(true);
448 }
449 mergeFrom(((OsmDataLayer) from).data, monitor);
450 monitor.close();
451 }
452
453 /**
454 * merges the primitives in dataset <code>from</code> into the dataset of
455 * this layer
456 *
457 * @param from the source data set
458 */
459 public void mergeFrom(final DataSet from) {
460 mergeFrom(from, null);
461 }
462
463 /**
464 * merges the primitives in dataset <code>from</code> into the dataset of this layer
465 *
466 * @param from the source data set
467 * @param progressMonitor the progress monitor, can be {@code null}
468 */
469 public void mergeFrom(final DataSet from, ProgressMonitor progressMonitor) {
470 final DataSetMerger visitor = new DataSetMerger(data, from);
471 try {
472 visitor.merge(progressMonitor);
473 } catch (DataIntegrityProblemException e) {
474 Main.error(e);
475 JOptionPane.showMessageDialog(
476 Main.parent,
477 e.getHtmlMessage() != null ? e.getHtmlMessage() : e.getMessage(),
478 tr("Error"),
479 JOptionPane.ERROR_MESSAGE
480 );
481 return;
482 }
483
484 Area a = data.getDataSourceArea();
485
486 // copy the merged layer's data source info.
487 // only add source rectangles if they are not contained in the layer already.
488 for (DataSource src : from.dataSources) {
489 if (a == null || !a.contains(src.bounds.asRect())) {
490 data.dataSources.add(src);
491 }
492 }
493
494 // copy the merged layer's API version
495 if (data.getVersion() == null) {
496 data.setVersion(from.getVersion());
497 }
498
499 int numNewConflicts = 0;
500 for (Conflict<?> c : visitor.getConflicts()) {
501 if (!conflicts.hasConflict(c)) {
502 numNewConflicts++;
503 conflicts.add(c);
504 }
505 }
506 // repaint to make sure new data is displayed properly.
507 if (Main.isDisplayingMapView()) {
508 Main.map.mapView.repaint();
509 }
510 // warn about new conflicts
511 if (numNewConflicts > 0 && Main.map != null && Main.map.conflictDialog != null) {
512 Main.map.conflictDialog.warnNumNewConflicts(numNewConflicts);
513 }
514 }
515
516 @Override
517 public boolean isMergable(final Layer other) {
518 // allow merging between normal layers and discouraged layers with a warning (see #7684)
519 return other instanceof OsmDataLayer;
520 }
521
522 @Override
523 public void visitBoundingBox(final BoundingXYVisitor v) {
524 for (final Node n: data.getNodes()) {
525 if (n.isUsable()) {
526 v.visit(n);
527 }
528 }
529 }
530
531 /**
532 * Clean out the data behind the layer. This means clearing the redo/undo lists,
533 * really deleting all deleted objects and reset the modified flags. This should
534 * be done after an upload, even after a partial upload.
535 *
536 * @param processed A list of all objects that were actually uploaded.
537 * May be <code>null</code>, which means nothing has been uploaded
538 */
539 public void cleanupAfterUpload(final Collection<? extends IPrimitive> processed) {
540 // return immediately if an upload attempt failed
541 if (processed == null || processed.isEmpty())
542 return;
543
544 Main.main.undoRedo.clean(this);
545
546 // if uploaded, clean the modified flags as well
547 data.cleanupDeletedPrimitives();
548 data.beginUpdate();
549 try {
550 for (OsmPrimitive p: data.allPrimitives()) {
551 if (processed.contains(p)) {
552 p.setModified(false);
553 }
554 }
555 } finally {
556 data.endUpdate();
557 }
558 }
559
560 @Override
561 public Object getInfoComponent() {
562 final DataCountVisitor counter = new DataCountVisitor();
563 for (final OsmPrimitive osm : data.allPrimitives()) {
564 osm.accept(counter);
565 }
566 final JPanel p = new JPanel(new GridBagLayout());
567
568 String nodeText = trn("{0} node", "{0} nodes", counter.nodes, counter.nodes);
569 if (counter.deletedNodes > 0) {
570 nodeText += " ("+trn("{0} deleted", "{0} deleted", counter.deletedNodes, counter.deletedNodes)+')';
571 }
572
573 String wayText = trn("{0} way", "{0} ways", counter.ways, counter.ways);
574 if (counter.deletedWays > 0) {
575 wayText += " ("+trn("{0} deleted", "{0} deleted", counter.deletedWays, counter.deletedWays)+')';
576 }
577
578 String relationText = trn("{0} relation", "{0} relations", counter.relations, counter.relations);
579 if (counter.deletedRelations > 0) {
580 relationText += " ("+trn("{0} deleted", "{0} deleted", counter.deletedRelations, counter.deletedRelations)+')';
581 }
582
583 p.add(new JLabel(tr("{0} consists of:", getName())), GBC.eol());
584 p.add(new JLabel(nodeText, ImageProvider.get("data", "node"), JLabel.HORIZONTAL), GBC.eop().insets(15, 0, 0, 0));
585 p.add(new JLabel(wayText, ImageProvider.get("data", "way"), JLabel.HORIZONTAL), GBC.eop().insets(15, 0, 0, 0));
586 p.add(new JLabel(relationText, ImageProvider.get("data", "relation"), JLabel.HORIZONTAL), GBC.eop().insets(15, 0, 0, 0));
587 p.add(new JLabel(tr("API version: {0}", (data.getVersion() != null) ? data.getVersion() : tr("unset"))),
588 GBC.eop().insets(15, 0, 0, 0));
589 if (isUploadDiscouraged()) {
590 p.add(new JLabel(tr("Upload is discouraged")), GBC.eop().insets(15, 0, 0, 0));
591 }
592
593 return p;
594 }
595
596 @Override public Action[] getMenuEntries() {
597 List<Action> actions = new ArrayList<>();
598 actions.addAll(Arrays.asList(new Action[]{
599 LayerListDialog.getInstance().createActivateLayerAction(this),
600 LayerListDialog.getInstance().createShowHideLayerAction(),
601 LayerListDialog.getInstance().createDeleteLayerAction(),
602 SeparatorLayerAction.INSTANCE,
603 LayerListDialog.getInstance().createMergeLayerAction(this),
604 LayerListDialog.getInstance().createDuplicateLayerAction(this),
605 new LayerSaveAction(this),
606 new LayerSaveAsAction(this),
607 }));
608 if (ExpertToggleAction.isExpert()) {
609 actions.addAll(Arrays.asList(new Action[]{
610 new LayerGpxExportAction(this),
611 new ConvertToGpxLayerAction()}));
612 }
613 actions.addAll(Arrays.asList(new Action[]{
614 SeparatorLayerAction.INSTANCE,
615 new RenameLayerAction(getAssociatedFile(), this)}));
616 if (ExpertToggleAction.isExpert()) {
617 actions.add(new ToggleUploadDiscouragedLayerAction(this));
618 }
619 actions.addAll(Arrays.asList(new Action[]{
620 new ConsistencyTestAction(),
621 SeparatorLayerAction.INSTANCE,
622 new LayerListPopup.InfoAction(this)}));
623 return actions.toArray(new Action[actions.size()]);
624 }
625
626 /**
627 * Converts given OSM dataset to GPX data.
628 * @param data OSM dataset
629 * @param file output .gpx file
630 * @return GPX data
631 */
632 public static GpxData toGpxData(DataSet data, File file) {
633 GpxData gpxData = new GpxData();
634 gpxData.storageFile = file;
635 Set<Node> doneNodes = new HashSet<>();
636 waysToGpxData(data.getWays(), gpxData, doneNodes);
637 nodesToGpxData(data.getNodes(), gpxData, doneNodes);
638 return gpxData;
639 }
640
641 private static void waysToGpxData(Collection<Way> ways, GpxData gpxData, Set<Node> doneNodes) {
642 /* When the dataset has been obtained from a gpx layer and now is being converted back,
643 * the ways have negative ids. The first created way corresponds to the first gpx segment,
644 * and has the highest id (i.e., closest to zero).
645 * Thus, sorting by OsmPrimitive#getUniqueId gives the original order.
646 * (Only works if the data layer has not been saved to and been loaded from an osm file before.)
647 */
648 final List<Way> sortedWays = new ArrayList<>(ways);
649 sortedWays.sort(new OsmPrimitiveComparator(true, false)); // sort by OsmPrimitive#getUniqueId ascending
650 Collections.reverse(sortedWays); // sort by OsmPrimitive#getUniqueId descending
651 for (Way w : sortedWays) {
652 if (!w.isUsable()) {
653 continue;
654 }
655 Collection<Collection<WayPoint>> trk = new ArrayList<>();
656 Map<String, Object> trkAttr = new HashMap<>();
657
658 if (w.get("name") != null) {
659 trkAttr.put("name", w.get("name"));
660 }
661
662 List<WayPoint> trkseg = null;
663 for (Node n : w.getNodes()) {
664 if (!n.isUsable()) {
665 trkseg = null;
666 continue;
667 }
668 if (trkseg == null) {
669 trkseg = new ArrayList<>();
670 trk.add(trkseg);
671 }
672 if (!n.isTagged()) {
673 doneNodes.add(n);
674 }
675 trkseg.add(nodeToWayPoint(n));
676 }
677
678 gpxData.tracks.add(new ImmutableGpxTrack(trk, trkAttr));
679 }
680 }
681
682 private static WayPoint nodeToWayPoint(Node n) {
683 WayPoint wpt = new WayPoint(n.getCoor());
684
685 // Position info
686
687 addDoubleIfPresent(wpt, n, GpxConstants.PT_ELE);
688
689 if (!n.isTimestampEmpty()) {
690 wpt.put("time", DateUtils.fromTimestamp(n.getRawTimestamp()));
691 wpt.setTime();
692 }
693
694 addDoubleIfPresent(wpt, n, GpxConstants.PT_MAGVAR);
695 addDoubleIfPresent(wpt, n, GpxConstants.PT_GEOIDHEIGHT);
696
697 // Description info
698
699 addStringIfPresent(wpt, n, GpxConstants.GPX_NAME);
700 addStringIfPresent(wpt, n, GpxConstants.GPX_DESC, "description");
701 addStringIfPresent(wpt, n, GpxConstants.GPX_CMT, "comment");
702 addStringIfPresent(wpt, n, GpxConstants.GPX_SRC, "source", "source:position");
703
704 Collection<GpxLink> links = new ArrayList<>();
705 for (String key : new String[]{"link", "url", "website", "contact:website"}) {
706 String value = n.get(key);
707 if (value != null) {
708 links.add(new GpxLink(value));
709 }
710 }
711 wpt.put(GpxConstants.META_LINKS, links);
712
713 addStringIfPresent(wpt, n, GpxConstants.PT_SYM, "wpt_symbol");
714 addStringIfPresent(wpt, n, GpxConstants.PT_TYPE);
715
716 // Accuracy info
717 addStringIfPresent(wpt, n, GpxConstants.PT_FIX, "gps:fix");
718 addIntegerIfPresent(wpt, n, GpxConstants.PT_SAT, "gps:sat");
719 addDoubleIfPresent(wpt, n, GpxConstants.PT_HDOP, "gps:hdop");
720 addDoubleIfPresent(wpt, n, GpxConstants.PT_VDOP, "gps:vdop");
721 addDoubleIfPresent(wpt, n, GpxConstants.PT_PDOP, "gps:pdop");
722 addDoubleIfPresent(wpt, n, GpxConstants.PT_AGEOFDGPSDATA, "gps:ageofdgpsdata");
723 addIntegerIfPresent(wpt, n, GpxConstants.PT_DGPSID, "gps:dgpsid");
724
725 return wpt;
726 }
727
728 private static void nodesToGpxData(Collection<Node> nodes, GpxData gpxData, Set<Node> doneNodes) {
729 List<Node> sortedNodes = new ArrayList<>(nodes);
730 sortedNodes.removeAll(doneNodes);
731 Collections.sort(sortedNodes);
732 for (Node n : sortedNodes) {
733 if (n.isIncomplete() || n.isDeleted()) {
734 continue;
735 }
736 gpxData.waypoints.add(nodeToWayPoint(n));
737 }
738 }
739
740 private static void addIntegerIfPresent(WayPoint wpt, OsmPrimitive p, String gpxKey, String ... osmKeys) {
741 List<String> possibleKeys = new ArrayList<>(Arrays.asList(osmKeys));
742 possibleKeys.add(0, gpxKey);
743 for (String key : possibleKeys) {
744 String value = p.get(key);
745 if (value != null) {
746 try {
747 int i = Integer.parseInt(value);
748 // Sanity checks
749 if ((!GpxConstants.PT_SAT.equals(gpxKey) || i >= 0) &&
750 (!GpxConstants.PT_DGPSID.equals(gpxKey) || (0 <= i && i <= 1023))) {
751 wpt.put(gpxKey, value);
752 break;
753 }
754 } catch (NumberFormatException e) {
755 Main.trace(e);
756 }
757 }
758 }
759 }
760
761 private static void addDoubleIfPresent(WayPoint wpt, OsmPrimitive p, String gpxKey, String ... osmKeys) {
762 List<String> possibleKeys = new ArrayList<>(Arrays.asList(osmKeys));
763 possibleKeys.add(0, gpxKey);
764 for (String key : possibleKeys) {
765 String value = p.get(key);
766 if (value != null) {
767 try {
768 double d = Double.parseDouble(value);
769 // Sanity checks
770 if (!GpxConstants.PT_MAGVAR.equals(gpxKey) || (0.0 <= d && d < 360.0)) {
771 wpt.put(gpxKey, value);
772 break;
773 }
774 } catch (NumberFormatException e) {
775 Main.trace(e);
776 }
777 }
778 }
779 }
780
781 private static void addStringIfPresent(WayPoint wpt, OsmPrimitive p, String gpxKey, String ... osmKeys) {
782 List<String> possibleKeys = new ArrayList<>(Arrays.asList(osmKeys));
783 possibleKeys.add(0, gpxKey);
784 for (String key : possibleKeys) {
785 String value = p.get(key);
786 // Sanity checks
787 if (value != null && (!GpxConstants.PT_FIX.equals(gpxKey) || GpxConstants.FIX_VALUES.contains(value))) {
788 wpt.put(gpxKey, value);
789 break;
790 }
791 }
792 }
793
794 /**
795 * Converts OSM data behind this layer to GPX data.
796 * @return GPX data
797 */
798 public GpxData toGpxData() {
799 return toGpxData(data, getAssociatedFile());
800 }
801
802 /**
803 * Action that converts this OSM layer to a GPX layer.
804 */
805 public class ConvertToGpxLayerAction extends AbstractAction {
806 /**
807 * Constructs a new {@code ConvertToGpxLayerAction}.
808 */
809 public ConvertToGpxLayerAction() {
810 super(tr("Convert to GPX layer"), ImageProvider.get("converttogpx"));
811 putValue("help", ht("/Action/ConvertToGpxLayer"));
812 }
813
814 @Override
815 public void actionPerformed(ActionEvent e) {
816 final GpxData gpxData = toGpxData();
817 final GpxLayer gpxLayer = new GpxLayer(gpxData, tr("Converted from: {0}", getName()));
818 if (getAssociatedFile() != null) {
819 String filename = getAssociatedFile().getName().replaceAll(Pattern.quote(".gpx.osm") + '$', "") + ".gpx";
820 gpxLayer.setAssociatedFile(new File(getAssociatedFile().getParentFile(), filename));
821 }
822 Main.getLayerManager().addLayer(gpxLayer);
823 if (Main.pref.getBoolean("marker.makeautomarkers", true) && !gpxData.waypoints.isEmpty()) {
824 Main.getLayerManager().addLayer(new MarkerLayer(gpxData, tr("Converted from: {0}", getName()), null, gpxLayer));
825 }
826 Main.getLayerManager().removeLayer(OsmDataLayer.this);
827 }
828 }
829
830 /**
831 * Determines if this layer contains data at the given coordinate.
832 * @param coor the coordinate
833 * @return {@code true} if data sources bounding boxes contain {@code coor}
834 */
835 public boolean containsPoint(LatLon coor) {
836 // we'll assume that if this has no data sources
837 // that it also has no borders
838 if (this.data.dataSources.isEmpty())
839 return true;
840
841 boolean layerBoundsPoint = false;
842 for (DataSource src : this.data.dataSources) {
843 if (src.bounds.contains(coor)) {
844 layerBoundsPoint = true;
845 break;
846 }
847 }
848 return layerBoundsPoint;
849 }
850
851 /**
852 * Replies the set of conflicts currently managed in this layer.
853 *
854 * @return the set of conflicts currently managed in this layer
855 */
856 public ConflictCollection getConflicts() {
857 return conflicts;
858 }
859
860 @Override
861 public boolean isUploadable() {
862 return true;
863 }
864
865 @Override
866 public boolean requiresUploadToServer() {
867 return requiresUploadToServer;
868 }
869
870 @Override
871 public boolean requiresSaveToFile() {
872 return getAssociatedFile() != null && requiresSaveToFile;
873 }
874
875 @Override
876 public void onPostLoadFromFile() {
877 setRequiresSaveToFile(false);
878 setRequiresUploadToServer(isModified());
879 invalidate();
880 }
881
882 /**
883 * Actions run after data has been downloaded to this layer.
884 */
885 public void onPostDownloadFromServer() {
886 setRequiresSaveToFile(true);
887 setRequiresUploadToServer(isModified());
888 invalidate();
889 }
890
891 @Override
892 public boolean isChanged() {
893 return highlightUpdateCount != data.getHighlightUpdateCount();
894 }
895
896 @Override
897 public void onPostSaveToFile() {
898 setRequiresSaveToFile(false);
899 setRequiresUploadToServer(isModified());
900 }
901
902 @Override
903 public void onPostUploadToServer() {
904 setRequiresUploadToServer(isModified());
905 // keep requiresSaveToDisk unchanged
906 }
907
908 private class ConsistencyTestAction extends AbstractAction {
909
910 ConsistencyTestAction() {
911 super(tr("Dataset consistency test"));
912 }
913
914 @Override
915 public void actionPerformed(ActionEvent e) {
916 String result = DatasetConsistencyTest.runTests(data);
917 if (result.isEmpty()) {
918 JOptionPane.showMessageDialog(Main.parent, tr("No problems found"));
919 } else {
920 JPanel p = new JPanel(new GridBagLayout());
921 p.add(new JLabel(tr("Following problems found:")), GBC.eol());
922 JosmTextArea info = new JosmTextArea(result, 20, 60);
923 info.setCaretPosition(0);
924 info.setEditable(false);
925 p.add(new JScrollPane(info), GBC.eop());
926
927 JOptionPane.showMessageDialog(Main.parent, p, tr("Warning"), JOptionPane.WARNING_MESSAGE);
928 }
929 }
930 }
931
932 @Override
933 public void destroy() {
934 super.destroy();
935 DataSet.removeSelectionListener(this);
936 }
937
938 @Override
939 public void processDatasetEvent(AbstractDatasetChangedEvent event) {
940 invalidate();
941 setRequiresSaveToFile(true);
942 setRequiresUploadToServer(true);
943 }
944
945 @Override
946 public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) {
947 invalidate();
948 }
949
950 @Override
951 public void projectionChanged(Projection oldValue, Projection newValue) {
952 // No reprojection required. The dataset itself is registered as projection
953 // change listener and already got notified.
954 }
955
956 @Override
957 public final boolean isUploadDiscouraged() {
958 return data.isUploadDiscouraged();
959 }
960
961 /**
962 * Sets the "discouraged upload" flag.
963 * @param uploadDiscouraged {@code true} if upload of data managed by this layer is discouraged.
964 * This feature allows to use "private" data layers.
965 */
966 public final void setUploadDiscouraged(boolean uploadDiscouraged) {
967 if (uploadDiscouraged ^ isUploadDiscouraged()) {
968 data.setUploadDiscouraged(uploadDiscouraged);
969 for (LayerStateChangeListener l : layerStateChangeListeners) {
970 l.uploadDiscouragedChanged(this, uploadDiscouraged);
971 }
972 }
973 }
974
975 @Override
976 public final boolean isModified() {
977 return data.isModified();
978 }
979
980 @Override
981 public boolean isSavable() {
982 return true; // With OsmExporter
983 }
984
985 @Override
986 public boolean checkSaveConditions() {
987 if (isDataSetEmpty() && 1 != GuiHelper.runInEDTAndWaitAndReturn(() -> {
988 if (GraphicsEnvironment.isHeadless()) {
989 return 2;
990 }
991 ExtendedDialog dialog = new ExtendedDialog(
992 Main.parent,
993 tr("Empty document"),
994 new String[] {tr("Save anyway"), tr("Cancel")}
995 );
996 dialog.setContent(tr("The document contains no data."));
997 dialog.setButtonIcons(new String[] {"save", "cancel"});
998 return dialog.showDialog().getValue();
999 })) {
1000 return false;
1001 }
1002
1003 ConflictCollection conflictsCol = getConflicts();
1004 if (conflictsCol != null && !conflictsCol.isEmpty() && 1 != GuiHelper.runInEDTAndWaitAndReturn(() -> {
1005 ExtendedDialog dialog = new ExtendedDialog(
1006 Main.parent,
1007 /* I18N: Display title of the window showing conflicts */
1008 tr("Conflicts"),
1009 new String[] {tr("Reject Conflicts and Save"), tr("Cancel")}
1010 );
1011 dialog.setContent(
1012 tr("There are unresolved conflicts. Conflicts will not be saved and handled as if you rejected all. Continue?"));
1013 dialog.setButtonIcons(new String[] {"save", "cancel"});
1014 return dialog.showDialog().getValue();
1015 })) {
1016 return false;
1017 }
1018 return true;
1019 }
1020
1021 /**
1022 * Check the data set if it would be empty on save. It is empty, if it contains
1023 * no objects (after all objects that are created and deleted without being
1024 * transferred to the server have been removed).
1025 *
1026 * @return <code>true</code>, if a save result in an empty data set.
1027 */
1028 private boolean isDataSetEmpty() {
1029 if (data != null) {
1030 for (OsmPrimitive osm : data.allNonDeletedPrimitives()) {
1031 if (!osm.isDeleted() || !osm.isNewOrUndeleted())
1032 return false;
1033 }
1034 }
1035 return true;
1036 }
1037
1038 @Override
1039 public File createAndOpenSaveFileChooser() {
1040 String extension = PROPERTY_SAVE_EXTENSION.get();
1041 File file = getAssociatedFile();
1042 if (file == null && isRenamed()) {
1043 String filename = Main.pref.get("lastDirectory") + '/' + getName();
1044 if (!OsmImporter.FILE_FILTER.acceptName(filename))
1045 filename = filename + '.' + extension;
1046 file = new File(filename);
1047 }
1048 return new FileChooserManager()
1049 .title(tr("Save OSM file"))
1050 .extension(extension)
1051 .file(file)
1052 .allTypes(true)
1053 .getFileForSave();
1054 }
1055
1056 @Override
1057 public AbstractIOTask createUploadTask(final ProgressMonitor monitor) {
1058 UploadDialog dialog = UploadDialog.getUploadDialog();
1059 return new UploadLayerTask(
1060 dialog.getUploadStrategySpecification(),
1061 this,
1062 monitor,
1063 dialog.getChangeset());
1064 }
1065
1066 @Override
1067 public AbstractUploadDialog getUploadDialog() {
1068 UploadDialog dialog = UploadDialog.getUploadDialog();
1069 dialog.setUploadedPrimitives(new APIDataSet(data));
1070 return dialog;
1071 }
1072
1073 @Override
1074 public ProjectionBounds getViewProjectionBounds() {
1075 BoundingXYVisitor v = new BoundingXYVisitor();
1076 v.visit(data.getDataSourceBoundingBox());
1077 if (!v.hasExtend()) {
1078 v.computeBoundingBox(data.getNodes());
1079 }
1080 return v.getBounds();
1081 }
1082}
Note: See TracBrowser for help on using the repository browser.