1 | package org.openstreetmap.josm.command;
|
---|
2 |
|
---|
3 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
4 |
|
---|
5 | import java.util.Collection;
|
---|
6 | import java.util.HashMap;
|
---|
7 | import java.util.HashSet;
|
---|
8 | import java.util.List;
|
---|
9 | import java.util.Map;
|
---|
10 | import java.util.Set;
|
---|
11 | import java.util.Map.Entry;
|
---|
12 |
|
---|
13 | import javax.swing.JLabel;
|
---|
14 | import javax.swing.tree.DefaultMutableTreeNode;
|
---|
15 | import javax.swing.tree.MutableTreeNode;
|
---|
16 |
|
---|
17 | import org.openstreetmap.josm.Main;
|
---|
18 | import org.openstreetmap.josm.data.conflict.ConflictItem;
|
---|
19 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
20 | import org.openstreetmap.josm.gui.ConflictResolver;
|
---|
21 | import org.openstreetmap.josm.gui.dialogs.ConflictDialog;
|
---|
22 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
23 |
|
---|
24 | public class ConflictResolveCommand extends Command {
|
---|
25 |
|
---|
26 | private final Collection<ConflictItem> conflicts;
|
---|
27 | private final Map<OsmPrimitive, OsmPrimitive> resolved;
|
---|
28 | private Map<OsmPrimitive, OsmPrimitive> origAllConflicts;
|
---|
29 | private final ConflictDialog conflictDialog;
|
---|
30 |
|
---|
31 | public ConflictResolveCommand(List<ConflictItem> conflicts, Map<OsmPrimitive, OsmPrimitive> resolved) {
|
---|
32 | this.conflicts = conflicts;
|
---|
33 | this.resolved = resolved;
|
---|
34 | conflictDialog = Main.map.conflictDialog;
|
---|
35 | }
|
---|
36 |
|
---|
37 | @Override public void executeCommand() {
|
---|
38 | super.executeCommand();
|
---|
39 |
|
---|
40 | origAllConflicts = new HashMap<OsmPrimitive, OsmPrimitive>(conflictDialog.conflicts);
|
---|
41 |
|
---|
42 | Set<OsmPrimitive> completed = new HashSet<OsmPrimitive>(resolved.keySet());
|
---|
43 | for (ConflictItem ci : conflicts) {
|
---|
44 | for (Entry<OsmPrimitive, OsmPrimitive> e : resolved.entrySet()) {
|
---|
45 | if (ci.resolution == ConflictResolver.Resolution.THEIR)
|
---|
46 | ci.apply(e.getKey(), e.getValue());
|
---|
47 | else if (ci.resolution == ConflictResolver.Resolution.MY)
|
---|
48 | ci.apply(e.getValue(), e.getKey());
|
---|
49 | else if (ci.hasConflict(e.getKey(), e.getValue()))
|
---|
50 | completed.remove(e.getKey());
|
---|
51 | }
|
---|
52 | }
|
---|
53 | if (!completed.isEmpty()) {
|
---|
54 | for (OsmPrimitive k : completed)
|
---|
55 | conflictDialog.conflicts.remove(k);
|
---|
56 | conflictDialog.rebuildList();
|
---|
57 | Main.map.mapView.recalculateCenterScale(); // in case of auto-zoom
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | @Override public void undoCommand() {
|
---|
62 | super.undoCommand();
|
---|
63 | Main.map.conflictDialog.conflicts.clear();
|
---|
64 | Main.map.conflictDialog.conflicts.putAll(origAllConflicts);
|
---|
65 | Main.map.conflictDialog.rebuildList();
|
---|
66 | }
|
---|
67 |
|
---|
68 | @Override public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
|
---|
69 | modified.addAll(resolved.keySet());
|
---|
70 | }
|
---|
71 |
|
---|
72 | @Override public MutableTreeNode description() {
|
---|
73 | int i = 0;
|
---|
74 | for (ConflictItem c : conflicts)
|
---|
75 | if (c.resolution != null)
|
---|
76 | i++;
|
---|
77 | return new DefaultMutableTreeNode(new JLabel(tr("Resolve {0} conflicts in {1} objects",i,resolved.size()), ImageProvider.get("data", "primitive"), JLabel.HORIZONTAL));
|
---|
78 | }
|
---|
79 | }
|
---|