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.Objects; 007 008import org.openstreetmap.josm.Main; 009import org.openstreetmap.josm.command.Command; 010import org.openstreetmap.josm.data.conflict.Conflict; 011import org.openstreetmap.josm.data.conflict.ConflictCollection; 012import org.openstreetmap.josm.data.osm.DataSet; 013import org.openstreetmap.josm.tools.Logging; 014 015/** 016 * This is the common base class for {@link Command}s which manipulate {@link Conflict}s in 017 * addition to {@link org.openstreetmap.josm.data.osm.OsmPrimitive}s. 018 * 019 * A ConflictResolverCommand can remember a collection of conflicts it resolves. Upon undoing 020 * it reconstitutes them. 021 * 022 */ 023public abstract class ConflictResolveCommand extends Command { 024 /** the list of resolved conflicts */ 025 private final ConflictCollection resolvedConflicts = new ConflictCollection(); 026 027 /** 028 * Constructs a new {@code ConflictResolveCommand} in the context of a given data set. 029 * @param ds the data set. Must not be null. 030 */ 031 public ConflictResolveCommand(DataSet ds) { 032 super(ds); 033 } 034 035 /** 036 * remembers a conflict in the internal list of remembered conflicts 037 * 038 * @param c the remembered conflict 039 */ 040 protected void rememberConflict(Conflict<?> c) { 041 if (!resolvedConflicts.hasConflictForMy(c.getMy())) { 042 resolvedConflicts.add(c); 043 } 044 } 045 046 /** 047 * reconstitutes all remembered conflicts. Add the remembered conflicts to the 048 * set of conflicts of the {@link DataSet} this command was applied to. 049 * 050 */ 051 protected void reconstituteConflicts() { 052 DataSet ds = getAffectedDataSet(); 053 for (Conflict<?> c : resolvedConflicts) { 054 if (!ds.getConflicts().hasConflictForMy(c.getMy())) { 055 ds.getConflicts().add(c); 056 } 057 } 058 } 059 060 @Override 061 public void undoCommand() { 062 super.undoCommand(); 063 064 DataSet ds = getAffectedDataSet(); 065 if (Main.main != null) { 066 if (!Main.main.containsDataSet(ds)) { 067 Logging.warn(tr("Cannot undo command ''{0}'' because layer ''{1}'' is not present any more", 068 this.toString(), 069 ds.getName() 070 )); 071 return; 072 } 073 074 Main.main.setActiveDataSet(ds); 075 } 076 reconstituteConflicts(); 077 } 078 079 @Override 080 public int hashCode() { 081 return Objects.hash(super.hashCode(), resolvedConflicts); 082 } 083 084 @Override 085 public boolean equals(Object obj) { 086 if (this == obj) return true; 087 if (obj == null || getClass() != obj.getClass()) return false; 088 if (!super.equals(obj)) return false; 089 ConflictResolveCommand that = (ConflictResolveCommand) obj; 090 return Objects.equals(resolvedConflicts, that.resolvedConflicts); 091 } 092}