Changeset 8294 in josm for trunk/src/org


Ignore:
Timestamp:
2015-04-30T00:01:40+02:00 (9 years ago)
Author:
Balaitous
Message:

fix #10396 - Disconnecting nodes from way could be easier

  • Ignore non-connected nodes
  • When exactly one way is selected, ignore nodes non-connected to this way
  • Replace message dialog by notification.
  • Add test case
File:
1 edited

Legend:

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

    r7937 r8294  
    1111import java.util.Collection;
    1212import java.util.Iterator;
     13import java.util.LinkedList;
    1314import java.util.List;
    1415
     
    2021import org.openstreetmap.josm.data.osm.OsmPrimitive;
    2122import org.openstreetmap.josm.data.osm.Way;
     23import org.openstreetmap.josm.gui.Notification;
    2224import org.openstreetmap.josm.tools.Shortcut;
    2325
     
    3941    }
    4042
     43    /**
     44     * Called when the action is executed.
     45     */
    4146    @Override
    4247    public void actionPerformed(ActionEvent e) {
     
    4651        List<Node> selectedNodes = OsmPrimitive.getFilteredList(selection, Node.class);
    4752        List<Way> selectedWays = OsmPrimitive.getFilteredList(selection, Way.class);
     53
     54        selectedNodes = cleanSelectedNodes(selectedWays, selectedNodes);
     55
    4856        List<Way> applicableWays = getApplicableWays(selectedWays, selectedNodes);
    4957
    5058        if (applicableWays == null) {
    51             JOptionPane.showMessageDialog(
    52                     Main.parent,
    53                     tr("Select at least one node to be disconnected."),
    54                     tr("Warning"),
    55                     JOptionPane.WARNING_MESSAGE);
     59            notify(tr("Select at least one node to be disconnected."),
     60                   JOptionPane.WARNING_MESSAGE);
    5661            return;
    5762        } else if (applicableWays.isEmpty()) {
    58             JOptionPane.showMessageDialog(Main.parent,
    59                     trn("Selected node cannot be disconnected from anything.",
    60                         "Selected nodes cannot be disconnected from anything.",
    61                         selectedNodes.size()),
    62                     tr("Warning"),
    63                     JOptionPane.WARNING_MESSAGE);
     63            notify(trn("Selected node cannot be disconnected from anything.",
     64                       "Selected nodes cannot be disconnected from anything.",
     65                       selectedNodes.size()),
     66                   JOptionPane.WARNING_MESSAGE);
    6467            return;
    6568        } else if (applicableWays.size() > 1) {
    66             JOptionPane.showMessageDialog(Main.parent,
    67                     trn("There is more than one way using the node you selected. Please select the way also.",
    68                         "There is more than one way using the nodes you selected. Please select the way also.",
    69                         selectedNodes.size()),
    70                     tr("Warning"),
    71                     JOptionPane.WARNING_MESSAGE);
     69            notify(trn("There is more than one way using the node you selected. "
     70                       + "Please select the way also.",
     71                       "There is more than one way using the nodes you selected. "
     72                       + "Please select the way also.",
     73                       selectedNodes.size()),
     74                   JOptionPane.WARNING_MESSAGE);
    7275            return;
    7376        } else if (applicableWays.get(0).getRealNodesCount() < selectedNodes.size() + 2) {
    7477            // there is only one affected way, but removing the selected nodes would only leave it
    7578            // with less than 2 nodes
    76             JOptionPane.showMessageDialog(Main.parent,
    77                     trn("The affected way would disappear after disconnecting the selected node.",
    78                         "The affected way would disappear after disconnecting the selected nodes.",
    79                         selectedNodes.size()),
    80                     tr("Warning"),
    81                     JOptionPane.WARNING_MESSAGE);
     79            notify(trn("The affected way would disappear after disconnecting the "
     80                       + "selected node.",
     81                       "The affected way would disappear after disconnecting the "
     82                       + "selected nodes.",
     83                       selectedNodes.size()),
     84                   JOptionPane.WARNING_MESSAGE);
    8285            return;
    8386        }
    84 
    8587
    8688        // Finally, applicableWays contains only one perfect way
     
    9294    }
    9395
    94     // Find ways to which the disconnect can be applied. This is the list of ways with more
    95     // than two nodes which pass through all the given nodes, intersected with the selected ways (if any)
     96    /**
     97     * Send a notification message.
     98     * @param msg Message to be sent.
     99     * @param messageType Nature of the message.
     100     */
     101    public void notify(String msg, int messageType) {
     102        new Notification(msg).setIcon(messageType).show();
     103    }
     104
     105    /**
     106     * Removes irrelevant nodes from user selection.
     107     *
     108     * The action can be performed reliably even if we remove :
     109     *   * Nodes not referenced by any ways
     110     *   * When only one way is selected, nodes not part of this way (#10396).
     111     *
     112     * @param selectedWays  List of user selected way.
     113     * @param selectedNodes List of user selected nodes.
     114     * @return New list of nodes cleaned of irrelevant nodes.
     115     */
     116    private List<Node> cleanSelectedNodes(List<Way> selectedWays,
     117                                          List<Node> selectedNodes) {
     118        List<Node> resultingNodes = new LinkedList<>();
     119
     120        // List of node referenced by a route
     121        for (Node n: selectedNodes) {
     122            if (n.isReferredByWays(1)) {
     123                resultingNodes.add(n);
     124            }
     125        }
     126        // If exactly one selected way, remove node not referencing par this way.
     127        if (selectedWays.size() == 1) {
     128            Way w = selectedWays.get(0);
     129            for (Node n: new ArrayList<Node>(resultingNodes)) {
     130                if (!w.containsNode(n)) {
     131                    resultingNodes.remove(n);
     132                }
     133            }
     134        }
     135        // Warn if nodes were removed
     136        if (resultingNodes.size() != selectedNodes.size()) {
     137            notify(tr("Some irrelevant nodes have been removed from the selection"),
     138                   JOptionPane.INFORMATION_MESSAGE);
     139        }
     140        return resultingNodes;
     141    }
     142
     143    /**
     144     * Find ways to which the disconnect can be applied. This is the list of ways
     145     * with more than two nodes which pass through all the given nodes, intersected
     146     * with the selected ways (if any)
     147     * @param selectedWays List of user selected ways.
     148     * @param selectedNodes List of user selected nodes.
     149     * @return List of relevant ways
     150     */
    96151    private List<Way> getApplicableWays(List<Way> selectedWays, List<Node> selectedNodes) {
    97152        if (selectedNodes.isEmpty())
Note: See TracChangeset for help on using the changeset viewer.