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.gui.layer.OsmDataLayer;
|
---|
14 | import org.openstreetmap.josm.tools.Logging;
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * This is the common base class for {@link Command}s which manipulate {@link Conflict}s in
|
---|
18 | * addition to {@link org.openstreetmap.josm.data.osm.OsmPrimitive}s.
|
---|
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 */
|
---|
26 | private final ConflictCollection resolvedConflicts = new ConflictCollection();
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Constructs a new {@code ConflictResolveCommand} in the context of the current edit layer, if any.
|
---|
30 | */
|
---|
31 | public ConflictResolveCommand() {
|
---|
32 | // Do nothing
|
---|
33 | }
|
---|
34 |
|
---|
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.
|
---|
38 | * @deprecated to be removed end of 2017. Use {@link #ConflictResolveCommand(DataSet)} instead
|
---|
39 | */
|
---|
40 | @Deprecated
|
---|
41 | public ConflictResolveCommand(OsmDataLayer layer) {
|
---|
42 | super(layer);
|
---|
43 | }
|
---|
44 |
|
---|
45 | /**
|
---|
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 | /**
|
---|
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) {
|
---|
59 | if (!resolvedConflicts.hasConflictForMy(c.getMy())) {
|
---|
60 | resolvedConflicts.add(c);
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * reconstitutes all remembered conflicts. Add the remembered conflicts to the
|
---|
66 | * set of conflicts of the {@link DataSet} this command was applied to.
|
---|
67 | *
|
---|
68 | */
|
---|
69 | protected void reconstituteConflicts() {
|
---|
70 | DataSet ds = getAffectedDataSet();
|
---|
71 | for (Conflict<?> c : resolvedConflicts) {
|
---|
72 | if (!ds.getConflicts().hasConflictForMy(c.getMy())) {
|
---|
73 | ds.getConflicts().add(c);
|
---|
74 | }
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | @Override
|
---|
79 | public void undoCommand() {
|
---|
80 | super.undoCommand();
|
---|
81 |
|
---|
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 | }
|
---|
90 |
|
---|
91 | Main.main.setEditDataSet(ds);
|
---|
92 | reconstituteConflicts();
|
---|
93 | }
|
---|
94 |
|
---|
95 | @Override
|
---|
96 | public int hashCode() {
|
---|
97 | return Objects.hash(super.hashCode(), resolvedConflicts);
|
---|
98 | }
|
---|
99 |
|
---|
100 | @Override
|
---|
101 | public boolean equals(Object obj) {
|
---|
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);
|
---|
107 | }
|
---|
108 | }
|
---|