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

Last change on this file since 1150 was 1150, checked in by stoecker, 15 years ago

clear selection after delete

  • Property svn:eol-style set to native
File size: 4.0 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.actions.mapmode;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.event.ActionEvent;
7import java.awt.event.KeyEvent;
8import java.awt.event.MouseEvent;
9import java.util.Collections;
10
11import org.openstreetmap.josm.Main;
12import org.openstreetmap.josm.command.Command;
13import org.openstreetmap.josm.command.DeleteCommand;
14import org.openstreetmap.josm.data.osm.OsmPrimitive;
15import org.openstreetmap.josm.data.osm.WaySegment;
16import org.openstreetmap.josm.gui.MapFrame;
17import org.openstreetmap.josm.tools.ImageProvider;
18import org.openstreetmap.josm.tools.Shortcut;
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 gets 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 * {@link #deleteWithReferences deleteWithReferences}.
27 *
28 * If the user did not press Ctrl and the object has any references, the user
29 * is informed and nothing is deleted.
30 *
31 * If the user enters the mapmode and any object is selected, all selected
32 * objects that can be deleted will.
33 *
34 * @author imi
35 */
36public class DeleteAction extends MapMode {
37
38 /**
39 * Construct a new DeleteAction. Mnemonic is the delete - key.
40 * @param mapFrame The frame this action belongs to.
41 */
42 public DeleteAction(MapFrame mapFrame) {
43 super(tr("Delete Mode"),
44 "delete",
45 tr("Delete nodes or ways."),
46 Shortcut.registerShortcut("mapmode:delete", tr("Mode: {0}",tr("Delete")), KeyEvent.VK_D, Shortcut.GROUP_EDIT),
47 mapFrame,
48 ImageProvider.getCursor("normal", "delete"));
49 }
50
51 @Override public void enterMode() {
52 super.enterMode();
53 Main.map.mapView.addMouseListener(this);
54 }
55
56 @Override public void exitMode() {
57 super.exitMode();
58 Main.map.mapView.removeMouseListener(this);
59 }
60
61
62 @Override public void actionPerformed(ActionEvent e) {
63 super.actionPerformed(e);
64 if(!Main.map.mapView.isDrawableLayer())
65 return;
66 doActionPerformed(e);
67 }
68
69 public void doActionPerformed(ActionEvent e) {
70 if(!Main.map.mapView.isDrawableLayer())
71 return;
72 boolean ctrl = (e.getModifiers() & ActionEvent.CTRL_MASK) != 0;
73 boolean alt = (e.getModifiers() & ActionEvent.ALT_MASK) != 0;
74
75 Command c;
76 if (ctrl) {
77 c = DeleteCommand.deleteWithReferences(Main.ds.getSelected());
78 } else {
79 c = DeleteCommand.delete(Main.ds.getSelected(), !alt);
80 }
81 if (c != null) {
82 Main.main.undoRedo.add(c);
83 }
84
85 Main.ds.setSelected();
86 Main.map.repaint();
87 }
88
89 /**
90 * If user clicked with the left button, delete the nearest object.
91 * position.
92 */
93 @Override public void mouseClicked(MouseEvent e) {
94 if (e.getButton() != MouseEvent.BUTTON1)
95 return;
96 if(!Main.map.mapView.isDrawableLayer())
97 return;
98 boolean ctrl = (e.getModifiers() & ActionEvent.CTRL_MASK) != 0;
99 boolean shift = (e.getModifiers() & ActionEvent.SHIFT_MASK) != 0;
100 boolean alt = (e.getModifiers() & ActionEvent.ALT_MASK) != 0;
101
102 OsmPrimitive sel = Main.map.mapView.getNearestNode(e.getPoint());
103 Command c = null;
104 if (sel == null) {
105 WaySegment ws = Main.map.mapView.getNearestWaySegment(e.getPoint());
106 if (ws != null) {
107 if (shift) {
108 c = DeleteCommand.deleteWaySegment(ws);
109 } else if (ctrl) {
110 c = DeleteCommand.deleteWithReferences(Collections.singleton((OsmPrimitive)ws.way));
111 } else {
112 c = DeleteCommand.delete(Collections.singleton((OsmPrimitive)ws.way), !alt);
113 }
114 }
115 } else if (ctrl) {
116 c = DeleteCommand.deleteWithReferences(Collections.singleton(sel));
117 } else {
118 c = DeleteCommand.delete(Collections.singleton(sel), !alt);
119 }
120 if (c != null) {
121 Main.main.undoRedo.add(c);
122 }
123
124 Main.ds.setSelected();
125 Main.map.mapView.repaint();
126 }
127
128 @Override public String getModeHelpText() {
129 return tr("Click to delete. Shift: delete way segment. Alt: don't delete unused nodes when deleting a way. Ctrl: delete referring objects.");
130 }
131}
Note: See TracBrowser for help on using the repository browser.