001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.io;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.util.List;
007
008import org.openstreetmap.josm.data.osm.Node;
009import org.openstreetmap.josm.data.osm.OsmPrimitive;
010import org.openstreetmap.josm.data.osm.PrimitiveId;
011import org.openstreetmap.josm.data.osm.Relation;
012import org.openstreetmap.josm.data.osm.Way;
013import org.openstreetmap.josm.gui.layer.OsmDataLayer;
014import org.openstreetmap.josm.gui.progress.ProgressMonitor;
015import org.openstreetmap.josm.io.MultiFetchServerObjectReader;
016
017/**
018 * Task downloading a set of OSM primitives.
019 * @since 4081
020 */
021public class DownloadPrimitivesTask extends AbstractPrimitiveTask {
022
023    private final List<PrimitiveId> ids;
024
025    /**
026     * Constructs a new {@code DownloadPrimitivesTask}.
027     *
028     * @param layer the layer in which primitives are updated. Must not be null.
029     * @param ids a collection of primitives to update from the server. Set to
030     * the empty collection if null.
031     * @param fullRelation true if a full download is required, i.e.,
032     * a download including the immediate children of a relation.
033     * @throws IllegalArgumentException if layer is null.
034     */
035    public DownloadPrimitivesTask(OsmDataLayer layer, List<PrimitiveId> ids, boolean fullRelation) {
036        this(layer, ids, fullRelation, null);
037    }
038
039    /**
040     * Constructs a new {@code DownloadPrimitivesTask}.
041     *
042     * @param layer the layer in which primitives are updated. Must not be null.
043     * @param ids a collection of primitives to update from the server. Set to
044     *     the empty collection if null.
045     * @param fullRelation true if a full download is required, i.e.,
046     *     a download including the immediate children of a relation.
047     * @param progressMonitor ProgressMonitor to use or null to create a new one.
048     * @throws IllegalArgumentException if layer is null.
049     */
050    public DownloadPrimitivesTask(OsmDataLayer layer, List<PrimitiveId> ids, boolean fullRelation,
051            ProgressMonitor progressMonitor) {
052        super(tr("Download objects"), progressMonitor, layer);
053        this.ids = ids;
054        setZoom(true);
055        setDownloadRelations(true, fullRelation);
056    }
057
058    @Override
059    protected void initMultiFetchReader(MultiFetchServerObjectReader reader) {
060        getProgressMonitor().indeterminateSubTask(tr("Initializing nodes to download ..."));
061        for (PrimitiveId id : ids) {
062            OsmPrimitive osm = layer.data.getPrimitiveById(id);
063            if (osm == null) {
064                switch (id.getType()) {
065                    case NODE:
066                        osm = new Node(id.getUniqueId());
067                        break;
068                    case WAY:
069                        osm = new Way(id.getUniqueId());
070                        break;
071                    case RELATION:
072                        osm = new Relation(id.getUniqueId());
073                        break;
074                    default: throw new AssertionError();
075                }
076            }
077            reader.append(osm);
078        }
079    }
080}