[2512] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.command;
|
---|
| 3 |
|
---|
[2844] | 4 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
[2512] | 5 |
|
---|
| 6 | import java.util.ArrayList;
|
---|
| 7 | import java.util.Collection;
|
---|
[2972] | 8 | import java.util.Collections;
|
---|
[2512] | 9 | import java.util.List;
|
---|
[2972] | 10 | import java.util.ListIterator;
|
---|
[2512] | 11 |
|
---|
| 12 | import javax.swing.JLabel;
|
---|
| 13 |
|
---|
[2972] | 14 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
| 15 | import org.openstreetmap.josm.data.osm.Node;
|
---|
[2512] | 16 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
[2972] | 17 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
| 18 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
| 19 | import org.openstreetmap.josm.data.osm.Way;
|
---|
[2512] | 20 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
| 21 |
|
---|
| 22 | /**
|
---|
| 23 | * Represents a command for undeleting an {@see OsmPrimitive} which was deleted on the server.
|
---|
| 24 | * The command remembers the former node id and sets the node id to 0. This turns
|
---|
| 25 | * the node into a new node which can be uploaded to the server.
|
---|
| 26 | *
|
---|
| 27 | */
|
---|
[3362] | 28 | @Deprecated
|
---|
[2512] | 29 | public class UndeletePrimitivesCommand extends ConflictResolveCommand {
|
---|
[2972] | 30 | //static private final Logger logger = Logger.getLogger(UndeletePrimitivesCommand.class.getName());
|
---|
[2512] | 31 |
|
---|
| 32 | /** the node to undelete */
|
---|
| 33 | private final List<OsmPrimitive> toUndelete = new ArrayList<OsmPrimitive>();
|
---|
[2972] | 34 | /** primitives that replaced undeleted primitives */
|
---|
| 35 | private final List<OsmPrimitive> replacedPrimitives = new ArrayList<OsmPrimitive>();
|
---|
[2512] | 36 |
|
---|
| 37 | /**
|
---|
| 38 | * constructor
|
---|
| 39 | * @param node the node to undelete
|
---|
| 40 | */
|
---|
| 41 | public UndeletePrimitivesCommand(OsmPrimitive node) {
|
---|
| 42 | toUndelete.add(node);
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | /**
|
---|
| 46 | * constructor
|
---|
| 47 | * @param node the node to undelete
|
---|
| 48 | */
|
---|
| 49 | public UndeletePrimitivesCommand(OsmPrimitive ... toUndelete) {
|
---|
| 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.toUndelete.addAll(toUndelete);
|
---|
| 61 | }
|
---|
| 62 |
|
---|
[3262] | 63 | @Override public JLabel getDescription() {
|
---|
| 64 | return new JLabel(
|
---|
[3995] | 65 | trn("Undelete {0} object", "Undelete {0} objects", toUndelete.size(), toUndelete.size()),
|
---|
| 66 | ImageProvider.get("data", "object"),
|
---|
| 67 | JLabel.HORIZONTAL
|
---|
[2512] | 68 | );
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | @Override
|
---|
| 72 | public boolean executeCommand() {
|
---|
| 73 | super.executeCommand();
|
---|
| 74 |
|
---|
[2972] | 75 | replacedPrimitives.clear();
|
---|
[2512] | 76 | for(OsmPrimitive primitive: toUndelete) {
|
---|
| 77 | if(getLayer().getConflicts().hasConflictForMy(primitive)) {
|
---|
| 78 | rememberConflict(getLayer().getConflicts().getConflictForMy(primitive));
|
---|
| 79 | getLayer().getConflicts().remove(primitive);
|
---|
| 80 | }
|
---|
[2972] | 81 | OsmPrimitive prim;
|
---|
| 82 | switch (primitive.getType()) {
|
---|
| 83 | case NODE:
|
---|
| 84 | prim = new Node((Node)primitive, true);
|
---|
| 85 | break;
|
---|
| 86 | case WAY:
|
---|
| 87 | prim = new Way((Way)primitive, true);
|
---|
| 88 | break;
|
---|
| 89 | case RELATION:
|
---|
| 90 | prim = new Relation((Relation)primitive, true);
|
---|
| 91 | break;
|
---|
| 92 | default:
|
---|
| 93 | throw new AssertionError();
|
---|
| 94 | }
|
---|
| 95 | replacedPrimitives.add(prim);
|
---|
| 96 | replacePrimitive(getLayer().data, primitive, prim);
|
---|
[2512] | 97 | }
|
---|
| 98 | return true;
|
---|
| 99 | }
|
---|
| 100 |
|
---|
[2972] | 101 | private void replacePrimitive(DataSet dataSet, OsmPrimitive oldPrim, OsmPrimitive newPrim) {
|
---|
| 102 | dataSet.addPrimitive(newPrim);
|
---|
| 103 | for (OsmPrimitive referrer: oldPrim.getReferrers()) {
|
---|
| 104 | if (referrer instanceof Way) {
|
---|
| 105 | Way w = (Way)referrer;
|
---|
| 106 | List<Node> nodes = w.getNodes();
|
---|
| 107 | Collections.replaceAll(nodes, (Node)oldPrim, (Node)newPrim);
|
---|
| 108 | w.setNodes(nodes);
|
---|
| 109 | w.setModified(true);
|
---|
| 110 | } else if (referrer instanceof Relation) {
|
---|
| 111 | Relation r = (Relation)referrer;
|
---|
| 112 | List<RelationMember> members = r.getMembers();
|
---|
| 113 | ListIterator<RelationMember> it = members.listIterator();
|
---|
| 114 | while (it.hasNext()) {
|
---|
| 115 | RelationMember rm = it.next();
|
---|
| 116 | if (rm.getMember() == oldPrim) {
|
---|
| 117 | it.set(new RelationMember(rm.getRole(), newPrim));
|
---|
| 118 | }
|
---|
| 119 | }
|
---|
| 120 | r.setMembers(members);
|
---|
| 121 | r.setModified(true);
|
---|
| 122 | }
|
---|
| 123 | }
|
---|
| 124 | dataSet.removePrimitive(oldPrim);
|
---|
| 125 | }
|
---|
| 126 |
|
---|
[2512] | 127 | @Override
|
---|
[2972] | 128 | public void undoCommand() {
|
---|
| 129 | for (int i=0; i<toUndelete.size(); i++) {
|
---|
| 130 | replacePrimitive(getLayer().data, replacedPrimitives.get(i), toUndelete.get(i));
|
---|
| 131 | }
|
---|
| 132 | super.undoCommand();
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | @Override
|
---|
[2512] | 136 | public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted,
|
---|
| 137 | Collection<OsmPrimitive> added) {
|
---|
| 138 | }
|
---|
| 139 | }
|
---|