[626] | 1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
| 2 | package org.openstreetmap.josm.actions;
|
---|
| 3 |
|
---|
[2412] | 4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
[626] | 5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 6 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
| 7 |
|
---|
| 8 | import java.awt.event.ActionEvent;
|
---|
| 9 | import java.awt.event.KeyEvent;
|
---|
| 10 | import java.util.ArrayList;
|
---|
[4254] | 11 | import java.util.Arrays;
|
---|
[626] | 12 | import java.util.Collection;
|
---|
[5570] | 13 | import java.util.Collections;
|
---|
[626] | 14 | import java.util.HashSet;
|
---|
| 15 | import java.util.Iterator;
|
---|
| 16 | import java.util.LinkedList;
|
---|
| 17 | import java.util.List;
|
---|
| 18 | import java.util.Set;
|
---|
| 19 |
|
---|
| 20 | import javax.swing.JOptionPane;
|
---|
| 21 |
|
---|
| 22 | import org.openstreetmap.josm.Main;
|
---|
| 23 | import org.openstreetmap.josm.command.AddCommand;
|
---|
| 24 | import org.openstreetmap.josm.command.ChangeCommand;
|
---|
| 25 | import org.openstreetmap.josm.command.Command;
|
---|
| 26 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
| 27 | import org.openstreetmap.josm.data.osm.Node;
|
---|
| 28 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
[2521] | 29 | import org.openstreetmap.josm.data.osm.PrimitiveId;
|
---|
[626] | 30 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
[668] | 31 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
[626] | 32 | import org.openstreetmap.josm.data.osm.Way;
|
---|
[1990] | 33 | import org.openstreetmap.josm.gui.DefaultNameFormatter;
|
---|
[3152] | 34 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
| 35 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
[1084] | 36 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
[626] | 37 |
|
---|
| 38 | /**
|
---|
| 39 | * Splits a way into multiple ways (all identical except for their node list).
|
---|
[1023] | 40 | *
|
---|
[626] | 41 | * Ways are just split at the selected nodes. The nodes remain in their
|
---|
| 42 | * original order. Selected nodes at the end of a way are ignored.
|
---|
| 43 | */
|
---|
| 44 |
|
---|
[1820] | 45 | public class SplitWayAction extends JosmAction {
|
---|
[626] | 46 |
|
---|
[5401] | 47 | /**
|
---|
| 48 | * Represents the result of a {@link SplitWayAction}
|
---|
| 49 | * @see SplitWayAction#splitWay
|
---|
| 50 | * @see SplitWayAction#split
|
---|
| 51 | */
|
---|
[2521] | 52 | public static class SplitWayResult {
|
---|
| 53 | private final Command command;
|
---|
| 54 | private final List<? extends PrimitiveId> newSelection;
|
---|
[3152] | 55 | private Way originalWay;
|
---|
| 56 | private List<Way> newWays;
|
---|
[2521] | 57 |
|
---|
[5401] | 58 | /**
|
---|
| 59 | * @param command The command to be performed to split the way (which is saved for later retrieval by the {@link #getCommand} method)
|
---|
| 60 | * @param newSelection The new list of selected primitives ids (which is saved for later retrieval by the {@link #getNewSelection} method)
|
---|
| 61 | * @param originalWay The original way being split (which is saved for later retrieval by the {@link #getOriginalWay} method)
|
---|
| 62 | * @param newWays The resulting new ways (which is saved for later retrieval by the {@link #getOriginalWay} method)
|
---|
| 63 | */
|
---|
[3152] | 64 | public SplitWayResult(Command command, List<? extends PrimitiveId> newSelection, Way originalWay, List<Way> newWays) {
|
---|
[2521] | 65 | this.command = command;
|
---|
| 66 | this.newSelection = newSelection;
|
---|
[3152] | 67 | this.originalWay = originalWay;
|
---|
| 68 | this.newWays = newWays;
|
---|
[2521] | 69 | }
|
---|
| 70 |
|
---|
[5401] | 71 | /**
|
---|
| 72 | * Replies the command to be performed to split the way
|
---|
| 73 | * @return The command to be performed to split the way
|
---|
| 74 | */
|
---|
[2521] | 75 | public Command getCommand() {
|
---|
| 76 | return command;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
[5401] | 79 | /**
|
---|
| 80 | * Replies the new list of selected primitives ids
|
---|
| 81 | * @return The new list of selected primitives ids
|
---|
| 82 | */
|
---|
[2521] | 83 | public List<? extends PrimitiveId> getNewSelection() {
|
---|
| 84 | return newSelection;
|
---|
| 85 | }
|
---|
[3152] | 86 |
|
---|
[5401] | 87 | /**
|
---|
| 88 | * Replies the original way being split
|
---|
| 89 | * @return The original way being split
|
---|
| 90 | */
|
---|
[3152] | 91 | public Way getOriginalWay() {
|
---|
| 92 | return originalWay;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
[5401] | 95 | /**
|
---|
| 96 | * Replies the resulting new ways
|
---|
| 97 | * @return The resulting new ways
|
---|
| 98 | */
|
---|
[3152] | 99 | public List<Way> getNewWays() {
|
---|
| 100 | return newWays;
|
---|
| 101 | }
|
---|
[2521] | 102 | }
|
---|
| 103 |
|
---|
[1169] | 104 | /**
|
---|
| 105 | * Create a new SplitWayAction.
|
---|
| 106 | */
|
---|
| 107 | public SplitWayAction() {
|
---|
| 108 | super(tr("Split Way"), "splitway", tr("Split a way at the selected node."),
|
---|
[4982] | 109 | Shortcut.registerShortcut("tools:splitway", tr("Tool: {0}", tr("Split Way")), KeyEvent.VK_P, Shortcut.DIRECT), true);
|
---|
[2323] | 110 | putValue("help", ht("/Action/SplitWay"));
|
---|
[1169] | 111 | }
|
---|
[626] | 112 |
|
---|
[1169] | 113 | /**
|
---|
| 114 | * Called when the action is executed.
|
---|
| 115 | *
|
---|
| 116 | * This method performs an expensive check whether the selection clearly defines one
|
---|
| 117 | * of the split actions outlined above, and if yes, calls the splitWay method.
|
---|
| 118 | */
|
---|
| 119 | public void actionPerformed(ActionEvent e) {
|
---|
[626] | 120 |
|
---|
[1814] | 121 | Collection<OsmPrimitive> selection = getCurrentDataSet().getSelected();
|
---|
[626] | 122 |
|
---|
[3152] | 123 | List<Node> selectedNodes = OsmPrimitive.getFilteredList(selection, Node.class);
|
---|
| 124 | List<Way> selectedWays = OsmPrimitive.getFilteredList(selection, Way.class);
|
---|
[3392] | 125 | List<Relation> selectedRelations = OsmPrimitive.getFilteredList(selection, Relation.class);
|
---|
| 126 | List<Way> applicableWays = getApplicableWays(selectedWays, selectedNodes);
|
---|
[3152] | 127 |
|
---|
[3392] | 128 | if (applicableWays == null) {
|
---|
[2017] | 129 | JOptionPane.showMessageDialog(
|
---|
[1857] | 130 | Main.parent,
|
---|
[3392] | 131 | tr("The current selection cannot be used for splitting - no node is selected."),
|
---|
[1857] | 132 | tr("Warning"),
|
---|
[3392] | 133 | JOptionPane.WARNING_MESSAGE);
|
---|
[1169] | 134 | return;
|
---|
[3392] | 135 | } else if (applicableWays.isEmpty()) {
|
---|
| 136 | JOptionPane.showMessageDialog(Main.parent,
|
---|
| 137 | tr("The selected nodes do not share the same way."),
|
---|
| 138 | tr("Warning"),
|
---|
| 139 | JOptionPane.WARNING_MESSAGE);
|
---|
| 140 | return;
|
---|
[1169] | 141 | }
|
---|
[626] | 142 |
|
---|
[5401] | 143 | // If several ways have been found, remove ways that doesn't have selected node in the middle
|
---|
| 144 | if (applicableWays.size() > 1) {
|
---|
[3392] | 145 | WAY_LOOP:
|
---|
[5401] | 146 | for (Iterator<Way> it = applicableWays.iterator(); it.hasNext();) {
|
---|
[3392] | 147 | Way w = it.next();
|
---|
| 148 | for (Node n : selectedNodes) {
|
---|
[5401] | 149 | if (!w.isInnerNode(n)) {
|
---|
[3392] | 150 | it.remove();
|
---|
| 151 | continue WAY_LOOP;
|
---|
[1169] | 152 | }
|
---|
| 153 | }
|
---|
| 154 | }
|
---|
[3392] | 155 | }
|
---|
[626] | 156 |
|
---|
[3392] | 157 | if (applicableWays.isEmpty()) {
|
---|
| 158 | JOptionPane.showMessageDialog(Main.parent,
|
---|
| 159 | trn("The selected node is not in the middle of any way.",
|
---|
| 160 | "The selected nodes are not in the middle of any way.",
|
---|
| 161 | selectedNodes.size()),
|
---|
| 162 | tr("Warning"),
|
---|
| 163 | JOptionPane.WARNING_MESSAGE);
|
---|
| 164 | return;
|
---|
| 165 | } else if (applicableWays.size() > 1) {
|
---|
| 166 | JOptionPane.showMessageDialog(Main.parent,
|
---|
| 167 | trn("There is more than one way using the node you selected. Please select the way also.",
|
---|
| 168 | "There is more than one way using the nodes you selected. Please select the way also.",
|
---|
| 169 | selectedNodes.size()),
|
---|
| 170 | tr("Warning"),
|
---|
| 171 | JOptionPane.WARNING_MESSAGE);
|
---|
| 172 | return;
|
---|
| 173 | }
|
---|
[626] | 174 |
|
---|
[3392] | 175 | // Finally, applicableWays contains only one perfect way
|
---|
| 176 | Way selectedWay = applicableWays.get(0);
|
---|
[626] | 177 |
|
---|
[3152] | 178 | List<List<Node>> wayChunks = buildSplitChunks(selectedWay, selectedNodes);
|
---|
[3156] | 179 | if (wayChunks != null) {
|
---|
[3392] | 180 | List<OsmPrimitive> sel = new ArrayList<OsmPrimitive>(selectedWays.size() + selectedRelations.size());
|
---|
| 181 | sel.addAll(selectedWays);
|
---|
| 182 | sel.addAll(selectedRelations);
|
---|
| 183 | SplitWayResult result = splitWay(getEditLayer(),selectedWay, wayChunks, sel);
|
---|
[3156] | 184 | Main.main.undoRedo.add(result.getCommand());
|
---|
| 185 | getCurrentDataSet().setSelected(result.getNewSelection());
|
---|
| 186 | }
|
---|
[1169] | 187 | }
|
---|
[626] | 188 |
|
---|
[3392] | 189 | private List<Way> getApplicableWays(List<Way> selectedWays, List<Node> selectedNodes) {
|
---|
| 190 | if (selectedNodes.isEmpty())
|
---|
| 191 | return null;
|
---|
| 192 |
|
---|
[5570] | 193 | // Special case - one of the selected ways touches (not cross) way that we want to split
|
---|
| 194 | if (selectedNodes.size() == 1) {
|
---|
| 195 | Node n = selectedNodes.get(0);
|
---|
| 196 | List<Way> referedWays = OsmPrimitive.getFilteredList(n.getReferrers(), Way.class);
|
---|
| 197 | Way inTheMiddle = null;
|
---|
| 198 | boolean foundSelected = false;
|
---|
| 199 | for (Way w: referedWays) {
|
---|
| 200 | if (selectedWays.contains(w)) {
|
---|
| 201 | foundSelected = true;
|
---|
| 202 | }
|
---|
| 203 | if (w.getNode(0) != n && w.getNode(w.getNodesCount() - 1) != n) {
|
---|
| 204 | if (inTheMiddle == null) {
|
---|
| 205 | inTheMiddle = w;
|
---|
| 206 | } else {
|
---|
| 207 | inTheMiddle = null;
|
---|
| 208 | break;
|
---|
| 209 | }
|
---|
| 210 | }
|
---|
| 211 | }
|
---|
| 212 | if (foundSelected && inTheMiddle != null)
|
---|
| 213 | return Collections.singletonList(inTheMiddle);
|
---|
| 214 | }
|
---|
| 215 |
|
---|
[3392] | 216 | // List of ways shared by all nodes
|
---|
| 217 | List<Way> result = new ArrayList<Way>(OsmPrimitive.getFilteredList(selectedNodes.get(0).getReferrers(), Way.class));
|
---|
| 218 | for (int i=1; i<selectedNodes.size(); i++) {
|
---|
| 219 | List<OsmPrimitive> ref = selectedNodes.get(i).getReferrers();
|
---|
[5401] | 220 | for (Iterator<Way> it = result.iterator(); it.hasNext(); ) {
|
---|
[3392] | 221 | if (!ref.contains(it.next())) {
|
---|
| 222 | it.remove();
|
---|
| 223 | }
|
---|
| 224 | }
|
---|
[1169] | 225 | }
|
---|
[3392] | 226 |
|
---|
[5401] | 227 | // Remove broken ways
|
---|
| 228 | for (Iterator<Way> it = result.iterator(); it.hasNext(); ) {
|
---|
| 229 | if (it.next().getNodesCount() <= 2) {
|
---|
| 230 | it.remove();
|
---|
[3392] | 231 | }
|
---|
| 232 | }
|
---|
| 233 |
|
---|
| 234 | if (selectedWays.isEmpty())
|
---|
| 235 | return result;
|
---|
| 236 | else {
|
---|
| 237 | // Return only selected ways
|
---|
[5401] | 238 | for (Iterator<Way> it = result.iterator(); it.hasNext(); ) {
|
---|
[3392] | 239 | if (!selectedWays.contains(it.next())) {
|
---|
| 240 | it.remove();
|
---|
| 241 | }
|
---|
| 242 | }
|
---|
| 243 | return result;
|
---|
| 244 | }
|
---|
[1169] | 245 | }
|
---|
[626] | 246 |
|
---|
[1169] | 247 | /**
|
---|
[3152] | 248 | * Splits the nodes of {@code wayToSplit} into a list of node sequences
|
---|
| 249 | * which are separated at the nodes in {@code splitPoints}.
|
---|
[3156] | 250 | *
|
---|
[3152] | 251 | * This method displays warning messages if {@code wayToSplit} and/or
|
---|
| 252 | * {@code splitPoints} aren't consistent.
|
---|
[3156] | 253 | *
|
---|
[3152] | 254 | * Returns null, if building the split chunks fails.
|
---|
[3156] | 255 | *
|
---|
[3152] | 256 | * @param wayToSplit the way to split. Must not be null.
|
---|
| 257 | * @param splitPoints the nodes where the way is split. Must not be null.
|
---|
| 258 | * @return the list of chunks
|
---|
[1169] | 259 | */
|
---|
[3152] | 260 | static public List<List<Node>> buildSplitChunks(Way wayToSplit, List<Node> splitPoints){
|
---|
| 261 | CheckParameterUtil.ensureParameterNotNull(wayToSplit, "wayToSplit");
|
---|
| 262 | CheckParameterUtil.ensureParameterNotNull(splitPoints, "splitPoints");
|
---|
[626] | 263 |
|
---|
[3152] | 264 | Set<Node> nodeSet = new HashSet<Node>(splitPoints);
|
---|
[1169] | 265 | List<List<Node>> wayChunks = new LinkedList<List<Node>>();
|
---|
| 266 | List<Node> currentWayChunk = new ArrayList<Node>();
|
---|
| 267 | wayChunks.add(currentWayChunk);
|
---|
[626] | 268 |
|
---|
[3152] | 269 | Iterator<Node> it = wayToSplit.getNodes().iterator();
|
---|
[1169] | 270 | while (it.hasNext()) {
|
---|
| 271 | Node currentNode = it.next();
|
---|
| 272 | boolean atEndOfWay = currentWayChunk.isEmpty() || !it.hasNext();
|
---|
| 273 | currentWayChunk.add(currentNode);
|
---|
| 274 | if (nodeSet.contains(currentNode) && !atEndOfWay) {
|
---|
| 275 | currentWayChunk = new ArrayList<Node>();
|
---|
| 276 | currentWayChunk.add(currentNode);
|
---|
| 277 | wayChunks.add(currentWayChunk);
|
---|
| 278 | }
|
---|
| 279 | }
|
---|
[626] | 280 |
|
---|
[1169] | 281 | // Handle circular ways specially.
|
---|
| 282 | // If you split at a circular way at two nodes, you just want to split
|
---|
| 283 | // it at these points, not also at the former endpoint.
|
---|
| 284 | // So if the last node is the same first node, join the last and the
|
---|
| 285 | // first way chunk.
|
---|
| 286 | List<Node> lastWayChunk = wayChunks.get(wayChunks.size() - 1);
|
---|
| 287 | if (wayChunks.size() >= 2
|
---|
| 288 | && wayChunks.get(0).get(0) == lastWayChunk.get(lastWayChunk.size() - 1)
|
---|
| 289 | && !nodeSet.contains(wayChunks.get(0).get(0))) {
|
---|
| 290 | if (wayChunks.size() == 2) {
|
---|
[2017] | 291 | JOptionPane.showMessageDialog(
|
---|
[1857] | 292 | Main.parent,
|
---|
| 293 | tr("You must select two or more nodes to split a circular way."),
|
---|
| 294 | tr("Warning"),
|
---|
| 295 | JOptionPane.WARNING_MESSAGE);
|
---|
[3152] | 296 | return null;
|
---|
[1169] | 297 | }
|
---|
| 298 | lastWayChunk.remove(lastWayChunk.size() - 1);
|
---|
| 299 | lastWayChunk.addAll(wayChunks.get(0));
|
---|
| 300 | wayChunks.remove(wayChunks.size() - 1);
|
---|
| 301 | wayChunks.set(0, lastWayChunk);
|
---|
| 302 | }
|
---|
[626] | 303 |
|
---|
[1169] | 304 | if (wayChunks.size() < 2) {
|
---|
[2381] | 305 | if (wayChunks.get(0).get(0) == wayChunks.get(0).get(wayChunks.get(0).size() - 1)) {
|
---|
[2017] | 306 | JOptionPane.showMessageDialog(
|
---|
[1857] | 307 | Main.parent,
|
---|
| 308 | tr("You must select two or more nodes to split a circular way."),
|
---|
| 309 | tr("Warning"),
|
---|
| 310 | JOptionPane.WARNING_MESSAGE);
|
---|
[1809] | 311 | } else {
|
---|
[2017] | 312 | JOptionPane.showMessageDialog(
|
---|
[1857] | 313 | Main.parent,
|
---|
| 314 | tr("The way cannot be split at the selected nodes. (Hint: Select nodes in the middle of the way.)"),
|
---|
| 315 | tr("Warning"),
|
---|
| 316 | JOptionPane.WARNING_MESSAGE);
|
---|
[1809] | 317 | }
|
---|
[3152] | 318 | return null;
|
---|
[1169] | 319 | }
|
---|
[3152] | 320 | return wayChunks;
|
---|
[2521] | 321 | }
|
---|
| 322 |
|
---|
[3152] | 323 | /**
|
---|
[5401] | 324 | * Splits the way {@code way} into chunks of {@code wayChunks} and replies
|
---|
| 325 | * the result of this process in an instance of {@link SplitWayResult}.
|
---|
| 326 | *
|
---|
| 327 | * Note that changes are not applied to the data yet. You have to
|
---|
| 328 | * submit the command in {@link SplitWayResult#getCommand()} first,
|
---|
| 329 | * i.e. {@code Main.main.undoredo.add(result.getCommand())}.
|
---|
| 330 | *
|
---|
| 331 | * @param layer the layer which the way belongs to. Must not be null.
|
---|
| 332 | * @param way the way to split. Must not be null.
|
---|
| 333 | * @param wayChunks the list of way chunks into the way is split. Must not be null.
|
---|
| 334 | * @param selection The list of currently selected primitives
|
---|
| 335 | * @return the result from the split operation
|
---|
[3152] | 336 | */
|
---|
[3392] | 337 | public static SplitWayResult splitWay(OsmDataLayer layer, Way way, List<List<Node>> wayChunks, Collection<? extends OsmPrimitive> selection) {
|
---|
[1169] | 338 | // build a list of commands, and also a new selection list
|
---|
| 339 | Collection<Command> commandList = new ArrayList<Command>(wayChunks.size());
|
---|
[3392] | 340 | List<OsmPrimitive> newSelection = new ArrayList<OsmPrimitive>(selection.size() + wayChunks.size());
|
---|
| 341 | newSelection.addAll(selection);
|
---|
[1023] | 342 |
|
---|
[1169] | 343 | Iterator<List<Node>> chunkIt = wayChunks.iterator();
|
---|
[4254] | 344 | Collection<String> nowarnroles = Main.pref.getCollection("way.split.roles.nowarn",
|
---|
[5041] | 345 | Arrays.asList("outer", "inner", "forward", "backward", "north", "south", "east", "west"));
|
---|
[1023] | 346 |
|
---|
[1169] | 347 | // First, change the original way
|
---|
[2521] | 348 | Way changedWay = new Way(way);
|
---|
[1862] | 349 | changedWay.setNodes(chunkIt.next());
|
---|
[2521] | 350 | commandList.add(new ChangeCommand(way, changedWay));
|
---|
[3392] | 351 | if (!newSelection.contains(way)) {
|
---|
| 352 | newSelection.add(way);
|
---|
| 353 | }
|
---|
[626] | 354 |
|
---|
[3152] | 355 | List<Way> newWays = new ArrayList<Way>();
|
---|
[1169] | 356 | // Second, create new ways
|
---|
| 357 | while (chunkIt.hasNext()) {
|
---|
| 358 | Way wayToAdd = new Way();
|
---|
[2521] | 359 | wayToAdd.setKeys(way.getKeys());
|
---|
[1169] | 360 | newWays.add(wayToAdd);
|
---|
[1898] | 361 | wayToAdd.setNodes(chunkIt.next());
|
---|
[3152] | 362 | commandList.add(new AddCommand(layer,wayToAdd));
|
---|
[1169] | 363 | newSelection.add(wayToAdd);
|
---|
[668] | 364 |
|
---|
[1169] | 365 | }
|
---|
[3152] | 366 | boolean warnmerole = false;
|
---|
| 367 | boolean warnme = false;
|
---|
[1169] | 368 | // now copy all relations to new way also
|
---|
[1814] | 369 |
|
---|
[2521] | 370 | for (Relation r : OsmPrimitive.getFilteredList(way.getReferrers(), Relation.class)) {
|
---|
[2120] | 371 | if (!r.isUsable()) {
|
---|
[1809] | 372 | continue;
|
---|
| 373 | }
|
---|
[1600] | 374 | Relation c = null;
|
---|
[1843] | 375 | String type = r.get("type");
|
---|
| 376 | if (type == null) {
|
---|
| 377 | type = "";
|
---|
| 378 | }
|
---|
[1600] | 379 |
|
---|
[3437] | 380 | int i_c = 0, i_r = 0;
|
---|
[2764] | 381 | List<RelationMember> relationMembers = r.getMembers();
|
---|
| 382 | for (RelationMember rm: relationMembers) {
|
---|
[2628] | 383 | if (rm.isWay() && rm.getMember() == way) {
|
---|
| 384 | boolean insert = true;
|
---|
| 385 | if ("restriction".equals(type))
|
---|
| 386 | {
|
---|
| 387 | /* this code assumes the restriction is correct. No real error checking done */
|
---|
| 388 | String role = rm.getRole();
|
---|
| 389 | if("from".equals(role) || "to".equals(role))
|
---|
| 390 | {
|
---|
| 391 | OsmPrimitive via = null;
|
---|
| 392 | for (RelationMember rmv : r.getMembers()) {
|
---|
| 393 | if("via".equals(rmv.getRole())){
|
---|
| 394 | via = rmv.getMember();
|
---|
| 395 | }
|
---|
| 396 | }
|
---|
| 397 | List<Node> nodes = new ArrayList<Node>();
|
---|
| 398 | if(via != null) {
|
---|
| 399 | if(via instanceof Node) {
|
---|
| 400 | nodes.add((Node)via);
|
---|
| 401 | } else if(via instanceof Way) {
|
---|
| 402 | nodes.add(((Way)via).lastNode());
|
---|
| 403 | nodes.add(((Way)via).firstNode());
|
---|
| 404 | }
|
---|
| 405 | }
|
---|
| 406 | Way res = null;
|
---|
| 407 | for(Node n : nodes) {
|
---|
| 408 | if(changedWay.isFirstLastNode(n)) {
|
---|
| 409 | res = way;
|
---|
| 410 | }
|
---|
| 411 | }
|
---|
| 412 | if(res == null)
|
---|
| 413 | {
|
---|
| 414 | for (Way wayToAdd : newWays) {
|
---|
| 415 | for(Node n : nodes) {
|
---|
| 416 | if(wayToAdd.isFirstLastNode(n)) {
|
---|
| 417 | res = wayToAdd;
|
---|
| 418 | }
|
---|
| 419 | }
|
---|
| 420 | }
|
---|
| 421 | if(res != null)
|
---|
| 422 | {
|
---|
| 423 | if (c == null) {
|
---|
| 424 | c = new Relation(r);
|
---|
| 425 | }
|
---|
| 426 | c.addMember(new RelationMember(role, res));
|
---|
| 427 | c.removeMembersFor(way);
|
---|
| 428 | insert = false;
|
---|
| 429 | }
|
---|
[2764] | 430 | } else {
|
---|
| 431 | insert = false;
|
---|
[2628] | 432 | }
|
---|
| 433 | }
|
---|
[2764] | 434 | else if(!"via".equals(role)) {
|
---|
[1706] | 435 | warnme = true;
|
---|
[2764] | 436 | }
|
---|
[2628] | 437 | }
|
---|
| 438 | else if (!("route".equals(type)) && !("multipolygon".equals(type))) {
|
---|
| 439 | warnme = true;
|
---|
| 440 | }
|
---|
| 441 | if (c == null) {
|
---|
| 442 | c = new Relation(r);
|
---|
| 443 | }
|
---|
[1600] | 444 |
|
---|
[2628] | 445 | if(insert)
|
---|
| 446 | {
|
---|
[4254] | 447 | if (rm.hasRole() && !nowarnroles.contains(rm.getRole())) {
|
---|
[2764] | 448 | warnmerole = true;
|
---|
| 449 | }
|
---|
| 450 |
|
---|
| 451 | Boolean backwards = null;
|
---|
| 452 | int k = 1;
|
---|
[3437] | 453 | while (i_r - k >= 0 || i_r + k < relationMembers.size()) {
|
---|
| 454 | if ((i_r - k >= 0) && relationMembers.get(i_r - k).isWay()){
|
---|
| 455 | Way w = relationMembers.get(i_r - k).getWay();
|
---|
[2764] | 456 | if ((w.lastNode() == way.firstNode()) || w.firstNode() == way.firstNode()) {
|
---|
| 457 | backwards = false;
|
---|
| 458 | } else if ((w.firstNode() == way.lastNode()) || w.lastNode() == way.lastNode()) {
|
---|
| 459 | backwards = true;
|
---|
| 460 | }
|
---|
| 461 | break;
|
---|
| 462 | }
|
---|
[3437] | 463 | if ((i_r + k < relationMembers.size()) && relationMembers.get(i_r + k).isWay()){
|
---|
| 464 | Way w = relationMembers.get(i_r + k).getWay();
|
---|
[2764] | 465 | if ((w.lastNode() == way.firstNode()) || w.firstNode() == way.firstNode()) {
|
---|
| 466 | backwards = true;
|
---|
| 467 | } else if ((w.firstNode() == way.lastNode()) || w.lastNode() == way.lastNode()) {
|
---|
| 468 | backwards = false;
|
---|
| 469 | }
|
---|
| 470 | break;
|
---|
| 471 | }
|
---|
[2792] | 472 | k++;
|
---|
[2764] | 473 | }
|
---|
| 474 |
|
---|
[3437] | 475 | int j = i_c;
|
---|
[2381] | 476 | for (Way wayToAdd : newWays) {
|
---|
[1930] | 477 | RelationMember em = new RelationMember(rm.getRole(), wayToAdd);
|
---|
[1609] | 478 | j++;
|
---|
[2764] | 479 | if ((backwards != null) && backwards) {
|
---|
[3437] | 480 | c.addMember(i_c, em);
|
---|
[1809] | 481 | } else {
|
---|
[1951] | 482 | c.addMember(j, em);
|
---|
[1809] | 483 | }
|
---|
[1169] | 484 | }
|
---|
[3437] | 485 | i_c = j;
|
---|
[1169] | 486 | }
|
---|
| 487 | }
|
---|
[3437] | 488 | i_c++; i_r++;
|
---|
[1169] | 489 | }
|
---|
[1600] | 490 |
|
---|
[1809] | 491 | if (c != null) {
|
---|
[3152] | 492 | commandList.add(new ChangeCommand(layer,r, c));
|
---|
[1809] | 493 | }
|
---|
[1169] | 494 | }
|
---|
[2381] | 495 | if (warnmerole) {
|
---|
[2017] | 496 | JOptionPane.showMessageDialog(
|
---|
[1857] | 497 | Main.parent,
|
---|
| 498 | tr("<html>A role based relation membership was copied to all new ways.<br>You should verify this and correct it when necessary.</html>"),
|
---|
| 499 | tr("Warning"),
|
---|
| 500 | JOptionPane.WARNING_MESSAGE);
|
---|
[2381] | 501 | } else if (warnme) {
|
---|
[2017] | 502 | JOptionPane.showMessageDialog(
|
---|
[1857] | 503 | Main.parent,
|
---|
| 504 | tr("<html>A relation membership was copied to all new ways.<br>You should verify this and correct it when necessary.</html>"),
|
---|
| 505 | tr("Warning"),
|
---|
| 506 | JOptionPane.WARNING_MESSAGE);
|
---|
[1809] | 507 | }
|
---|
[626] | 508 |
|
---|
[3152] | 509 | return new SplitWayResult(
|
---|
| 510 | new SequenceCommand(
|
---|
| 511 | tr("Split way {0} into {1} parts", way.getDisplayName(DefaultNameFormatter.getInstance()),wayChunks.size()),
|
---|
| 512 | commandList
|
---|
[5570] | 513 | ),
|
---|
| 514 | newSelection,
|
---|
| 515 | way,
|
---|
| 516 | newWays
|
---|
| 517 | );
|
---|
[1169] | 518 | }
|
---|
[626] | 519 |
|
---|
[3152] | 520 | /**
|
---|
| 521 | * Splits the way {@code way} at the nodes in {@code atNodes} and replies
|
---|
[5266] | 522 | * the result of this process in an instance of {@link SplitWayResult}.
|
---|
[3156] | 523 | *
|
---|
[3152] | 524 | * Note that changes are not applied to the data yet. You have to
|
---|
[5266] | 525 | * submit the command in {@link SplitWayResult#getCommand()} first,
|
---|
[3152] | 526 | * i.e. {@code Main.main.undoredo.add(result.getCommand())}.
|
---|
[3156] | 527 | *
|
---|
[3152] | 528 | * Replies null if the way couldn't be split at the given nodes.
|
---|
[3156] | 529 | *
|
---|
[3152] | 530 | * @param layer the layer which the way belongs to. Must not be null.
|
---|
| 531 | * @param way the way to split. Must not be null.
|
---|
| 532 | * @param atNodes the list of nodes where the way is split. Must not be null.
|
---|
[5401] | 533 | * @param selection The list of currently selected primitives
|
---|
[3152] | 534 | * @return the result from the split operation
|
---|
| 535 | */
|
---|
[5401] | 536 | static public SplitWayResult split(OsmDataLayer layer, Way way, List<Node> atNodes, Collection<? extends OsmPrimitive> selection) {
|
---|
[3152] | 537 | List<List<Node>> chunks = buildSplitChunks(way, atNodes);
|
---|
| 538 | if (chunks == null) return null;
|
---|
[3392] | 539 | return splitWay(layer,way, chunks, selection);
|
---|
[3152] | 540 | }
|
---|
| 541 |
|
---|
[1820] | 542 | @Override
|
---|
| 543 | protected void updateEnabledState() {
|
---|
| 544 | if (getCurrentDataSet() == null) {
|
---|
| 545 | setEnabled(false);
|
---|
[2256] | 546 | } else {
|
---|
| 547 | updateEnabledState(getCurrentDataSet().getSelected());
|
---|
| 548 | }
|
---|
| 549 | }
|
---|
| 550 |
|
---|
| 551 | @Override
|
---|
| 552 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
---|
| 553 | if (selection == null) {
|
---|
| 554 | setEnabled(false);
|
---|
[1820] | 555 | return;
|
---|
| 556 | }
|
---|
[3392] | 557 | for (OsmPrimitive primitive: selection) {
|
---|
| 558 | if (primitive instanceof Node) {
|
---|
| 559 | setEnabled(true); // Selection still can be wrong, but let SplitWayAction process and tell user what's wrong
|
---|
| 560 | return;
|
---|
| 561 | }
|
---|
| 562 | }
|
---|
| 563 | setEnabled(false);
|
---|
[1169] | 564 | }
|
---|
[626] | 565 | }
|
---|