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

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