001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.command;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.util.Collection;
007import java.util.List;
008
009import javax.swing.Icon;
010
011import org.openstreetmap.josm.Main;
012import org.openstreetmap.josm.data.conflict.Conflict;
013import org.openstreetmap.josm.data.osm.Node;
014import org.openstreetmap.josm.data.osm.OsmPrimitive;
015import org.openstreetmap.josm.data.osm.Way;
016import org.openstreetmap.josm.tools.ImageProvider;
017
018/**
019 * Represent a command for resolving conflicts in the node list of two
020 * {@link Way}s.
021 *
022 */
023public class WayNodesConflictResolverCommand extends ConflictResolveCommand {
024    /** the conflict to resolve */
025    private Conflict<Way> conflict;
026
027    /** the list of merged nodes. This becomes the list of news of my way after the
028     *  command is executed
029     */
030    private final List<Node> mergedNodeList;
031
032    /**
033     * @param conflict the conflict data set
034     * @param mergedNodeList the list of merged nodes
035     */
036    @SuppressWarnings("unchecked")
037    public WayNodesConflictResolverCommand(Conflict<? extends OsmPrimitive> conflict, List<Node> mergedNodeList) {
038        this.conflict = (Conflict<Way>) conflict;
039        this.mergedNodeList = mergedNodeList;
040    }
041    @Override
042    public String getDescriptionText() {
043        return tr("Resolve conflicts in node list of way {0}", conflict.getMy().getId());
044    }
045
046    @Override
047    public Icon getDescriptionIcon() {
048        return ImageProvider.get("data", "object");
049    }
050
051    @Override
052    public boolean executeCommand() {
053        // remember the current state of 'my' way
054        //
055        super.executeCommand();
056
057        // replace the list of nodes of 'my' way by the list of merged nodes
058        //
059        for (Node n:mergedNodeList) {
060            if (! getLayer().data.getNodes().contains(n)) {
061                Main.warn(tr("Main dataset does not include node {0}", n.toString()));
062            }
063        }
064        conflict.getMy().setNodes(mergedNodeList);
065        rememberConflict(conflict);
066        return true;
067    }
068
069    @Override
070    public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted,
071            Collection<OsmPrimitive> added) {
072        modified.add(conflict.getMy());
073    }
074}