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 conflict between the modified flag of two {@link OsmPrimitive}s.
019 * @since 2624
020 */
021public class ModifiedConflictResolveCommand 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 ModifiedConflictResolveCommand(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("Set the ''modified'' flag for node {0}"); break;
040        case WAY: msg = marktr("Set the ''modified'' flag for way {0}"); break;
041        case RELATION: msg = marktr("Set the ''modified'' flag 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() && !conflict.getMy().isDeleted()
056                && conflict.getMy().hasEqualSemanticAttributes(conflict.getTheir())) {
057            conflict.getMy().setModified(conflict.getTheir().isModified());
058        }
059        getAffectedDataSet().getConflicts().remove(conflict);
060        rememberConflict(conflict);
061        return true;
062    }
063
064    @Override
065    public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted,
066            Collection<OsmPrimitive> added) {
067        modified.add(conflict.getMy());
068    }
069
070    @Override
071    public int hashCode() {
072        return Objects.hash(super.hashCode(), conflict);
073    }
074
075    @Override
076    public boolean equals(Object obj) {
077        if (this == obj) return true;
078        if (obj == null || getClass() != obj.getClass()) return false;
079        if (!super.equals(obj)) return false;
080        ModifiedConflictResolveCommand that = (ModifiedConflictResolveCommand) obj;
081        return Objects.equals(conflict, that.conflict);
082    }
083}