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