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 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
052        // OSM primitive 'my'
053        //
054        super.executeCommand();
055
056        if (decision.equals(MergeDecisionType.KEEP_MINE)) {
057            // do nothing
058        } else if (decision.equals(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        //
068        rememberConflict(conflict);
069
070        return true;
071    }
072
073    @Override
074    public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted,
075            Collection<OsmPrimitive> added) {
076        modified.add(conflict.getMy());
077    }
078}