Changeset 3152 in josm for trunk/src/org/openstreetmap


Ignore:
Timestamp:
2010-03-23T13:02:14+01:00 (15 years ago)
Author:
Gubaer
Message:

SplitWayAction slightly refactored, going to need this somewhere else

Location:
trunk/src/org/openstreetmap/josm
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/SplitWayAction.java

    r2792 r3152  
    3232import org.openstreetmap.josm.data.osm.RelationMember;
    3333import org.openstreetmap.josm.data.osm.Way;
    34 import org.openstreetmap.josm.data.osm.visitor.AbstractVisitor;
    35 import org.openstreetmap.josm.data.osm.visitor.Visitor;
    3634import org.openstreetmap.josm.gui.DefaultNameFormatter;
     35import org.openstreetmap.josm.gui.layer.OsmDataLayer;
     36import org.openstreetmap.josm.tools.CheckParameterUtil;
    3737import org.openstreetmap.josm.tools.Shortcut;
    3838
     
    4646public class SplitWayAction extends JosmAction {
    4747
    48     private Way selectedWay;
    49     private List<Node> selectedNodes;
    5048
    5149    public static class SplitWayResult {
    5250        private final Command command;
    5351        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) {
    5656            this.command = command;
    5757            this.newSelection = newSelection;
     58            this.originalWay = originalWay;
     59            this.newWays = newWays;
    5860        }
    5961
     
    6466        public List<? extends PrimitiveId> getNewSelection() {
    6567            return newSelection;
     68        }
     69
     70        public Way getOriginalWay() {
     71            return originalWay;
     72        }
     73
     74        public List<Way> getNewWays() {
     75            return newWays;
    6676        }
    6777    }
     
    8595
    8696        Collection<OsmPrimitive> selection = getCurrentDataSet().getSelected();
     97
     98        List<Node> selectedNodes = OsmPrimitive.getFilteredList(selection, Node.class);
     99        List<Way> selectedWays = OsmPrimitive.getFilteredList(selection, Way.class);
    87100
    88101        if (!checkSelection(selection)) {
     
    96109        }
    97110
    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);
    118115        }
    119116
    120117        // If only nodes are selected, try to guess which way to split. This works if there
    121118        // is exactly one way that all nodes are part of.
    122         if (selectedWay == null && selectedNodes != null) {
     119        if (selectedWay == null && !selectedNodes.isEmpty()) {
    123120            Map<Way, Integer> wayOccurenceCounter = new HashMap<Way, Integer>();
    124121            for (Node n : selectedNodes) {
     
    177174
    178175            // 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()) {
    180177
    181178            HashSet<Node> nds = new HashSet<Node>(selectedNodes);
    182             for (Node n : selectedWay.getNodes()) {
    183                 nds.remove(n);
    184             }
     179            nds.removeAll(selectedWay.getNodes());
    185180            if (!nds.isEmpty()) {
    186181                JOptionPane.showMessageDialog(Main.parent,
     
    194189        }
    195190
    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());
    198195    }
    199196
     
    220217
    221218    /**
    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
    223230     */
    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);
    230236        List<List<Node>> wayChunks = new LinkedList<List<Node>>();
    231237        List<Node> currentWayChunk = new ArrayList<Node>();
    232238        wayChunks.add(currentWayChunk);
    233239
    234         Iterator<Node> it = selectedWay.getNodes().iterator();
     240        Iterator<Node> it = wayToSplit.getNodes().iterator();
    235241        while (it.hasNext()) {
    236242            Node currentNode = it.next();
     
    259265                        tr("Warning"),
    260266                        JOptionPane.WARNING_MESSAGE);
    261                 return;
     267                return null;
    262268            }
    263269            lastWayChunk.remove(lastWayChunk.size() - 1);
     
    281287                        JOptionPane.WARNING_MESSAGE);
    282288            }
    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) {
    294302        // build a list of commands, and also a new selection list
    295303        Collection<Command> commandList = new ArrayList<Command>(wayChunks.size());
     
    304312        newSelection.add(way);
    305313
    306         Collection<Way> newWays = new ArrayList<Way>();
     314        List<Way> newWays = new ArrayList<Way>();
    307315        // Second, create new ways
    308316        while (chunkIt.hasNext()) {
     
    311319            newWays.add(wayToAdd);
    312320            wayToAdd.setNodes(chunkIt.next());
    313             commandList.add(new AddCommand(wayToAdd));
     321            commandList.add(new AddCommand(layer,wayToAdd));
    314322            //Main.debug("wayToAdd: " + wayToAdd);
    315323            newSelection.add(wayToAdd);
    316324
    317325        }
    318         Boolean warnmerole = false;
    319         Boolean warnme = false;
     326        boolean warnmerole = false;
     327        boolean warnme = false;
    320328        // now copy all relations to new way also
    321329
     
    442450
    443451            if (c != null) {
    444                 commandList.add(new ChangeCommand(r, c));
     452                commandList.add(new ChangeCommand(layer,r, c));
    445453            }
    446454        }
     
    459467        }
    460468
    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);
    465499    }
    466500
  • trunk/src/org/openstreetmap/josm/command/ChangeCommand.java

    r2025 r3152  
    1414import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
    1515import org.openstreetmap.josm.gui.DefaultNameFormatter;
     16import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    1617import org.openstreetmap.josm.tools.ImageProvider;
    1718
     
    2728    private final OsmPrimitive newOsm;
    2829
     30
    2931    public ChangeCommand(OsmPrimitive osm, OsmPrimitive newOsm) {
    3032        super();
     33        this.osm = osm;
     34        this.newOsm = newOsm;
     35    }
     36
     37    public ChangeCommand(OsmDataLayer layer, OsmPrimitive osm, OsmPrimitive newOsm) {
     38        super(layer);
    3139        this.osm = osm;
    3240        this.newOsm = newOsm;
     
    4755        String msg = "";
    4856        switch(OsmPrimitiveType.from(osm)) {
    49             case NODE: msg = marktr("Change node {0}"); break;
    50             case WAY: msg = marktr("Change way {0}"); break;
    51             case RELATION: msg = marktr("Change relation {0}"); break;
     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;
    5260        }
    5361        return new DefaultMutableTreeNode(
  • trunk/src/org/openstreetmap/josm/command/DeleteCommand.java

    r3055 r3152  
    407407            chunks.add(n1);
    408408            chunks.add(n2);
    409             return SplitWayAction.splitWay(ws.way, chunks).getCommand();
     409            return SplitWayAction.splitWay(layer,ws.way, chunks).getCommand();
    410410        }
    411411    }
Note: See TracChangeset for help on using the changeset viewer.