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