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.ArrayList;
|
---|
10 | import java.util.Collection;
|
---|
11 | import java.util.Collections;
|
---|
12 | import java.util.HashSet;
|
---|
13 | import java.util.HashMap;
|
---|
14 | import java.util.LinkedList;
|
---|
15 | import java.util.List;
|
---|
16 |
|
---|
17 | import javax.swing.JOptionPane;
|
---|
18 |
|
---|
19 | import org.openstreetmap.josm.Main;
|
---|
20 | import org.openstreetmap.josm.command.*;
|
---|
21 | import org.openstreetmap.josm.data.osm.Node;
|
---|
22 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
23 | import org.openstreetmap.josm.data.osm.Way;
|
---|
24 | import org.openstreetmap.josm.data.osm.WaySegment;
|
---|
25 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
26 | import org.openstreetmap.josm.data.osm.visitor.CollectBackReferencesVisitor;
|
---|
27 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
28 | import org.openstreetmap.josm.tools.*;
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * An action that enables the user to delete nodes and other objects.
|
---|
32 | *
|
---|
33 | * The user can click on an object, which get deleted if possible. When Ctrl is
|
---|
34 | * pressed when releasing the button, the objects and all its references are
|
---|
35 | * deleted. The exact definition of "all its references" are in
|
---|
36 | * @see #deleteWithReferences(OsmPrimitive)
|
---|
37 | *
|
---|
38 | * If the user did not press Ctrl and the object has any references, the user
|
---|
39 | * is informed and nothing is deleted.
|
---|
40 | *
|
---|
41 | * If the user enters the mapmode and any object is selected, all selected
|
---|
42 | * objects that can be deleted will.
|
---|
43 | *
|
---|
44 | * @author imi
|
---|
45 | */
|
---|
46 | public class DeleteAction extends MapMode {
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Construct a new DeleteAction. Mnemonic is the delete - key.
|
---|
50 | * @param mapFrame The frame this action belongs to.
|
---|
51 | */
|
---|
52 | public DeleteAction(MapFrame mapFrame) {
|
---|
53 | super(tr("Delete"),
|
---|
54 | "delete",
|
---|
55 | tr("Delete nodes or ways."),
|
---|
56 | KeyEvent.VK_D,
|
---|
57 | mapFrame,
|
---|
58 | ImageProvider.getCursor("normal", "delete"));
|
---|
59 | }
|
---|
60 |
|
---|
61 | @Override public void enterMode() {
|
---|
62 | super.enterMode();
|
---|
63 | Main.map.mapView.addMouseListener(this);
|
---|
64 | }
|
---|
65 |
|
---|
66 | @Override public void exitMode() {
|
---|
67 | super.exitMode();
|
---|
68 | Main.map.mapView.removeMouseListener(this);
|
---|
69 | }
|
---|
70 |
|
---|
71 |
|
---|
72 | @Override public void actionPerformed(ActionEvent e) {
|
---|
73 | super.actionPerformed(e);
|
---|
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 = deleteWithReferences(Main.ds.getSelected());
|
---|
80 | } else {
|
---|
81 | c = delete(Main.ds.getSelected(), !alt);
|
---|
82 | }
|
---|
83 | if (c != null) {
|
---|
84 | Main.main.undoRedo.add(c);
|
---|
85 | }
|
---|
86 |
|
---|
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 | boolean ctrl = (e.getModifiers() & ActionEvent.CTRL_MASK) != 0;
|
---|
98 | boolean shift = (e.getModifiers() & ActionEvent.SHIFT_MASK) != 0;
|
---|
99 | boolean alt = (e.getModifiers() & ActionEvent.ALT_MASK) != 0;
|
---|
100 |
|
---|
101 | OsmPrimitive sel = Main.map.mapView.getNearestNode(e.getPoint());
|
---|
102 | Command c = null;
|
---|
103 | if (sel == null) {
|
---|
104 | WaySegment ws = Main.map.mapView.getNearestWaySegment(e.getPoint());
|
---|
105 | if (ws != null) {
|
---|
106 | if (shift) {
|
---|
107 | c = deleteWaySegment(ws);
|
---|
108 | } else if (ctrl) {
|
---|
109 | c = deleteWithReferences(Collections.singleton((OsmPrimitive)ws.way));
|
---|
110 | } else {
|
---|
111 | c = delete(Collections.singleton((OsmPrimitive)ws.way), !alt);
|
---|
112 | }
|
---|
113 | }
|
---|
114 | } else if (ctrl) {
|
---|
115 | c = deleteWithReferences(Collections.singleton(sel));
|
---|
116 | } else {
|
---|
117 | c = delete(Collections.singleton(sel), !alt);
|
---|
118 | }
|
---|
119 | if (c != null) {
|
---|
120 | Main.main.undoRedo.add(c);
|
---|
121 | }
|
---|
122 |
|
---|
123 | Main.map.mapView.repaint();
|
---|
124 | }
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * Delete the primitives and everything they reference.
|
---|
128 | *
|
---|
129 | * If a node is deleted, the node and all ways and relations
|
---|
130 | * the node is part of are deleted as well.
|
---|
131 | *
|
---|
132 | * If a way is deleted, all relations the way is member of are also deleted.
|
---|
133 | *
|
---|
134 | * If a way is deleted, only the way and no nodes are deleted.
|
---|
135 | *
|
---|
136 | * @param selection The list of all object to be deleted.
|
---|
137 | * @return command A command to perform the deletions, or null of there is
|
---|
138 | * nothing to delete.
|
---|
139 | */
|
---|
140 | private Command deleteWithReferences(Collection<OsmPrimitive> selection) {
|
---|
141 | CollectBackReferencesVisitor v = new CollectBackReferencesVisitor(Main.ds);
|
---|
142 | for (OsmPrimitive osm : selection)
|
---|
143 | osm.visit(v);
|
---|
144 | v.data.addAll(selection);
|
---|
145 | if (v.data.isEmpty()) {
|
---|
146 | return null;
|
---|
147 | } else {
|
---|
148 | return new DeleteCommand(v.data);
|
---|
149 | }
|
---|
150 | }
|
---|
151 |
|
---|
152 | /**
|
---|
153 | * Try to delete all given primitives.
|
---|
154 | *
|
---|
155 | * If a node is used by a way, it's removed from that way. If a node or a
|
---|
156 | * way is used by a relation, inform the user and do not delete.
|
---|
157 | *
|
---|
158 | * If this would cause ways with less than 2 nodes to be created, delete
|
---|
159 | * these ways instead. If they are part of a relation, inform the user
|
---|
160 | * and do not delete.
|
---|
161 | *
|
---|
162 | * @param selection The objects to delete.
|
---|
163 | * @param alsoDeleteNodesInWay true if nodes should be deleted as well
|
---|
164 | * @return command A command to perform the deletions, or null of there is
|
---|
165 | * nothing to delete.
|
---|
166 | */
|
---|
167 | private Command delete(Collection<OsmPrimitive> selection, boolean alsoDeleteNodesInWay) {
|
---|
168 | if (selection.isEmpty()) return null;
|
---|
169 |
|
---|
170 | Collection<OsmPrimitive> del = new HashSet<OsmPrimitive>(selection);
|
---|
171 | Collection<Way> waysToBeChanged = new HashSet<Way>();
|
---|
172 |
|
---|
173 | if (alsoDeleteNodesInWay) {
|
---|
174 | // Delete untagged nodes that are to be unreferenced.
|
---|
175 | Collection<OsmPrimitive> delNodes = new HashSet<OsmPrimitive>();
|
---|
176 | for (OsmPrimitive osm : del) {
|
---|
177 | if (osm instanceof Way) {
|
---|
178 | for (Node n : ((Way)osm).nodes) {
|
---|
179 | if (!n.tagged) {
|
---|
180 | CollectBackReferencesVisitor v = new CollectBackReferencesVisitor(Main.ds, false);
|
---|
181 | n.visit(v);
|
---|
182 | v.data.removeAll(del);
|
---|
183 | if (v.data.isEmpty()) {
|
---|
184 | delNodes.add(n);
|
---|
185 | }
|
---|
186 | }
|
---|
187 | }
|
---|
188 | }
|
---|
189 | }
|
---|
190 | del.addAll(delNodes);
|
---|
191 | }
|
---|
192 |
|
---|
193 | for (OsmPrimitive osm : del) {
|
---|
194 | CollectBackReferencesVisitor v = new CollectBackReferencesVisitor(Main.ds, false);
|
---|
195 | osm.visit(v);
|
---|
196 | for (OsmPrimitive ref : v.data) {
|
---|
197 | if (del.contains(ref)) continue;
|
---|
198 | if (ref instanceof Way) {
|
---|
199 | waysToBeChanged.add((Way) ref);
|
---|
200 | } else if (ref instanceof Relation) {
|
---|
201 | JOptionPane.showMessageDialog(Main.parent,
|
---|
202 | tr("Cannot delete: Selection is used by relation"));
|
---|
203 | return null;
|
---|
204 | } else {
|
---|
205 | return null;
|
---|
206 | }
|
---|
207 | }
|
---|
208 | }
|
---|
209 |
|
---|
210 | Collection<Command> cmds = new LinkedList<Command>();
|
---|
211 | for (Way w : waysToBeChanged) {
|
---|
212 | Way wnew = new Way(w);
|
---|
213 | wnew.nodes.removeAll(del);
|
---|
214 | if (wnew.nodes.size() < 2) {
|
---|
215 | del.add(w);
|
---|
216 |
|
---|
217 | CollectBackReferencesVisitor v = new CollectBackReferencesVisitor(Main.ds, false);
|
---|
218 | w.visit(v);
|
---|
219 | for (OsmPrimitive ref : v.data) {
|
---|
220 | if (del.contains(ref)) continue;
|
---|
221 | if (ref instanceof Relation) {
|
---|
222 | JOptionPane.showMessageDialog(Main.parent,
|
---|
223 | tr("Cannot delete: Selection is used by relation"));
|
---|
224 | } else {
|
---|
225 | return null;
|
---|
226 | }
|
---|
227 | }
|
---|
228 | } else {
|
---|
229 | cmds.add(new ChangeCommand(w, wnew));
|
---|
230 | }
|
---|
231 | }
|
---|
232 |
|
---|
233 | if (!del.isEmpty()) cmds.add(new DeleteCommand(del));
|
---|
234 |
|
---|
235 | return new SequenceCommand(tr("Delete"), cmds);
|
---|
236 | }
|
---|
237 |
|
---|
238 | private Command deleteWaySegment(WaySegment ws) {
|
---|
239 | List<Node> n1 = new ArrayList<Node>(),
|
---|
240 | n2 = new ArrayList<Node>();
|
---|
241 |
|
---|
242 | n1.addAll(ws.way.nodes.subList(0, ws.lowerIndex + 1));
|
---|
243 | n2.addAll(ws.way.nodes.subList(ws.lowerIndex + 1, ws.way.nodes.size()));
|
---|
244 |
|
---|
245 | if (n1.size() < 2 && n2.size() < 2) {
|
---|
246 | return new DeleteCommand(Collections.singleton(ws.way));
|
---|
247 | }
|
---|
248 |
|
---|
249 | Way wnew = new Way(ws.way);
|
---|
250 | wnew.nodes.clear();
|
---|
251 |
|
---|
252 | if (n1.size() < 2) {
|
---|
253 | wnew.nodes.addAll(n2);
|
---|
254 | return new ChangeCommand(ws.way, wnew);
|
---|
255 | } else if (n2.size() < 2) {
|
---|
256 | wnew.nodes.addAll(n1);
|
---|
257 | return new ChangeCommand(ws.way, wnew);
|
---|
258 | } else {
|
---|
259 | Collection<Command> cmds = new LinkedList<Command>();
|
---|
260 |
|
---|
261 | wnew.nodes.addAll(n1);
|
---|
262 | cmds.add(new ChangeCommand(ws.way, wnew));
|
---|
263 |
|
---|
264 | Way wnew2 = new Way();
|
---|
265 | if (wnew.keys != null) {
|
---|
266 | wnew2.keys = new HashMap<String, String>(wnew.keys);
|
---|
267 | wnew2.checkTagged();
|
---|
268 | }
|
---|
269 | wnew2.nodes.addAll(n2);
|
---|
270 | cmds.add(new AddCommand(wnew2));
|
---|
271 |
|
---|
272 | return new SequenceCommand(tr("Split way segment"), cmds);
|
---|
273 | }
|
---|
274 | }
|
---|
275 |
|
---|
276 | @Override public String getModeHelpText() {
|
---|
277 | return tr("Click to delete. Shift: delete way segment. Alt: don't delete unused nodes when deleting a way. Ctrl: delete referring objects.");
|
---|
278 | }
|
---|
279 | }
|
---|