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