Changeset 3152 in josm for trunk/src/org/openstreetmap
- Timestamp:
- 2010-03-23T13:02:14+01:00 (15 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/actions/SplitWayAction.java
r2792 r3152 32 32 import org.openstreetmap.josm.data.osm.RelationMember; 33 33 import org.openstreetmap.josm.data.osm.Way; 34 import org.openstreetmap.josm.data.osm.visitor.AbstractVisitor;35 import org.openstreetmap.josm.data.osm.visitor.Visitor;36 34 import org.openstreetmap.josm.gui.DefaultNameFormatter; 35 import org.openstreetmap.josm.gui.layer.OsmDataLayer; 36 import org.openstreetmap.josm.tools.CheckParameterUtil; 37 37 import org.openstreetmap.josm.tools.Shortcut; 38 38 … … 46 46 public class SplitWayAction extends JosmAction { 47 47 48 private Way selectedWay;49 private List<Node> selectedNodes;50 48 51 49 public static class SplitWayResult { 52 50 private final Command command; 53 51 private final List<? extends PrimitiveId> newSelection; 54 55 public SplitWayResult(Command command, List<? extends PrimitiveId> newSelection) { 52 private Way originalWay; 53 private List<Way> newWays; 54 55 public SplitWayResult(Command command, List<? extends PrimitiveId> newSelection, Way originalWay, List<Way> newWays) { 56 56 this.command = command; 57 57 this.newSelection = newSelection; 58 this.originalWay = originalWay; 59 this.newWays = newWays; 58 60 } 59 61 … … 64 66 public List<? extends PrimitiveId> getNewSelection() { 65 67 return newSelection; 68 } 69 70 public Way getOriginalWay() { 71 return originalWay; 72 } 73 74 public List<Way> getNewWays() { 75 return newWays; 66 76 } 67 77 } … … 85 95 86 96 Collection<OsmPrimitive> selection = getCurrentDataSet().getSelected(); 97 98 List<Node> selectedNodes = OsmPrimitive.getFilteredList(selection, Node.class); 99 List<Way> selectedWays = OsmPrimitive.getFilteredList(selection, Way.class); 87 100 88 101 if (!checkSelection(selection)) { … … 96 109 } 97 110 98 selectedWay = null; 99 selectedNodes = null; 100 101 Visitor splitVisitor = new AbstractVisitor() { 102 public void visit(Node n) { 103 if (selectedNodes == null) { 104 selectedNodes = new LinkedList<Node>(); 105 } 106 selectedNodes.add(n); 107 } 108 public void visit(Way w) { 109 selectedWay = w; 110 } 111 public void visit(Relation e) { 112 // enties are not considered 113 } 114 }; 115 116 for (OsmPrimitive p : selection) { 117 p.visit(splitVisitor); 111 112 Way selectedWay = null; 113 if (!selectedWays.isEmpty()){ 114 selectedWay = selectedWays.get(0); 118 115 } 119 116 120 117 // If only nodes are selected, try to guess which way to split. This works if there 121 118 // is exactly one way that all nodes are part of. 122 if (selectedWay == null && selectedNodes != null) {119 if (selectedWay == null && !selectedNodes.isEmpty()) { 123 120 Map<Way, Integer> wayOccurenceCounter = new HashMap<Way, Integer>(); 124 121 for (Node n : selectedNodes) { … … 177 174 178 175 // If a way and nodes are selected, verify that the nodes are part of the way. 179 } else if (selectedWay != null && selectedNodes != null) {176 } else if (selectedWay != null && !selectedNodes.isEmpty()) { 180 177 181 178 HashSet<Node> nds = new HashSet<Node>(selectedNodes); 182 for (Node n : selectedWay.getNodes()) { 183 nds.remove(n); 184 } 179 nds.removeAll(selectedWay.getNodes()); 185 180 if (!nds.isEmpty()) { 186 181 JOptionPane.showMessageDialog(Main.parent, … … 194 189 } 195 190 196 // and then do the work. 197 splitWay(); 191 List<List<Node>> wayChunks = buildSplitChunks(selectedWay, selectedNodes); 192 SplitWayResult result = splitWay(getEditLayer(),selectedWay, wayChunks); 193 Main.main.undoRedo.add(result.getCommand()); 194 getCurrentDataSet().setSelected(result.getNewSelection()); 198 195 } 199 196 … … 220 217 221 218 /** 222 * Split a way into two or more parts, starting at a selected node. 219 * Splits the nodes of {@code wayToSplit} into a list of node sequences 220 * which are separated at the nodes in {@code splitPoints}. 221 * 222 * This method displays warning messages if {@code wayToSplit} and/or 223 * {@code splitPoints} aren't consistent. 224 * 225 * Returns null, if building the split chunks fails. 226 * 227 * @param wayToSplit the way to split. Must not be null. 228 * @param splitPoints the nodes where the way is split. Must not be null. 229 * @return the list of chunks 223 230 */ 224 private void splitWay() { 225 // We take our way's list of nodes and copy them to a way chunk (a 226 // list of nodes). Whenever we stumble upon a selected node, we start 227 // a new way chunk. 228 229 Set<Node> nodeSet = new HashSet<Node>(selectedNodes); 231 static public List<List<Node>> buildSplitChunks(Way wayToSplit, List<Node> splitPoints){ 232 CheckParameterUtil.ensureParameterNotNull(wayToSplit, "wayToSplit"); 233 CheckParameterUtil.ensureParameterNotNull(splitPoints, "splitPoints"); 234 235 Set<Node> nodeSet = new HashSet<Node>(splitPoints); 230 236 List<List<Node>> wayChunks = new LinkedList<List<Node>>(); 231 237 List<Node> currentWayChunk = new ArrayList<Node>(); 232 238 wayChunks.add(currentWayChunk); 233 239 234 Iterator<Node> it = selectedWay.getNodes().iterator();240 Iterator<Node> it = wayToSplit.getNodes().iterator(); 235 241 while (it.hasNext()) { 236 242 Node currentNode = it.next(); … … 259 265 tr("Warning"), 260 266 JOptionPane.WARNING_MESSAGE); 261 return ;267 return null; 262 268 } 263 269 lastWayChunk.remove(lastWayChunk.size() - 1); … … 281 287 JOptionPane.WARNING_MESSAGE); 282 288 } 283 return; 284 } 285 //Main.debug("wayChunks.size(): " + wayChunks.size()); 286 //Main.debug("way id: " + selectedWay.id); 287 288 SplitWayResult result = splitWay(selectedWay, wayChunks); 289 Main.main.undoRedo.add(result.getCommand()); 290 getCurrentDataSet().setSelected(result.getNewSelection()); 291 } 292 293 public static SplitWayResult splitWay(Way way, List<List<Node>> wayChunks) { 289 return null; 290 } 291 return wayChunks; 292 } 293 294 /** 295 * Splits a way 296 * @param layer 297 * @param way 298 * @param wayChunks 299 * @return 300 */ 301 public static SplitWayResult splitWay(OsmDataLayer layer, Way way, List<List<Node>> wayChunks) { 294 302 // build a list of commands, and also a new selection list 295 303 Collection<Command> commandList = new ArrayList<Command>(wayChunks.size()); … … 304 312 newSelection.add(way); 305 313 306 Collection<Way> newWays = new ArrayList<Way>();314 List<Way> newWays = new ArrayList<Way>(); 307 315 // Second, create new ways 308 316 while (chunkIt.hasNext()) { … … 311 319 newWays.add(wayToAdd); 312 320 wayToAdd.setNodes(chunkIt.next()); 313 commandList.add(new AddCommand( wayToAdd));321 commandList.add(new AddCommand(layer,wayToAdd)); 314 322 //Main.debug("wayToAdd: " + wayToAdd); 315 323 newSelection.add(wayToAdd); 316 324 317 325 } 318 Boolean warnmerole = false;319 Boolean warnme = false;326 boolean warnmerole = false; 327 boolean warnme = false; 320 328 // now copy all relations to new way also 321 329 … … 442 450 443 451 if (c != null) { 444 commandList.add(new ChangeCommand( r, c));452 commandList.add(new ChangeCommand(layer,r, c)); 445 453 } 446 454 } … … 459 467 } 460 468 461 return new SplitWayResult(new SequenceCommand(tr("Split way {0} into {1} parts", 462 way.getDisplayName(DefaultNameFormatter.getInstance()), 463 wayChunks.size()), 464 commandList), newSelection); 469 return new SplitWayResult( 470 new SequenceCommand( 471 tr("Split way {0} into {1} parts", way.getDisplayName(DefaultNameFormatter.getInstance()),wayChunks.size()), 472 commandList 473 ), 474 newSelection, 475 way, 476 newWays 477 ); 478 } 479 480 /** 481 * Splits the way {@code way} at the nodes in {@code atNodes} and replies 482 * the result of this process in an instance of {@see SplitWayResult}. 483 * 484 * Note that changes are not applied to the data yet. You have to 485 * submit the command in {@see SplitWayResult#getCommand()} first, 486 * i.e. {@code Main.main.undoredo.add(result.getCommand())}. 487 * 488 * Replies null if the way couldn't be split at the given nodes. 489 * 490 * @param layer the layer which the way belongs to. Must not be null. 491 * @param way the way to split. Must not be null. 492 * @param atNodes the list of nodes where the way is split. Must not be null. 493 * @return the result from the split operation 494 */ 495 static public SplitWayResult split(OsmDataLayer layer, Way way, List<Node> atNodes){ 496 List<List<Node>> chunks = buildSplitChunks(way, atNodes); 497 if (chunks == null) return null; 498 return splitWay(layer,way, chunks); 465 499 } 466 500 -
trunk/src/org/openstreetmap/josm/command/ChangeCommand.java
r2025 r3152 14 14 import org.openstreetmap.josm.data.osm.OsmPrimitiveType; 15 15 import org.openstreetmap.josm.gui.DefaultNameFormatter; 16 import org.openstreetmap.josm.gui.layer.OsmDataLayer; 16 17 import org.openstreetmap.josm.tools.ImageProvider; 17 18 … … 27 28 private final OsmPrimitive newOsm; 28 29 30 29 31 public ChangeCommand(OsmPrimitive osm, OsmPrimitive newOsm) { 30 32 super(); 33 this.osm = osm; 34 this.newOsm = newOsm; 35 } 36 37 public ChangeCommand(OsmDataLayer layer, OsmPrimitive osm, OsmPrimitive newOsm) { 38 super(layer); 31 39 this.osm = osm; 32 40 this.newOsm = newOsm; … … 47 55 String msg = ""; 48 56 switch(OsmPrimitiveType.from(osm)) { 49 50 51 57 case NODE: msg = marktr("Change node {0}"); break; 58 case WAY: msg = marktr("Change way {0}"); break; 59 case RELATION: msg = marktr("Change relation {0}"); break; 52 60 } 53 61 return new DefaultMutableTreeNode( -
trunk/src/org/openstreetmap/josm/command/DeleteCommand.java
r3055 r3152 407 407 chunks.add(n1); 408 408 chunks.add(n2); 409 return SplitWayAction.splitWay( ws.way, chunks).getCommand();409 return SplitWayAction.splitWay(layer,ws.way, chunks).getCommand(); 410 410 } 411 411 }
Note:
See TracChangeset
for help on using the changeset viewer.