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