Ticket #23735: 23735-auto-download.patch

File 23735-auto-download.patch, 4.5 KB (added by GerdP, 6 months ago)

Solution for automatic download of parents? It checks if preference combine.download-parents is true.

  • src/org/openstreetmap/josm/actions/CombineWayAction.java

     
    1010import java.util.ArrayList;
    1111import java.util.Collection;
    1212import java.util.Collections;
    13 import java.util.HashSet;
    1413import java.util.LinkedHashSet;
    1514import java.util.LinkedList;
    1615import java.util.List;
    1716import java.util.Objects;
    18 import java.util.Set;
    1917import java.util.stream.Collectors;
    2018import java.util.stream.IntStream;
    2119
     
    2220import javax.swing.JOptionPane;
    2321
    2422import org.openstreetmap.josm.actions.corrector.ReverseWayTagCorrector;
     23import org.openstreetmap.josm.actions.downloadtasks.DownloadReferrersTask;
    2524import org.openstreetmap.josm.command.ChangeNodesCommand;
    2625import org.openstreetmap.josm.command.Command;
    2726import org.openstreetmap.josm.command.DeleteCommand;
     
    5857public class CombineWayAction extends JosmAction {
    5958
    6059    private static final BooleanProperty PROP_REVERSE_WAY = new BooleanProperty("tag-correction.reverse-way", true);
     60    private static final BooleanProperty PROP_DOWNLOAD_PARENTS = new BooleanProperty("combine.download-parents", false);
    6161
    6262    /**
    6363     * Constructs a new {@code CombineWayAction}.
     
    279279            return;
    280280        }
    281281
    282         // see #18083: check if we will combine ways at nodes outside of the download area
    283         Set<Node> endNodesOutside = new HashSet<>();
    284         for (Way w : selectedWays) {
    285             final Node[] endnodes = {w.firstNode(), w.lastNode()};
    286             for (Node n : endnodes) {
    287                 if (!n.isNew() && !n.isReferrersDownloaded() && !endNodesOutside.add(n)) {
    288                     new Notification(tr("Combine ways refused<br>" + "(A shared node may have additional referrers)"))
    289                             .setIcon(JOptionPane.INFORMATION_MESSAGE).show();
    290                     return;
    291 
    292                 }
    293             }
    294         }
    295 
    296282        // combine and update gui
    297283        Pair<Way, Command> combineResult;
    298284        try {
     
    304290
    305291        if (combineResult == null)
    306292            return;
     293        // see #18083 and #23735: check if we know the parents of the combined ways
     294        Collection<OsmPrimitive> missingParents = selectedWays.stream().filter(w -> !w.isReferrersDownloaded())
     295                .collect(Collectors.toList());
     296        if (!missingParents.isEmpty() && Boolean.TRUE.equals(PROP_DOWNLOAD_PARENTS.get())
     297                && event.getSource() != this) { // avoid endless recursion
     298            MainApplication.worker.submit(() -> {
     299                new DownloadReferrersTask(getLayerManager().getEditLayer(), missingParents).run();
     300                GuiHelper.runInEDT(() -> actionPerformed(new ActionEvent(this, 0, null)));
     301            });
     302            return;
     303        }
     304        if (!checkAndConfirmCombineOutlyingWays(selectedWays))
     305            return;
    307306
    308307        final Way selectedWay = combineResult.a;
    309308        UndoRedoHandler.getInstance().add(combineResult.b);
     
    346345        setEnabled(numWays >= 2);
    347346    }
    348347
     348    /**
     349     * Check whether user is about to combine ways with unknown parents.
     350     * Request confirmation if he is.
     351     * @param ways the primitives to operate on
     352     * @return true, if operating on outlying primitives is OK; false, otherwise
     353     */
     354    private static boolean checkAndConfirmCombineOutlyingWays(Collection<Way> ways) {
     355        DownloadReferrersAction action = MainApplication.getMenu().downloadReferrers;
     356        final String downloadHint = tr("You should use {0}->{1}({2}) first.",
     357                MainApplication.getMenu().editMenu.getText(), action.getValue(NAME), action.getShortcut().toString());
     358        return Boolean.TRUE.equals(GuiHelper.runInEDTAndWaitAndReturn(() -> checkAndConfirmOutlyingOperation("combine",
     359                tr("Combine confirmation"),
     360                tr("You are about to combine ways which can be members of relations not yet downloaded."
     361                        + "<br>"
     362                        + "This can lead to damaging these parent relations (that you do not see)."
     363                        + "<br>"
     364                        + "{0}"
     365                        + "<br><br>"
     366                        + "Do you really want to combine without downloading?", downloadHint),
     367                "", // not used, we never combine incomplete ways
     368                ways, Collections.emptyList())));
     369    }
     370
    349371}