1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.command;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.util.Collection;
|
---|
7 |
|
---|
8 | import javax.swing.JLabel;
|
---|
9 | import javax.swing.tree.DefaultMutableTreeNode;
|
---|
10 | import javax.swing.tree.MutableTreeNode;
|
---|
11 |
|
---|
12 | import org.openstreetmap.josm.Main;
|
---|
13 | import org.openstreetmap.josm.data.osm.Node;
|
---|
14 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
15 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
16 | import org.openstreetmap.josm.data.osm.Way;
|
---|
17 | import org.openstreetmap.josm.gui.conflict.MergeDecisionType;
|
---|
18 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * Represents a the resolution of a conflict between the coordinates of two {@see Node}s
|
---|
22 | *
|
---|
23 | */
|
---|
24 | public class CoordinateConflictResolveCommand extends Command {
|
---|
25 |
|
---|
26 | /** my node (in the local dataset). merge decisions are applied to this
|
---|
27 | * node
|
---|
28 | */
|
---|
29 | private final Node my;
|
---|
30 | /** their node (in the server dataset) */
|
---|
31 | private final Node their;
|
---|
32 |
|
---|
33 | /** the merge decision */
|
---|
34 | private final MergeDecisionType decision;
|
---|
35 |
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * replies a (localized) display name for the type of an OSM primitive
|
---|
39 | *
|
---|
40 | * @param primitive the primitive
|
---|
41 | * @return a localized display name
|
---|
42 | */
|
---|
43 | protected String getPrimitiveTypeAsString(OsmPrimitive primitive) {
|
---|
44 | if (primitive instanceof Node) return tr("node");
|
---|
45 | if (primitive instanceof Way) return tr("way");
|
---|
46 | if (primitive instanceof Relation) return tr("relation");
|
---|
47 | return "";
|
---|
48 | }
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * constructor
|
---|
52 | *
|
---|
53 | * @param my my node
|
---|
54 | * @param their their node
|
---|
55 | * @param decision the merge decision
|
---|
56 | */
|
---|
57 | public CoordinateConflictResolveCommand(Node my, Node their, MergeDecisionType decision) {
|
---|
58 | this.my = my;
|
---|
59 | this.their = their;
|
---|
60 | this.decision = decision;
|
---|
61 | }
|
---|
62 |
|
---|
63 |
|
---|
64 | @Override
|
---|
65 | public MutableTreeNode description() {
|
---|
66 | return new DefaultMutableTreeNode(
|
---|
67 | new JLabel(
|
---|
68 | tr("Resolve conflicts in coordinates in {0}",my.id),
|
---|
69 | ImageProvider.get("data", "object"),
|
---|
70 | JLabel.HORIZONTAL
|
---|
71 | )
|
---|
72 | );
|
---|
73 | }
|
---|
74 |
|
---|
75 | @Override
|
---|
76 | public boolean executeCommand() {
|
---|
77 | // remember the current state of modified primitives, i.e. of
|
---|
78 | // OSM primitive 'my'
|
---|
79 | //
|
---|
80 | super.executeCommand();
|
---|
81 |
|
---|
82 | if (decision.equals(MergeDecisionType.KEEP_MINE)) {
|
---|
83 | // do nothing
|
---|
84 | } else if (decision.equals(MergeDecisionType.KEEP_THEIR)) {
|
---|
85 | my.setCoor(their.getCoor());
|
---|
86 | } else
|
---|
87 | // should not happen
|
---|
88 | throw new IllegalStateException(tr("cannot resolve undecided conflict"));
|
---|
89 |
|
---|
90 | return true;
|
---|
91 | }
|
---|
92 |
|
---|
93 | @Override
|
---|
94 | public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted,
|
---|
95 | Collection<OsmPrimitive> added) {
|
---|
96 | modified.add(my);
|
---|
97 | }
|
---|
98 |
|
---|
99 | @Override
|
---|
100 | public void undoCommand() {
|
---|
101 | // restore former state of modified primitives
|
---|
102 | //
|
---|
103 | super.undoCommand();
|
---|
104 |
|
---|
105 | // restore a conflict if necessary
|
---|
106 | //
|
---|
107 | if (!Main.map.conflictDialog.conflicts.containsKey(my)) {
|
---|
108 | Main.map.conflictDialog.addConflict(my, their);
|
---|
109 | }
|
---|
110 | }
|
---|
111 | }
|
---|