[2512] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[6887] | 2 | package org.openstreetmap.josm.command.conflict;
|
---|
[2512] | 3 |
|
---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 5 |
|
---|
[9371] | 6 | import java.util.Objects;
|
---|
| 7 |
|
---|
[12718] | 8 | import org.openstreetmap.josm.Main;
|
---|
[6887] | 9 | import org.openstreetmap.josm.command.Command;
|
---|
[2512] | 10 | import org.openstreetmap.josm.data.conflict.Conflict;
|
---|
| 11 | import org.openstreetmap.josm.data.conflict.ConflictCollection;
|
---|
[12672] | 12 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
[2512] | 13 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
[12620] | 14 | import org.openstreetmap.josm.tools.Logging;
|
---|
[2512] | 15 |
|
---|
| 16 | /**
|
---|
[5266] | 17 | * This is the common base class for {@link Command}s which manipulate {@link Conflict}s in
|
---|
[5816] | 18 | * addition to {@link org.openstreetmap.josm.data.osm.OsmPrimitive}s.
|
---|
[2512] | 19 | *
|
---|
| 20 | * A ConflictResolverCommand can remember a collection of conflicts it resolves. Upon undoing
|
---|
| 21 | * it reconstitutes them.
|
---|
| 22 | *
|
---|
| 23 | */
|
---|
| 24 | public abstract class ConflictResolveCommand extends Command {
|
---|
| 25 | /** the list of resolved conflicts */
|
---|
[12718] | 26 | private final ConflictCollection resolvedConflicts = new ConflictCollection();
|
---|
[2512] | 27 |
|
---|
[6881] | 28 | /**
|
---|
| 29 | * Constructs a new {@code ConflictResolveCommand} in the context of the current edit layer, if any.
|
---|
| 30 | */
|
---|
[2512] | 31 | public ConflictResolveCommand() {
|
---|
[12718] | 32 | // Do nothing
|
---|
[2512] | 33 | }
|
---|
| 34 |
|
---|
[6881] | 35 | /**
|
---|
| 36 | * Constructs a new {@code ConflictResolveCommand} in the context of a given data layer.
|
---|
| 37 | * @param layer the data layer. Must not be null.
|
---|
[12718] | 38 | * @deprecated to be removed end of 2017. Use {@link #ConflictResolveCommand(DataSet)} instead
|
---|
[6881] | 39 | */
|
---|
[12718] | 40 | @Deprecated
|
---|
[2512] | 41 | public ConflictResolveCommand(OsmDataLayer layer) {
|
---|
| 42 | super(layer);
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | /**
|
---|
[12718] | 46 | * Constructs a new {@code ConflictResolveCommand} in the context of a given data set.
|
---|
| 47 | * @param ds the data set. Must not be null.
|
---|
| 48 | */
|
---|
| 49 | public ConflictResolveCommand(DataSet ds) {
|
---|
| 50 | super(ds);
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | /**
|
---|
[2512] | 54 | * remembers a conflict in the internal list of remembered conflicts
|
---|
| 55 | *
|
---|
| 56 | * @param c the remembered conflict
|
---|
| 57 | */
|
---|
| 58 | protected void rememberConflict(Conflict<?> c) {
|
---|
[8444] | 59 | if (!resolvedConflicts.hasConflictForMy(c.getMy())) {
|
---|
[2512] | 60 | resolvedConflicts.add(c);
|
---|
| 61 | }
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | /**
|
---|
| 65 | * reconstitutes all remembered conflicts. Add the remembered conflicts to the
|
---|
[12718] | 66 | * set of conflicts of the {@link DataSet} this command was applied to.
|
---|
[2512] | 67 | *
|
---|
| 68 | */
|
---|
| 69 | protected void reconstituteConflicts() {
|
---|
[12672] | 70 | DataSet ds = getAffectedDataSet();
|
---|
[8510] | 71 | for (Conflict<?> c : resolvedConflicts) {
|
---|
[12672] | 72 | if (!ds.getConflicts().hasConflictForMy(c.getMy())) {
|
---|
| 73 | ds.getConflicts().add(c);
|
---|
[2512] | 74 | }
|
---|
| 75 | }
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | @Override
|
---|
| 79 | public void undoCommand() {
|
---|
| 80 | super.undoCommand();
|
---|
| 81 |
|
---|
[12718] | 82 | DataSet ds = getAffectedDataSet();
|
---|
| 83 | if (!Main.main.containsDataSet(ds)) {
|
---|
| 84 | Logging.warn(tr("Cannot undo command ''{0}'' because layer ''{1}'' is not present any more",
|
---|
| 85 | this.toString(),
|
---|
| 86 | ds.getName()
|
---|
| 87 | ));
|
---|
| 88 | return;
|
---|
| 89 | }
|
---|
[8914] | 90 |
|
---|
[12718] | 91 | Main.main.setEditDataSet(ds);
|
---|
[2512] | 92 | reconstituteConflicts();
|
---|
| 93 | }
|
---|
[8456] | 94 |
|
---|
| 95 | @Override
|
---|
| 96 | public int hashCode() {
|
---|
[9371] | 97 | return Objects.hash(super.hashCode(), resolvedConflicts);
|
---|
[8456] | 98 | }
|
---|
| 99 |
|
---|
| 100 | @Override
|
---|
| 101 | public boolean equals(Object obj) {
|
---|
[9371] | 102 | if (this == obj) return true;
|
---|
| 103 | if (obj == null || getClass() != obj.getClass()) return false;
|
---|
| 104 | if (!super.equals(obj)) return false;
|
---|
| 105 | ConflictResolveCommand that = (ConflictResolveCommand) obj;
|
---|
| 106 | return Objects.equals(resolvedConflicts, that.resolvedConflicts);
|
---|
[8456] | 107 | }
|
---|
[2512] | 108 | }
|
---|