001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.command.conflict;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.util.Collection;
007import java.util.List;
008import java.util.Objects;
009
010import javax.swing.Icon;
011
012import org.openstreetmap.josm.Main;
013import org.openstreetmap.josm.data.conflict.Conflict;
014import org.openstreetmap.josm.data.osm.Node;
015import org.openstreetmap.josm.data.osm.OsmPrimitive;
016import org.openstreetmap.josm.data.osm.Way;
017import org.openstreetmap.josm.tools.ImageProvider;
018
019/**
020 * Represents the resolution of conflicts in the node list of two {@link Way}s.
021 *
022 */
023public class WayNodesConflictResolverCommand extends ConflictResolveCommand {
024    /** the conflict to resolve */
025    private final 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
042    @Override
043    public String getDescriptionText() {
044        return tr("Resolve conflicts in node list of way {0}", conflict.getMy().getId());
045    }
046
047    @Override
048    public Icon getDescriptionIcon() {
049        return ImageProvider.get("data", "object");
050    }
051
052    @Override
053    public boolean executeCommand() {
054        // remember the current state of 'my' way
055        //
056        super.executeCommand();
057
058        // replace the list of nodes of 'my' way by the list of merged nodes
059        //
060        for (Node n:mergedNodeList) {
061            if (!getLayer().data.getNodes().contains(n)) {
062                Main.warn(tr("Main dataset does not include node {0}", n.toString()));
063            }
064        }
065        conflict.getMy().setNodes(mergedNodeList);
066        rememberConflict(conflict);
067        return true;
068    }
069
070    @Override
071    public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted,
072            Collection<OsmPrimitive> added) {
073        modified.add(conflict.getMy());
074    }
075
076    @Override
077    public int hashCode() {
078        return Objects.hash(super.hashCode(), conflict, mergedNodeList);
079    }
080
081    @Override
082    public boolean equals(Object obj) {
083        if (this == obj) return true;
084        if (obj == null || getClass() != obj.getClass()) return false;
085        if (!super.equals(obj)) return false;
086        WayNodesConflictResolverCommand that = (WayNodesConflictResolverCommand) obj;
087        return Objects.equals(conflict, that.conflict) &&
088                Objects.equals(mergedNodeList, that.mergedNodeList);
089    }
090}