Changeset 27972 in osm for applications/editors/josm


Ignore:
Timestamp:
2012-03-02T15:21:09+01:00 (13 years ago)
Author:
joshdoe
Message:

utilsplugin2: Replace Geometry now replaces a node with a node

Almost identical to Merge Nodes command, except target node is uploaded node, and other node can't be a member of a way.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/utilsplugin2/src/utilsplugin2/dumbutils/ReplaceGeometryAction.java

    r27971 r27972  
    99import org.openstreetmap.josm.Main;
    1010import org.openstreetmap.josm.actions.JosmAction;
     11import org.openstreetmap.josm.actions.MergeNodesAction;
    1112import org.openstreetmap.josm.command.*;
    1213import org.openstreetmap.josm.data.osm.Node;
     
    5657       
    5758        try {
    58             replace(firstObject, secondObject);
     59            replaceWithNew(firstObject, secondObject);
    5960        } catch (IllegalArgumentException ex) {
    6061            JOptionPane.showMessageDialog(Main.parent,
     
    6364         
    6465    }
    65     public boolean replace(OsmPrimitive firstObject, OsmPrimitive secondObject) {
    66         if (firstObject instanceof Way && secondObject instanceof Way) {
    67             return replaceWayWithWay(Arrays.asList((Way) firstObject, (Way) secondObject));
    68         } else if (firstObject instanceof Node && secondObject instanceof Node) {
    69             throw new IllegalArgumentException(tr("To replace a node with a node, use the node merge tool."));
     66   
     67    /**
     68     * Replace new or uploaded object with new object
     69     *
     70     * @param firstObject
     71     * @param secondObject
     72     * @return
     73     */
     74    public boolean replaceWithNew(OsmPrimitive firstObject, OsmPrimitive secondObject) {
     75        if (firstObject instanceof Node && secondObject instanceof Node) {
     76            return replaceNodeWithNew((Node) firstObject, (Node) secondObject);
     77        } else if (firstObject instanceof Way && secondObject instanceof Way) {
     78            return replaceWayWithNew(Arrays.asList((Way) firstObject, (Way) secondObject));
    7079        } else if (firstObject instanceof Node) {
    71             return replaceNode((Node) firstObject, secondObject);
     80            return upgradeNode((Node) firstObject, secondObject);
    7281        } else if (secondObject instanceof Node) {
    73             return replaceNode((Node) secondObject, firstObject);
     82            return upgradeNode((Node) secondObject, firstObject);
    7483        } else {
    75             throw new IllegalArgumentException(tr("This tool can only replace a node with a way, a node with a multipolygon, or a way with a way."));
    76         }
    77     }
    78 
    79     /**
    80      * Replace or upgrade a node to a way or multipolygon
     84            throw new IllegalArgumentException(tr("This tool can only replace a node, upgrade a node to a way or a multipolygon, or replace a way with a way."));
     85        }
     86    }
     87
     88    /**
     89     * Replace a new or uploaded node with a new node
     90     * @param firstNode
     91     * @param secondNode
     92     * @return
     93     */
     94    public boolean replaceNodeWithNew(Node firstNode, Node secondNode) {
     95        if (firstNode.isNew() && !secondNode.isNew())
     96            return replaceNode(secondNode, firstNode);
     97        else if (!firstNode.isNew() && secondNode.isNew())
     98            return replaceNode(firstNode, secondNode);
     99        else
     100            // both nodes are new OR uploaded, act like MergeNodes, moving first
     101            // node to second
     102            return replaceNode(firstNode, secondNode);
     103    }
     104   
     105    /**
     106     * Replace a node with another node (similar to MergeNodesAction)
     107     *
     108     * @param subjectNode
     109     * @param referenceNode
     110     * @return
     111     */
     112    public boolean replaceNode(Node subjectNode, Node referenceNode) {
     113        if (!OsmPrimitive.getFilteredList(subjectNode.getReferrers(), Way.class).isEmpty()) {
     114            JOptionPane.showMessageDialog(Main.parent, tr("Node belongs to way(s), cannot replace."),
     115                    TITLE, JOptionPane.INFORMATION_MESSAGE);
     116            return false;
     117        }
     118        // FIXME: handle different layers
     119        List<Command> commands = new ArrayList<Command>();
     120        commands.add(MergeNodesAction.mergeNodes(Main.main.getEditLayer(), Arrays.asList(subjectNode, referenceNode), referenceNode));
     121
     122        Main.main.undoRedo.add(new SequenceCommand(
     123                tr("Replace geometry for node {0}", subjectNode.getDisplayName(DefaultNameFormatter.getInstance())),
     124                commands));
     125        return true;
     126    }
     127   
     128    /**
     129     * Upgrade a node to a way or multipolygon
    81130     *
    82131     * @param subjectNode node to be replaced
    83132     * @param referenceObject object with greater spatial quality
    84133     */
    85     public boolean replaceNode(Node subjectNode, OsmPrimitive referenceObject) {
     134    public boolean upgradeNode(Node subjectNode, OsmPrimitive referenceObject) {
    86135        if (!OsmPrimitive.getFilteredList(subjectNode.getReferrers(), Way.class).isEmpty()) {
    87136            JOptionPane.showMessageDialog(Main.parent, tr("Node belongs to way(s), cannot replace."),
     
    164213    }
    165214   
    166     public boolean replaceWayWithWay(List<Way> selection) {
     215    public boolean replaceWayWithNew(List<Way> selection) {
    167216        // determine which way will be replaced and which will provide the geometry
    168217        boolean overrideNewCheck = false;
Note: See TracChangeset for help on using the changeset viewer.