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 | import java.util.List;
|
---|
8 |
|
---|
9 | import javax.swing.JLabel;
|
---|
10 | import javax.swing.tree.DefaultMutableTreeNode;
|
---|
11 | import javax.swing.tree.MutableTreeNode;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.Main;
|
---|
14 | import org.openstreetmap.josm.data.osm.Node;
|
---|
15 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
16 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
17 | import org.openstreetmap.josm.data.osm.Way;
|
---|
18 | import org.openstreetmap.josm.gui.conflict.MergeDecisionType;
|
---|
19 | import org.openstreetmap.josm.gui.conflict.tags.TagMergeItem;
|
---|
20 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Represents a the resolution of a tag conflict in an {@see OsmPrimitive}
|
---|
24 | *
|
---|
25 | */
|
---|
26 | public class TagConflictResolveCommand extends Command {
|
---|
27 |
|
---|
28 | /** my primitive (in the local dataset). merge decisions are applied to this
|
---|
29 | * primitive
|
---|
30 | */
|
---|
31 | private final OsmPrimitive my;
|
---|
32 | /** their primitive (in the server dataset) */
|
---|
33 | private final OsmPrimitive their;
|
---|
34 |
|
---|
35 | /** the list of merge decisions, represented as {@see TagMergeItem}s */
|
---|
36 | private final List<TagMergeItem> mergeItems;
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * replies the number of decided conflicts
|
---|
40 | *
|
---|
41 | * @return the number of decided conflicts
|
---|
42 | */
|
---|
43 | public int getNumDecidedConflicts() {
|
---|
44 | int n = 0;
|
---|
45 | for (TagMergeItem item: mergeItems) {
|
---|
46 | if (!item.getMergeDecision().equals(MergeDecisionType.UNDECIDED)) {
|
---|
47 | n++;
|
---|
48 | }
|
---|
49 | }
|
---|
50 | return n;
|
---|
51 | }
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * replies a (localized) display name for the type of an OSM primitive
|
---|
55 | *
|
---|
56 | * @param primitive the primitive
|
---|
57 | * @return a localized display name
|
---|
58 | */
|
---|
59 | protected String getPrimitiveTypeAsString(OsmPrimitive primitive) {
|
---|
60 | if (primitive instanceof Node) return tr("node");
|
---|
61 | if (primitive instanceof Way) return tr("way");
|
---|
62 | if (primitive instanceof Relation) return tr("relation");
|
---|
63 | return "";
|
---|
64 | }
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * constructor
|
---|
68 | *
|
---|
69 | * @param my my primitive
|
---|
70 | * @param their their primitive
|
---|
71 | * @param mergeItems the list of merge decisions, represented as {@see TagMergeItem}s
|
---|
72 | */
|
---|
73 | public TagConflictResolveCommand(OsmPrimitive my, OsmPrimitive their, List<TagMergeItem> mergeItems) {
|
---|
74 | this.my = my;
|
---|
75 | this.their = their;
|
---|
76 | this.mergeItems = mergeItems;
|
---|
77 | }
|
---|
78 |
|
---|
79 |
|
---|
80 | @Override
|
---|
81 | public MutableTreeNode description() {
|
---|
82 | return new DefaultMutableTreeNode(
|
---|
83 | new JLabel(
|
---|
84 | tr("Resolve {0} tag conflicts in {1} {2}",getNumDecidedConflicts(), getPrimitiveTypeAsString(my), my.id),
|
---|
85 | ImageProvider.get("data", "object"),
|
---|
86 | JLabel.HORIZONTAL
|
---|
87 | )
|
---|
88 | );
|
---|
89 | }
|
---|
90 |
|
---|
91 | @Override
|
---|
92 | public boolean executeCommand() {
|
---|
93 | // remember the current state of modified primitives, i.e. of
|
---|
94 | // OSM primitive 'my'
|
---|
95 | //
|
---|
96 | super.executeCommand();
|
---|
97 |
|
---|
98 | // apply the merge decisions to OSM primitive 'my'
|
---|
99 | //
|
---|
100 | for (TagMergeItem item: mergeItems) {
|
---|
101 | if (! item.getMergeDecision().equals(MergeDecisionType.UNDECIDED)) {
|
---|
102 | item.applyToMyPrimitive(my);
|
---|
103 | }
|
---|
104 | }
|
---|
105 | return true;
|
---|
106 | }
|
---|
107 |
|
---|
108 | @Override
|
---|
109 | public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted,
|
---|
110 | Collection<OsmPrimitive> added) {
|
---|
111 | modified.add(my);
|
---|
112 | }
|
---|
113 |
|
---|
114 | @Override
|
---|
115 | public void undoCommand() {
|
---|
116 | // restore former state of modified primitives
|
---|
117 | //
|
---|
118 | super.undoCommand();
|
---|
119 |
|
---|
120 | // restore a conflict if necessary
|
---|
121 | //
|
---|
122 | if (!Main.map.conflictDialog.conflicts.containsKey(my)) {
|
---|
123 | Main.map.conflictDialog.addConflict(my, their);
|
---|
124 | }
|
---|
125 | }
|
---|
126 | }
|
---|