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