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