1 | // License: GPL. For details, see LICENSE file.
|
---|
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 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
7 |
|
---|
8 | import java.awt.Point;
|
---|
9 | import java.awt.event.ActionEvent;
|
---|
10 | import java.awt.event.KeyEvent;
|
---|
11 | import java.util.ArrayList;
|
---|
12 | import java.util.Collection;
|
---|
13 | import java.util.Collections;
|
---|
14 | import java.util.HashMap;
|
---|
15 | import java.util.HashSet;
|
---|
16 | import java.util.List;
|
---|
17 | import java.util.Map;
|
---|
18 | import java.util.Set;
|
---|
19 | import java.util.stream.Collectors;
|
---|
20 |
|
---|
21 | import javax.swing.JOptionPane;
|
---|
22 |
|
---|
23 | import org.openstreetmap.josm.command.AddCommand;
|
---|
24 | import org.openstreetmap.josm.command.ChangeCommand;
|
---|
25 | import org.openstreetmap.josm.command.ChangeNodesCommand;
|
---|
26 | import org.openstreetmap.josm.command.Command;
|
---|
27 | import org.openstreetmap.josm.command.MoveCommand;
|
---|
28 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
29 | import org.openstreetmap.josm.data.UndoRedoHandler;
|
---|
30 | import org.openstreetmap.josm.data.osm.DefaultNameFormatter;
|
---|
31 | import org.openstreetmap.josm.data.osm.Node;
|
---|
32 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
33 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
34 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
35 | import org.openstreetmap.josm.data.osm.Way;
|
---|
36 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
37 | import org.openstreetmap.josm.gui.MapView;
|
---|
38 | import org.openstreetmap.josm.gui.Notification;
|
---|
39 | import org.openstreetmap.josm.gui.dialogs.PropertiesMembershipChoiceDialog;
|
---|
40 | import org.openstreetmap.josm.gui.dialogs.PropertiesMembershipChoiceDialog.ExistingBothNew;
|
---|
41 | import org.openstreetmap.josm.tools.Logging;
|
---|
42 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
43 | import org.openstreetmap.josm.tools.UserCancelException;
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Duplicate nodes that are used by multiple ways or tagged nodes used by a single way
|
---|
47 | * or nodes which referenced more than once by a single way.
|
---|
48 | *
|
---|
49 | * This is the opposite of the MergeNodesAction.
|
---|
50 | *
|
---|
51 | */
|
---|
52 | public class UnGlueAction extends JosmAction {
|
---|
53 |
|
---|
54 | private transient Node selectedNode;
|
---|
55 | private transient Way selectedWay;
|
---|
56 | private transient Set<Node> selectedNodes;
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Create a new UnGlueAction.
|
---|
60 | */
|
---|
61 | public UnGlueAction() {
|
---|
62 | super(tr("UnGlue Ways"), "unglueways", tr("Duplicate nodes that are used by multiple ways."),
|
---|
63 | Shortcut.registerShortcut("tools:unglue", tr("Tool: {0}", tr("UnGlue Ways")), KeyEvent.VK_G, Shortcut.DIRECT), true);
|
---|
64 | setHelpId(ht("/Action/UnGlue"));
|
---|
65 | }
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Called when the action is executed.
|
---|
69 | *
|
---|
70 | * This method does some checking on the selection and calls the matching unGlueWay method.
|
---|
71 | */
|
---|
72 | @Override
|
---|
73 | public void actionPerformed(ActionEvent e) {
|
---|
74 | try {
|
---|
75 | unglue();
|
---|
76 | } catch (UserCancelException ignore) {
|
---|
77 | Logging.trace(ignore);
|
---|
78 | } finally {
|
---|
79 | cleanup();
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | protected void unglue() throws UserCancelException {
|
---|
84 |
|
---|
85 | Collection<OsmPrimitive> selection = getLayerManager().getEditDataSet().getSelected();
|
---|
86 |
|
---|
87 | String errMsg = null;
|
---|
88 | int errorTime = Notification.TIME_DEFAULT;
|
---|
89 |
|
---|
90 | if (checkSelectionOneNodeAtMostOneWay(selection)) {
|
---|
91 | checkAndConfirmOutlyingUnglue();
|
---|
92 | List<Way> parentWays = selectedNode.getParentWays().stream().filter(Way::isUsable).collect(Collectors.toList());
|
---|
93 |
|
---|
94 | if (parentWays.size() < 2) {
|
---|
95 | if (!parentWays.isEmpty()) {
|
---|
96 | // single way
|
---|
97 | Way way = selectedWay == null ? parentWays.get(0) : selectedWay;
|
---|
98 | boolean closedOrSelfCrossing = way.getNodes().stream().filter(n -> n == selectedNode).count() > 1;
|
---|
99 |
|
---|
100 | final PropertiesMembershipChoiceDialog dialog = PropertiesMembershipChoiceDialog.showIfNecessary(
|
---|
101 | Collections.singleton(selectedNode), !selectedNode.isTagged());
|
---|
102 | if (dialog != null && dialog.getTags().isPresent()) {
|
---|
103 | unglueOneNodeAtMostOneWay(way, dialog);
|
---|
104 | return;
|
---|
105 | } else if (closedOrSelfCrossing) {
|
---|
106 | unglueClosedOrSelfCrossingWay(way, dialog);
|
---|
107 | return;
|
---|
108 | }
|
---|
109 | }
|
---|
110 | errorTime = Notification.TIME_SHORT;
|
---|
111 | errMsg = tr("This node is not glued to anything else.");
|
---|
112 | } else {
|
---|
113 | // and then do the work.
|
---|
114 | unglueWays();
|
---|
115 | }
|
---|
116 | } else if (checkSelectionOneWayAnyNodes(selection)) {
|
---|
117 | checkAndConfirmOutlyingUnglue();
|
---|
118 | selectedNodes.removeIf(n -> n.getParentWays().stream().filter(Way::isUsable).count() < 2);
|
---|
119 | if (selectedNodes.isEmpty()) {
|
---|
120 | if (selection.size() > 1) {
|
---|
121 | errMsg = tr("None of these nodes are glued to anything else.");
|
---|
122 | } else {
|
---|
123 | errMsg = tr("None of this way''s nodes are glued to anything else.");
|
---|
124 | }
|
---|
125 | } else if (selectedNodes.size() == 1) {
|
---|
126 | selectedNode = selectedNodes.iterator().next();
|
---|
127 | unglueWays();
|
---|
128 | } else {
|
---|
129 | // and then do the work.
|
---|
130 | unglueOneWayAnyNodes();
|
---|
131 | }
|
---|
132 | } else {
|
---|
133 | errorTime = Notification.TIME_VERY_LONG;
|
---|
134 | errMsg =
|
---|
135 | tr("The current selection cannot be used for unglueing.")+'\n'+
|
---|
136 | '\n'+
|
---|
137 | tr("Select either:")+'\n'+
|
---|
138 | tr("* One tagged node, or")+'\n'+
|
---|
139 | tr("* One node that is used by more than one way, or")+'\n'+
|
---|
140 | tr("* One node that is used by more than one way and one of those ways, or")+'\n'+
|
---|
141 | tr("* One way that has one or more nodes that are used by more than one way, or")+'\n'+
|
---|
142 | tr("* One way and one or more of its nodes that are used by more than one way.")+'\n'+
|
---|
143 | '\n'+
|
---|
144 | tr("Note: If a way is selected, this way will get fresh copies of the unglued\n"+
|
---|
145 | "nodes and the new nodes will be selected. Otherwise, all ways will get their\n"+
|
---|
146 | "own copy and all nodes will be selected.");
|
---|
147 | }
|
---|
148 |
|
---|
149 | if (errMsg != null) {
|
---|
150 | new Notification(
|
---|
151 | errMsg)
|
---|
152 | .setIcon(JOptionPane.ERROR_MESSAGE)
|
---|
153 | .setDuration(errorTime)
|
---|
154 | .show();
|
---|
155 | }
|
---|
156 | }
|
---|
157 |
|
---|
158 | private void cleanup() {
|
---|
159 | selectedNode = null;
|
---|
160 | selectedWay = null;
|
---|
161 | selectedNodes = null;
|
---|
162 | }
|
---|
163 |
|
---|
164 | static void update(PropertiesMembershipChoiceDialog dialog, Node existingNode, List<Node> newNodes, List<Command> cmds) {
|
---|
165 | updateMemberships(dialog.getMemberships().orElse(null), existingNode, newNodes, cmds);
|
---|
166 | updateProperties(dialog.getTags().orElse(null), existingNode, newNodes, cmds);
|
---|
167 | }
|
---|
168 |
|
---|
169 | private static void updateProperties(ExistingBothNew tags, Node existingNode, Iterable<Node> newNodes, List<Command> cmds) {
|
---|
170 | if (ExistingBothNew.NEW == tags) {
|
---|
171 | final Node newSelectedNode = new Node(existingNode);
|
---|
172 | newSelectedNode.removeAll();
|
---|
173 | cmds.add(new ChangeCommand(existingNode, newSelectedNode));
|
---|
174 | } else if (ExistingBothNew.OLD == tags) {
|
---|
175 | for (Node newNode : newNodes) {
|
---|
176 | newNode.removeAll();
|
---|
177 | }
|
---|
178 | }
|
---|
179 | }
|
---|
180 |
|
---|
181 | /**
|
---|
182 | * Assumes there is one tagged Node stored in selectedNode that it will try to unglue.
|
---|
183 | * (i.e. copy node and remove all tags from the old one.)
|
---|
184 | * @param way way to modify
|
---|
185 | * @param dialog the user dialog
|
---|
186 | */
|
---|
187 | private void unglueOneNodeAtMostOneWay(Way way, PropertiesMembershipChoiceDialog dialog) {
|
---|
188 | List<Command> cmds = new ArrayList<>();
|
---|
189 | List<Node> newNodes = new ArrayList<>();
|
---|
190 | cmds.add(new ChangeNodesCommand(way, modifyWay(selectedNode, way, cmds, newNodes)));
|
---|
191 | if (dialog != null) {
|
---|
192 | update(dialog, selectedNode, newNodes, cmds);
|
---|
193 | }
|
---|
194 |
|
---|
195 | // Place the selected node where the cursor is or some pixels above
|
---|
196 | MapView mv = MainApplication.getMap().mapView;
|
---|
197 | Point currMousePos = mv.getMousePosition();
|
---|
198 | if (currMousePos != null) {
|
---|
199 | cmds.add(new MoveCommand(selectedNode, mv.getLatLon(currMousePos.getX(), currMousePos.getY())));
|
---|
200 | } else {
|
---|
201 | cmds.add(new MoveCommand(selectedNode, 0, 5));
|
---|
202 | }
|
---|
203 | UndoRedoHandler.getInstance().add(new SequenceCommand(tr("Unglued Node"), cmds));
|
---|
204 | getLayerManager().getEditDataSet().setSelected(selectedNode);
|
---|
205 | }
|
---|
206 |
|
---|
207 | /**
|
---|
208 | * Checks if the selection consists of something we can work with.
|
---|
209 | * Checks only if the number and type of items selected looks good.
|
---|
210 | *
|
---|
211 | * If this method returns "true", selectedNode will be set, selectedWay might be set
|
---|
212 | *
|
---|
213 | * Returns true if either one node is selected or one node and one
|
---|
214 | * way are selected and the node is part of the way.
|
---|
215 | *
|
---|
216 | * The way will be put into the object variable "selectedWay", the node into "selectedNode".
|
---|
217 | * @param selection selected primitives
|
---|
218 | * @return true if either one node is selected or one node and one way are selected and the node is part of the way
|
---|
219 | */
|
---|
220 | private boolean checkSelectionOneNodeAtMostOneWay(Collection<? extends OsmPrimitive> selection) {
|
---|
221 |
|
---|
222 | int size = selection.size();
|
---|
223 | if (size < 1 || size > 2)
|
---|
224 | return false;
|
---|
225 |
|
---|
226 | selectedNode = null;
|
---|
227 | selectedWay = null;
|
---|
228 |
|
---|
229 | for (OsmPrimitive p : selection) {
|
---|
230 | if (p instanceof Node) {
|
---|
231 | selectedNode = (Node) p;
|
---|
232 | if (size == 1 || (selectedWay != null && selectedWay.containsNode(selectedNode)))
|
---|
233 | return true;
|
---|
234 | } else if (p instanceof Way) {
|
---|
235 | selectedWay = (Way) p;
|
---|
236 | if (size == 2 && selectedNode != null)
|
---|
237 | return selectedWay.containsNode(selectedNode);
|
---|
238 | }
|
---|
239 | }
|
---|
240 |
|
---|
241 | return false;
|
---|
242 | }
|
---|
243 |
|
---|
244 | /**
|
---|
245 | * Checks if the selection consists of something we can work with.
|
---|
246 | * Checks only if the number and type of items selected looks good.
|
---|
247 | *
|
---|
248 | * Returns true if one way and any number of nodes that are part of that way are selected.
|
---|
249 | * Note: "any" can be none, then all nodes of the way are used.
|
---|
250 | *
|
---|
251 | * The way will be put into the object variable "selectedWay", the nodes into "selectedNodes".
|
---|
252 | * @param selection selected primitives
|
---|
253 | * @return true if one way and any number of nodes that are part of that way are selected
|
---|
254 | */
|
---|
255 | private boolean checkSelectionOneWayAnyNodes(Collection<? extends OsmPrimitive> selection) {
|
---|
256 | if (selection.isEmpty())
|
---|
257 | return false;
|
---|
258 |
|
---|
259 | selectedWay = null;
|
---|
260 | for (OsmPrimitive p : selection) {
|
---|
261 | if (p instanceof Way) {
|
---|
262 | if (selectedWay != null)
|
---|
263 | return false;
|
---|
264 | selectedWay = (Way) p;
|
---|
265 | }
|
---|
266 | }
|
---|
267 | if (selectedWay == null)
|
---|
268 | return false;
|
---|
269 |
|
---|
270 | selectedNodes = new HashSet<>();
|
---|
271 | for (OsmPrimitive p : selection) {
|
---|
272 | if (p instanceof Node) {
|
---|
273 | Node n = (Node) p;
|
---|
274 | if (!selectedWay.containsNode(n))
|
---|
275 | return false;
|
---|
276 | selectedNodes.add(n);
|
---|
277 | }
|
---|
278 | }
|
---|
279 |
|
---|
280 | if (selectedNodes.isEmpty()) {
|
---|
281 | selectedNodes.addAll(selectedWay.getNodes());
|
---|
282 | }
|
---|
283 |
|
---|
284 | return true;
|
---|
285 | }
|
---|
286 |
|
---|
287 | /**
|
---|
288 | * dupe the given node of the given way
|
---|
289 | *
|
---|
290 | * assume that originalNode is in the way
|
---|
291 | * <ul>
|
---|
292 | * <li>the new node will be put into the parameter newNodes.</li>
|
---|
293 | * <li>the add-node command will be put into the parameter cmds.</li>
|
---|
294 | * <li>the changed way will be returned and must be put into cmds by the caller!</li>
|
---|
295 | * </ul>
|
---|
296 | * @param originalNode original node to duplicate
|
---|
297 | * @param w parent way
|
---|
298 | * @param cmds List of commands that will contain the new "add node" command
|
---|
299 | * @param newNodes List of nodes that will contain the new node
|
---|
300 | * @return new way The modified way. Change command must be handled by the caller
|
---|
301 | */
|
---|
302 | private static List<Node> modifyWay(Node originalNode, Way w, List<Command> cmds, List<Node> newNodes) {
|
---|
303 | // clone the node for the way
|
---|
304 | Node newNode = cloneNode(originalNode, cmds);
|
---|
305 | newNodes.add(newNode);
|
---|
306 |
|
---|
307 | List<Node> nn = new ArrayList<>(w.getNodes());
|
---|
308 | nn.replaceAll(n -> n == originalNode ? newNode : n);
|
---|
309 | return nn;
|
---|
310 | }
|
---|
311 |
|
---|
312 | private static Node cloneNode(Node originalNode, List<Command> cmds) {
|
---|
313 | Node newNode = new Node(originalNode, true /* clear OSM ID */);
|
---|
314 | cmds.add(new AddCommand(originalNode.getDataSet(), newNode));
|
---|
315 | return newNode;
|
---|
316 | }
|
---|
317 |
|
---|
318 | /**
|
---|
319 | * put all newNodes into the same relation(s) that originalNode is in
|
---|
320 | * @param memberships where the memberships should be places
|
---|
321 | * @param originalNode original node to duplicate
|
---|
322 | * @param cmds List of commands that will contain the new "change relation" commands
|
---|
323 | * @param newNodes List of nodes that contain the new node
|
---|
324 | */
|
---|
325 | private static void updateMemberships(ExistingBothNew memberships, Node originalNode, List<Node> newNodes, List<Command> cmds) {
|
---|
326 | if (memberships == null || ExistingBothNew.OLD == memberships) {
|
---|
327 | return;
|
---|
328 | }
|
---|
329 | // modify all relations containing the node
|
---|
330 | for (Relation r : OsmPrimitive.getParentRelations(Collections.singleton(originalNode))) {
|
---|
331 | if (r.isDeleted()) {
|
---|
332 | continue;
|
---|
333 | }
|
---|
334 | Relation newRel = new Relation(r);
|
---|
335 | // loop backwards because we add or remove members, works also when nodes appear
|
---|
336 | // multiple times in the same relation
|
---|
337 | boolean changed = false;
|
---|
338 | for (int i = r.getMembersCount() - 1; i >= 0; i--) {
|
---|
339 | RelationMember rm = r.getMember(i);
|
---|
340 | if (rm.getMember() != originalNode)
|
---|
341 | continue;
|
---|
342 | for (Node n : newNodes) {
|
---|
343 | newRel.addMember(i + 1, new RelationMember(rm.getRole(), n));
|
---|
344 | }
|
---|
345 | if (ExistingBothNew.NEW == memberships) {
|
---|
346 | // remove old member
|
---|
347 | newRel.removeMember(i);
|
---|
348 | }
|
---|
349 | changed = true;
|
---|
350 | }
|
---|
351 | if (changed) {
|
---|
352 | cmds.add(new ChangeCommand(r, newRel));
|
---|
353 | }
|
---|
354 | }
|
---|
355 | }
|
---|
356 |
|
---|
357 | /**
|
---|
358 | * dupe a single node into as many nodes as there are ways using it, OR
|
---|
359 | *
|
---|
360 | * dupe a single node once, and put the copy on the selected way
|
---|
361 | * @throws UserCancelException if user cancels choice
|
---|
362 | */
|
---|
363 | private void unglueWays() throws UserCancelException {
|
---|
364 | final PropertiesMembershipChoiceDialog dialog = PropertiesMembershipChoiceDialog
|
---|
365 | .showIfNecessary(Collections.singleton(selectedNode), false);
|
---|
366 | List<Command> cmds = new ArrayList<>();
|
---|
367 | List<Node> newNodes = new ArrayList<>();
|
---|
368 | List<Way> parentWays;
|
---|
369 | if (selectedWay == null) {
|
---|
370 | parentWays = selectedNode.referrers(Way.class).filter(Way::isUsable).collect(Collectors.toList());
|
---|
371 | // see #5452 and #18670
|
---|
372 | parentWays.sort((o1, o2) -> {
|
---|
373 | int d = Boolean.compare(!o1.isNew() && !o1.isModified(), !o2.isNew() && !o2.isModified());
|
---|
374 | if (d == 0) {
|
---|
375 | d = Integer.compare(o2.getReferrers().size(), o1.getReferrers().size()); // reversed
|
---|
376 | }
|
---|
377 | if (d == 0) {
|
---|
378 | d = Boolean.compare(o1.isFirstLastNode(selectedNode), o2.isFirstLastNode(selectedNode));
|
---|
379 | }
|
---|
380 | return d;
|
---|
381 | });
|
---|
382 | // first way should not be changed, preferring older ways and those with fewer parents
|
---|
383 | parentWays.remove(0);
|
---|
384 | } else {
|
---|
385 | parentWays = Collections.singletonList(selectedWay);
|
---|
386 | }
|
---|
387 | Set<Way> warnParents = new HashSet<>();
|
---|
388 | for (Way w : parentWays) {
|
---|
389 | if (w.isFirstLastNode(selectedNode))
|
---|
390 | warnParents.add(w);
|
---|
391 | cmds.add(new ChangeNodesCommand(w, modifyWay(selectedNode, w, cmds, newNodes)));
|
---|
392 | }
|
---|
393 |
|
---|
394 | if (dialog != null) {
|
---|
395 | update(dialog, selectedNode, newNodes, cmds);
|
---|
396 | }
|
---|
397 | notifyWayPartOfRelation(warnParents);
|
---|
398 |
|
---|
399 | execCommands(cmds, newNodes);
|
---|
400 | }
|
---|
401 |
|
---|
402 | /**
|
---|
403 | * Add commands to undo-redo system.
|
---|
404 | * @param cmds Commands to execute
|
---|
405 | * @param newNodes New created nodes by this set of command
|
---|
406 | */
|
---|
407 | private void execCommands(List<Command> cmds, List<Node> newNodes) {
|
---|
408 | UndoRedoHandler.getInstance().add(new SequenceCommand(/* for correct i18n of plural forms - see #9110 */
|
---|
409 | trn("Dupe into {0} node", "Dupe into {0} nodes", newNodes.size() + 1L, newNodes.size() + 1L), cmds));
|
---|
410 | // select one of the new nodes
|
---|
411 | getLayerManager().getEditDataSet().setSelected(newNodes.get(0));
|
---|
412 | }
|
---|
413 |
|
---|
414 | /**
|
---|
415 | * Duplicates a node used several times by the same way. See #9896.
|
---|
416 | * First occurrence is kept. A closed way will be "opened" when the closing node is unglued.
|
---|
417 | * @param way way to modify
|
---|
418 | * @param dialog user dialog, might be null
|
---|
419 | * @return true if action is OK false if there is nothing to do
|
---|
420 | */
|
---|
421 | private boolean unglueClosedOrSelfCrossingWay(Way way, PropertiesMembershipChoiceDialog dialog) {
|
---|
422 | // According to previous check, only one valid way through that node
|
---|
423 | List<Command> cmds = new ArrayList<>();
|
---|
424 | List<Node> oldNodes = way.getNodes();
|
---|
425 | List<Node> newNodes = new ArrayList<>(oldNodes.size());
|
---|
426 | List<Node> addNodes = new ArrayList<>();
|
---|
427 | int count = 0;
|
---|
428 | for (Node n: oldNodes) {
|
---|
429 | if (n == selectedNode && count++ > 0) {
|
---|
430 | n = cloneNode(selectedNode, cmds);
|
---|
431 | addNodes.add(n);
|
---|
432 | }
|
---|
433 | newNodes.add(n);
|
---|
434 | }
|
---|
435 | if (addNodes.isEmpty()) {
|
---|
436 | // selectedNode doesn't need unglue
|
---|
437 | return false;
|
---|
438 | }
|
---|
439 | if (dialog != null) {
|
---|
440 | update(dialog, selectedNode, addNodes, cmds);
|
---|
441 | }
|
---|
442 | addCheckedChangeNodesCmd(cmds, way, newNodes);
|
---|
443 | execCommands(cmds, addNodes);
|
---|
444 | return true;
|
---|
445 | }
|
---|
446 |
|
---|
447 | /**
|
---|
448 | * dupe all nodes that are selected, and put the copies on the selected way
|
---|
449 | * @throws UserCancelException if user cancels choice
|
---|
450 | */
|
---|
451 | private void unglueOneWayAnyNodes() throws UserCancelException {
|
---|
452 | final PropertiesMembershipChoiceDialog dialog =
|
---|
453 | PropertiesMembershipChoiceDialog.showIfNecessary(selectedNodes, false);
|
---|
454 |
|
---|
455 | Map<Node, Node> replaced = new HashMap<>();
|
---|
456 | List<Command> cmds = new ArrayList<>();
|
---|
457 |
|
---|
458 | selectedNodes.forEach(n -> replaced.put(n, cloneNode(n, cmds)));
|
---|
459 | List<Node> modNodes = new ArrayList<>(selectedWay.getNodes());
|
---|
460 | modNodes.replaceAll(n -> replaced.getOrDefault(n, n));
|
---|
461 |
|
---|
462 | if (dialog != null) {
|
---|
463 | replaced.forEach((k, v) -> update(dialog, k, Collections.singletonList(v), cmds));
|
---|
464 | }
|
---|
465 |
|
---|
466 | // only one changeCommand for a way, else garbage will happen
|
---|
467 | addCheckedChangeNodesCmd(cmds, selectedWay, modNodes);
|
---|
468 | UndoRedoHandler.getInstance().add(new SequenceCommand(
|
---|
469 | trn("Dupe {0} node into {1} nodes", "Dupe {0} nodes into {1} nodes",
|
---|
470 | selectedNodes.size(), selectedNodes.size(), 2 * selectedNodes.size()), cmds));
|
---|
471 | getLayerManager().getEditDataSet().setSelected(replaced.values());
|
---|
472 | }
|
---|
473 |
|
---|
474 | private boolean addCheckedChangeNodesCmd(List<Command> cmds, Way w, List<Node> nodes) {
|
---|
475 | boolean relationCheck = !calcAffectedRelations(Collections.singleton(w)).isEmpty();
|
---|
476 | cmds.add(new ChangeNodesCommand(w, nodes));
|
---|
477 | if (relationCheck) {
|
---|
478 | notifyWayPartOfRelation(Collections.singleton(w));
|
---|
479 | }
|
---|
480 | return relationCheck;
|
---|
481 | }
|
---|
482 |
|
---|
483 | @Override
|
---|
484 | protected void updateEnabledState() {
|
---|
485 | updateEnabledStateOnCurrentSelection();
|
---|
486 | }
|
---|
487 |
|
---|
488 | @Override
|
---|
489 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
---|
490 | updateEnabledStateOnModifiableSelection(selection);
|
---|
491 | }
|
---|
492 |
|
---|
493 | protected void checkAndConfirmOutlyingUnglue() throws UserCancelException {
|
---|
494 | List<OsmPrimitive> primitives = new ArrayList<>(2 + (selectedNodes == null ? 0 : selectedNodes.size()));
|
---|
495 | if (selectedNodes != null)
|
---|
496 | primitives.addAll(selectedNodes);
|
---|
497 | if (selectedNode != null)
|
---|
498 | primitives.add(selectedNode);
|
---|
499 | final boolean ok = checkAndConfirmOutlyingOperation("unglue",
|
---|
500 | tr("Unglue confirmation"),
|
---|
501 | tr("You are about to unglue nodes which can have other referrers not yet downloaded."
|
---|
502 | + "<br>"
|
---|
503 | + "This can cause problems because other objects (that you do not see) might use them."
|
---|
504 | + "<br>"
|
---|
505 | + "Do you really want to unglue?"),
|
---|
506 | tr("You are about to unglue incomplete objects."
|
---|
507 | + "<br>"
|
---|
508 | + "This will cause problems because you don''t see the real object."
|
---|
509 | + "<br>" + "Do you really want to unglue?"),
|
---|
510 | primitives, null);
|
---|
511 | if (!ok) {
|
---|
512 | throw new UserCancelException();
|
---|
513 | }
|
---|
514 | }
|
---|
515 |
|
---|
516 | protected void notifyWayPartOfRelation(final Collection<Way> ways) {
|
---|
517 | Set<Relation> affectedRelations = calcAffectedRelations(ways);
|
---|
518 | if (affectedRelations.isEmpty()) {
|
---|
519 | return;
|
---|
520 | }
|
---|
521 | final int size = affectedRelations.size();
|
---|
522 | final String msg1 = trn("Unglueing possibly affected {0} relation: {1}", "Unglueing possibly affected {0} relations: {1}",
|
---|
523 | size, size, DefaultNameFormatter.getInstance().formatAsHtmlUnorderedList(affectedRelations, 20));
|
---|
524 | final String msg2 = trn("Ensure that the relation has not been broken!", "Ensure that the relations have not been broken!",
|
---|
525 | size);
|
---|
526 | new Notification(msg1 + msg2).setIcon(JOptionPane.WARNING_MESSAGE).show();
|
---|
527 | }
|
---|
528 |
|
---|
529 | protected Set<Relation> calcAffectedRelations(final Collection<Way> ways) {
|
---|
530 | final Set<Node> affectedNodes = (selectedNodes != null) ? selectedNodes : Collections.singleton(selectedNode);
|
---|
531 | return OsmPrimitive.getParentRelations(ways)
|
---|
532 | .stream().filter(r -> isRelationAffected(r, affectedNodes, ways))
|
---|
533 | .collect(Collectors.toSet());
|
---|
534 | }
|
---|
535 |
|
---|
536 | private static boolean isRelationAffected(Relation r, Set<Node> affectedNodes, Collection<Way> ways) {
|
---|
537 | if (!r.isUsable())
|
---|
538 | return false;
|
---|
539 | // see #18670: suppress notification when well known restriction types are not affected
|
---|
540 | if (!r.hasTag("type", "restriction", "connectivity", "destination_sign") || r.hasIncompleteMembers())
|
---|
541 | return true;
|
---|
542 | int count = 0;
|
---|
543 | for (RelationMember rm : r.getMembers()) {
|
---|
544 | if (rm.isNode() && affectedNodes.contains(rm.getNode()))
|
---|
545 | count++;
|
---|
546 | if (rm.isWay() && ways.contains(rm.getWay())) {
|
---|
547 | count++;
|
---|
548 | if ("via".equals(rm.getRole())) {
|
---|
549 | count++;
|
---|
550 | }
|
---|
551 | }
|
---|
552 | }
|
---|
553 | return count >= 2;
|
---|
554 | }
|
---|
555 | }
|
---|