source: josm/trunk/src/org/openstreetmap/josm/command/DeleteCommand.java@ 2850

Last change on this file since 2850 was 2844, checked in by mjulius, 14 years ago

fix messages for commands

  • Property svn:eol-style set to native
File size: 19.1 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.command;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5import static org.openstreetmap.josm.tools.I18n.trn;
6import static org.openstreetmap.josm.tools.I18n.marktr;
7
8import java.awt.GridBagLayout;
9import java.awt.geom.Area;
10import java.util.ArrayList;
11import java.util.Collection;
12import java.util.Collections;
13import java.util.HashSet;
14import java.util.Iterator;
15import java.util.LinkedList;
16import java.util.List;
17import java.util.Set;
18
19import javax.swing.JLabel;
20import javax.swing.JOptionPane;
21import javax.swing.JPanel;
22import javax.swing.tree.DefaultMutableTreeNode;
23import javax.swing.tree.MutableTreeNode;
24
25import org.openstreetmap.josm.Main;
26import org.openstreetmap.josm.actions.SplitWayAction;
27import org.openstreetmap.josm.data.osm.Node;
28import org.openstreetmap.josm.data.osm.OsmPrimitive;
29import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
30import org.openstreetmap.josm.data.osm.Relation;
31import org.openstreetmap.josm.data.osm.RelationToChildReference;
32import org.openstreetmap.josm.data.osm.Way;
33import org.openstreetmap.josm.data.osm.WaySegment;
34import org.openstreetmap.josm.gui.ConditionalOptionPaneUtil;
35import org.openstreetmap.josm.gui.DefaultNameFormatter;
36import org.openstreetmap.josm.gui.actionsupport.DeleteFromRelationConfirmationDialog;
37import org.openstreetmap.josm.gui.layer.OsmDataLayer;
38import org.openstreetmap.josm.tools.CheckParameterUtil;
39import org.openstreetmap.josm.tools.ImageProvider;
40
41/**
42 * A command to delete a number of primitives from the dataset.
43 *
44 */
45public class DeleteCommand extends Command {
46 /**
47 * The primitives that get deleted.
48 */
49 private final Collection<? extends OsmPrimitive> toDelete;
50
51 /**
52 * Constructor. Deletes a collection of primitives in the current edit layer.
53 *
54 * @param data the primitives to delete. Must neither be null nor empty.
55 * @throws IllegalArgumentException thrown if data is null or empty
56 */
57 public DeleteCommand(Collection<? extends OsmPrimitive> data) throws IllegalArgumentException {
58 if (data == null)
59 throw new IllegalArgumentException("Parameter 'data' must not be empty");
60 if (data.isEmpty())
61 throw new IllegalArgumentException(tr("At least one object to delete required, got empty collection"));
62 this.toDelete = data;
63 }
64
65 /**
66 * Constructor. Deletes a single primitive in the current edit layer.
67 *
68 * @param data the primitive to delete. Must not be null.
69 * @throws IllegalArgumentException thrown if data is null
70 */
71 public DeleteCommand(OsmPrimitive data) throws IllegalArgumentException {
72 CheckParameterUtil.ensureParameterNotNull(data, "data");
73 this.toDelete = Collections.singleton(data);
74 }
75
76 /**
77 * Constructor for a single data item. Use the collection constructor to delete multiple
78 * objects.
79 *
80 * @param layer the layer context for deleting this primitive. Must not be null.
81 * @param data the primitive to delete. Must not be null.
82 * @throws IllegalArgumentException thrown if data is null
83 * @throws IllegalArgumentException thrown if layer is null
84 */
85 public DeleteCommand(OsmDataLayer layer, OsmPrimitive data) throws IllegalArgumentException {
86 super(layer);
87 CheckParameterUtil.ensureParameterNotNull(data, "data");
88 this.toDelete = Collections.singleton(data);
89 }
90
91 /**
92 * Constructor for a collection of data to be deleted in the context of
93 * a specific layer
94 *
95 * @param layer the layer context for deleting these primitives. Must not be null.
96 * @param data the primitives to delete. Must neither be null nor empty.
97 * @throws IllegalArgumentException thrown if layer is null
98 * @throws IllegalArgumentException thrown if data is null or empty
99 */
100 public DeleteCommand(OsmDataLayer layer, Collection<? extends OsmPrimitive> data) throws IllegalArgumentException{
101 super(layer);
102 if (data == null)
103 throw new IllegalArgumentException("Parameter 'data' must not be empty");
104 if (data.isEmpty())
105 throw new IllegalArgumentException(tr("At least one object to delete required, got empty collection"));
106 this.toDelete = data;
107 }
108
109 protected void removeNewNodesFromDeletedWay(Way w) {
110 // #2707: ways to be deleted can include new nodes (with node.id == 0).
111 // Remove them from the way before the way is deleted. Otherwise the
112 // deleted way is saved (or sent to the API) with a dangling reference to a node
113 // Example:
114 // <node id='2' action='delete' visible='true' version='1' ... />
115 // <node id='1' action='delete' visible='true' version='1' ... />
116 // <!-- missing node with id -1 because new deleted nodes are not persisted -->
117 // <way id='3' action='delete' visible='true' version='1'>
118 // <nd ref='1' />
119 // <nd ref='-1' /> <!-- here's the problem -->
120 // <nd ref='2' />
121 // </way>
122 if (w.isNew())
123 return; // process existing ways only
124 List<Node> nodesToKeep = new ArrayList<Node>();
125 // lookup new nodes which have been added to the set of deleted
126 // nodes ...
127 Iterator<Node> it = nodesToKeep.iterator();
128 while(it.hasNext()) {
129 Node n = it.next();
130 if (n.isNew()) {
131 it.remove();
132 }
133 }
134 w.setNodes(nodesToKeep);
135 }
136
137 @Override
138 public boolean executeCommand() {
139 super.executeCommand();
140 for (OsmPrimitive osm : toDelete) {
141 osm.setDeleted(true);
142 if (osm instanceof Way) {
143 removeNewNodesFromDeletedWay((Way)osm);
144 }
145 }
146 return true;
147 }
148
149 @Override
150 public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted,
151 Collection<OsmPrimitive> added) {
152 deleted.addAll(toDelete);
153 }
154
155 @Override
156 public MutableTreeNode description() {
157 if (toDelete.size() == 1) {
158 OsmPrimitive primitive = toDelete.iterator().next();
159 String msg = "";
160 switch(OsmPrimitiveType.from(primitive)) {
161 case NODE: msg = marktr("Delete node {0}"); break;
162 case WAY: msg = marktr("Delete way {0}"); break;
163 case RELATION:msg = marktr("Delete relation {0}"); break;
164 }
165
166 return new DefaultMutableTreeNode(new JLabel(tr(msg, primitive.getDisplayName(DefaultNameFormatter.getInstance())),
167 ImageProvider.get(OsmPrimitiveType.from(primitive)), JLabel.HORIZONTAL));
168 }
169
170 Set<OsmPrimitiveType> typesToDelete = new HashSet<OsmPrimitiveType>();
171 for (OsmPrimitive osm : toDelete) {
172 typesToDelete.add(OsmPrimitiveType.from(osm));
173 }
174 String msg = "";
175 String apiname = "object";
176 if (typesToDelete.size() > 1) {
177 msg = trn("Delete {0} object", "Delete {0} objects", toDelete.size(), toDelete.size());
178 } else {
179 OsmPrimitiveType t = typesToDelete.iterator().next();
180 apiname = t.getAPIName();
181 switch(t) {
182 case NODE: msg = trn("Delete {0} node", "Delete {0} nodes", toDelete.size(), toDelete.size()); break;
183 case WAY: msg = trn("Delete {0} way", "Delete {0} ways", toDelete.size(), toDelete.size()); break;
184 case RELATION: msg = trn("Delete {0} relation", "Delete {0} relations", toDelete.size(), toDelete.size()); break;
185 }
186 }
187 DefaultMutableTreeNode root = new DefaultMutableTreeNode(
188 new JLabel(msg, ImageProvider.get("data", apiname), JLabel.HORIZONTAL)
189 );
190 for (OsmPrimitive osm : toDelete) {
191 root.add(new DefaultMutableTreeNode(new JLabel(
192 osm.getDisplayName(DefaultNameFormatter.getInstance()),
193 ImageProvider.get(OsmPrimitiveType.from(osm)), JLabel.HORIZONTAL)));
194 }
195 return root;
196 }
197
198 /**
199 * Delete the primitives and everything they reference.
200 *
201 * If a node is deleted, the node and all ways and relations the node is part of are deleted as
202 * well.
203 *
204 * If a way is deleted, all relations the way is member of are also deleted.
205 *
206 * If a way is deleted, only the way and no nodes are deleted.
207 *
208 * @param layer the {@see OsmDataLayer} in whose context primitives are deleted. Must not be null.
209 * @param selection The list of all object to be deleted.
210 * @param silent Set to true if the user should not be bugged with additional dialogs
211 * @return command A command to perform the deletions, or null of there is nothing to delete.
212 * @throws IllegalArgumentException thrown if layer is null
213 */
214 public static Command deleteWithReferences(OsmDataLayer layer, Collection<? extends OsmPrimitive> selection, boolean silent) throws IllegalArgumentException {
215 CheckParameterUtil.ensureParameterNotNull(layer, "layer");
216 if (selection == null || selection.isEmpty()) return null;
217 Set<OsmPrimitive> parents = OsmPrimitive.getReferrer(selection);
218 parents.addAll(selection);
219
220 if (parents.isEmpty())
221 return null;
222 if (!checkAndConfirmOutlyingDeletes(layer,parents) && !silent)
223 return null;
224 return new DeleteCommand(layer,parents);
225 }
226
227 public static Command deleteWithReferences(OsmDataLayer layer, Collection<? extends OsmPrimitive> selection) {
228 return deleteWithReferences(layer, selection, false);
229 }
230
231 public static Command delete(OsmDataLayer layer, Collection<? extends OsmPrimitive> selection) {
232 return delete(layer, selection, true, false);
233 }
234
235 /**
236 * Replies the collection of nodes referred to by primitives in <code>primitivesToDelete</code> which
237 * can be deleted too. A node can be deleted if
238 * <ul>
239 * <li>it is untagged (see {@see Node#isTagged()}</li>
240 * <li>it is not referred to by other non-deleted primitives outside of <code>primitivesToDelete</code></li>
241 * <ul>
242 * @param layer the layer in whose context primitives are deleted
243 * @param primitivesToDelete the primitives to delete
244 * @return the collection of nodes referred to by primitives in <code>primitivesToDelete</code> which
245 * can be deleted too
246 */
247 protected static Collection<Node> computeNodesToDelete(OsmDataLayer layer, Collection<OsmPrimitive> primitivesToDelete) {
248 Collection<Node> nodesToDelete = new HashSet<Node>();
249 for (Way way : OsmPrimitive.getFilteredList(primitivesToDelete, Way.class)) {
250 for (Node n : way.getNodes()) {
251 if (n.isTagged()) {
252 continue;
253 }
254 Collection<OsmPrimitive> referringPrimitives = n.getReferrers();
255 referringPrimitives.removeAll(primitivesToDelete);
256 int count = 0;
257 for (OsmPrimitive p : referringPrimitives) {
258 if (!p.isDeleted()) {
259 count++;
260 }
261 }
262 if (count == 0) {
263 nodesToDelete.add(n);
264 }
265 }
266 }
267 return nodesToDelete;
268 }
269
270 /**
271 * Try to delete all given primitives.
272 *
273 * If a node is used by a way, it's removed from that way. If a node or a way is used by a
274 * relation, inform the user and do not delete.
275 *
276 * If this would cause ways with less than 2 nodes to be created, delete these ways instead. If
277 * they are part of a relation, inform the user and do not delete.
278 *
279 * @param layer the {@see OsmDataLayer} in whose context the primitives are deleted
280 * @param selection the objects to delete.
281 * @param alsoDeleteNodesInWay <code>true</code> if nodes should be deleted as well
282 * @return command a command to perform the deletions, or null if there is nothing to delete.
283 */
284 public static Command delete(OsmDataLayer layer, Collection<? extends OsmPrimitive> selection,
285 boolean alsoDeleteNodesInWay) {
286 return delete(layer, selection, alsoDeleteNodesInWay, false /* not silent */);
287 }
288
289 /**
290 * Try to delete all given primitives.
291 *
292 * If a node is used by a way, it's removed from that way. If a node or a way is used by a
293 * relation, inform the user and do not delete.
294 *
295 * If this would cause ways with less than 2 nodes to be created, delete these ways instead. If
296 * they are part of a relation, inform the user and do not delete.
297 *
298 * @param layer the {@see OsmDataLayer} in whose context the primitives are deleted
299 * @param selection the objects to delete.
300 * @param alsoDeleteNodesInWay <code>true</code> if nodes should be deleted as well
301 * @param silent set to true if the user should not be bugged with additional questions
302 * @return command a command to perform the deletions, or null if there is nothing to delete.
303 */
304 public static Command delete(OsmDataLayer layer, Collection<? extends OsmPrimitive> selection,
305 boolean alsoDeleteNodesInWay, boolean silent) {
306 if (selection == null || selection.isEmpty())
307 return null;
308
309 Set<OsmPrimitive> primitivesToDelete = new HashSet<OsmPrimitive>(selection);
310 Collection<Way> waysToBeChanged = new HashSet<Way>();
311
312 if (alsoDeleteNodesInWay) {
313 // delete untagged nodes only referenced by primitives in primitivesToDelete,
314 // too
315 Collection<Node> nodesToDelete = computeNodesToDelete(layer, primitivesToDelete);
316 primitivesToDelete.addAll(nodesToDelete);
317 }
318
319 if (!silent && !checkAndConfirmOutlyingDeletes(layer,primitivesToDelete))
320 return null;
321
322 waysToBeChanged.addAll(OsmPrimitive.getFilteredSet(OsmPrimitive.getReferrer(primitivesToDelete), Way.class));
323
324 Collection<Command> cmds = new LinkedList<Command>();
325 for (Way w : waysToBeChanged) {
326 Way wnew = new Way(w);
327 wnew.removeNodes(primitivesToDelete);
328 if (wnew.getNodesCount() < 2) {
329 primitivesToDelete.add(w);
330 } else {
331 cmds.add(new ChangeCommand(w, wnew));
332 }
333 }
334
335 // get a confirmation that the objects to delete can be removed from their parent
336 // relations
337 //
338 if (!silent) {
339 Set<RelationToChildReference> references = RelationToChildReference.getRelationToChildReferences(primitivesToDelete);
340 Iterator<RelationToChildReference> it = references.iterator();
341 while(it.hasNext()) {
342 RelationToChildReference ref = it.next();
343 if (ref.getParent().isDeleted()) {
344 it.remove();
345 }
346 }
347 if (!references.isEmpty()) {
348 DeleteFromRelationConfirmationDialog dialog = DeleteFromRelationConfirmationDialog.getInstance();
349 dialog.getModel().populate(references);
350 dialog.setVisible(true);
351 if (dialog.isCanceled())
352 return null;
353 }
354 }
355
356 // remove the objects from their parent relations
357 //
358 Iterator<Relation> iterator = OsmPrimitive.getFilteredSet(OsmPrimitive.getReferrer(primitivesToDelete), Relation.class).iterator();
359 while (iterator.hasNext()) {
360 Relation cur = iterator.next();
361 Relation rel = new Relation(cur);
362 rel.removeMembersFor(primitivesToDelete);
363 cmds.add(new ChangeCommand(cur, rel));
364 }
365
366 // build the delete command
367 //
368 if (!primitivesToDelete.isEmpty()) {
369 cmds.add(new DeleteCommand(layer,primitivesToDelete));
370 }
371
372 return new SequenceCommand(tr("Delete"), cmds);
373 }
374
375 public static Command deleteWaySegment(OsmDataLayer layer, WaySegment ws) {
376 if (ws.way.getNodesCount() < 3)
377 return delete(layer, Collections.singleton(ws.way));
378
379 if (ws.way.firstNode() == ws.way.lastNode()) {
380 // If the way is circular (first and last nodes are the same),
381 // the way shouldn't be splitted
382
383 List<Node> n = new ArrayList<Node>();
384
385 n.addAll(ws.way.getNodes().subList(ws.lowerIndex + 1, ws.way.getNodesCount() - 1));
386 n.addAll(ws.way.getNodes().subList(0, ws.lowerIndex + 1));
387
388 Way wnew = new Way(ws.way);
389 wnew.setNodes(n);
390
391 return new ChangeCommand(ws.way, wnew);
392 }
393
394 List<Node> n1 = new ArrayList<Node>(), n2 = new ArrayList<Node>();
395
396 n1.addAll(ws.way.getNodes().subList(0, ws.lowerIndex + 1));
397 n2.addAll(ws.way.getNodes().subList(ws.lowerIndex + 1, ws.way.getNodesCount()));
398
399 Way wnew = new Way(ws.way);
400
401 if (n1.size() < 2) {
402 wnew.setNodes(n2);
403 return new ChangeCommand(ws.way, wnew);
404 } else if (n2.size() < 2) {
405 wnew.setNodes(n1);
406 return new ChangeCommand(ws.way, wnew);
407 } else {
408 List<List<Node>> chunks = new ArrayList<List<Node>>(2);
409 chunks.add(n1);
410 chunks.add(n2);
411 return SplitWayAction.splitWay(ws.way, chunks).getCommand();
412 }
413 }
414
415 /**
416 * Check whether user is about to delete data outside of the download area. Request confirmation
417 * if he is.
418 *
419 * @param layer the layer in whose context data is deleted
420 * @param primitivesToDelete the primitives to delete
421 * @return true, if deleting outlying primitives is OK; false, otherwise
422 */
423 private static boolean checkAndConfirmOutlyingDeletes(OsmDataLayer layer, Collection<OsmPrimitive> primitivesToDelete) {
424 Area a = layer.data.getDataSourceArea();
425 if (a != null) {
426 for (OsmPrimitive osm : primitivesToDelete) {
427 if (osm instanceof Node && !osm.isNew()) {
428 Node n = (Node) osm;
429 if (!a.contains(n.getCoor())) {
430 JPanel msg = new JPanel(new GridBagLayout());
431 msg.add(new JLabel(
432 "<html>" +
433 // leave message in one tr() as there is a grammatical
434 // connection.
435 tr("You are about to delete nodes outside of the area you have downloaded."
436 + "<br>"
437 + "This can cause problems because other objects (that you do not see) might use them."
438 + "<br>" + "Do you really want to delete?") + "</html>"));
439 return ConditionalOptionPaneUtil.showConfirmationDialog(
440 "delete_outside_nodes",
441 Main.parent,
442 msg,
443 tr("Delete confirmation"),
444 JOptionPane.YES_NO_OPTION,
445 JOptionPane.QUESTION_MESSAGE,
446 JOptionPane.YES_OPTION
447 );
448 }
449 }
450 }
451 }
452 return true;
453 }
454}
Note: See TracBrowser for help on using the repository browser.