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