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().hasEqualSemanticAttributes(conflict.getTheir())) {
056            conflict.getMy().setModified(conflict.getTheir().isModified());
057        }
058        getAffectedDataSet().getConflicts().remove(conflict);
059        rememberConflict(conflict);
060        return true;
061    }
062
063    @Override
064    public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted,
065            Collection<OsmPrimitive> added) {
066        modified.add(conflict.getMy());
067    }
068
069    @Override
070    public int hashCode() {
071        return Objects.hash(super.hashCode(), conflict);
072    }
073
074    @Override
075    public boolean equals(Object obj) {
076        if (this == obj) return true;
077        if (obj == null || getClass() != obj.getClass()) return false;
078        if (!super.equals(obj)) return false;
079        ModifiedConflictResolveCommand that = (ModifiedConflictResolveCommand) obj;
080        return Objects.equals(conflict, that.conflict);
081    }
082}