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