001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.layer;
003
004import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
005import static org.openstreetmap.josm.tools.I18n.marktr;
006import static org.openstreetmap.josm.tools.I18n.tr;
007import static org.openstreetmap.josm.tools.I18n.trn;
008
009import java.awt.AlphaComposite;
010import java.awt.Color;
011import java.awt.Composite;
012import java.awt.Graphics2D;
013import java.awt.GraphicsEnvironment;
014import java.awt.GridBagLayout;
015import java.awt.Rectangle;
016import java.awt.TexturePaint;
017import java.awt.event.ActionEvent;
018import java.awt.geom.Area;
019import java.awt.geom.Rectangle2D;
020import java.awt.image.BufferedImage;
021import java.io.File;
022import java.util.ArrayList;
023import java.util.Arrays;
024import java.util.Collection;
025import java.util.Collections;
026import java.util.HashMap;
027import java.util.HashSet;
028import java.util.LinkedHashMap;
029import java.util.List;
030import java.util.Map;
031import java.util.Set;
032import java.util.concurrent.CopyOnWriteArrayList;
033import java.util.concurrent.atomic.AtomicInteger;
034import java.util.regex.Pattern;
035
036import javax.swing.AbstractAction;
037import javax.swing.Action;
038import javax.swing.Icon;
039import javax.swing.JLabel;
040import javax.swing.JOptionPane;
041import javax.swing.JPanel;
042import javax.swing.JScrollPane;
043
044import org.openstreetmap.josm.Main;
045import org.openstreetmap.josm.actions.ExpertToggleAction;
046import org.openstreetmap.josm.actions.RenameLayerAction;
047import org.openstreetmap.josm.actions.ToggleUploadDiscouragedLayerAction;
048import org.openstreetmap.josm.data.APIDataSet;
049import org.openstreetmap.josm.data.Bounds;
050import org.openstreetmap.josm.data.DataSource;
051import org.openstreetmap.josm.data.ProjectionBounds;
052import org.openstreetmap.josm.data.SelectionChangedListener;
053import org.openstreetmap.josm.data.conflict.Conflict;
054import org.openstreetmap.josm.data.conflict.ConflictCollection;
055import org.openstreetmap.josm.data.coor.EastNorth;
056import org.openstreetmap.josm.data.coor.LatLon;
057import org.openstreetmap.josm.data.gpx.GpxConstants;
058import org.openstreetmap.josm.data.gpx.GpxData;
059import org.openstreetmap.josm.data.gpx.GpxLink;
060import org.openstreetmap.josm.data.gpx.ImmutableGpxTrack;
061import org.openstreetmap.josm.data.gpx.WayPoint;
062import org.openstreetmap.josm.data.osm.DataIntegrityProblemException;
063import org.openstreetmap.josm.data.osm.DataSet;
064import org.openstreetmap.josm.data.osm.DataSetMerger;
065import org.openstreetmap.josm.data.osm.DatasetConsistencyTest;
066import org.openstreetmap.josm.data.osm.IPrimitive;
067import org.openstreetmap.josm.data.osm.Node;
068import org.openstreetmap.josm.data.osm.OsmPrimitive;
069import org.openstreetmap.josm.data.osm.OsmPrimitiveComparator;
070import org.openstreetmap.josm.data.osm.Relation;
071import org.openstreetmap.josm.data.osm.Way;
072import org.openstreetmap.josm.data.osm.event.AbstractDatasetChangedEvent;
073import org.openstreetmap.josm.data.osm.event.DataSetListenerAdapter;
074import org.openstreetmap.josm.data.osm.event.DataSetListenerAdapter.Listener;
075import org.openstreetmap.josm.data.osm.visitor.AbstractVisitor;
076import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
077import org.openstreetmap.josm.data.osm.visitor.paint.MapRendererFactory;
078import org.openstreetmap.josm.data.osm.visitor.paint.Rendering;
079import org.openstreetmap.josm.data.osm.visitor.paint.relations.MultipolygonCache;
080import org.openstreetmap.josm.data.preferences.ColorProperty;
081import org.openstreetmap.josm.data.preferences.IntegerProperty;
082import org.openstreetmap.josm.data.preferences.StringProperty;
083import org.openstreetmap.josm.data.projection.Projection;
084import org.openstreetmap.josm.data.validation.TestError;
085import org.openstreetmap.josm.gui.ExtendedDialog;
086import org.openstreetmap.josm.gui.MapView;
087import org.openstreetmap.josm.gui.MapViewState.MapViewPoint;
088import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
089import org.openstreetmap.josm.gui.dialogs.LayerListPopup;
090import org.openstreetmap.josm.gui.io.AbstractIOTask;
091import org.openstreetmap.josm.gui.io.AbstractUploadDialog;
092import org.openstreetmap.josm.gui.io.UploadDialog;
093import org.openstreetmap.josm.gui.io.UploadLayerTask;
094import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
095import org.openstreetmap.josm.gui.progress.PleaseWaitProgressMonitor;
096import org.openstreetmap.josm.gui.progress.ProgressMonitor;
097import org.openstreetmap.josm.gui.util.GuiHelper;
098import org.openstreetmap.josm.gui.widgets.FileChooserManager;
099import 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        ways.stream()
649                .sorted(OsmPrimitiveComparator.comparingUniqueId().reversed())
650                .forEachOrdered(w -> {
651            if (!w.isUsable()) {
652                return;
653            }
654            Collection<Collection<WayPoint>> trk = new ArrayList<>();
655            Map<String, Object> trkAttr = new HashMap<>();
656
657            if (w.get("name") != null) {
658                trkAttr.put("name", w.get("name"));
659            }
660
661            List<WayPoint> trkseg = null;
662            for (Node n : w.getNodes()) {
663                if (!n.isUsable()) {
664                    trkseg = null;
665                    continue;
666                }
667                if (trkseg == null) {
668                    trkseg = new ArrayList<>();
669                    trk.add(trkseg);
670                }
671                if (!n.isTagged()) {
672                    doneNodes.add(n);
673                }
674                trkseg.add(nodeToWayPoint(n));
675            }
676
677            gpxData.tracks.add(new ImmutableGpxTrack(trk, trkAttr));
678        });
679    }
680
681    private static WayPoint nodeToWayPoint(Node n) {
682        WayPoint wpt = new WayPoint(n.getCoor());
683
684        // Position info
685
686        addDoubleIfPresent(wpt, n, GpxConstants.PT_ELE);
687
688        if (!n.isTimestampEmpty()) {
689            wpt.put("time", DateUtils.fromTimestamp(n.getRawTimestamp()));
690            wpt.setTime();
691        }
692
693        addDoubleIfPresent(wpt, n, GpxConstants.PT_MAGVAR);
694        addDoubleIfPresent(wpt, n, GpxConstants.PT_GEOIDHEIGHT);
695
696        // Description info
697
698        addStringIfPresent(wpt, n, GpxConstants.GPX_NAME);
699        addStringIfPresent(wpt, n, GpxConstants.GPX_DESC, "description");
700        addStringIfPresent(wpt, n, GpxConstants.GPX_CMT, "comment");
701        addStringIfPresent(wpt, n, GpxConstants.GPX_SRC, "source", "source:position");
702
703        Collection<GpxLink> links = new ArrayList<>();
704        for (String key : new String[]{"link", "url", "website", "contact:website"}) {
705            String value = n.get(key);
706            if (value != null) {
707                links.add(new GpxLink(value));
708            }
709        }
710        wpt.put(GpxConstants.META_LINKS, links);
711
712        addStringIfPresent(wpt, n, GpxConstants.PT_SYM, "wpt_symbol");
713        addStringIfPresent(wpt, n, GpxConstants.PT_TYPE);
714
715        // Accuracy info
716        addStringIfPresent(wpt, n, GpxConstants.PT_FIX, "gps:fix");
717        addIntegerIfPresent(wpt, n, GpxConstants.PT_SAT, "gps:sat");
718        addDoubleIfPresent(wpt, n, GpxConstants.PT_HDOP, "gps:hdop");
719        addDoubleIfPresent(wpt, n, GpxConstants.PT_VDOP, "gps:vdop");
720        addDoubleIfPresent(wpt, n, GpxConstants.PT_PDOP, "gps:pdop");
721        addDoubleIfPresent(wpt, n, GpxConstants.PT_AGEOFDGPSDATA, "gps:ageofdgpsdata");
722        addIntegerIfPresent(wpt, n, GpxConstants.PT_DGPSID, "gps:dgpsid");
723
724        return wpt;
725    }
726
727    private static void nodesToGpxData(Collection<Node> nodes, GpxData gpxData, Set<Node> doneNodes) {
728        List<Node> sortedNodes = new ArrayList<>(nodes);
729        sortedNodes.removeAll(doneNodes);
730        Collections.sort(sortedNodes);
731        for (Node n : sortedNodes) {
732            if (n.isIncomplete() || n.isDeleted()) {
733                continue;
734            }
735            gpxData.waypoints.add(nodeToWayPoint(n));
736        }
737    }
738
739    private static void addIntegerIfPresent(WayPoint wpt, OsmPrimitive p, String gpxKey, String ... osmKeys) {
740        List<String> possibleKeys = new ArrayList<>(Arrays.asList(osmKeys));
741        possibleKeys.add(0, gpxKey);
742        for (String key : possibleKeys) {
743            String value = p.get(key);
744            if (value != null) {
745                try {
746                    int i = Integer.parseInt(value);
747                    // Sanity checks
748                    if ((!GpxConstants.PT_SAT.equals(gpxKey) || i >= 0) &&
749                        (!GpxConstants.PT_DGPSID.equals(gpxKey) || (0 <= i && i <= 1023))) {
750                        wpt.put(gpxKey, value);
751                        break;
752                    }
753                } catch (NumberFormatException e) {
754                    Main.trace(e);
755                }
756            }
757        }
758    }
759
760    private static void addDoubleIfPresent(WayPoint wpt, OsmPrimitive p, String gpxKey, String ... osmKeys) {
761        List<String> possibleKeys = new ArrayList<>(Arrays.asList(osmKeys));
762        possibleKeys.add(0, gpxKey);
763        for (String key : possibleKeys) {
764            String value = p.get(key);
765            if (value != null) {
766                try {
767                    double d = Double.parseDouble(value);
768                    // Sanity checks
769                    if (!GpxConstants.PT_MAGVAR.equals(gpxKey) || (0.0 <= d && d < 360.0)) {
770                        wpt.put(gpxKey, value);
771                        break;
772                    }
773                } catch (NumberFormatException e) {
774                    Main.trace(e);
775                }
776            }
777        }
778    }
779
780    private static void addStringIfPresent(WayPoint wpt, OsmPrimitive p, String gpxKey, String ... osmKeys) {
781        List<String> possibleKeys = new ArrayList<>(Arrays.asList(osmKeys));
782        possibleKeys.add(0, gpxKey);
783        for (String key : possibleKeys) {
784            String value = p.get(key);
785            // Sanity checks
786            if (value != null && (!GpxConstants.PT_FIX.equals(gpxKey) || GpxConstants.FIX_VALUES.contains(value))) {
787                wpt.put(gpxKey, value);
788                break;
789            }
790        }
791    }
792
793    /**
794     * Converts OSM data behind this layer to GPX data.
795     * @return GPX data
796     */
797    public GpxData toGpxData() {
798        return toGpxData(data, getAssociatedFile());
799    }
800
801    /**
802     * Action that converts this OSM layer to a GPX layer.
803     */
804    public class ConvertToGpxLayerAction extends AbstractAction {
805        /**
806         * Constructs a new {@code ConvertToGpxLayerAction}.
807         */
808        public ConvertToGpxLayerAction() {
809            super(tr("Convert to GPX layer"), ImageProvider.get("converttogpx"));
810            putValue("help", ht("/Action/ConvertToGpxLayer"));
811        }
812
813        @Override
814        public void actionPerformed(ActionEvent e) {
815            final GpxData gpxData = toGpxData();
816            final GpxLayer gpxLayer = new GpxLayer(gpxData, tr("Converted from: {0}", getName()));
817            if (getAssociatedFile() != null) {
818                String filename = getAssociatedFile().getName().replaceAll(Pattern.quote(".gpx.osm") + '$', "") + ".gpx";
819                gpxLayer.setAssociatedFile(new File(getAssociatedFile().getParentFile(), filename));
820            }
821            Main.getLayerManager().addLayer(gpxLayer);
822            if (Main.pref.getBoolean("marker.makeautomarkers", true) && !gpxData.waypoints.isEmpty()) {
823                Main.getLayerManager().addLayer(new MarkerLayer(gpxData, tr("Converted from: {0}", getName()), null, gpxLayer));
824            }
825            Main.getLayerManager().removeLayer(OsmDataLayer.this);
826        }
827    }
828
829    /**
830     * Determines if this layer contains data at the given coordinate.
831     * @param coor the coordinate
832     * @return {@code true} if data sources bounding boxes contain {@code coor}
833     */
834    public boolean containsPoint(LatLon coor) {
835        // we'll assume that if this has no data sources
836        // that it also has no borders
837        if (this.data.dataSources.isEmpty())
838            return true;
839
840        boolean layerBoundsPoint = false;
841        for (DataSource src : this.data.dataSources) {
842            if (src.bounds.contains(coor)) {
843                layerBoundsPoint = true;
844                break;
845            }
846        }
847        return layerBoundsPoint;
848    }
849
850    /**
851     * Replies the set of conflicts currently managed in this layer.
852     *
853     * @return the set of conflicts currently managed in this layer
854     */
855    public ConflictCollection getConflicts() {
856        return conflicts;
857    }
858
859    @Override
860    public boolean isUploadable() {
861        return true;
862    }
863
864    @Override
865    public boolean requiresUploadToServer() {
866        return requiresUploadToServer;
867    }
868
869    @Override
870    public boolean requiresSaveToFile() {
871        return getAssociatedFile() != null && requiresSaveToFile;
872    }
873
874    @Override
875    public void onPostLoadFromFile() {
876        setRequiresSaveToFile(false);
877        setRequiresUploadToServer(isModified());
878        invalidate();
879    }
880
881    /**
882     * Actions run after data has been downloaded to this layer.
883     */
884    public void onPostDownloadFromServer() {
885        setRequiresSaveToFile(true);
886        setRequiresUploadToServer(isModified());
887        invalidate();
888    }
889
890    @Override
891    public boolean isChanged() {
892        return highlightUpdateCount != data.getHighlightUpdateCount();
893    }
894
895    @Override
896    public void onPostSaveToFile() {
897        setRequiresSaveToFile(false);
898        setRequiresUploadToServer(isModified());
899    }
900
901    @Override
902    public void onPostUploadToServer() {
903        setRequiresUploadToServer(isModified());
904        // keep requiresSaveToDisk unchanged
905    }
906
907    private class ConsistencyTestAction extends AbstractAction {
908
909        ConsistencyTestAction() {
910            super(tr("Dataset consistency test"));
911        }
912
913        @Override
914        public void actionPerformed(ActionEvent e) {
915            String result = DatasetConsistencyTest.runTests(data);
916            if (result.isEmpty()) {
917                JOptionPane.showMessageDialog(Main.parent, tr("No problems found"));
918            } else {
919                JPanel p = new JPanel(new GridBagLayout());
920                p.add(new JLabel(tr("Following problems found:")), GBC.eol());
921                JosmTextArea info = new JosmTextArea(result, 20, 60);
922                info.setCaretPosition(0);
923                info.setEditable(false);
924                p.add(new JScrollPane(info), GBC.eop());
925
926                JOptionPane.showMessageDialog(Main.parent, p, tr("Warning"), JOptionPane.WARNING_MESSAGE);
927            }
928        }
929    }
930
931    @Override
932    public void destroy() {
933        super.destroy();
934        DataSet.removeSelectionListener(this);
935    }
936
937    @Override
938    public void processDatasetEvent(AbstractDatasetChangedEvent event) {
939        invalidate();
940        setRequiresSaveToFile(true);
941        setRequiresUploadToServer(true);
942    }
943
944    @Override
945    public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) {
946        invalidate();
947    }
948
949    @Override
950    public void projectionChanged(Projection oldValue, Projection newValue) {
951         // No reprojection required. The dataset itself is registered as projection
952         // change listener and already got notified.
953    }
954
955    @Override
956    public final boolean isUploadDiscouraged() {
957        return data.isUploadDiscouraged();
958    }
959
960    /**
961     * Sets the "discouraged upload" flag.
962     * @param uploadDiscouraged {@code true} if upload of data managed by this layer is discouraged.
963     * This feature allows to use "private" data layers.
964     */
965    public final void setUploadDiscouraged(boolean uploadDiscouraged) {
966        if (uploadDiscouraged ^ isUploadDiscouraged()) {
967            data.setUploadDiscouraged(uploadDiscouraged);
968            for (LayerStateChangeListener l : layerStateChangeListeners) {
969                l.uploadDiscouragedChanged(this, uploadDiscouraged);
970            }
971        }
972    }
973
974    @Override
975    public final boolean isModified() {
976        return data.isModified();
977    }
978
979    @Override
980    public boolean isSavable() {
981        return true; // With OsmExporter
982    }
983
984    @Override
985    public boolean checkSaveConditions() {
986        if (isDataSetEmpty() && 1 != GuiHelper.runInEDTAndWaitAndReturn(() -> {
987            if (GraphicsEnvironment.isHeadless()) {
988                return 2;
989            }
990            ExtendedDialog dialog = new ExtendedDialog(
991                    Main.parent,
992                    tr("Empty document"),
993                    new String[] {tr("Save anyway"), tr("Cancel")}
994            );
995            dialog.setContent(tr("The document contains no data."));
996            dialog.setButtonIcons(new String[] {"save", "cancel"});
997            return dialog.showDialog().getValue();
998        })) {
999            return false;
1000        }
1001
1002        ConflictCollection conflictsCol = getConflicts();
1003        if (conflictsCol != null && !conflictsCol.isEmpty() && 1 != GuiHelper.runInEDTAndWaitAndReturn(() -> {
1004            ExtendedDialog dialog = new ExtendedDialog(
1005                    Main.parent,
1006                    /* I18N: Display title of the window showing conflicts */
1007                    tr("Conflicts"),
1008                    new String[] {tr("Reject Conflicts and Save"), tr("Cancel")}
1009            );
1010            dialog.setContent(
1011                    tr("There are unresolved conflicts. Conflicts will not be saved and handled as if you rejected all. Continue?"));
1012            dialog.setButtonIcons(new String[] {"save", "cancel"});
1013            return dialog.showDialog().getValue();
1014        })) {
1015            return false;
1016        }
1017        return true;
1018    }
1019
1020    /**
1021     * Check the data set if it would be empty on save. It is empty, if it contains
1022     * no objects (after all objects that are created and deleted without being
1023     * transferred to the server have been removed).
1024     *
1025     * @return <code>true</code>, if a save result in an empty data set.
1026     */
1027    private boolean isDataSetEmpty() {
1028        if (data != null) {
1029            for (OsmPrimitive osm : data.allNonDeletedPrimitives()) {
1030                if (!osm.isDeleted() || !osm.isNewOrUndeleted())
1031                    return false;
1032            }
1033        }
1034        return true;
1035    }
1036
1037    @Override
1038    public File createAndOpenSaveFileChooser() {
1039        String extension = PROPERTY_SAVE_EXTENSION.get();
1040        File file = getAssociatedFile();
1041        if (file == null && isRenamed()) {
1042            String filename = Main.pref.get("lastDirectory") + '/' + getName();
1043            if (!OsmImporter.FILE_FILTER.acceptName(filename))
1044                filename = filename + '.' + extension;
1045            file = new File(filename);
1046        }
1047        return new FileChooserManager()
1048            .title(tr("Save OSM file"))
1049            .extension(extension)
1050            .file(file)
1051            .allTypes(true)
1052            .getFileForSave();
1053    }
1054
1055    @Override
1056    public AbstractIOTask createUploadTask(final ProgressMonitor monitor) {
1057        UploadDialog dialog = UploadDialog.getUploadDialog();
1058        return new UploadLayerTask(
1059                dialog.getUploadStrategySpecification(),
1060                this,
1061                monitor,
1062                dialog.getChangeset());
1063    }
1064
1065    @Override
1066    public AbstractUploadDialog getUploadDialog() {
1067        UploadDialog dialog = UploadDialog.getUploadDialog();
1068        dialog.setUploadedPrimitives(new APIDataSet(data));
1069        return dialog;
1070    }
1071
1072    @Override
1073    public ProjectionBounds getViewProjectionBounds() {
1074        BoundingXYVisitor v = new BoundingXYVisitor();
1075        v.visit(data.getDataSourceBoundingBox());
1076        if (!v.hasExtend()) {
1077            v.computeBoundingBox(data.getNodes());
1078        }
1079        return v.getBounds();
1080    }
1081}