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.Collection;
|
---|
7 | import java.util.List;
|
---|
8 | import java.util.Objects;
|
---|
9 |
|
---|
10 | import javax.swing.Icon;
|
---|
11 |
|
---|
12 | import org.openstreetmap.josm.Main;
|
---|
13 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
14 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
15 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
16 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
17 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * Represents the resolution of conflicts in the member list of two {@link Relation}s.
|
---|
21 | *
|
---|
22 | */
|
---|
23 | public class RelationMemberConflictResolverCommand extends ConflictResolveCommand {
|
---|
24 | /** my relation */
|
---|
25 | private final Relation my;
|
---|
26 | /** their relation */
|
---|
27 | private final Relation their;
|
---|
28 | /** the list of merged nodes. This becomes the list of news of my way after the
|
---|
29 | * command is executed
|
---|
30 | */
|
---|
31 | private final List<RelationMember> mergedMembers;
|
---|
32 |
|
---|
33 | /**
|
---|
34 | *
|
---|
35 | * @param my my relation
|
---|
36 | * @param their their relation
|
---|
37 | * @param mergedMembers the list of merged relation members
|
---|
38 | */
|
---|
39 | public RelationMemberConflictResolverCommand(Relation my, Relation their, List<RelationMember> mergedMembers) {
|
---|
40 | this.my = my;
|
---|
41 | this.their = their;
|
---|
42 | this.mergedMembers = mergedMembers;
|
---|
43 | }
|
---|
44 |
|
---|
45 | @Override
|
---|
46 | public String getDescriptionText() {
|
---|
47 | return tr("Resolve conflicts in member list of relation {0}", my.getId());
|
---|
48 | }
|
---|
49 |
|
---|
50 | @Override
|
---|
51 | public Icon getDescriptionIcon() {
|
---|
52 | return ImageProvider.get("data", "object");
|
---|
53 | }
|
---|
54 |
|
---|
55 | @Override
|
---|
56 | public boolean executeCommand() {
|
---|
57 | // remember the current state of 'my' way
|
---|
58 | //
|
---|
59 | super.executeCommand();
|
---|
60 |
|
---|
61 | // replace the list of members of 'my' relation by the list of merged members
|
---|
62 | //
|
---|
63 | my.setMembers(mergedMembers);
|
---|
64 |
|
---|
65 | return true;
|
---|
66 | }
|
---|
67 |
|
---|
68 | @Override
|
---|
69 | public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted,
|
---|
70 | Collection<OsmPrimitive> added) {
|
---|
71 | modified.add(my);
|
---|
72 | }
|
---|
73 |
|
---|
74 | @Override
|
---|
75 | public void undoCommand() {
|
---|
76 | OsmDataLayer layer = getLayer();
|
---|
77 | if (!Main.map.mapView.hasLayer(layer)) {
|
---|
78 | Main.warn(tr("Cannot undo command ''{0}'' because layer ''{1}'' is not present any more",
|
---|
79 | this.toString(),
|
---|
80 | layer.toString()
|
---|
81 | ));
|
---|
82 | return;
|
---|
83 | }
|
---|
84 |
|
---|
85 | Main.map.mapView.setActiveLayer(layer);
|
---|
86 | OsmDataLayer editLayer = Main.main.getEditLayer();
|
---|
87 |
|
---|
88 | // restore the former state
|
---|
89 | //
|
---|
90 | super.undoCommand();
|
---|
91 |
|
---|
92 | // restore a conflict if necessary
|
---|
93 | //
|
---|
94 | if (!editLayer.getConflicts().hasConflictForMy(my)) {
|
---|
95 | editLayer.getConflicts().add(my, their);
|
---|
96 | }
|
---|
97 | }
|
---|
98 |
|
---|
99 | @Override
|
---|
100 | public int hashCode() {
|
---|
101 | return Objects.hash(super.hashCode(), my, their, mergedMembers);
|
---|
102 | }
|
---|
103 |
|
---|
104 | @Override
|
---|
105 | public boolean equals(Object obj) {
|
---|
106 | if (this == obj) return true;
|
---|
107 | if (obj == null || getClass() != obj.getClass()) return false;
|
---|
108 | if (!super.equals(obj)) return false;
|
---|
109 | RelationMemberConflictResolverCommand that = (RelationMemberConflictResolverCommand) obj;
|
---|
110 | return Objects.equals(my, that.my) &&
|
---|
111 | Objects.equals(their, that.their) &&
|
---|
112 | Objects.equals(mergedMembers, that.mergedMembers);
|
---|
113 | }
|
---|
114 | }
|
---|