001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.command.conflict;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.util.Collection;
007import java.util.Objects;
008
009import javax.swing.Icon;
010import javax.swing.JOptionPane;
011
012import org.openstreetmap.josm.Main;
013import org.openstreetmap.josm.command.Command;
014import org.openstreetmap.josm.data.conflict.Conflict;
015import org.openstreetmap.josm.data.osm.DataSet;
016import org.openstreetmap.josm.data.osm.DefaultNameFormatter;
017import org.openstreetmap.josm.data.osm.OsmPrimitive;
018import org.openstreetmap.josm.tools.ImageProvider;
019import org.openstreetmap.josm.tools.Logging;
020import org.openstreetmap.josm.tools.Utils;
021
022/**
023 * Command used to add a new conflict.
024 * @since 1857
025 */
026public class ConflictAddCommand extends Command {
027    private final Conflict<? extends OsmPrimitive> conflict;
028
029    /**
030     * Constructs a new {@code ConflictAddCommand}.
031     * @param ds the data set. Must not be null.
032     * @param conflict the conflict to add
033     * @since 12672
034     */
035    public ConflictAddCommand(DataSet ds, Conflict<? extends OsmPrimitive> conflict) {
036        super(ds);
037        this.conflict = conflict;
038    }
039
040    protected void warnBecauseOfDoubleConflict() {
041        JOptionPane.showMessageDialog(
042                Main.parent,
043                tr("<html>Layer ''{0}'' already has a conflict for object<br>"
044                        + "''{1}''.<br>"
045                        + "This conflict cannot be added.</html>",
046                        Utils.escapeReservedCharactersHTML(getAffectedDataSet().getName()),
047                        Utils.escapeReservedCharactersHTML(conflict.getMy().getDisplayName(DefaultNameFormatter.getInstance()))
048                ),
049                tr("Double conflict"),
050                JOptionPane.ERROR_MESSAGE
051        );
052    }
053
054    @Override
055    public boolean executeCommand() {
056        try {
057            getAffectedDataSet().getConflicts().add(conflict);
058        } catch (IllegalStateException e) {
059            Logging.error(e);
060            warnBecauseOfDoubleConflict();
061        }
062        return true;
063    }
064
065    @Override
066    public void undoCommand() {
067        DataSet ds = getAffectedDataSet();
068        if (Main.main != null && !Main.main.containsDataSet(ds)) {
069            Logging.warn(tr("Layer ''{0}'' does not exist any more. Cannot remove conflict for object ''{1}''.",
070                    ds.getName(),
071                    conflict.getMy().getDisplayName(DefaultNameFormatter.getInstance())
072            ));
073            return;
074        }
075        ds.getConflicts().remove(conflict);
076    }
077
078    @Override
079    public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
080        // nothing to fill
081    }
082
083    @Override
084    public String getDescriptionText() {
085        return tr("Add conflict for ''{0}''",
086                conflict.getMy().getDisplayName(DefaultNameFormatter.getInstance()));
087    }
088
089    @Override
090    public Icon getDescriptionIcon() {
091        return ImageProvider.get(conflict.getMy().getDisplayType());
092    }
093
094    @Override
095    public int hashCode() {
096        return Objects.hash(super.hashCode(), conflict);
097    }
098
099    @Override
100    public boolean equals(Object obj) {
101        if (this == obj) return true;
102        if (obj == null || getClass() != obj.getClass()) return false;
103        if (!super.equals(obj)) return false;
104        ConflictAddCommand that = (ConflictAddCommand) obj;
105        return Objects.equals(conflict, that.conflict);
106    }
107}