Changeset 8195 in josm for trunk


Ignore:
Timestamp:
2015-04-15T20:42:45+02:00 (5 years ago)
Author:
simon04
Message:

see #9907 - Make "Open Location" capable of downloading notes

If more than one download task is suitable, ask the user which tasks to perform.

Location:
trunk/src/org/openstreetmap/josm/actions
Files:
2 added
3 edited

Legend:

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

    r8061 r8195  
    77import java.awt.GridBagConstraints;
    88import java.awt.GridBagLayout;
     9import java.awt.GridLayout;
    910import java.awt.event.ActionEvent;
    1011import java.awt.event.KeyEvent;
     
    1819import javax.swing.JCheckBox;
    1920import javax.swing.JLabel;
     21import javax.swing.JList;
    2022import javax.swing.JOptionPane;
    2123import javax.swing.JPanel;
     
    2426import org.openstreetmap.josm.actions.downloadtasks.DownloadGpsTask;
    2527import org.openstreetmap.josm.actions.downloadtasks.DownloadNotesTask;
     28import org.openstreetmap.josm.actions.downloadtasks.DownloadNotesUrlBoundsTask;
     29import org.openstreetmap.josm.actions.downloadtasks.DownloadNotesUrlIdTask;
    2630import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmChangeCompressedTask;
    2731import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmChangeTask;
     
    6771        addDownloadTaskClass(DownloadOsmChangeCompressedTask.class);
    6872        addDownloadTaskClass(DownloadSessionTask.class);
     73        addDownloadTaskClass(DownloadNotesUrlBoundsTask.class);
     74        addDownloadTaskClass(DownloadNotesUrlIdTask.class);
    6975    }
    7076
     
    183189        PleaseWaitProgressMonitor monitor = new PleaseWaitProgressMonitor(tr("Download Data"));
    184190        Collection<DownloadTask> tasks = findDownloadTasks(url, false);
    185         DownloadTask task = null;
    186         Future<?> future = null;
    187         if (!tasks.isEmpty()) {
    188             // TODO: handle multiple suitable tasks ?
     191
     192        if (tasks.size() > 1) {
     193            tasks = askWhichTasksToLoad(tasks);
     194        } else if (tasks.isEmpty()) {
     195            warnNoSuitableTasks(url);
     196            return;
     197        }
     198
     199        for (final DownloadTask task : tasks) {
    189200            try {
    190                 task = tasks.iterator().next();
    191                 future = task.loadUrl(newLayer, url, monitor);
     201                Future<?> future = task.loadUrl(newLayer, url, monitor);
     202                Main.worker.submit(new PostDownloadHandler(task, future));
    192203            } catch (IllegalArgumentException e) {
    193204                Main.error(e);
    194205            }
    195206        }
    196         if (future != null) {
    197             Main.worker.submit(new PostDownloadHandler(task, future));
    198         } else {
    199             final String details = findSummaryDocumentation();    // Explain what patterns are supported
    200             HelpAwareOptionPane.showMessageDialogInEDT(Main.parent, "<html><p>" + tr(
    201                     "Cannot open URL ''{0}''<br>The following download tasks accept the URL patterns shown:<br>{1}",
    202                     url, details) + "</p></html>", tr("Download Location"), JOptionPane.ERROR_MESSAGE, HelpUtil.ht("/Action/OpenLocation"));
    203         }
     207
     208    }
     209
     210    /**
     211     * Asks the user which of the possible tasks to perform.
     212     * @param tasks a list of possible tasks
     213     * @return the selected tasks from the user or an empty list if the dialog has been canceled
     214     */
     215    Collection<DownloadTask> askWhichTasksToLoad(final Collection<DownloadTask> tasks) {
     216        final JList<DownloadTask> list = new JList<>(tasks.toArray(new DownloadTask[tasks.size()]));
     217        list.addSelectionInterval(0, tasks.size() - 1);
     218        final ExtendedDialog dialog = new ExtendedDialog(Main.parent, tr("Which tasks to perform?"), new String[]{tr("Ok"), tr("Cancel")}, true) {{
     219            final JPanel pane = new JPanel(new GridLayout(2, 1));
     220            pane.add(new JLabel(tr("Which tasks to perform?")));
     221            pane.add(list);
     222            setContent(pane);
     223        }};
     224        dialog.showDialog();
     225        return dialog.getValue() == 1 ? list.getSelectedValuesList() : Collections.<DownloadTask>emptyList();
     226    }
     227
     228    /**
     229     * Displays an error message dialog that no suitable tasks have been found for the given url.
     230     * @param url the given url
     231     */
     232    void warnNoSuitableTasks(final String url) {
     233        final String details = findSummaryDocumentation();    // Explain what patterns are supported
     234        HelpAwareOptionPane.showMessageDialogInEDT(Main.parent, "<html><p>" + tr(
     235                "Cannot open URL ''{0}''<br>The following download tasks accept the URL patterns shown:<br>{1}",
     236                url, details) + "</p></html>", tr("Download Location"), JOptionPane.ERROR_MESSAGE, HelpUtil.ht("/Action/OpenLocation"));
    204237    }
    205238
  • trunk/src/org/openstreetmap/josm/actions/downloadtasks/AbstractDownloadTask.java

    r7749 r8195  
    108108    }
    109109
     110    @Override
     111    public String toString() {
     112        return this.getTitle();
     113    }
     114
    110115    // Default pattern to keep old plugins compatible
    111116    @Override
  • trunk/src/org/openstreetmap/josm/actions/downloadtasks/DownloadNotesTask.java

    r8115 r8195  
    1616import org.openstreetmap.josm.gui.progress.ProgressMonitor;
    1717import org.openstreetmap.josm.io.BoundingBoxDownloader;
     18import org.openstreetmap.josm.io.OsmApi;
    1819import org.openstreetmap.josm.io.OsmServerLocationReader;
    1920import org.openstreetmap.josm.io.OsmServerReader;
     
    2829
    2930    private DownloadTask downloadTask;
     31
     32    public Future<?> download(boolean newLayer, long id, ProgressMonitor progressMonitor) {
     33        final String url = OsmApi.getOsmApi().getBaseUrl() + "notes/" + id;
     34        downloadTask = new DownloadRawUrlTask(new OsmServerLocationReader(url), progressMonitor);
     35        return Main.worker.submit(downloadTask);
     36    }
    3037
    3138    @Override
Note: See TracChangeset for help on using the changeset viewer.