| 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.ArrayList;
|
|---|
| 7 | import java.util.Collection;
|
|---|
| 8 | import java.util.HashMap;
|
|---|
| 9 | import java.util.Map;
|
|---|
| 10 |
|
|---|
| 11 | import javax.swing.JLabel;
|
|---|
| 12 | import javax.swing.tree.DefaultMutableTreeNode;
|
|---|
| 13 | import javax.swing.tree.MutableTreeNode;
|
|---|
| 14 |
|
|---|
| 15 | import org.openstreetmap.josm.Main;
|
|---|
| 16 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
|---|
| 17 | import org.openstreetmap.josm.tools.ImageProvider;
|
|---|
| 18 |
|
|---|
| 19 | /**
|
|---|
| 20 | * Represents a command for undeleting a node which was deleted on the server.
|
|---|
| 21 | * The command remembers the former node id and sets the node id to 0. This turns
|
|---|
| 22 | * the node into a new node which can be uploaded to the server.
|
|---|
| 23 | *
|
|---|
| 24 | */
|
|---|
| 25 | public class UndeletePrimitivesCommand extends Command {
|
|---|
| 26 |
|
|---|
| 27 | /** the node to undelete */
|
|---|
| 28 | private ArrayList<OsmPrimitive> toUndelete;
|
|---|
| 29 | private Map<OsmPrimitive,OsmPrimitive> resolvedConflicts;
|
|---|
| 30 |
|
|---|
| 31 | protected UndeletePrimitivesCommand() {
|
|---|
| 32 | toUndelete = new ArrayList<OsmPrimitive>();
|
|---|
| 33 | resolvedConflicts = new HashMap<OsmPrimitive, OsmPrimitive>();
|
|---|
| 34 | }
|
|---|
| 35 | /**
|
|---|
| 36 | * constructor
|
|---|
| 37 | * @param node the node to undelete
|
|---|
| 38 | */
|
|---|
| 39 | public UndeletePrimitivesCommand(OsmPrimitive node) {
|
|---|
| 40 | this();
|
|---|
| 41 | toUndelete.add(node);
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | /**
|
|---|
| 45 | * constructor
|
|---|
| 46 | * @param node the node to undelete
|
|---|
| 47 | */
|
|---|
| 48 | public UndeletePrimitivesCommand(OsmPrimitive ... toUndelete) {
|
|---|
| 49 | this();
|
|---|
| 50 | for (int i=0; i < toUndelete.length; i++) {
|
|---|
| 51 | this.toUndelete.add(toUndelete[i]);
|
|---|
| 52 | }
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | /**
|
|---|
| 56 | * constructor
|
|---|
| 57 | * @param node the node to undelete
|
|---|
| 58 | */
|
|---|
| 59 | public UndeletePrimitivesCommand(Collection<OsmPrimitive> toUndelete) {
|
|---|
| 60 | this();
|
|---|
| 61 | this.toUndelete.addAll(toUndelete);
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 | @Override
|
|---|
| 66 | public MutableTreeNode description() {
|
|---|
| 67 | return new DefaultMutableTreeNode(
|
|---|
| 68 | new JLabel(
|
|---|
| 69 | tr("Undelete {0} primitives", toUndelete.size()),
|
|---|
| 70 | ImageProvider.get("data", "object"),
|
|---|
| 71 | JLabel.HORIZONTAL
|
|---|
| 72 | )
|
|---|
| 73 | );
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | @Override
|
|---|
| 77 | public boolean executeCommand() {
|
|---|
| 78 | super.executeCommand();
|
|---|
| 79 | for(OsmPrimitive primitive: toUndelete) {
|
|---|
| 80 | if (Main.map.conflictDialog.conflicts.containsKey(primitive)) {
|
|---|
| 81 | resolvedConflicts.put(primitive, Main.map.conflictDialog.conflicts.get(primitive));
|
|---|
| 82 | Main.map.conflictDialog.removeConflictForPrimitive(primitive);
|
|---|
| 83 | }
|
|---|
| 84 | primitive.id = 0;
|
|---|
| 85 | }
|
|---|
| 86 | return true;
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | @Override
|
|---|
| 90 | public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted,
|
|---|
| 91 | Collection<OsmPrimitive> added) {
|
|---|
| 92 | modified.addAll(toUndelete);
|
|---|
| 93 | }
|
|---|
| 94 | @Override
|
|---|
| 95 | public void undoCommand() {
|
|---|
| 96 | super.undoCommand();
|
|---|
| 97 |
|
|---|
| 98 | for (OsmPrimitive my: resolvedConflicts.keySet()) {
|
|---|
| 99 | if (!Main.map.conflictDialog.conflicts.containsKey(my)) {
|
|---|
| 100 | Main.map.conflictDialog.addConflict(my, resolvedConflicts.get(my));
|
|---|
| 101 | }
|
|---|
| 102 | }
|
|---|
| 103 | }
|
|---|
| 104 | }
|
|---|