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

Last change on this file since 5059 was 5059, checked in by simon04, 12 years ago

fix #6561 - fix several overflowing dialog texts

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