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
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 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 public void registerListener() {
49 super.registerListener();
50 mv.addMouseListener(this);
51 }
52
53 @Override public void unregisterListener() {
54 super.unregisterListener();
55 mv.removeMouseListener(this);
56 }
57
58
59 @Override public void actionPerformed(ActionEvent e) {
60 super.actionPerformed(e);
61 boolean ctrl = (e.getModifiers() & ActionEvent.CTRL_MASK) != 0;
62 if (ctrl)
63 deleteWithReferences(Main.ds.getSelected());
64 else
65 delete(Main.ds.getSelected(), false);
66 mv.repaint();
67 }
68
69 /**
70 * If user clicked with the left button, delete the nearest object.
71 * position.
72 */
73 @Override public void mouseClicked(MouseEvent e) {
74 if (e.getButton() != MouseEvent.BUTTON1)
75 return;
76
77 OsmPrimitive sel = mv.getNearest(e.getPoint(), (e.getModifiersEx() & MouseEvent.ALT_DOWN_MASK) != 0);
78 if (sel == null)
79 return;
80
81 if ((e.getModifiersEx() & MouseEvent.CTRL_DOWN_MASK) != 0)
82 deleteWithReferences(Collections.singleton(sel));
83 else
84 delete(Collections.singleton(sel), true);
85
86 mv.repaint();
87 }
88
89 /**
90 * Delete the primitives and everything they references.
91 *
92 * If a node is deleted, the node and all segments, ways and areas
93 * the node is part of are deleted as well.
94 *
95 * If a segment is deleted, all ways the segment is part of
96 * are deleted as well. No nodes are deleted.
97 *
98 * If a way is deleted, only the way and no segments or nodes are
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.
104 */
105 private void deleteWithReferences(Collection<OsmPrimitive> selection) {
106 Collection<Command> deleteCommands = new LinkedList<Command>();
107 for (OsmPrimitive osm : selection)
108 deleteCommands.add(new DeleteCommand(osm));
109 if (!deleteCommands.isEmpty())
110 mv.editLayer().add(new SequenceCommand(deleteCommands));
111 }
112
113 /**
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.
117 *
118 * @param selection The objects to delete.
119 * @param msgBox Whether a message box for errors should be shown
120 */
121 private void delete(Collection<OsmPrimitive> selection, boolean msgBox) {
122 Collection<Command> deleteCommands = new LinkedList<Command>();
123 for (OsmPrimitive osm : selection) {
124 CollectBackReferencesVisitor v = new CollectBackReferencesVisitor(Main.ds);
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
130 deleteCommands.add(new DeleteCommand(osm));
131 }
132 if (!deleteCommands.isEmpty())
133 mv.editLayer().add(new SequenceCommand(deleteCommands));
134 }
135}
Note: See TracBrowser for help on using the repository browser.