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