Ticket #6836: 6836.patch

File 6836.patch, 2.6 KB (added by GerdP, 5 years ago)
  • src/org/openstreetmap/josm/gui/dialogs/PropertiesMembershipChoiceDialog.java

     
    55
    66import java.awt.GridBagLayout;
    77import java.util.Collection;
    8 import java.util.Objects;
     8import java.util.Collections;
    99import java.util.Optional;
    1010
    1111import javax.swing.AbstractButton;
     
    1515import javax.swing.JToggleButton;
    1616
    1717import org.openstreetmap.josm.data.osm.Node;
     18import org.openstreetmap.josm.data.osm.OsmPrimitive;
    1819import org.openstreetmap.josm.data.osm.Relation;
    1920import org.openstreetmap.josm.gui.ExtendedDialog;
    2021import org.openstreetmap.josm.gui.MainApplication;
     
    137138    public static PropertiesMembershipChoiceDialog showIfNecessary(Collection<Node> selectedNodes, boolean preselectNew)
    138139            throws UserCancelException {
    139140        final boolean queryTags = isTagged(selectedNodes);
    140         final boolean queryMemberships = isUsedInRelations(selectedNodes);
     141        final boolean queryMemberships = isMembershipUsableByNew(selectedNodes);
    141142        if (queryTags || queryMemberships) {
    142143            final PropertiesMembershipChoiceDialog dialog = new PropertiesMembershipChoiceDialog(preselectNew, queryTags, queryMemberships);
    143144            dialog.showDialog();
     
    153154        return existingNodes.stream().anyMatch(Node::hasKeys);
    154155    }
    155156
    156     private static boolean isUsedInRelations(final Collection<Node> existingNodes) {
    157         return existingNodes.stream().anyMatch(
    158                 selectedNode -> selectedNode.referrers(Relation.class).anyMatch(Objects::nonNull));
     157    /**
     158     * If the node is member of a relation the membership might be wanted on the copy, but that is rarely useful.
     159     * @param selectedNodes selected nodes
     160     * @return true if it might be useful to copy/move the membership.
     161     */
     162    private static boolean isMembershipUsableByNew(final Collection<Node> selectedNodes) {
     163        final String[] noCopyRoles = {"via"};
     164        for (Relation r : OsmPrimitive.getParentRelations(selectedNodes)) {
     165            if (r.hasTag("type", "restriction", "connectivity"))
     166                    continue;
     167            for (Node n: selectedNodes) {
     168                if (r.getMembersFor(Collections.singleton(n)).stream().anyMatch(rm -> !rm.hasRole(noCopyRoles)))
     169                    return true;
     170            }
     171        }
     172        return false;
    159173    }
    160174}