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        super(conflict.getMy().getDataSet());
037        this.conflict = conflict;
038        this.decision = decision;
039    }
040
041    @Override
042    public String getDescriptionText() {
043        return tr("Resolve conflicts in coordinates in {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 modified primitives, i.e. of OSM primitive 'my'
054        super.executeCommand();
055
056        if (decision == MergeDecisionType.KEEP_MINE) {
057            // do nothing
058        } else if (decision == MergeDecisionType.KEEP_THEIR) {
059            Node my = (Node) conflict.getMy();
060            Node their = (Node) conflict.getTheir();
061            my.setCoor(their.getCoor());
062        } else
063            // should not happen
064            throw new IllegalStateException(tr("Cannot resolve undecided conflict."));
065
066        // remember the layer this command was applied to
067        rememberConflict(conflict);
068
069        return true;
070    }
071
072    @Override
073    public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted,
074            Collection<OsmPrimitive> added) {
075        modified.add(conflict.getMy());
076    }
077
078    @Override
079    public int hashCode() {
080        return Objects.hash(super.hashCode(), conflict, decision);
081    }
082
083    @Override
084    public boolean equals(Object obj) {
085        if (this == obj) return true;
086        if (obj == null || getClass() != obj.getClass()) return false;
087        if (!super.equals(obj)) return false;
088        CoordinateConflictResolveCommand that = (CoordinateConflictResolveCommand) obj;
089        return decision == that.decision && Objects.equals(conflict, that.conflict);
090    }
091}