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