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

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

apply #2127. patch by xeen

  • Property svn:eol-style set to native
File size: 4.7 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.gui.layer.Layer;
18import org.openstreetmap.josm.gui.layer.OsmDataLayer;
19import org.openstreetmap.josm.tools.ImageProvider;
20import org.openstreetmap.josm.tools.Shortcut;
21
22/**
23 * An action that enables the user to delete nodes and other objects.
24 *
25 * The user can click on an object, which gets deleted if possible. When Ctrl is
26 * pressed when releasing the button, the objects and all its references are
27 * deleted. The exact definition of "all its references" are in
28 * {@link #deleteWithReferences deleteWithReferences}.
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(tr("Delete Mode"),
46 "delete",
47 tr("Delete nodes or ways."),
48 Shortcut.registerShortcut("mapmode:delete", tr("Mode: {0}",tr("Delete")), KeyEvent.VK_D, Shortcut.GROUP_EDIT),
49 mapFrame,
50 ImageProvider.getCursor("normal", "delete"));
51 }
52
53 @Override public void enterMode() {
54 super.enterMode();
55 Main.map.mapView.addMouseListener(this);
56 }
57
58 @Override public void exitMode() {
59 super.exitMode();
60 Main.map.mapView.removeMouseListener(this);
61 }
62
63
64 @Override public void actionPerformed(ActionEvent e) {
65 super.actionPerformed(e);
66 if(!Main.map.mapView.isDrawableLayer())
67 return;
68 doActionPerformed(e);
69 }
70
71 public void doActionPerformed(ActionEvent e) {
72 if(!Main.map.mapView.isDrawableLayer())
73 return;
74 boolean ctrl = (e.getModifiers() & ActionEvent.CTRL_MASK) != 0;
75 boolean alt = (e.getModifiers() & ActionEvent.ALT_MASK) != 0;
76
77 Command c;
78 if (ctrl) {
79 c = DeleteCommand.deleteWithReferences(Main.ds.getSelected());
80 } else {
81 c = DeleteCommand.delete(Main.ds.getSelected(), !alt);
82 }
83 if (c != null) {
84 Main.main.undoRedo.add(c);
85 }
86
87 Main.ds.setSelected();
88 Main.map.repaint();
89 }
90
91 /**
92 * If user clicked with the left button, delete the nearest object.
93 * position.
94 */
95 @Override public void mouseClicked(MouseEvent e) {
96 if (e.getButton() != MouseEvent.BUTTON1)
97 return;
98 if(!Main.map.mapView.isDrawableLayer())
99 return;
100 boolean ctrl = (e.getModifiers() & ActionEvent.CTRL_MASK) != 0;
101 boolean shift = (e.getModifiers() & ActionEvent.SHIFT_MASK) != 0;
102 boolean alt = (e.getModifiers() & ActionEvent.ALT_MASK) != 0;
103
104 OsmPrimitive sel = Main.map.mapView.getNearestNode(e.getPoint());
105 Command c = null;
106 if (sel == null) {
107 WaySegment ws = Main.map.mapView.getNearestWaySegment(e.getPoint());
108 if (ws != null) {
109 if (shift) {
110 c = DeleteCommand.deleteWaySegment(ws);
111 } else if (ctrl) {
112 c = DeleteCommand.deleteWithReferences(Collections.singleton((OsmPrimitive)ws.way));
113 } else {
114 c = DeleteCommand.delete(Collections.singleton((OsmPrimitive)ws.way), !alt);
115 }
116 }
117 } else if (ctrl) {
118 c = DeleteCommand.deleteWithReferences(Collections.singleton(sel));
119 } else {
120 c = DeleteCommand.delete(Collections.singleton(sel), !alt);
121 }
122 if (c != null) {
123 Main.main.undoRedo.add(c);
124 }
125
126 Main.ds.setSelected();
127 Main.map.mapView.repaint();
128 }
129
130 @Override public String getModeHelpText() {
131 return tr("Click to delete. Shift: delete way segment. Alt: don't delete unused nodes when deleting a way. Ctrl: delete referring objects.");
132 }
133
134 @Override public boolean layerIsSupported(Layer l) {
135 return l instanceof OsmDataLayer;
136 }
137}
Note: See TracBrowser for help on using the repository browser.