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