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 org.openstreetmap.josm.Main;
|
---|
7 | import org.openstreetmap.josm.command.Command;
|
---|
8 | import org.openstreetmap.josm.data.conflict.Conflict;
|
---|
9 | import org.openstreetmap.josm.data.conflict.ConflictCollection;
|
---|
10 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
11 |
|
---|
12 | /**
|
---|
13 | * This is the common base class for {@link Command}s which manipulate {@link Conflict}s in
|
---|
14 | * addition to {@link org.openstreetmap.josm.data.osm.OsmPrimitive}s.
|
---|
15 | *
|
---|
16 | * A ConflictResolverCommand can remember a collection of conflicts it resolves. Upon undoing
|
---|
17 | * it reconstitutes them.
|
---|
18 | *
|
---|
19 | */
|
---|
20 | public abstract class ConflictResolveCommand extends Command {
|
---|
21 | /** the list of resolved conflicts */
|
---|
22 | private ConflictCollection resolvedConflicts;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * Constructs a new {@code ConflictResolveCommand} in the context of the current edit layer, if any.
|
---|
26 | */
|
---|
27 | public ConflictResolveCommand() {
|
---|
28 | super();
|
---|
29 | resolvedConflicts = new ConflictCollection();
|
---|
30 | }
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Constructs a new {@code ConflictResolveCommand} in the context of a given data layer.
|
---|
34 | * @param layer the data layer. Must not be null.
|
---|
35 | */
|
---|
36 | public ConflictResolveCommand(OsmDataLayer layer) {
|
---|
37 | super(layer);
|
---|
38 | resolvedConflicts = new ConflictCollection();
|
---|
39 | }
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * remembers a conflict in the internal list of remembered conflicts
|
---|
43 | *
|
---|
44 | * @param c the remembered conflict
|
---|
45 | */
|
---|
46 | protected void rememberConflict(Conflict<?> c) {
|
---|
47 | if (!resolvedConflicts.hasConflictForMy(c.getMy())) {
|
---|
48 | resolvedConflicts.add(c);
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * reconstitutes all remembered conflicts. Add the remembered conflicts to the
|
---|
54 | * set of conflicts of the {@link OsmDataLayer} this command was applied to.
|
---|
55 | *
|
---|
56 | */
|
---|
57 | protected void reconstituteConflicts() {
|
---|
58 | OsmDataLayer editLayer = getLayer();
|
---|
59 | for (Conflict<?> c : resolvedConflicts) {
|
---|
60 | if (!editLayer.getConflicts().hasConflictForMy(c.getMy())) {
|
---|
61 | editLayer.getConflicts().add(c);
|
---|
62 | }
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | @Override
|
---|
67 | public void undoCommand() {
|
---|
68 | super.undoCommand();
|
---|
69 |
|
---|
70 | if (!Main.map.mapView.hasLayer(getLayer())) {
|
---|
71 | Main.warn(tr("Cannot undo command ''{0}'' because layer ''{1}'' is not present any more",
|
---|
72 | this.toString(),
|
---|
73 | getLayer().toString()
|
---|
74 | ));
|
---|
75 | return;
|
---|
76 | }
|
---|
77 |
|
---|
78 | Main.map.mapView.setActiveLayer(getLayer());
|
---|
79 | reconstituteConflicts();
|
---|
80 | }
|
---|
81 |
|
---|
82 | @Override
|
---|
83 | public int hashCode() {
|
---|
84 | final int prime = 31;
|
---|
85 | int result = super.hashCode();
|
---|
86 | result = prime * result + ((resolvedConflicts == null) ? 0 : resolvedConflicts.hashCode());
|
---|
87 | return result;
|
---|
88 | }
|
---|
89 |
|
---|
90 | @Override
|
---|
91 | public boolean equals(Object obj) {
|
---|
92 | if (this == obj)
|
---|
93 | return true;
|
---|
94 | if (!super.equals(obj))
|
---|
95 | return false;
|
---|
96 | if (getClass() != obj.getClass())
|
---|
97 | return false;
|
---|
98 | ConflictResolveCommand other = (ConflictResolveCommand) obj;
|
---|
99 | if (resolvedConflicts == null) {
|
---|
100 | if (other.resolvedConflicts != null)
|
---|
101 | return false;
|
---|
102 | } else if (!resolvedConflicts.equals(other.resolvedConflicts))
|
---|
103 | return false;
|
---|
104 | return true;
|
---|
105 | }
|
---|
106 | }
|
---|