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