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.Objects;
008
009import javax.swing.Icon;
010
011import org.openstreetmap.josm.data.conflict.Conflict;
012import org.openstreetmap.josm.data.osm.Node;
013import org.openstreetmap.josm.data.osm.OsmPrimitive;
014import org.openstreetmap.josm.gui.conflict.pair.MergeDecisionType;
015import org.openstreetmap.josm.tools.ImageProvider;
016
017/**
018 * Represents the resolution of a conflict between the coordinates of two {@link Node}s.
019 *
020 */
021public class CoordinateConflictResolveCommand extends ConflictResolveCommand {
022
023    /** the conflict to resolve */
024    private final Conflict<? extends OsmPrimitive> conflict;
025
026    /** the merge decision */
027    private final MergeDecisionType decision;
028
029    /**
030     * constructor for coordinate conflict
031     *
032     * @param conflict the conflict data set
033     * @param decision the merge decision
034     */
035    public CoordinateConflictResolveCommand(Conflict<? extends OsmPrimitive> conflict, MergeDecisionType decision) {
036        this.conflict = conflict;
037        this.decision = decision;
038    }
039
040    @Override
041    public String getDescriptionText() {
042        return tr("Resolve conflicts in coordinates in {0}", conflict.getMy().getId());
043    }
044
045    @Override
046    public Icon getDescriptionIcon() {
047        return ImageProvider.get("data", "object");
048    }
049
050    @Override
051    public boolean executeCommand() {
052        // remember the current state of modified primitives, i.e. of OSM primitive 'my'
053        super.executeCommand();
054
055        if (decision.equals(MergeDecisionType.KEEP_MINE)) {
056            // do nothing
057        } else if (decision.equals(MergeDecisionType.KEEP_THEIR)) {
058            Node my = (Node) conflict.getMy();
059            Node their = (Node) conflict.getTheir();
060            my.setCoor(their.getCoor());
061        } else
062            // should not happen
063            throw new IllegalStateException(tr("Cannot resolve undecided conflict."));
064
065        // remember the layer this command was applied to
066        rememberConflict(conflict);
067
068        return true;
069    }
070
071    @Override
072    public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted,
073            Collection<OsmPrimitive> added) {
074        modified.add(conflict.getMy());
075    }
076
077    @Override
078    public int hashCode() {
079        return Objects.hash(super.hashCode(), conflict, decision);
080    }
081
082    @Override
083    public boolean equals(Object obj) {
084        if (this == obj) return true;
085        if (obj == null || getClass() != obj.getClass()) return false;
086        if (!super.equals(obj)) return false;
087        CoordinateConflictResolveCommand that = (CoordinateConflictResolveCommand) obj;
088        return Objects.equals(conflict, that.conflict) &&
089                decision == that.decision;
090    }
091}