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        super(conflict.getMy().getDataSet());
032        this.conflict = conflict;
033    }
034
035    @Override
036    public String getDescriptionText() {
037        String msg;
038        switch(OsmPrimitiveType.from(conflict.getMy())) {
039        case NODE: msg = marktr("Resolve version conflict for node {0}"); break;
040        case WAY: msg = marktr("Resolve version conflict for way {0}"); break;
041        case RELATION: msg = marktr("Resolve version conflict for relation {0}"); break;
042        default: throw new AssertionError();
043        }
044        return tr(msg, conflict.getMy().getId());
045    }
046
047    @Override
048    public Icon getDescriptionIcon() {
049        return ImageProvider.get("data", "object");
050    }
051
052    @Override
053    public boolean executeCommand() {
054        super.executeCommand();
055        if (!conflict.getMy().isNew()) {
056            long myVersion = conflict.getMy().getVersion();
057            long theirVersion = conflict.getTheir().getVersion();
058            conflict.getMy().setOsmId(
059                    conflict.getMy().getId(),
060                    (int) Math.max(myVersion, theirVersion)
061            );
062            // update visiblity state
063            if (theirVersion >= myVersion) {
064                conflict.getMy().setVisible(conflict.getTheir().isVisible());
065            }
066        }
067        getAffectedDataSet().getConflicts().remove(conflict);
068        rememberConflict(conflict);
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);
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        VersionConflictResolveCommand that = (VersionConflictResolveCommand) obj;
089        return Objects.equals(conflict, that.conflict);
090    }
091}