source: josm/trunk/src/org/openstreetmap/josm/actions/SplitWayAction.java@ 10630

Last change on this file since 10630 was 10601, checked in by Don-vip, 8 years ago

see #11390 - sonar - squid:S1604 - Java 8: Anonymous inner classes containing only one method should become lambdas

  • Property svn:eol-style set to native
File size: 32.5 KB
RevLine 
[6380]1// License: GPL. For details, see LICENSE file.
[626]2package org.openstreetmap.josm.actions;
3
[2412]4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
[626]5import static org.openstreetmap.josm.tools.I18n.tr;
6import static org.openstreetmap.josm.tools.I18n.trn;
7
[8886]8import java.awt.Component;
[8897]9import java.awt.GridBagLayout;
[626]10import java.awt.event.ActionEvent;
11import java.awt.event.KeyEvent;
12import java.util.ArrayList;
[4254]13import java.util.Arrays;
[626]14import java.util.Collection;
[5570]15import java.util.Collections;
[626]16import java.util.HashSet;
17import java.util.Iterator;
18import java.util.LinkedList;
19import java.util.List;
20import java.util.Set;
[9245]21import java.util.concurrent.atomic.AtomicInteger;
[626]22
[8886]23import javax.swing.DefaultListCellRenderer;
24import javax.swing.JLabel;
25import javax.swing.JList;
[626]26import javax.swing.JOptionPane;
[8886]27import javax.swing.JPanel;
28import javax.swing.ListSelectionModel;
[626]29
30import org.openstreetmap.josm.Main;
31import org.openstreetmap.josm.command.AddCommand;
32import org.openstreetmap.josm.command.ChangeCommand;
33import org.openstreetmap.josm.command.Command;
34import org.openstreetmap.josm.command.SequenceCommand;
35import org.openstreetmap.josm.data.osm.Node;
36import org.openstreetmap.josm.data.osm.OsmPrimitive;
[2521]37import org.openstreetmap.josm.data.osm.PrimitiveId;
[626]38import org.openstreetmap.josm.data.osm.Relation;
[668]39import org.openstreetmap.josm.data.osm.RelationMember;
[626]40import org.openstreetmap.josm.data.osm.Way;
[8886]41import org.openstreetmap.josm.data.osm.WaySegment;
[1990]42import org.openstreetmap.josm.gui.DefaultNameFormatter;
[8886]43import org.openstreetmap.josm.gui.ExtendedDialog;
[6130]44import org.openstreetmap.josm.gui.Notification;
[3152]45import org.openstreetmap.josm.gui.layer.OsmDataLayer;
46import org.openstreetmap.josm.tools.CheckParameterUtil;
[8897]47import org.openstreetmap.josm.tools.GBC;
[1084]48import org.openstreetmap.josm.tools.Shortcut;
[626]49
50/**
51 * Splits a way into multiple ways (all identical except for their node list).
[1023]52 *
[626]53 * Ways are just split at the selected nodes. The nodes remain in their
54 * original order. Selected nodes at the end of a way are ignored.
55 */
[1820]56public class SplitWayAction extends JosmAction {
[626]57
[5401]58 /**
59 * Represents the result of a {@link SplitWayAction}
60 * @see SplitWayAction#splitWay
61 * @see SplitWayAction#split
62 */
[2521]63 public static class SplitWayResult {
64 private final Command command;
65 private final List<? extends PrimitiveId> newSelection;
[8931]66 private final Way originalWay;
67 private final List<Way> newWays;
[2521]68
[5401]69 /**
[8540]70 * @param command The command to be performed to split the way (which is saved for later retrieval with {@link #getCommand})
71 * @param newSelection The new list of selected primitives ids (which is saved for later retrieval with {@link #getNewSelection})
72 * @param originalWay The original way being split (which is saved for later retrieval with {@link #getOriginalWay})
73 * @param newWays The resulting new ways (which is saved for later retrieval with {@link #getOriginalWay})
[5401]74 */
[3152]75 public SplitWayResult(Command command, List<? extends PrimitiveId> newSelection, Way originalWay, List<Way> newWays) {
[2521]76 this.command = command;
77 this.newSelection = newSelection;
[3152]78 this.originalWay = originalWay;
79 this.newWays = newWays;
[2521]80 }
81
[5401]82 /**
83 * Replies the command to be performed to split the way
84 * @return The command to be performed to split the way
85 */
[2521]86 public Command getCommand() {
87 return command;
88 }
89
[5401]90 /**
91 * Replies the new list of selected primitives ids
92 * @return The new list of selected primitives ids
93 */
[2521]94 public List<? extends PrimitiveId> getNewSelection() {
95 return newSelection;
96 }
[3152]97
[5401]98 /**
99 * Replies the original way being split
100 * @return The original way being split
101 */
[3152]102 public Way getOriginalWay() {
103 return originalWay;
104 }
105
[5401]106 /**
107 * Replies the resulting new ways
108 * @return The resulting new ways
109 */
[3152]110 public List<Way> getNewWays() {
111 return newWays;
112 }
[2521]113 }
114
[1169]115 /**
116 * Create a new SplitWayAction.
117 */
118 public SplitWayAction() {
119 super(tr("Split Way"), "splitway", tr("Split a way at the selected node."),
[4982]120 Shortcut.registerShortcut("tools:splitway", tr("Tool: {0}", tr("Split Way")), KeyEvent.VK_P, Shortcut.DIRECT), true);
[2323]121 putValue("help", ht("/Action/SplitWay"));
[1169]122 }
[626]123
[1169]124 /**
125 * Called when the action is executed.
126 *
127 * This method performs an expensive check whether the selection clearly defines one
128 * of the split actions outlined above, and if yes, calls the splitWay method.
129 */
[6084]130 @Override
[1169]131 public void actionPerformed(ActionEvent e) {
[626]132
[9245]133 if (SegmentToKeepSelectionDialog.DISPLAY_COUNT.get() > 0) {
134 new Notification(tr("Cannot split since another split operation is already in progress"))
135 .setIcon(JOptionPane.WARNING_MESSAGE).show();
136 return;
137 }
138
[10382]139 Collection<OsmPrimitive> selection = getLayerManager().getEditDataSet().getSelected();
[626]140
[3152]141 List<Node> selectedNodes = OsmPrimitive.getFilteredList(selection, Node.class);
142 List<Way> selectedWays = OsmPrimitive.getFilteredList(selection, Way.class);
[3392]143 List<Way> applicableWays = getApplicableWays(selectedWays, selectedNodes);
[3152]144
[3392]145 if (applicableWays == null) {
[6130]146 new Notification(
147 tr("The current selection cannot be used for splitting - no node is selected."))
148 .setIcon(JOptionPane.WARNING_MESSAGE)
149 .show();
[1169]150 return;
[3392]151 } else if (applicableWays.isEmpty()) {
[6130]152 new Notification(
153 tr("The selected nodes do not share the same way."))
154 .setIcon(JOptionPane.WARNING_MESSAGE)
155 .show();
[3392]156 return;
[1169]157 }
[626]158
[8276]159 // If several ways have been found, remove ways that doesn't have selected
160 // node in the middle
[5401]161 if (applicableWays.size() > 1) {
[8276]162 for (Iterator<Way> it = applicableWays.iterator(); it.hasNext();) {
163 Way w = it.next();
164 for (Node n : selectedNodes) {
165 if (!w.isInnerNode(n)) {
166 it.remove();
167 break;
[1169]168 }
169 }
[8276]170 }
[3392]171 }
[626]172
[3392]173 if (applicableWays.isEmpty()) {
[6130]174 new Notification(
[3392]175 trn("The selected node is not in the middle of any way.",
[6130]176 "The selected nodes are not in the middle of any way.",
177 selectedNodes.size()))
178 .setIcon(JOptionPane.WARNING_MESSAGE)
179 .show();
[3392]180 return;
181 } else if (applicableWays.size() > 1) {
[6130]182 new Notification(
[3392]183 trn("There is more than one way using the node you selected. Please select the way also.",
[6130]184 "There is more than one way using the nodes you selected. Please select the way also.",
185 selectedNodes.size()))
186 .setIcon(JOptionPane.WARNING_MESSAGE)
187 .show();
[3392]188 return;
189 }
[626]190
[3392]191 // Finally, applicableWays contains only one perfect way
[8886]192 final Way selectedWay = applicableWays.get(0);
193 final List<List<Node>> wayChunks = buildSplitChunks(selectedWay, selectedNodes);
[3156]194 if (wayChunks != null) {
[10448]195 List<Relation> selectedRelations = OsmPrimitive.getFilteredList(selection, Relation.class);
[8886]196 final List<OsmPrimitive> sel = new ArrayList<>(selectedWays.size() + selectedRelations.size());
[3392]197 sel.addAll(selectedWays);
198 sel.addAll(selectedRelations);
[8886]199
200 final List<Way> newWays = createNewWaysFromChunks(selectedWay, wayChunks);
[8955]201 final Way wayToKeep = Strategy.keepLongestChunk().determineWayToKeep(newWays);
[8886]202
[8978]203 if (ExpertToggleAction.isExpert() && !selectedWay.isNew()) {
[8886]204 final ExtendedDialog dialog = new SegmentToKeepSelectionDialog(selectedWay, newWays, wayToKeep, sel);
[8978]205 dialog.toggleEnable("way.split.segment-selection-dialog");
206 if (!dialog.toggleCheckState()) {
207 dialog.setModal(false);
208 dialog.showDialog();
209 return; // splitting is performed in SegmentToKeepSelectionDialog.buttonAction()
210 }
[8886]211 }
[10131]212 if (wayToKeep != null) {
[10382]213 final SplitWayResult result = doSplitWay(getLayerManager().getEditLayer(), selectedWay, wayToKeep, newWays, sel);
[10131]214 Main.main.undoRedo.add(result.getCommand());
[10382]215 getLayerManager().getEditDataSet().setSelected(result.getNewSelection());
[10131]216 }
[3156]217 }
[1169]218 }
[626]219
[8276]220 /**
[8886]221 * A dialog to query which way segment should reuse the history of the way to split.
222 */
223 static class SegmentToKeepSelectionDialog extends ExtendedDialog {
[9245]224 static final AtomicInteger DISPLAY_COUNT = new AtomicInteger();
[10254]225 final transient Way selectedWay;
226 final transient List<Way> newWays;
[8886]227 final JList<Way> list;
[10254]228 final transient List<OsmPrimitive> selection;
229 final transient Way wayToKeep;
[8886]230
231 SegmentToKeepSelectionDialog(Way selectedWay, List<Way> newWays, Way wayToKeep, List<OsmPrimitive> selection) {
232 super(Main.parent, tr("Which way segment should reuse the history of {0}?", selectedWay.getId()),
233 new String[]{tr("Ok"), tr("Cancel")}, true);
234
235 this.selectedWay = selectedWay;
236 this.newWays = newWays;
237 this.selection = selection;
[8988]238 this.wayToKeep = wayToKeep;
[8886]239 this.list = new JList<>(newWays.toArray(new Way[newWays.size()]));
[8988]240 configureList();
[8886]241
242 setButtonIcons(new String[]{"ok", "cancel"});
[8897]243 final JPanel pane = new JPanel(new GridBagLayout());
244 pane.add(new JLabel(getTitle()), GBC.eol().fill(GBC.HORIZONTAL));
245 pane.add(list, GBC.eop().fill(GBC.HORIZONTAL));
[8886]246 setContent(pane);
[9245]247 setDefaultCloseOperation(HIDE_ON_CLOSE);
[8886]248 }
249
[8978]250 private void configureList() {
[8886]251 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
[10601]252 list.addListSelectionListener(e -> {
253 final Way selected = list.getSelectedValue();
254 if (Main.isDisplayingMapView() && selected != null && selected.getNodesCount() > 1) {
255 final Collection<WaySegment> segments = new ArrayList<>(selected.getNodesCount() - 1);
256 final Iterator<Node> it = selected.getNodes().iterator();
257 Node previousNode = it.next();
258 while (it.hasNext()) {
259 final Node node = it.next();
260 segments.add(WaySegment.forNodePair(selectedWay, previousNode, node));
261 previousNode = node;
[8886]262 }
[10601]263 setHighlightedWaySegments(segments);
[8886]264 }
265 });
266 list.setCellRenderer(new DefaultListCellRenderer() {
267 @Override
268 public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
269 final Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
[8897]270 final String name = DefaultNameFormatter.getInstance().format((Way) value);
271 // get rid of id from DefaultNameFormatter.decorateNameWithId()
[8963]272 final String nameWithoutId = name
273 .replace(tr(" [id: {0}]", ((Way) value).getId()), "")
274 .replace(tr(" [id: {0}]", ((Way) value).getUniqueId()), "");
[8897]275 ((JLabel) c).setText(tr("Segment {0}: {1}", index + 1, nameWithoutId));
[8886]276 return c;
277 }
278 });
279 }
280
281 protected void setHighlightedWaySegments(Collection<WaySegment> segments) {
282 selectedWay.getDataSet().setHighlightedWaySegments(segments);
283 Main.map.mapView.repaint();
284 }
285
286 @Override
287 public void setVisible(boolean visible) {
288 super.setVisible(visible);
289 if (visible) {
[9245]290 DISPLAY_COUNT.incrementAndGet();
[8988]291 list.setSelectedValue(wayToKeep, true);
[8886]292 } else {
293 setHighlightedWaySegments(Collections.<WaySegment>emptyList());
[9245]294 DISPLAY_COUNT.decrementAndGet();
[8886]295 }
296 }
297
298 @Override
299 protected void buttonAction(int buttonIndex, ActionEvent evt) {
300 super.buttonAction(buttonIndex, evt);
[8978]301 toggleSaveState(); // necessary since #showDialog() does not handle it due to the non-modal dialog
[8886]302 if (getValue() == 1) {
[10448]303 SplitWayResult result = doSplitWay(Main.getLayerManager().getEditLayer(),
304 selectedWay, list.getSelectedValue(), newWays, selection);
[8886]305 Main.main.undoRedo.add(result.getCommand());
[10448]306 Main.getLayerManager().getEditDataSet().setSelected(result.getNewSelection());
[8886]307 }
308 }
309 }
310
311 /**
[8955]312 * Determines which way chunk should reuse the old id and its history
313 *
314 * @since 8954
[10599]315 * @since 10599 (functional interface)
[8886]316 */
[10599]317 @FunctionalInterface
318 public interface Strategy {
[8955]319
320 /**
321 * Determines which way chunk should reuse the old id and its history.
322 *
323 * @param wayChunks the way chunks
324 * @return the way to keep
325 */
[10599]326 Way determineWayToKeep(Iterable<Way> wayChunks);
[8955]327
328 /**
329 * Returns a strategy which selects the way chunk with the highest node count to keep.
[9230]330 * @return strategy which selects the way chunk with the highest node count to keep
[8955]331 */
[10599]332 static Strategy keepLongestChunk() {
333 return wayChunks -> {
[8955]334 Way wayToKeep = null;
335 for (Way i : wayChunks) {
336 if (wayToKeep == null || i.getNodesCount() > wayToKeep.getNodesCount()) {
337 wayToKeep = i;
338 }
339 }
340 return wayToKeep;
[10599]341 };
[8886]342 }
[8955]343
344 /**
345 * Returns a strategy which selects the first way chunk.
[9230]346 * @return strategy which selects the first way chunk
[8955]347 */
[10599]348 static Strategy keepFirstChunk() {
349 return wayChunks -> wayChunks.iterator().next();
[8955]350 }
[8886]351 }
352
353 /**
354 * Determine which ways to split.
[8276]355 * @param selectedWays List of user selected ways.
356 * @param selectedNodes List of user selected nodes.
357 * @return List of ways to split
358 */
[10074]359 static List<Way> getApplicableWays(List<Way> selectedWays, List<Node> selectedNodes) {
[3392]360 if (selectedNodes.isEmpty())
361 return null;
362
[10074]363 // Special case - one of the selected ways touches (not cross) way that we want to split
[5570]364 if (selectedNodes.size() == 1) {
365 Node n = selectedNodes.get(0);
[8276]366 List<Way> referedWays =
367 OsmPrimitive.getFilteredList(n.getReferrers(), Way.class);
[5570]368 Way inTheMiddle = null;
369 for (Way w: referedWays) {
[8276]370 // Need to look at all nodes see #11184 for a case where node n is
371 // firstNode, lastNode and also in the middle
372 if (selectedWays.contains(w) && w.isInnerNode(n)) {
[5570]373 if (inTheMiddle == null) {
374 inTheMiddle = w;
375 } else {
376 inTheMiddle = null;
377 break;
378 }
379 }
380 }
[8276]381 if (inTheMiddle != null)
[5570]382 return Collections.singletonList(inTheMiddle);
383 }
384
[3392]385 // List of ways shared by all nodes
[10074]386 return UnJoinNodeWayAction.getApplicableWays(selectedWays, selectedNodes);
[1169]387 }
[626]388
[1169]389 /**
[3152]390 * Splits the nodes of {@code wayToSplit} into a list of node sequences
391 * which are separated at the nodes in {@code splitPoints}.
[3156]392 *
[3152]393 * This method displays warning messages if {@code wayToSplit} and/or
394 * {@code splitPoints} aren't consistent.
[3156]395 *
[3152]396 * Returns null, if building the split chunks fails.
[3156]397 *
[3152]398 * @param wayToSplit the way to split. Must not be null.
399 * @param splitPoints the nodes where the way is split. Must not be null.
400 * @return the list of chunks
[1169]401 */
[8510]402 public static List<List<Node>> buildSplitChunks(Way wayToSplit, List<Node> splitPoints) {
[3152]403 CheckParameterUtil.ensureParameterNotNull(wayToSplit, "wayToSplit");
404 CheckParameterUtil.ensureParameterNotNull(splitPoints, "splitPoints");
[626]405
[7005]406 Set<Node> nodeSet = new HashSet<>(splitPoints);
407 List<List<Node>> wayChunks = new LinkedList<>();
408 List<Node> currentWayChunk = new ArrayList<>();
[1169]409 wayChunks.add(currentWayChunk);
[626]410
[3152]411 Iterator<Node> it = wayToSplit.getNodes().iterator();
[1169]412 while (it.hasNext()) {
413 Node currentNode = it.next();
414 boolean atEndOfWay = currentWayChunk.isEmpty() || !it.hasNext();
415 currentWayChunk.add(currentNode);
416 if (nodeSet.contains(currentNode) && !atEndOfWay) {
[7005]417 currentWayChunk = new ArrayList<>();
[1169]418 currentWayChunk.add(currentNode);
419 wayChunks.add(currentWayChunk);
420 }
421 }
[626]422
[1169]423 // Handle circular ways specially.
424 // If you split at a circular way at two nodes, you just want to split
425 // it at these points, not also at the former endpoint.
426 // So if the last node is the same first node, join the last and the
427 // first way chunk.
428 List<Node> lastWayChunk = wayChunks.get(wayChunks.size() - 1);
429 if (wayChunks.size() >= 2
430 && wayChunks.get(0).get(0) == lastWayChunk.get(lastWayChunk.size() - 1)
431 && !nodeSet.contains(wayChunks.get(0).get(0))) {
432 if (wayChunks.size() == 2) {
[6130]433 new Notification(
434 tr("You must select two or more nodes to split a circular way."))
435 .setIcon(JOptionPane.WARNING_MESSAGE)
436 .show();
[3152]437 return null;
[1169]438 }
439 lastWayChunk.remove(lastWayChunk.size() - 1);
440 lastWayChunk.addAll(wayChunks.get(0));
441 wayChunks.remove(wayChunks.size() - 1);
442 wayChunks.set(0, lastWayChunk);
443 }
[626]444
[1169]445 if (wayChunks.size() < 2) {
[2381]446 if (wayChunks.get(0).get(0) == wayChunks.get(0).get(wayChunks.get(0).size() - 1)) {
[6130]447 new Notification(
448 tr("You must select two or more nodes to split a circular way."))
449 .setIcon(JOptionPane.WARNING_MESSAGE)
450 .show();
[1809]451 } else {
[6130]452 new Notification(
453 tr("The way cannot be split at the selected nodes. (Hint: Select nodes in the middle of the way.)"))
454 .setIcon(JOptionPane.WARNING_MESSAGE)
455 .show();
[1809]456 }
[3152]457 return null;
[1169]458 }
[3152]459 return wayChunks;
[2521]460 }
461
[3152]462 /**
[8886]463 * Creates new way objects for the way chunks and transfers the keys from the original way.
464 * @param way the original way whose keys are transferred
465 * @param wayChunks the way chunks
466 * @return the new way objects
467 */
468 protected static List<Way> createNewWaysFromChunks(Way way, Iterable<List<Node>> wayChunks) {
469 final List<Way> newWays = new ArrayList<>();
470 for (List<Node> wayChunk : wayChunks) {
471 Way wayToAdd = new Way();
472 wayToAdd.setKeys(way.getKeys());
473 wayToAdd.setNodes(wayChunk);
474 newWays.add(wayToAdd);
475 }
476 return newWays;
477 }
478
479 /**
[5401]480 * Splits the way {@code way} into chunks of {@code wayChunks} and replies
481 * the result of this process in an instance of {@link SplitWayResult}.
482 *
483 * Note that changes are not applied to the data yet. You have to
484 * submit the command in {@link SplitWayResult#getCommand()} first,
485 * i.e. {@code Main.main.undoredo.add(result.getCommand())}.
486 *
487 * @param layer the layer which the way belongs to. Must not be null.
488 * @param way the way to split. Must not be null.
489 * @param wayChunks the list of way chunks into the way is split. Must not be null.
490 * @param selection The list of currently selected primitives
491 * @return the result from the split operation
[3152]492 */
[8540]493 public static SplitWayResult splitWay(OsmDataLayer layer, Way way, List<List<Node>> wayChunks,
494 Collection<? extends OsmPrimitive> selection) {
[8955]495 return splitWay(layer, way, wayChunks, selection, Strategy.keepLongestChunk());
496 }
497
498 /**
499 * Splits the way {@code way} into chunks of {@code wayChunks} and replies
500 * the result of this process in an instance of {@link SplitWayResult}.
501 * The {@link org.openstreetmap.josm.actions.SplitWayAction.Strategy} is used to determine which
502 * way chunk should reuse the old id and its history.
503 *
504 * Note that changes are not applied to the data yet. You have to
505 * submit the command in {@link SplitWayResult#getCommand()} first,
506 * i.e. {@code Main.main.undoredo.add(result.getCommand())}.
507 *
508 * @param layer the layer which the way belongs to. Must not be null.
509 * @param way the way to split. Must not be null.
510 * @param wayChunks the list of way chunks into the way is split. Must not be null.
511 * @param selection The list of currently selected primitives
512 * @param splitStrategy The strategy used to determine which way chunk should reuse the old id and its history
513 * @return the result from the split operation
514 * @since 8954
515 */
516 public static SplitWayResult splitWay(OsmDataLayer layer, Way way, List<List<Node>> wayChunks,
517 Collection<? extends OsmPrimitive> selection, Strategy splitStrategy) {
[1169]518 // build a list of commands, and also a new selection list
[8886]519 final List<OsmPrimitive> newSelection = new ArrayList<>(selection.size() + wayChunks.size());
[3392]520 newSelection.addAll(selection);
[1023]521
[8886]522 // Create all potential new ways
523 final List<Way> newWays = createNewWaysFromChunks(way, wayChunks);
524
525 // Determine which part reuses the existing way
[8955]526 final Way wayToKeep = splitStrategy.determineWayToKeep(newWays);
[8886]527
[10131]528 return wayToKeep != null ? doSplitWay(layer, way, wayToKeep, newWays, newSelection) : null;
[8886]529 }
530
531 static SplitWayResult doSplitWay(OsmDataLayer layer, Way way, Way wayToKeep, List<Way> newWays,
532 List<OsmPrimitive> newSelection) {
533
534 Collection<Command> commandList = new ArrayList<>(newWays.size());
[4254]535 Collection<String> nowarnroles = Main.pref.getCollection("way.split.roles.nowarn",
[5041]536 Arrays.asList("outer", "inner", "forward", "backward", "north", "south", "east", "west"));
[1023]537
[8886]538 // Change the original way
539 final Way changedWay = new Way(way);
540 changedWay.setNodes(wayToKeep.getNodes());
[2521]541 commandList.add(new ChangeCommand(way, changedWay));
[3392]542 if (!newSelection.contains(way)) {
543 newSelection.add(way);
544 }
[8965]545 final int indexOfWayToKeep = newWays.indexOf(wayToKeep);
[8886]546 newWays.remove(wayToKeep);
[626]547
[9999]548 newSelection.addAll(newWays);
[8886]549 for (Way wayToAdd : newWays) {
[7534]550 commandList.add(new AddCommand(layer, wayToAdd));
[1169]551 }
[8886]552
[3152]553 boolean warnmerole = false;
554 boolean warnme = false;
[1169]555 // now copy all relations to new way also
[1814]556
[2521]557 for (Relation r : OsmPrimitive.getFilteredList(way.getReferrers(), Relation.class)) {
[2120]558 if (!r.isUsable()) {
[1809]559 continue;
560 }
[1600]561 Relation c = null;
[1843]562 String type = r.get("type");
563 if (type == null) {
564 type = "";
565 }
[1600]566
[10001]567 int ic = 0;
568 int ir = 0;
[2764]569 List<RelationMember> relationMembers = r.getMembers();
570 for (RelationMember rm: relationMembers) {
[2628]571 if (rm.isWay() && rm.getMember() == way) {
572 boolean insert = true;
[9442]573 if ("restriction".equals(type) || "destination_sign".equals(type)) {
[2628]574 /* this code assumes the restriction is correct. No real error checking done */
575 String role = rm.getRole();
[8510]576 if ("from".equals(role) || "to".equals(role)) {
[9968]577 OsmPrimitive via = findVia(r, type);
[7005]578 List<Node> nodes = new ArrayList<>();
[7534]579 if (via != null) {
580 if (via instanceof Node) {
[8510]581 nodes.add((Node) via);
[7534]582 } else if (via instanceof Way) {
[8510]583 nodes.add(((Way) via).lastNode());
584 nodes.add(((Way) via).firstNode());
[2628]585 }
586 }
587 Way res = null;
[7534]588 for (Node n : nodes) {
[8510]589 if (changedWay.isFirstLastNode(n)) {
[2628]590 res = way;
591 }
592 }
[7534]593 if (res == null) {
[2628]594 for (Way wayToAdd : newWays) {
[8510]595 for (Node n : nodes) {
596 if (wayToAdd.isFirstLastNode(n)) {
[2628]597 res = wayToAdd;
598 }
599 }
600 }
[7534]601 if (res != null) {
[2628]602 if (c == null) {
603 c = new Relation(r);
604 }
605 c.addMember(new RelationMember(role, res));
606 c.removeMembersFor(way);
607 insert = false;
608 }
[2764]609 } else {
610 insert = false;
[2628]611 }
[8510]612 } else if (!"via".equals(role)) {
[1706]613 warnme = true;
[2764]614 }
[7534]615 } else if (!("route".equals(type)) && !("multipolygon".equals(type))) {
[2628]616 warnme = true;
617 }
618 if (c == null) {
619 c = new Relation(r);
620 }
[1600]621
[7534]622 if (insert) {
[4254]623 if (rm.hasRole() && !nowarnroles.contains(rm.getRole())) {
[2764]624 warnmerole = true;
625 }
626
627 Boolean backwards = null;
628 int k = 1;
[10001]629 while (ir - k >= 0 || ir + k < relationMembers.size()) {
630 if ((ir - k >= 0) && relationMembers.get(ir - k).isWay()) {
631 Way w = relationMembers.get(ir - k).getWay();
[2764]632 if ((w.lastNode() == way.firstNode()) || w.firstNode() == way.firstNode()) {
[8846]633 backwards = Boolean.FALSE;
[2764]634 } else if ((w.firstNode() == way.lastNode()) || w.lastNode() == way.lastNode()) {
[8846]635 backwards = Boolean.TRUE;
[2764]636 }
637 break;
638 }
[10001]639 if ((ir + k < relationMembers.size()) && relationMembers.get(ir + k).isWay()) {
640 Way w = relationMembers.get(ir + k).getWay();
[2764]641 if ((w.lastNode() == way.firstNode()) || w.firstNode() == way.firstNode()) {
[8846]642 backwards = Boolean.TRUE;
[2764]643 } else if ((w.firstNode() == way.lastNode()) || w.lastNode() == way.lastNode()) {
[8846]644 backwards = Boolean.FALSE;
[2764]645 }
646 break;
647 }
[2792]648 k++;
[2764]649 }
650
[10001]651 int j = ic;
[8965]652 final List<Way> waysToAddBefore = newWays.subList(0, indexOfWayToKeep);
653 for (Way wayToAdd : waysToAddBefore) {
[1930]654 RelationMember em = new RelationMember(rm.getRole(), wayToAdd);
[1609]655 j++;
[8965]656 if (Boolean.TRUE.equals(backwards)) {
[10001]657 c.addMember(ic + 1, em);
[8965]658 } else {
659 c.addMember(j - 1, em);
660 }
661 }
662 final List<Way> waysToAddAfter = newWays.subList(indexOfWayToKeep, newWays.size());
663 for (Way wayToAdd : waysToAddAfter) {
664 RelationMember em = new RelationMember(rm.getRole(), wayToAdd);
665 j++;
666 if (Boolean.TRUE.equals(backwards)) {
[10001]667 c.addMember(ic, em);
[1809]668 } else {
[1951]669 c.addMember(j, em);
[1809]670 }
[1169]671 }
[10001]672 ic = j;
[1169]673 }
674 }
[10001]675 ic++;
676 ir++;
[1169]677 }
[1600]678
[1809]679 if (c != null) {
[7534]680 commandList.add(new ChangeCommand(layer, r, c));
[1809]681 }
[1169]682 }
[2381]683 if (warnmerole) {
[6130]684 new Notification(
685 tr("A role based relation membership was copied to all new ways.<br>You should verify this and correct it when necessary."))
686 .setIcon(JOptionPane.WARNING_MESSAGE)
687 .show();
[2381]688 } else if (warnme) {
[6130]689 new Notification(
690 tr("A relation membership was copied to all new ways.<br>You should verify this and correct it when necessary."))
691 .setIcon(JOptionPane.WARNING_MESSAGE)
692 .show();
[1809]693 }
[626]694
[3152]695 return new SplitWayResult(
696 new SequenceCommand(
[6507]697 /* for correct i18n of plural forms - see #9110 */
[9872]698 trn("Split way {0} into {1} part", "Split way {0} into {1} parts", newWays.size() + 1,
699 way.getDisplayName(DefaultNameFormatter.getInstance()), newWays.size() + 1),
[3152]700 commandList
[5570]701 ),
702 newSelection,
703 way,
704 newWays
705 );
[1169]706 }
[626]707
[9968]708 static OsmPrimitive findVia(Relation r, String type) {
709 for (RelationMember rmv : r.getMembers()) {
710 if (("restriction".equals(type) && "via".equals(rmv.getRole()))
711 || ("destination_sign".equals(type) && rmv.hasRole("sign", "intersection"))) {
712 return rmv.getMember();
713 }
714 }
715 return null;
716 }
717
[3152]718 /**
719 * Splits the way {@code way} at the nodes in {@code atNodes} and replies
[5266]720 * the result of this process in an instance of {@link SplitWayResult}.
[3156]721 *
[3152]722 * Note that changes are not applied to the data yet. You have to
[5266]723 * submit the command in {@link SplitWayResult#getCommand()} first,
[3152]724 * i.e. {@code Main.main.undoredo.add(result.getCommand())}.
[3156]725 *
[3152]726 * Replies null if the way couldn't be split at the given nodes.
[3156]727 *
[3152]728 * @param layer the layer which the way belongs to. Must not be null.
729 * @param way the way to split. Must not be null.
730 * @param atNodes the list of nodes where the way is split. Must not be null.
[5401]731 * @param selection The list of currently selected primitives
[3152]732 * @return the result from the split operation
733 */
[6889]734 public static SplitWayResult split(OsmDataLayer layer, Way way, List<Node> atNodes, Collection<? extends OsmPrimitive> selection) {
[3152]735 List<List<Node>> chunks = buildSplitChunks(way, atNodes);
[10174]736 return chunks != null ? splitWay(layer, way, chunks, selection) : null;
[3152]737 }
738
[1820]739 @Override
740 protected void updateEnabledState() {
[10548]741 updateEnabledStateOnCurrentSelection();
[2256]742 }
743
744 @Override
745 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
746 if (selection == null) {
747 setEnabled(false);
[1820]748 return;
749 }
[3392]750 for (OsmPrimitive primitive: selection) {
751 if (primitive instanceof Node) {
752 setEnabled(true); // Selection still can be wrong, but let SplitWayAction process and tell user what's wrong
753 return;
754 }
755 }
756 setEnabled(false);
[1169]757 }
[626]758}
Note: See TracBrowser for help on using the repository browser.