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 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
6 |
|
---|
7 | import java.awt.GridBagLayout;
|
---|
8 | import java.awt.geom.Area;
|
---|
9 | import java.util.ArrayList;
|
---|
10 | import java.util.Collection;
|
---|
11 | import java.util.Collections;
|
---|
12 | import java.util.HashMap;
|
---|
13 | import java.util.HashSet;
|
---|
14 | import java.util.Iterator;
|
---|
15 | import java.util.LinkedList;
|
---|
16 | import java.util.List;
|
---|
17 |
|
---|
18 | import javax.swing.JLabel;
|
---|
19 | import javax.swing.JOptionPane;
|
---|
20 | import javax.swing.JPanel;
|
---|
21 | import javax.swing.tree.DefaultMutableTreeNode;
|
---|
22 | import javax.swing.tree.MutableTreeNode;
|
---|
23 |
|
---|
24 | import org.openstreetmap.josm.Main;
|
---|
25 | import org.openstreetmap.josm.data.osm.Node;
|
---|
26 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
27 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
28 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
29 | import org.openstreetmap.josm.data.osm.Way;
|
---|
30 | import org.openstreetmap.josm.data.osm.WaySegment;
|
---|
31 | import org.openstreetmap.josm.data.osm.visitor.CollectBackReferencesVisitor;
|
---|
32 | import org.openstreetmap.josm.data.osm.visitor.NameVisitor;
|
---|
33 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
34 | import org.openstreetmap.josm.tools.DontShowAgainInfo;
|
---|
35 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * A command to delete a number of primitives from the dataset.
|
---|
39 | * @author imi
|
---|
40 | */
|
---|
41 | public class DeleteCommand extends Command {
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * The primitives that get deleted.
|
---|
45 | */
|
---|
46 | private final Collection<? extends OsmPrimitive> toDelete;
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Constructor for a collection of data
|
---|
50 | */
|
---|
51 | public DeleteCommand(Collection<? extends OsmPrimitive> data) {
|
---|
52 | super();
|
---|
53 | this.toDelete = data;
|
---|
54 | }
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Constructor for a single data item. Use the collection constructor to delete multiple
|
---|
58 | * objects.
|
---|
59 | */
|
---|
60 | public DeleteCommand(OsmPrimitive data) {
|
---|
61 | this.toDelete = Collections.singleton(data);
|
---|
62 | }
|
---|
63 |
|
---|
64 | @Override public boolean executeCommand() {
|
---|
65 | super.executeCommand();
|
---|
66 | for (OsmPrimitive osm : toDelete) {
|
---|
67 | osm.delete(true);
|
---|
68 | }
|
---|
69 | return true;
|
---|
70 | }
|
---|
71 |
|
---|
72 | @Override public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted,
|
---|
73 | Collection<OsmPrimitive> added) {
|
---|
74 | deleted.addAll(toDelete);
|
---|
75 | }
|
---|
76 |
|
---|
77 | @Override public MutableTreeNode description() {
|
---|
78 | NameVisitor v = new NameVisitor();
|
---|
79 |
|
---|
80 | if (toDelete.size() == 1) {
|
---|
81 | toDelete.iterator().next().visit(v);
|
---|
82 | return new DefaultMutableTreeNode(new JLabel(tr("Delete {1} {0}", v.name, tr(v.className)), v.icon,
|
---|
83 | JLabel.HORIZONTAL));
|
---|
84 | }
|
---|
85 |
|
---|
86 | String cname = null;
|
---|
87 | String cnamem = null;
|
---|
88 | for (OsmPrimitive osm : toDelete) {
|
---|
89 | osm.visit(v);
|
---|
90 | if (cname == null) {
|
---|
91 | cname = v.className;
|
---|
92 | cnamem = v.classNamePlural;
|
---|
93 | } else if (!cname.equals(v.className)) {
|
---|
94 | cname = "object";
|
---|
95 | cnamem = trn("object", "objects", 2);
|
---|
96 | }
|
---|
97 | }
|
---|
98 | DefaultMutableTreeNode root = new DefaultMutableTreeNode(new JLabel(tr("Delete {0} {1}", toDelete.size(), trn(
|
---|
99 | cname, cnamem, toDelete.size())), ImageProvider.get("data", cname), JLabel.HORIZONTAL));
|
---|
100 | for (OsmPrimitive osm : toDelete) {
|
---|
101 | osm.visit(v);
|
---|
102 | root.add(new DefaultMutableTreeNode(v.toLabel()));
|
---|
103 | }
|
---|
104 | return root;
|
---|
105 | }
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * Delete the primitives and everything they reference.
|
---|
109 | *
|
---|
110 | * If a node is deleted, the node and all ways and relations the node is part of are deleted as
|
---|
111 | * well.
|
---|
112 | *
|
---|
113 | * If a way is deleted, all relations the way is member of are also deleted.
|
---|
114 | *
|
---|
115 | * If a way is deleted, only the way and no nodes are deleted.
|
---|
116 | *
|
---|
117 | * @param selection The list of all object to be deleted.
|
---|
118 | * @return command A command to perform the deletions, or null of there is nothing to delete.
|
---|
119 | */
|
---|
120 | public static Command deleteWithReferences(Collection<? extends OsmPrimitive> selection) {
|
---|
121 | CollectBackReferencesVisitor v = new CollectBackReferencesVisitor(Main.ds);
|
---|
122 | for (OsmPrimitive osm : selection) {
|
---|
123 | osm.visit(v);
|
---|
124 | }
|
---|
125 | v.data.addAll(selection);
|
---|
126 | if (v.data.isEmpty())
|
---|
127 | return null;
|
---|
128 | if (!checkAndConfirmOutlyingDeletes(v.data))
|
---|
129 | return null;
|
---|
130 | return new DeleteCommand(v.data);
|
---|
131 | }
|
---|
132 |
|
---|
133 | private static int testRelation(Relation ref, OsmPrimitive osm) {
|
---|
134 | NameVisitor n = new NameVisitor();
|
---|
135 | ref.visit(n);
|
---|
136 | NameVisitor s = new NameVisitor();
|
---|
137 | osm.visit(s);
|
---|
138 | String role = new String();
|
---|
139 | for (RelationMember m : ref.members) {
|
---|
140 | if (m.member == osm) {
|
---|
141 | role = m.role;
|
---|
142 | break;
|
---|
143 | }
|
---|
144 | }
|
---|
145 | if (role.length() > 0)
|
---|
146 | return new ExtendedDialog(Main.parent,
|
---|
147 | tr("Conflicting relation"),
|
---|
148 | tr("Selection \"{0}\" is used by relation \"{1}\" with role {2}.\nDelete from relation?",
|
---|
149 | s.name, n.name, role),
|
---|
150 | new String[] {tr("Delete from relation"), tr("Cancel")},
|
---|
151 | new String[] {"dialogs/delete.png", "cancel.png"}).getValue();
|
---|
152 | else
|
---|
153 | return new ExtendedDialog(Main.parent,
|
---|
154 | tr("Conflicting relation"),
|
---|
155 | tr("Selection \"{0}\" is used by relation \"{1}\".\nDelete from relation?",
|
---|
156 | s.name, n.name),
|
---|
157 | new String[] {tr("Delete from relation"), tr("Cancel")},
|
---|
158 | new String[] {"dialogs/delete.png", "cancel.png"}).getValue();
|
---|
159 | }
|
---|
160 |
|
---|
161 | public static Command delete(Collection<? extends OsmPrimitive> selection) {
|
---|
162 | return delete(selection, true);
|
---|
163 | }
|
---|
164 |
|
---|
165 | /**
|
---|
166 | * Try to delete all given primitives.
|
---|
167 | *
|
---|
168 | * If a node is used by a way, it's removed from that way. If a node or a way is used by a
|
---|
169 | * relation, inform the user and do not delete.
|
---|
170 | *
|
---|
171 | * If this would cause ways with less than 2 nodes to be created, delete these ways instead. If
|
---|
172 | * they are part of a relation, inform the user and do not delete.
|
---|
173 | *
|
---|
174 | * @param selection The objects to delete.
|
---|
175 | * @param alsoDeleteNodesInWay <code>true</code> if nodes should be deleted as well
|
---|
176 | * @return command A command to perform the deletions, or null of there is nothing to delete.
|
---|
177 | */
|
---|
178 | public static Command delete(Collection<? extends OsmPrimitive> selection, boolean alsoDeleteNodesInWay) {
|
---|
179 | if (selection.isEmpty())
|
---|
180 | return null;
|
---|
181 |
|
---|
182 | Collection<OsmPrimitive> del = new HashSet<OsmPrimitive>(selection);
|
---|
183 | Collection<Way> waysToBeChanged = new HashSet<Way>();
|
---|
184 | HashMap<OsmPrimitive, Collection<OsmPrimitive>> relationsToBeChanged = new HashMap<OsmPrimitive, Collection<OsmPrimitive>>();
|
---|
185 |
|
---|
186 | if (alsoDeleteNodesInWay) {
|
---|
187 | // Delete untagged nodes that are to be unreferenced.
|
---|
188 | Collection<OsmPrimitive> delNodes = new HashSet<OsmPrimitive>();
|
---|
189 | for (OsmPrimitive osm : del) {
|
---|
190 | if (osm instanceof Way) {
|
---|
191 | for (Node n : ((Way) osm).nodes) {
|
---|
192 | if (!n.isTagged()) {
|
---|
193 | CollectBackReferencesVisitor v = new CollectBackReferencesVisitor(Main.ds, false);
|
---|
194 | n.visit(v);
|
---|
195 | v.data.removeAll(del);
|
---|
196 | if (v.data.isEmpty()) {
|
---|
197 | delNodes.add(n);
|
---|
198 | }
|
---|
199 | }
|
---|
200 | }
|
---|
201 | }
|
---|
202 | }
|
---|
203 | del.addAll(delNodes);
|
---|
204 | }
|
---|
205 |
|
---|
206 | if (!checkAndConfirmOutlyingDeletes(del))
|
---|
207 | return null;
|
---|
208 |
|
---|
209 | for (OsmPrimitive osm : del) {
|
---|
210 | CollectBackReferencesVisitor v = new CollectBackReferencesVisitor(Main.ds, false);
|
---|
211 | osm.visit(v);
|
---|
212 | for (OsmPrimitive ref : v.data) {
|
---|
213 | if (del.contains(ref)) {
|
---|
214 | continue;
|
---|
215 | }
|
---|
216 | if (ref instanceof Way) {
|
---|
217 | waysToBeChanged.add((Way) ref);
|
---|
218 | } else if (ref instanceof Relation) {
|
---|
219 | if (testRelation((Relation) ref, osm) == 1) {
|
---|
220 | Collection<OsmPrimitive> relset = relationsToBeChanged.get(ref);
|
---|
221 | if (relset == null) {
|
---|
222 | relset = new HashSet<OsmPrimitive>();
|
---|
223 | }
|
---|
224 | relset.add(osm);
|
---|
225 | relationsToBeChanged.put(ref, relset);
|
---|
226 | } else
|
---|
227 | return null;
|
---|
228 | } else
|
---|
229 | return null;
|
---|
230 | }
|
---|
231 | }
|
---|
232 |
|
---|
233 | Collection<Command> cmds = new LinkedList<Command>();
|
---|
234 | for (Way w : waysToBeChanged) {
|
---|
235 | Way wnew = new Way(w);
|
---|
236 | wnew.removeNodes(del);
|
---|
237 | if (wnew.nodes.size() < 2) {
|
---|
238 | del.add(w);
|
---|
239 |
|
---|
240 | CollectBackReferencesVisitor v = new CollectBackReferencesVisitor(Main.ds, false);
|
---|
241 | w.visit(v);
|
---|
242 | for (OsmPrimitive ref : v.data) {
|
---|
243 | if (del.contains(ref)) {
|
---|
244 | continue;
|
---|
245 | }
|
---|
246 | if (ref instanceof Relation) {
|
---|
247 | Boolean found = false;
|
---|
248 | Collection<OsmPrimitive> relset = relationsToBeChanged.get(ref);
|
---|
249 | if (relset == null) {
|
---|
250 | relset = new HashSet<OsmPrimitive>();
|
---|
251 | } else {
|
---|
252 | for (OsmPrimitive m : relset) {
|
---|
253 | if (m == w) {
|
---|
254 | found = true;
|
---|
255 | break;
|
---|
256 | }
|
---|
257 | }
|
---|
258 | }
|
---|
259 | if (!found) {
|
---|
260 | if (testRelation((Relation) ref, w) == 1) {
|
---|
261 | relset.add(w);
|
---|
262 | relationsToBeChanged.put(ref, relset);
|
---|
263 | } else
|
---|
264 | return null;
|
---|
265 | }
|
---|
266 | } else
|
---|
267 | return null;
|
---|
268 | }
|
---|
269 | } else {
|
---|
270 | cmds.add(new ChangeCommand(w, wnew));
|
---|
271 | }
|
---|
272 | }
|
---|
273 |
|
---|
274 | Iterator<OsmPrimitive> iterator = relationsToBeChanged.keySet().iterator();
|
---|
275 | while (iterator.hasNext()) {
|
---|
276 | Relation cur = (Relation) iterator.next();
|
---|
277 | Relation rel = new Relation(cur);
|
---|
278 | for (OsmPrimitive osm : relationsToBeChanged.get(cur)) {
|
---|
279 | rel.removeMembersFor(osm);
|
---|
280 | }
|
---|
281 | cmds.add(new ChangeCommand(cur, rel));
|
---|
282 | }
|
---|
283 |
|
---|
284 | // #2707: ways to be deleted can include new nodes (with node.id == 0).
|
---|
285 | // Remove them from the way before the way is deleted. Otherwise the
|
---|
286 | // deleted way is saved (or sent to the API) with a dangling reference to a node
|
---|
287 | // Example:
|
---|
288 | // <node id='2' action='delete' visible='true' version='1' ... />
|
---|
289 | // <node id='1' action='delete' visible='true' version='1' ... />
|
---|
290 | // <!-- missing node with id -1 because new deleted nodes are not persisted -->
|
---|
291 | // <way id='3' action='delete' visible='true' version='1'>
|
---|
292 | // <nd ref='1' />
|
---|
293 | // <nd ref='-1' /> <!-- heres the problem -->
|
---|
294 | // <nd ref='2' />
|
---|
295 | // </way>
|
---|
296 | for (OsmPrimitive primitive : del) {
|
---|
297 | if (! (primitive instanceof Way)) {
|
---|
298 | continue;
|
---|
299 | }
|
---|
300 | Way w = (Way)primitive;
|
---|
301 | if (w.id == 0) { // new ways with id == 0 are fine,
|
---|
302 | continue; // process existing ways only
|
---|
303 | }
|
---|
304 | Way wnew = new Way(w);
|
---|
305 | ArrayList<Node> nodesToStrip = new ArrayList<Node>();
|
---|
306 | // lookup new nodes which have been added to the set of deleted
|
---|
307 | // nodes ...
|
---|
308 | for (Node n : wnew.nodes) {
|
---|
309 | if (n.id == 0 && del.contains(n)) {
|
---|
310 | nodesToStrip.add(n);
|
---|
311 | }
|
---|
312 | }
|
---|
313 | // .. and remove them from the way
|
---|
314 | //
|
---|
315 | wnew.nodes.removeAll(nodesToStrip);
|
---|
316 | if (!nodesToStrip.isEmpty()) {
|
---|
317 | cmds.add(new ChangeCommand(w,wnew));
|
---|
318 | }
|
---|
319 | }
|
---|
320 |
|
---|
321 | if (!del.isEmpty()) {
|
---|
322 | cmds.add(new DeleteCommand(del));
|
---|
323 | }
|
---|
324 |
|
---|
325 | return new SequenceCommand(tr("Delete"), cmds);
|
---|
326 | }
|
---|
327 |
|
---|
328 | public static Command deleteWaySegment(WaySegment ws) {
|
---|
329 | List<Node> n1 = new ArrayList<Node>(), n2 = new ArrayList<Node>();
|
---|
330 |
|
---|
331 | n1.addAll(ws.way.nodes.subList(0, ws.lowerIndex + 1));
|
---|
332 | n2.addAll(ws.way.nodes.subList(ws.lowerIndex + 1, ws.way.nodes.size()));
|
---|
333 |
|
---|
334 | if (n1.size() < 2 && n2.size() < 2)
|
---|
335 | return new DeleteCommand(Collections.singleton(ws.way));
|
---|
336 |
|
---|
337 | Way wnew = new Way(ws.way);
|
---|
338 | wnew.nodes.clear();
|
---|
339 |
|
---|
340 | if (n1.size() < 2) {
|
---|
341 | wnew.nodes.addAll(n2);
|
---|
342 | return new ChangeCommand(ws.way, wnew);
|
---|
343 | } else if (n2.size() < 2) {
|
---|
344 | wnew.nodes.addAll(n1);
|
---|
345 | return new ChangeCommand(ws.way, wnew);
|
---|
346 | } else {
|
---|
347 | Collection<Command> cmds = new LinkedList<Command>();
|
---|
348 |
|
---|
349 | wnew.nodes.addAll(n1);
|
---|
350 | cmds.add(new ChangeCommand(ws.way, wnew));
|
---|
351 |
|
---|
352 | Way wnew2 = new Way();
|
---|
353 | if (wnew.keys != null) {
|
---|
354 | wnew2.keys = new HashMap<String, String>(wnew.keys);
|
---|
355 | }
|
---|
356 | wnew2.nodes.addAll(n2);
|
---|
357 | cmds.add(new AddCommand(wnew2));
|
---|
358 |
|
---|
359 | return new SequenceCommand(tr("Split way segment"), cmds);
|
---|
360 | }
|
---|
361 | }
|
---|
362 |
|
---|
363 | /**
|
---|
364 | * Check whether user is about to delete data outside of the download area.
|
---|
365 | * Request confirmation if he is.
|
---|
366 | */
|
---|
367 | private static boolean checkAndConfirmOutlyingDeletes(Collection<OsmPrimitive> del) {
|
---|
368 | Area a = Main.ds.getDataSourceArea();
|
---|
369 | if (a != null) {
|
---|
370 | for (OsmPrimitive osm : del) {
|
---|
371 | if (osm instanceof Node && osm.id != 0) {
|
---|
372 | Node n = (Node) osm;
|
---|
373 | if (!a.contains(n.getCoor())) {
|
---|
374 | JPanel msg = new JPanel(new GridBagLayout());
|
---|
375 | msg.add(new JLabel(
|
---|
376 | "<html>" +
|
---|
377 | // leave message in one tr() as there is a grammatical connection.
|
---|
378 | tr("You are about to delete nodes outside of the area you have downloaded." +
|
---|
379 | "<br>" +
|
---|
380 | "This can cause problems because other objects (that you don't see) might use them." +
|
---|
381 | "<br>" +
|
---|
382 | "Do you really want to delete?") + "</html>"));
|
---|
383 | return DontShowAgainInfo.show("delete_outside_nodes", msg, false, JOptionPane.YES_NO_OPTION, JOptionPane.YES_OPTION);
|
---|
384 | }
|
---|
385 |
|
---|
386 | }
|
---|
387 | }
|
---|
388 | }
|
---|
389 | return true;
|
---|
390 | }
|
---|
391 | }
|
---|