1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 |
|
---|
7 | import java.awt.event.ActionEvent;
|
---|
8 | import java.awt.event.KeyEvent;
|
---|
9 | import java.util.ArrayList;
|
---|
10 | import java.util.Collection;
|
---|
11 | import java.util.Collections;
|
---|
12 | import java.util.HashSet;
|
---|
13 | import java.util.LinkedList;
|
---|
14 | import java.util.List;
|
---|
15 |
|
---|
16 | import javax.swing.JOptionPane;
|
---|
17 | import javax.swing.JPanel;
|
---|
18 |
|
---|
19 | import org.openstreetmap.josm.Main;
|
---|
20 | import org.openstreetmap.josm.command.AddCommand;
|
---|
21 | import org.openstreetmap.josm.command.ChangeCommand;
|
---|
22 | import org.openstreetmap.josm.command.Command;
|
---|
23 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
24 | import org.openstreetmap.josm.data.osm.Node;
|
---|
25 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
26 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
27 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
28 | import org.openstreetmap.josm.data.osm.Way;
|
---|
29 | import org.openstreetmap.josm.gui.MapView;
|
---|
30 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Duplicate nodes that are used by multiple ways.
|
---|
34 | *
|
---|
35 | * Resulting nodes are identical, up to their position.
|
---|
36 | *
|
---|
37 | * This is the opposite of the MergeNodesAction.
|
---|
38 | *
|
---|
39 | * If a single node is selected, it will copy that node and remove all tags from the old one
|
---|
40 | */
|
---|
41 |
|
---|
42 | public class UnGlueAction extends JosmAction {
|
---|
43 |
|
---|
44 | private Node selectedNode;
|
---|
45 | private Way selectedWay;
|
---|
46 | private ArrayList<Node> selectedNodes;
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Create a new UnGlueAction.
|
---|
50 | */
|
---|
51 | public UnGlueAction() {
|
---|
52 | super(tr("UnGlue Ways"), "unglueways", tr("Duplicate nodes that are used by multiple ways."),
|
---|
53 | Shortcut.registerShortcut("tools:unglue", tr("Tool: {0}", tr("UnGlue Ways")), KeyEvent.VK_G, Shortcut.GROUP_EDIT), true);
|
---|
54 | putValue("help", ht("/Action/UnGlue"));
|
---|
55 | }
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * Called when the action is executed.
|
---|
59 | *
|
---|
60 | * This method does some checking on the selection and calls the matching unGlueWay method.
|
---|
61 | */
|
---|
62 | public void actionPerformed(ActionEvent e) {
|
---|
63 |
|
---|
64 | Collection<OsmPrimitive> selection = getCurrentDataSet().getSelected();
|
---|
65 |
|
---|
66 | String errMsg = null;
|
---|
67 | if (checkSelection(selection)) {
|
---|
68 | int count = 0;
|
---|
69 | for (Way w : OsmPrimitive.getFilteredList(selectedNode.getReferrers(), Way.class)) {
|
---|
70 | if (!w.isUsable() || w.getNodesCount() < 1) {
|
---|
71 | continue;
|
---|
72 | }
|
---|
73 | count++;
|
---|
74 | }
|
---|
75 | if (count < 2) {
|
---|
76 | // If there aren't enough ways, maybe the user wanted to unglue the nodes
|
---|
77 | // (= copy tags to a new node)
|
---|
78 | if (checkForUnglueNode(selection)) {
|
---|
79 | unglueNode(e);
|
---|
80 | } else {
|
---|
81 | errMsg = tr("This node is not glued to anything else.");
|
---|
82 | }
|
---|
83 | } else {
|
---|
84 | // and then do the work.
|
---|
85 | unglueWays();
|
---|
86 | }
|
---|
87 | } else if (checkSelection2(selection)) {
|
---|
88 | ArrayList<Node> tmpNodes = new ArrayList<Node>();
|
---|
89 | for (Node n : selectedNodes) {
|
---|
90 | int count = 0;
|
---|
91 | for (Way w : OsmPrimitive.getFilteredList(n.getReferrers(), Way.class)) {
|
---|
92 | if (w.isDeleted() || w.incomplete) {
|
---|
93 | continue;
|
---|
94 | }
|
---|
95 | count++;
|
---|
96 | }
|
---|
97 | if (count >= 2) {
|
---|
98 | tmpNodes.add(n);
|
---|
99 | }
|
---|
100 | }
|
---|
101 | if (tmpNodes.size() < 1) {
|
---|
102 | if (selection.size() > 1) {
|
---|
103 | errMsg = tr("None of these nodes are glued to anything else.");
|
---|
104 | } else {
|
---|
105 | errMsg = tr("None of this way's nodes are glued to anything else.");
|
---|
106 | }
|
---|
107 | } else {
|
---|
108 | // and then do the work.
|
---|
109 | selectedNodes = tmpNodes;
|
---|
110 | unglueWays2();
|
---|
111 | }
|
---|
112 | } else {
|
---|
113 | errMsg =
|
---|
114 | tr("The current selection cannot be used for unglueing.")+"\n"+
|
---|
115 | "\n"+
|
---|
116 | tr("Select either:")+"\n"+
|
---|
117 | tr("* One tagged node, or")+"\n"+
|
---|
118 | tr("* One node that is used by more than one way, or")+"\n"+
|
---|
119 | tr("* One node that is used by more than one way and one of those ways, or")+"\n"+
|
---|
120 | tr("* One way that has one or more nodes that are used by more than one way, or")+"\n"+
|
---|
121 | tr("* One way and one or more of its nodes that are used by more than one way.")+"\n"+
|
---|
122 | "\n"+
|
---|
123 | tr("Note: If a way is selected, this way will get fresh copies of the unglued\n"+
|
---|
124 | "nodes and the new nodes will be selected. Otherwise, all ways will get their\n"+
|
---|
125 | "own copy and all nodes will be selected.");
|
---|
126 | }
|
---|
127 |
|
---|
128 | if(errMsg != null) {
|
---|
129 | JOptionPane.showMessageDialog(
|
---|
130 | Main.parent,
|
---|
131 | errMsg,
|
---|
132 | tr("Error"),
|
---|
133 | JOptionPane.ERROR_MESSAGE);
|
---|
134 | }
|
---|
135 |
|
---|
136 | selectedNode = null;
|
---|
137 | selectedWay = null;
|
---|
138 | selectedNodes = null;
|
---|
139 | }
|
---|
140 |
|
---|
141 | /**
|
---|
142 | * Assumes there is one tagged Node stored in selectedNode that it will try to unglue
|
---|
143 | * (= copy node and remove all tags from the old one. Relations will not be removed)
|
---|
144 | */
|
---|
145 | private void unglueNode(ActionEvent e) {
|
---|
146 | LinkedList<Command> cmds = new LinkedList<Command>();
|
---|
147 |
|
---|
148 | Node c = new Node(selectedNode);
|
---|
149 | c.removeAll();
|
---|
150 | getCurrentDataSet().clearSelection(c);
|
---|
151 | cmds.add(new ChangeCommand(selectedNode, c));
|
---|
152 |
|
---|
153 | Node n = new Node(selectedNode, true);
|
---|
154 |
|
---|
155 | // If this wasn't called from menu, place it where the cursor is/was
|
---|
156 | if(e.getSource() instanceof JPanel) {
|
---|
157 | MapView mv = Main.map.mapView;
|
---|
158 | n.setCoor(mv.getLatLon(mv.lastMEvent.getX(), mv.lastMEvent.getY()));
|
---|
159 | }
|
---|
160 |
|
---|
161 | cmds.add(new AddCommand(n));
|
---|
162 |
|
---|
163 | fixRelations(selectedNode, cmds, Collections.singletonList(n));
|
---|
164 |
|
---|
165 | Main.main.undoRedo.add(new SequenceCommand(tr("Unglued Node"), cmds));
|
---|
166 | getCurrentDataSet().setSelected(n);
|
---|
167 | Main.map.mapView.repaint();
|
---|
168 | }
|
---|
169 |
|
---|
170 | /**
|
---|
171 | * Checks if selection is suitable for ungluing. This is the case when there's a single,
|
---|
172 | * tagged node selected that's part of at least one way (ungluing an unconnected node does
|
---|
173 | * not make sense. Due to the call order in actionPerformed, this is only called when the
|
---|
174 | * node is only part of one or less ways.
|
---|
175 | *
|
---|
176 | * @param The selection to check against
|
---|
177 | * @return Selection is suitable
|
---|
178 | */
|
---|
179 | private boolean checkForUnglueNode(Collection<? extends OsmPrimitive> selection) {
|
---|
180 | if (selection.size() != 1)
|
---|
181 | return false;
|
---|
182 | OsmPrimitive n = (OsmPrimitive) selection.toArray()[0];
|
---|
183 | if (!(n instanceof Node))
|
---|
184 | return false;
|
---|
185 | if (OsmPrimitive.getFilteredList(n.getReferrers(), Way.class).isEmpty())
|
---|
186 | return false;
|
---|
187 |
|
---|
188 | selectedNode = (Node) n;
|
---|
189 | return selectedNode.isTagged();
|
---|
190 | }
|
---|
191 |
|
---|
192 | /**
|
---|
193 | * Checks if the selection consists of something we can work with.
|
---|
194 | * Checks only if the number and type of items selected looks good;
|
---|
195 | * does not check whether the selected items are really a valid
|
---|
196 | * input for splitting (this would be too expensive to be carried
|
---|
197 | * out from the selectionChanged listener).
|
---|
198 | *
|
---|
199 | * If this method returns "true", selectedNode and selectedWay will
|
---|
200 | * be set.
|
---|
201 | *
|
---|
202 | * Returns true if either one node is selected or one node and one
|
---|
203 | * way are selected and the node is part of the way.
|
---|
204 | *
|
---|
205 | * The way will be put into the object variable "selectedWay", the
|
---|
206 | * node into "selectedNode".
|
---|
207 | */
|
---|
208 | private boolean checkSelection(Collection<? extends OsmPrimitive> selection) {
|
---|
209 |
|
---|
210 | int size = selection.size();
|
---|
211 | if (size < 1 || size > 2)
|
---|
212 | return false;
|
---|
213 |
|
---|
214 | selectedNode = null;
|
---|
215 | selectedWay = null;
|
---|
216 |
|
---|
217 | for (OsmPrimitive p : selection) {
|
---|
218 | if (p instanceof Node) {
|
---|
219 | selectedNode = (Node) p;
|
---|
220 | if (size == 1 || selectedWay != null)
|
---|
221 | return size == 1 || selectedWay.containsNode(selectedNode);
|
---|
222 | } else if (p instanceof Way) {
|
---|
223 | selectedWay = (Way) p;
|
---|
224 | if (size == 2 && selectedNode != null)
|
---|
225 | return selectedWay.containsNode(selectedNode);
|
---|
226 | }
|
---|
227 | }
|
---|
228 |
|
---|
229 | return false;
|
---|
230 | }
|
---|
231 |
|
---|
232 | /**
|
---|
233 | * Checks if the selection consists of something we can work with.
|
---|
234 | * Checks only if the number and type of items selected looks good;
|
---|
235 | * does not check whether the selected items are really a valid
|
---|
236 | * input for splitting (this would be too expensive to be carried
|
---|
237 | * out from the selectionChanged listener).
|
---|
238 | *
|
---|
239 | * Returns true if one way and any number of nodes that are part of
|
---|
240 | * that way are selected. Note: "any" can be none, then all nodes of
|
---|
241 | * the way are used.
|
---|
242 | *
|
---|
243 | * The way will be put into the object variable "selectedWay", the
|
---|
244 | * nodes into "selectedNodes".
|
---|
245 | */
|
---|
246 | private boolean checkSelection2(Collection<? extends OsmPrimitive> selection) {
|
---|
247 | if (selection.size() < 1)
|
---|
248 | return false;
|
---|
249 |
|
---|
250 | selectedWay = null;
|
---|
251 | for (OsmPrimitive p : selection) {
|
---|
252 | if (p instanceof Way) {
|
---|
253 | if (selectedWay != null)
|
---|
254 | return false;
|
---|
255 | selectedWay = (Way) p;
|
---|
256 | }
|
---|
257 | }
|
---|
258 | if (selectedWay == null)
|
---|
259 | return false;
|
---|
260 |
|
---|
261 | selectedNodes = new ArrayList<Node>();
|
---|
262 | for (OsmPrimitive p : selection) {
|
---|
263 | if (p instanceof Node) {
|
---|
264 | Node n = (Node) p;
|
---|
265 | if (!selectedWay.containsNode(n))
|
---|
266 | return false;
|
---|
267 | selectedNodes.add(n);
|
---|
268 | }
|
---|
269 | }
|
---|
270 |
|
---|
271 | if (selectedNodes.size() < 1) {
|
---|
272 | selectedNodes.addAll(selectedWay.getNodes());
|
---|
273 | }
|
---|
274 |
|
---|
275 | return true;
|
---|
276 | }
|
---|
277 |
|
---|
278 | /**
|
---|
279 | * dupe the given node of the given way
|
---|
280 | *
|
---|
281 | * -> the new node will be put into the parameter newNodes.
|
---|
282 | * -> the add-node command will be put into the parameter cmds.
|
---|
283 | * -> the changed way will be returned and must be put into cmds by the caller!
|
---|
284 | */
|
---|
285 | private Way modifyWay(Node originalNode, Way w, List<Command> cmds, List<Node> newNodes) {
|
---|
286 | ArrayList<Node> nn = new ArrayList<Node>();
|
---|
287 | for (Node pushNode : w.getNodes()) {
|
---|
288 | if (originalNode == pushNode) {
|
---|
289 | // clone the node for all other ways
|
---|
290 | pushNode = new Node(pushNode, true);
|
---|
291 | newNodes.add(pushNode);
|
---|
292 | cmds.add(new AddCommand(pushNode));
|
---|
293 | }
|
---|
294 | nn.add(pushNode);
|
---|
295 | }
|
---|
296 | Way newWay = new Way(w);
|
---|
297 | newWay.setNodes(nn);
|
---|
298 |
|
---|
299 | return newWay;
|
---|
300 | }
|
---|
301 |
|
---|
302 | /**
|
---|
303 | * put all newNodes into the same relation(s) that originalNode is in
|
---|
304 | */
|
---|
305 | private void fixRelations(Node originalNode, List<Command> cmds, List<Node> newNodes) {
|
---|
306 | // modify all relations containing the node
|
---|
307 | Relation newRel = null;
|
---|
308 | HashSet<String> rolesToReAdd = null;
|
---|
309 | for (Relation r : OsmPrimitive.getFilteredList(originalNode.getReferrers(), Relation.class)) {
|
---|
310 | if (r.isDeleted() || r.incomplete) {
|
---|
311 | continue;
|
---|
312 | }
|
---|
313 | newRel = null;
|
---|
314 | rolesToReAdd = null;
|
---|
315 | for (RelationMember rm : r.getMembers()) {
|
---|
316 | if (rm.isNode()) {
|
---|
317 | if (rm.getMember() == originalNode) {
|
---|
318 | if (newRel == null) {
|
---|
319 | newRel = new Relation(r);
|
---|
320 | newRel.setMembers(null);
|
---|
321 | rolesToReAdd = new HashSet<String>();
|
---|
322 | }
|
---|
323 | rolesToReAdd.add(rm.getRole());
|
---|
324 | }
|
---|
325 | }
|
---|
326 | }
|
---|
327 | if (newRel != null) {
|
---|
328 | for (RelationMember rm : r.getMembers()) {
|
---|
329 | newRel.addMember(rm);
|
---|
330 | }
|
---|
331 | for (Node n : newNodes) {
|
---|
332 | for (String role : rolesToReAdd) {
|
---|
333 | newRel.addMember(new RelationMember(role, n));
|
---|
334 | }
|
---|
335 | }
|
---|
336 | cmds.add(new ChangeCommand(r, newRel));
|
---|
337 | }
|
---|
338 | }
|
---|
339 | }
|
---|
340 |
|
---|
341 | /**
|
---|
342 | * dupe a single node into as many nodes as there are ways using it, OR
|
---|
343 | *
|
---|
344 | * dupe a single node once, and put the copy on the selected way
|
---|
345 | */
|
---|
346 | private void unglueWays() {
|
---|
347 | LinkedList<Command> cmds = new LinkedList<Command>();
|
---|
348 | List<Node> newNodes = new LinkedList<Node>();
|
---|
349 |
|
---|
350 | if (selectedWay == null) {
|
---|
351 | boolean firstway = true;
|
---|
352 | // modify all ways containing the nodes
|
---|
353 | for (Way w : OsmPrimitive.getFilteredList(selectedNode.getReferrers(), Way.class)) {
|
---|
354 | if (w.isDeleted() || w.incomplete) {
|
---|
355 | continue;
|
---|
356 | }
|
---|
357 | if (!firstway) {
|
---|
358 | cmds.add(new ChangeCommand(w, modifyWay(selectedNode, w, cmds, newNodes)));
|
---|
359 | }
|
---|
360 | firstway = false;
|
---|
361 | }
|
---|
362 | } else {
|
---|
363 | cmds.add(new ChangeCommand(selectedWay, modifyWay(selectedNode, selectedWay, cmds, newNodes)));
|
---|
364 | }
|
---|
365 |
|
---|
366 | fixRelations(selectedNode, cmds, newNodes);
|
---|
367 |
|
---|
368 | Main.main.undoRedo.add(new SequenceCommand(tr("Dupe into {0} nodes", newNodes.size()+1), cmds));
|
---|
369 | if (selectedWay == null) { // if a node has been selected, new selection is ALL nodes
|
---|
370 | newNodes.add(selectedNode);
|
---|
371 | } // if a node and a way has been selected, new selection is only the new node that was added to the selected way
|
---|
372 | getCurrentDataSet().setSelected(newNodes);
|
---|
373 | }
|
---|
374 |
|
---|
375 | /**
|
---|
376 | * dupe all nodes that are selected, and put the copies on the selected way
|
---|
377 | *
|
---|
378 | */
|
---|
379 | private void unglueWays2() {
|
---|
380 | LinkedList<Command> cmds = new LinkedList<Command>();
|
---|
381 | List<Node> allNewNodes = new LinkedList<Node>();
|
---|
382 | Way tmpWay = selectedWay;
|
---|
383 |
|
---|
384 | for (Node n : selectedNodes) {
|
---|
385 | List<Node> newNodes = new LinkedList<Node>();
|
---|
386 | tmpWay = modifyWay(n, tmpWay, cmds, newNodes);
|
---|
387 | fixRelations(n, cmds, newNodes);
|
---|
388 | allNewNodes.addAll(newNodes);
|
---|
389 | }
|
---|
390 | cmds.add(new ChangeCommand(selectedWay, tmpWay)); // only one changeCommand for a way, else garbage will happen
|
---|
391 |
|
---|
392 | Main.main.undoRedo.add(new SequenceCommand(tr("Dupe {0} nodes into {1} nodes", selectedNodes.size(), selectedNodes.size()+allNewNodes.size()), cmds));
|
---|
393 | getCurrentDataSet().setSelected(allNewNodes);
|
---|
394 | }
|
---|
395 |
|
---|
396 | @Override
|
---|
397 | protected void updateEnabledState() {
|
---|
398 | if (getCurrentDataSet() == null) {
|
---|
399 | setEnabled(false);
|
---|
400 | } else {
|
---|
401 | updateEnabledState(getCurrentDataSet().getSelected());
|
---|
402 | }
|
---|
403 | }
|
---|
404 |
|
---|
405 | @Override
|
---|
406 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
---|
407 | setEnabled(selection != null && !selection.isEmpty());
|
---|
408 | }
|
---|
409 | }
|
---|