source: josm/src/org/openstreetmap/josm/actions/mapmode/DeleteAction.java@ 86

Last change on this file since 86 was 86, checked in by imi, 19 years ago
  • added conflicts and resolve conflict dialog

This is one of those "changed everything" checkpoint.

File size: 4.2 KB
RevLine 
[7]1package org.openstreetmap.josm.actions.mapmode;
2
[30]3import java.awt.event.ActionEvent;
[7]4import java.awt.event.KeyEvent;
5import java.awt.event.MouseEvent;
[23]6import java.util.Collection;
[31]7import java.util.Collections;
[8]8import java.util.LinkedList;
[7]9
10import javax.swing.JOptionPane;
11
[21]12import org.openstreetmap.josm.Main;
[31]13import org.openstreetmap.josm.command.Command;
[23]14import org.openstreetmap.josm.command.DeleteCommand;
[31]15import org.openstreetmap.josm.command.SequenceCommand;
[7]16import org.openstreetmap.josm.data.osm.OsmPrimitive;
[31]17import org.openstreetmap.josm.data.osm.visitor.CollectBackReferencesVisitor;
[7]18import org.openstreetmap.josm.gui.MapFrame;
19
20/**
21 * An action that enables the user to delete nodes and other objects.
[31]22 *
[7]23 * The user can click on an object, which get deleted if possible. When Ctrl is
24 * pressed when releasing the button, the objects and all its references are
[31]25 * deleted. The exact definition of "all its references" are in
[7]26 * @see #deleteWithReferences(OsmPrimitive)
27 *
[86]28 * Pressing Alt will select the way instead of a segment, as usual.
[31]29 *
30 * If the user did not press Ctrl and the object has any references, the user
31 * is informed and nothing is deleted.
[7]32 *
33 * If the user enters the mapmode and any object is selected, all selected
[31]34 * objects that can be deleted will.
[7]35 *
36 * @author imi
37 */
38public class DeleteAction extends MapMode {
39
40 /**
41 * Construct a new DeleteAction. Mnemonic is the delete - key.
42 * @param mapFrame The frame this action belongs to.
43 */
44 public DeleteAction(MapFrame mapFrame) {
[68]45 super("Delete", "delete", "Delete nodes, streets or segments.", "Delete", KeyEvent.VK_DELETE, mapFrame);
[7]46 }
47
[86]48 @Override public void registerListener() {
[7]49 super.registerListener();
[16]50 mv.addMouseListener(this);
[7]51 }
52
[86]53 @Override public void unregisterListener() {
[7]54 super.unregisterListener();
[16]55 mv.removeMouseListener(this);
[7]56 }
57
[30]58
[86]59 @Override public void actionPerformed(ActionEvent e) {
[30]60 super.actionPerformed(e);
61 boolean ctrl = (e.getModifiers() & ActionEvent.CTRL_MASK) != 0;
[31]62 if (ctrl)
[86]63 deleteWithReferences(Main.ds.getSelected());
[31]64 else
[86]65 delete(Main.ds.getSelected(), false);
[30]66 mv.repaint();
67 }
68
[7]69 /**
70 * If user clicked with the left button, delete the nearest object.
71 * position.
72 */
[86]73 @Override public void mouseClicked(MouseEvent e) {
[7]74 if (e.getButton() != MouseEvent.BUTTON1)
75 return;
76
[16]77 OsmPrimitive sel = mv.getNearest(e.getPoint(), (e.getModifiersEx() & MouseEvent.ALT_DOWN_MASK) != 0);
[7]78 if (sel == null)
79 return;
80
81 if ((e.getModifiersEx() & MouseEvent.CTRL_DOWN_MASK) != 0)
[31]82 deleteWithReferences(Collections.singleton(sel));
[7]83 else
[31]84 delete(Collections.singleton(sel), true);
[7]85
[16]86 mv.repaint();
[7]87 }
88
89 /**
[31]90 * Delete the primitives and everything they references.
[7]91 *
[86]92 * If a node is deleted, the node and all segments, ways and areas
[31]93 * the node is part of are deleted as well.
[30]94 *
[86]95 * If a segment is deleted, all ways the segment is part of
[31]96 * are deleted as well. No nodes are deleted.
97 *
[86]98 * If a way is deleted, only the way and no segments or nodes are
[31]99 * deleted.
100 *
101 * If an area is deleted, only the area gets deleted.
102 *
103 * @param selection The list of all object to be deleted.
[7]104 */
[31]105 private void deleteWithReferences(Collection<OsmPrimitive> selection) {
106 Collection<Command> deleteCommands = new LinkedList<Command>();
107 for (OsmPrimitive osm : selection)
[86]108 deleteCommands.add(new DeleteCommand(osm));
[32]109 if (!deleteCommands.isEmpty())
110 mv.editLayer().add(new SequenceCommand(deleteCommands));
[7]111 }
112
113 /**
[31]114 * Try to delete all given primitives. If a primitive is
115 * used somewhere and that "somewhere" is not going to be deleted,
116 * inform the user and do not delete.
[7]117 *
[31]118 * @param selection The objects to delete.
[30]119 * @param msgBox Whether a message box for errors should be shown
[7]120 */
[31]121 private void delete(Collection<OsmPrimitive> selection, boolean msgBox) {
122 Collection<Command> deleteCommands = new LinkedList<Command>();
123 for (OsmPrimitive osm : selection) {
[86]124 CollectBackReferencesVisitor v = new CollectBackReferencesVisitor(Main.ds);
[31]125 osm.visit(v);
126 if (!selection.containsAll(v.data)) {
127 if (msgBox)
128 JOptionPane.showMessageDialog(Main.main, "This object is in use.");
129 } else
[86]130 deleteCommands.add(new DeleteCommand(osm));
[7]131 }
[32]132 if (!deleteCommands.isEmpty())
133 mv.editLayer().add(new SequenceCommand(deleteCommands));
[7]134 }
135}
Note: See TracBrowser for help on using the repository browser.