Changeset 3134

Show
Ignore:
Timestamp:
14.03.2010 15:05:55 (6 months ago)
Author:
bastiK
Message:

fixed #4606 - merging a new and an already existing node (better selection of "surviving" node)

Files:
1 modified

Legend:

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

    r2842 r3134  
    1 //License: GPL. Copyright 2007 by Immanuel Scholz and others 
     1//License: GPL. Copyright 2007 by Immanuel Scholz and others. See LICENSE file for details. 
    22package org.openstreetmap.josm.actions; 
    33 
     
    4141 * Merges a collection of nodes into one node. 
    4242 * 
     43 * The "surviving" node will be the one with the lowest positive id. 
     44 * (I.e. it was uploaded to the server and is the oldest one.) 
     45 *  
     46 * However we use the location of the node that was selected *last*. 
     47 * The "surviving" node will be moved to that location if it is 
     48 * different from the last selected node. 
    4349 */ 
    4450public class MergeNodesAction extends JosmAction { 
     
    6672 
    6773        Node targetNode = selectTargetNode(selectedNodes); 
    68         Command cmd = mergeNodes(Main.main.getEditLayer(), selectedNodes, targetNode); 
     74        Node targetLocationNode = selectTargetLocationNode(selectedNodes); 
     75        Command cmd = mergeNodes(Main.main.getEditLayer(), selectedNodes, targetNode, targetLocationNode); 
    6976        if (cmd != null) { 
    7077            Main.main.undoRedo.add(cmd); 
     
    7481 
    7582    /** 
     83     * Select the location of the target node after merge. 
     84     * 
     85     * @param candidates the collection of candidate nodes 
     86     * @return the coordinates of this node are later used for the target node 
     87     */ 
     88    public static Node selectTargetLocationNode(LinkedHashSet<Node> candidates) { 
     89        Node targetNode = null; 
     90        for (Node n : candidates) { // pick last one 
     91            targetNode = n; 
     92        } 
     93        return targetNode; 
     94    } 
     95     
     96    /** 
    7697     * Find which node to merge into (i.e. which one will be left) 
    77      * The last selected node will become the target node the remaining 
    78      * nodes are merged to. 
    7998     * 
    8099     * @param candidates the collection of candidate nodes 
     
    83102    public static Node selectTargetNode(LinkedHashSet<Node> candidates) { 
    84103        Node targetNode = null; 
    85         for (Node n : candidates) { // pick last one 
    86             targetNode = n; 
     104        Node lastNode = null; 
     105        for (Node n : candidates) { 
     106            if (!n.isNew()) { 
     107                if (targetNode == null) { 
     108                    targetNode = n; 
     109                } else if (n.getId() < targetNode.getId()) { 
     110                    targetNode = n; 
     111                } 
     112            } 
     113            lastNode = n; 
     114        } 
     115        if (targetNode == null) { 
     116            targetNode = lastNode; 
    87117        } 
    88118        return targetNode; 
    89119    } 
     120     
    90121 
    91122    /** 
     
    160191    } 
    161192 
     193    public static Command mergeNodes(OsmDataLayer layer, Collection<Node> nodes, Node targetNode) { 
     194        return mergeNodes(layer, nodes, targetNode, targetNode); 
     195    } 
     196     
    162197    /** 
    163198     * Merges the nodes in <code>nodes</code> onto one of the nodes. Uses the dataset 
     
    167202     * @param nodes the collection of nodes. Ignored if null. 
    168203     * @param targetNode the target node the collection of nodes is merged to. Must not be null. 
     204     * @param targetLocationNode this node's location will be used for the targetNode. 
    169205     * @throw IllegalArgumentException thrown if layer is null 
    170206     */ 
    171     public static Command mergeNodes(OsmDataLayer layer,Collection<Node> nodes, Node targetNode) { 
     207    public static Command mergeNodes(OsmDataLayer layer, Collection<Node> nodes, Node targetNode, Node targetLocationNode) { 
    172208        CheckParameterUtil.ensureParameterNotNull(layer, "layer"); 
    173209        CheckParameterUtil.ensureParameterNotNull(targetNode, "targetNode"); 
     
    220256        // build the commands 
    221257        // 
     258        if (targetNode != targetLocationNode) { 
     259            Node newTargetNode = new Node(targetNode); 
     260            newTargetNode.setCoor(targetLocationNode.getCoor()); 
     261            cmds.add(new ChangeCommand(targetNode, newTargetNode)); 
     262        } 
    222263        if (!nodesToDelete.isEmpty()) { 
    223264            cmds.add(new DeleteCommand(nodesToDelete));