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.List;
|
---|
9 | import java.util.logging.Logger;
|
---|
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.conflict.ConflictCollection;
|
---|
17 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
18 | import org.openstreetmap.josm.data.osm.Node;
|
---|
19 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
20 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
21 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
22 | import org.openstreetmap.josm.data.osm.Way;
|
---|
23 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Physically removes an {@see OsmPrimitive} from the dataset of the edit
|
---|
27 | * layer and disconnects any references from {@see Way}s or {@see Relation}s
|
---|
28 | * to this primitive.
|
---|
29 | *
|
---|
30 | * This command is necessary if a local {@see OsmPrimitive} has been deleted on
|
---|
31 | * the server by another user and if the local user decides to delete his version
|
---|
32 | * too. If he only deleted it "logically" JOSM would try to delete it on the server
|
---|
33 | * which would result in an non resolvable conflict.
|
---|
34 | *
|
---|
35 | */
|
---|
36 | public class PurgePrimitivesCommand extends ConflictResolveCommand{
|
---|
37 |
|
---|
38 | static private final Logger logger = Logger.getLogger(PurgePrimitivesCommand.class.getName());
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Represents a pair of {@see OsmPrimitive} where the parent referrs to
|
---|
42 | * the child, either because a {@see Way} includes a {@see Node} or
|
---|
43 | * because a {@see Relation} refers to any other {@see OsmPrimitive}
|
---|
44 | * via a relation member.
|
---|
45 | *
|
---|
46 | */
|
---|
47 | static class OsmParentChildPair {
|
---|
48 | private OsmPrimitive parent;
|
---|
49 | private OsmPrimitive child;
|
---|
50 |
|
---|
51 |
|
---|
52 | public OsmParentChildPair(OsmPrimitive parent, OsmPrimitive child) {
|
---|
53 | this.parent = parent;
|
---|
54 | this.child = child;
|
---|
55 | }
|
---|
56 |
|
---|
57 | public OsmPrimitive getParent() {
|
---|
58 | return parent;
|
---|
59 | }
|
---|
60 |
|
---|
61 | public OsmPrimitive getChild() {
|
---|
62 | return child;
|
---|
63 | }
|
---|
64 |
|
---|
65 | @Override
|
---|
66 | public int hashCode() {
|
---|
67 | final int prime = 31;
|
---|
68 | int result = 1;
|
---|
69 | result = prime * result + ((child == null) ? 0 : child.hashCode());
|
---|
70 | result = prime * result + ((parent == null) ? 0 : parent.hashCode());
|
---|
71 | return result;
|
---|
72 | }
|
---|
73 |
|
---|
74 | @Override
|
---|
75 | public boolean equals(Object obj) {
|
---|
76 | if (this == obj)
|
---|
77 | return true;
|
---|
78 | if (obj == null)
|
---|
79 | return false;
|
---|
80 | if (getClass() != obj.getClass())
|
---|
81 | return false;
|
---|
82 | OsmParentChildPair other = (OsmParentChildPair) obj;
|
---|
83 | if (child == null) {
|
---|
84 | if (other.child != null)
|
---|
85 | return false;
|
---|
86 | } else if (child != other.child)
|
---|
87 | return false;
|
---|
88 | if (parent == null) {
|
---|
89 | if (other.parent != null)
|
---|
90 | return false;
|
---|
91 | } else if (parent != other.parent)
|
---|
92 | return false;
|
---|
93 | return true;
|
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * creates a list of all {@see OsmParentChildPair}s for a given {@see OsmPrimitive}
|
---|
99 | * as child and given set of parents. We don't use {@see CollectBackReferencesVisitor}
|
---|
100 | * because it seems quite inefficient.
|
---|
101 | *
|
---|
102 | * @param parents the set of potential parents
|
---|
103 | * @param child the child
|
---|
104 | * @return the list of {@see OsmParentChildPair}
|
---|
105 | */
|
---|
106 | protected List<OsmParentChildPair> getParentChildPairs(List<OsmPrimitive> parents, OsmPrimitive child) {
|
---|
107 | ArrayList<OsmParentChildPair> pairs = new ArrayList<OsmParentChildPair>();
|
---|
108 | for (OsmPrimitive parent : parents) {
|
---|
109 | if (parent instanceof Way) {
|
---|
110 | Way w = (Way)parent;
|
---|
111 | for (OsmPrimitive node : w.getNodes()) {
|
---|
112 | if (node == child) {
|
---|
113 | OsmParentChildPair pair = new OsmParentChildPair(parent, node);
|
---|
114 | if (! pairs.contains(pair)) {
|
---|
115 | pairs.add(pair);
|
---|
116 | }
|
---|
117 | }
|
---|
118 | }
|
---|
119 | } else if (parent instanceof Relation) {
|
---|
120 | Relation r = (Relation)parent;
|
---|
121 | for (RelationMember member : r.getMembers()) {
|
---|
122 | if (member.getMember() == child) {
|
---|
123 | OsmParentChildPair pair = new OsmParentChildPair(parent, member.getMember());
|
---|
124 | if (! pairs.contains(pair)) {
|
---|
125 | pairs.add(pair);
|
---|
126 | }
|
---|
127 | }
|
---|
128 | }
|
---|
129 | }
|
---|
130 | }
|
---|
131 | return pairs;
|
---|
132 | }
|
---|
133 |
|
---|
134 | /** the primitive to purge */
|
---|
135 | private OsmPrimitive primitive;
|
---|
136 |
|
---|
137 | /** the set of primitives to purge as consequence of purging
|
---|
138 | * {@see #primitive}, including {@see #primitive}
|
---|
139 | */
|
---|
140 | private ArrayList<OsmPrimitive> purgedPrimitives;
|
---|
141 |
|
---|
142 | /** the set of {@see OsmParentChildPair}. We keep a reference
|
---|
143 | * to this set for the {@see #fillModifiedData(Collection, Collection, Collection)} operation
|
---|
144 | */
|
---|
145 | private ArrayList<OsmParentChildPair> pairs;
|
---|
146 |
|
---|
147 |
|
---|
148 | /**
|
---|
149 | * constructor
|
---|
150 | * @param node the node to undelete
|
---|
151 | */
|
---|
152 | public PurgePrimitivesCommand(OsmPrimitive primitive) {
|
---|
153 | this.primitive = primitive;
|
---|
154 | purgedPrimitives = new ArrayList<OsmPrimitive>();
|
---|
155 | pairs = new ArrayList<OsmParentChildPair>();
|
---|
156 | }
|
---|
157 |
|
---|
158 | @Override
|
---|
159 | public MutableTreeNode description() {
|
---|
160 | return new DefaultMutableTreeNode(
|
---|
161 | new JLabel(
|
---|
162 | tr("Purging 1 primitive"),
|
---|
163 | ImageProvider.get("data", "object"),
|
---|
164 | JLabel.HORIZONTAL
|
---|
165 | )
|
---|
166 | );
|
---|
167 | }
|
---|
168 |
|
---|
169 | /**
|
---|
170 | * Purges an {@see OsmPrimitive} <code>toPurge</code> from a {@see DataSet}.
|
---|
171 | *
|
---|
172 | * @param toPurge the primitive to purge
|
---|
173 | * @param ds the dataset to purge from
|
---|
174 | * @param hive the hive of {@see OsmPrimitive}s we remember other {@see OsmPrimitive}
|
---|
175 | * we have to purge because we purge <code>toPurge</code>.
|
---|
176 | *
|
---|
177 | */
|
---|
178 | protected void purge(OsmPrimitive toPurge, DataSet ds, ArrayList<OsmPrimitive> hive) {
|
---|
179 | ArrayList<OsmPrimitive> parents = new ArrayList<OsmPrimitive>();
|
---|
180 | parents.addAll(getLayer().data.ways);
|
---|
181 | parents.addAll(getLayer().data.relations);
|
---|
182 | List<OsmParentChildPair> pairs = getParentChildPairs(parents, primitive);
|
---|
183 | hive.remove(toPurge);
|
---|
184 | for (OsmParentChildPair pair: pairs) {
|
---|
185 | if (pair.getParent() instanceof Way) {
|
---|
186 | Way w = (Way)pair.getParent();
|
---|
187 | System.out.println(tr("removing reference from way {0}",w.getId()));
|
---|
188 | List<Node> wayNodes = w.getNodes();
|
---|
189 | wayNodes.remove(primitive);
|
---|
190 | w.setNodes(wayNodes);
|
---|
191 | // if a way ends up with less than two node we
|
---|
192 | // remember it on the "hive"
|
---|
193 | //
|
---|
194 | if (w.getNodesCount() < 2) {
|
---|
195 | System.out.println(tr("Warning: Purging way {0} because number of nodes dropped below 2. Current is {1}",
|
---|
196 | w.getId(),w.getNodesCount()));
|
---|
197 | if (!hive.contains(w)) {
|
---|
198 | hive.add(w);
|
---|
199 | }
|
---|
200 | }
|
---|
201 | } else if (pair.getParent() instanceof Relation) {
|
---|
202 | Relation r = (Relation)pair.getParent();
|
---|
203 | System.out.println(tr("removing reference from relation {0}",r.getId()));
|
---|
204 | r.removeMembersFor(primitive);
|
---|
205 | }
|
---|
206 | }
|
---|
207 | }
|
---|
208 |
|
---|
209 | @Override
|
---|
210 | public boolean executeCommand() {
|
---|
211 | ArrayList<OsmPrimitive> hive = new ArrayList<OsmPrimitive>();
|
---|
212 |
|
---|
213 | // iteratively purge the primitive and all primitives
|
---|
214 | // which violate invariants after they loose a reference to
|
---|
215 | // the primitive (i.e. ways which end up with less than two
|
---|
216 | // nodes)
|
---|
217 | hive.add(primitive);
|
---|
218 | while(! hive.isEmpty()) {
|
---|
219 | OsmPrimitive toPurge = hive.get(0);
|
---|
220 | purge(toPurge, getLayer().data, hive);
|
---|
221 | if (toPurge instanceof Node) {
|
---|
222 | getLayer().data.nodes.remove(toPurge);
|
---|
223 | } else if (primitive instanceof Way) {
|
---|
224 | getLayer().data.ways.remove(toPurge);
|
---|
225 | } else if (primitive instanceof Relation) {
|
---|
226 | getLayer().data.relations.remove(toPurge);
|
---|
227 | }
|
---|
228 | purgedPrimitives.add(toPurge);
|
---|
229 | ConflictCollection conflicts = getLayer().getConflicts();
|
---|
230 | if (conflicts.hasConflictForMy(toPurge)) {
|
---|
231 | rememberConflict(conflicts.getConflictForMy(toPurge));
|
---|
232 | conflicts.remove(toPurge);
|
---|
233 | }
|
---|
234 | }
|
---|
235 | return super.executeCommand();
|
---|
236 | }
|
---|
237 |
|
---|
238 | @Override
|
---|
239 | public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted,
|
---|
240 | Collection<OsmPrimitive> added) {
|
---|
241 | for (OsmParentChildPair pair : pairs) {
|
---|
242 | modified.add(pair.getParent());
|
---|
243 | }
|
---|
244 | // we don't need pairs anymore
|
---|
245 | pairs = null;
|
---|
246 | }
|
---|
247 |
|
---|
248 | @Override
|
---|
249 | public void undoCommand() {
|
---|
250 | if (! Main.map.mapView.hasLayer(getLayer())) {
|
---|
251 | logger.warning(tr("Can't undo command ''{0}'' because layer ''{1}'' is not present any more",
|
---|
252 | this.toString(),
|
---|
253 | getLayer().toString()
|
---|
254 | ));
|
---|
255 | return;
|
---|
256 | }
|
---|
257 | Main.map.mapView.setActiveLayer(getLayer());
|
---|
258 |
|
---|
259 | // restore purged primitives
|
---|
260 | //
|
---|
261 | for (OsmPrimitive purged : purgedPrimitives) {
|
---|
262 | getLayer().data.addPrimitive(purged);
|
---|
263 | }
|
---|
264 | reconstituteConflicts();
|
---|
265 | // will restore the former references to the purged nodes
|
---|
266 | //
|
---|
267 | super.undoCommand();
|
---|
268 | }
|
---|
269 | }
|
---|