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.gui.MainApplication;
|
---|
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;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Constructs a new {@code ConflictResolveCommand} in the context of the current edit layer, if any.
|
---|
30 | */
|
---|
31 | public ConflictResolveCommand() {
|
---|
32 | super();
|
---|
33 | resolvedConflicts = new ConflictCollection();
|
---|
34 | }
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Constructs a new {@code ConflictResolveCommand} in the context of a given data layer.
|
---|
38 | * @param layer the data layer. Must not be null.
|
---|
39 | */
|
---|
40 | public ConflictResolveCommand(OsmDataLayer layer) {
|
---|
41 | super(layer);
|
---|
42 | resolvedConflicts = new ConflictCollection();
|
---|
43 | }
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * remembers a conflict in the internal list of remembered conflicts
|
---|
47 | *
|
---|
48 | * @param c the remembered conflict
|
---|
49 | */
|
---|
50 | protected void rememberConflict(Conflict<?> c) {
|
---|
51 | if (!resolvedConflicts.hasConflictForMy(c.getMy())) {
|
---|
52 | resolvedConflicts.add(c);
|
---|
53 | }
|
---|
54 | }
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * reconstitutes all remembered conflicts. Add the remembered conflicts to the
|
---|
58 | * set of conflicts of the {@link OsmDataLayer} this command was applied to.
|
---|
59 | *
|
---|
60 | */
|
---|
61 | protected void reconstituteConflicts() {
|
---|
62 | OsmDataLayer editLayer = getLayer();
|
---|
63 | for (Conflict<?> c : resolvedConflicts) {
|
---|
64 | if (!editLayer.getConflicts().hasConflictForMy(c.getMy())) {
|
---|
65 | editLayer.getConflicts().add(c);
|
---|
66 | }
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | @Override
|
---|
71 | public void undoCommand() {
|
---|
72 | super.undoCommand();
|
---|
73 |
|
---|
74 | if (MainApplication.isDisplayingMapView()) {
|
---|
75 | if (!Main.getLayerManager().containsLayer(getLayer())) {
|
---|
76 | Logging.warn(tr("Cannot undo command ''{0}'' because layer ''{1}'' is not present any more",
|
---|
77 | this.toString(),
|
---|
78 | getLayer().toString()
|
---|
79 | ));
|
---|
80 | return;
|
---|
81 | }
|
---|
82 |
|
---|
83 | Main.getLayerManager().setActiveLayer(getLayer());
|
---|
84 | }
|
---|
85 | reconstituteConflicts();
|
---|
86 | }
|
---|
87 |
|
---|
88 | @Override
|
---|
89 | public int hashCode() {
|
---|
90 | return Objects.hash(super.hashCode(), resolvedConflicts);
|
---|
91 | }
|
---|
92 |
|
---|
93 | @Override
|
---|
94 | public boolean equals(Object obj) {
|
---|
95 | if (this == obj) return true;
|
---|
96 | if (obj == null || getClass() != obj.getClass()) return false;
|
---|
97 | if (!super.equals(obj)) return false;
|
---|
98 | ConflictResolveCommand that = (ConflictResolveCommand) obj;
|
---|
99 | return Objects.equals(resolvedConflicts, that.resolvedConflicts);
|
---|
100 | }
|
---|
101 | }
|
---|