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;
008
009import javax.swing.Icon;
010
011import org.openstreetmap.josm.data.conflict.Conflict;
012import org.openstreetmap.josm.data.osm.OsmPrimitive;
013import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
014import org.openstreetmap.josm.tools.ImageProvider;
015
016/**
017 * Represents the resolution of a version conflict between two {@link OsmPrimitive}s.
018 *
019 *
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        }
042        return tr(msg, conflict.getMy().getId());
043    }
044
045    @Override
046    public Icon getDescriptionIcon() {
047        return ImageProvider.get("data", "object");
048    }
049
050    @Override
051    public boolean executeCommand() {
052        super.executeCommand();
053        if (!conflict.getMy().isNew()) {
054            long myVersion = conflict.getMy().getVersion();
055            long theirVersion = conflict.getTheir().getVersion();
056            conflict.getMy().setOsmId(
057                    conflict.getMy().getId(),
058                    (int) Math.max(myVersion, theirVersion)
059            );
060            // update visiblity state
061            if (theirVersion >= myVersion) {
062                conflict.getMy().setVisible(conflict.getTheir().isVisible());
063            }
064        }
065        getLayer().getConflicts().remove(conflict);
066        rememberConflict(conflict);
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        return result;
082    }
083
084    @Override
085    public boolean equals(Object obj) {
086        if (this == obj)
087            return true;
088        if (!super.equals(obj))
089            return false;
090        if (getClass() != obj.getClass())
091            return false;
092        VersionConflictResolveCommand other = (VersionConflictResolveCommand) obj;
093        if (conflict == null) {
094            if (other.conflict != null)
095                return false;
096        } else if (!conflict.equals(other.conflict))
097            return false;
098        return true;
099    }
100}