001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.command.conflict;
003
004import static org.openstreetmap.josm.tools.I18n.marktr;
005import static org.openstreetmap.josm.tools.I18n.tr;
006
007import java.util.Collection;
008import java.util.Objects;
009
010import javax.swing.Icon;
011
012import org.openstreetmap.josm.data.conflict.Conflict;
013import org.openstreetmap.josm.data.osm.OsmPrimitive;
014import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
015import org.openstreetmap.josm.tools.ImageProvider;
016
017/**
018 * Represents the resolution of a version conflict between two {@link OsmPrimitive}s.
019 * @since 1622
020 */
021public class VersionConflictResolveCommand extends ConflictResolveCommand {
022
023    /** the conflict to resolve */
024    private final Conflict<? extends OsmPrimitive> conflict;
025
026    /**
027     * constructor
028     * @param conflict the conflict data set
029     */
030    public VersionConflictResolveCommand(Conflict<? extends OsmPrimitive> conflict) {
031        this.conflict = conflict;
032    }
033
034    @Override
035    public String getDescriptionText() {
036        String msg;
037        switch(OsmPrimitiveType.from(conflict.getMy())) {
038        case NODE: msg = marktr("Resolve version conflict for node {0}"); break;
039        case WAY: msg = marktr("Resolve version conflict for way {0}"); break;
040        case RELATION: msg = marktr("Resolve version conflict for relation {0}"); break;
041        default: throw new AssertionError();
042        }
043        return tr(msg, 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        super.executeCommand();
054        if (!conflict.getMy().isNew()) {
055            long myVersion = conflict.getMy().getVersion();
056            long theirVersion = conflict.getTheir().getVersion();
057            conflict.getMy().setOsmId(
058                    conflict.getMy().getId(),
059                    (int) Math.max(myVersion, theirVersion)
060            );
061            // update visiblity state
062            if (theirVersion >= myVersion) {
063                conflict.getMy().setVisible(conflict.getTheir().isVisible());
064            }
065        }
066        getLayer().getConflicts().remove(conflict);
067        rememberConflict(conflict);
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);
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        VersionConflictResolveCommand that = (VersionConflictResolveCommand) obj;
088        return Objects.equals(conflict, that.conflict);
089    }
090}