[6380] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[1146] | 2 | package org.openstreetmap.josm.actions;
|
---|
| 3 |
|
---|
[2323] | 4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
[1146] | 5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 6 |
|
---|
| 7 | import java.awt.GridBagLayout;
|
---|
[8195] | 8 | import java.awt.GridLayout;
|
---|
[1146] | 9 | import java.awt.event.ActionEvent;
|
---|
| 10 | import java.awt.event.KeyEvent;
|
---|
[4521] | 11 | import java.util.ArrayList;
|
---|
[5691] | 12 | import java.util.Collection;
|
---|
[2285] | 13 | import java.util.Collections;
|
---|
| 14 | import java.util.LinkedList;
|
---|
| 15 | import java.util.List;
|
---|
[11279] | 16 | import java.util.Objects;
|
---|
[2322] | 17 | import java.util.concurrent.Future;
|
---|
[11279] | 18 | import java.util.stream.Collectors;
|
---|
[1146] | 19 |
|
---|
| 20 | import javax.swing.JCheckBox;
|
---|
| 21 | import javax.swing.JLabel;
|
---|
[8195] | 22 | import javax.swing.JList;
|
---|
[4521] | 23 | import javax.swing.JOptionPane;
|
---|
[1146] | 24 | import javax.swing.JPanel;
|
---|
| 25 |
|
---|
| 26 | import org.openstreetmap.josm.Main;
|
---|
[4521] | 27 | import org.openstreetmap.josm.actions.downloadtasks.DownloadGpsTask;
|
---|
[7531] | 28 | import org.openstreetmap.josm.actions.downloadtasks.DownloadNotesTask;
|
---|
[8195] | 29 | import org.openstreetmap.josm.actions.downloadtasks.DownloadNotesUrlBoundsTask;
|
---|
| 30 | import org.openstreetmap.josm.actions.downloadtasks.DownloadNotesUrlIdTask;
|
---|
[5361] | 31 | import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmChangeCompressedTask;
|
---|
[6032] | 32 | import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmChangeTask;
|
---|
[5361] | 33 | import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmCompressedTask;
|
---|
[8240] | 34 | import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmIdTask;
|
---|
[1146] | 35 | import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmTask;
|
---|
[4996] | 36 | import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmUrlTask;
|
---|
[6245] | 37 | import org.openstreetmap.josm.actions.downloadtasks.DownloadSessionTask;
|
---|
[4521] | 38 | import org.openstreetmap.josm.actions.downloadtasks.DownloadTask;
|
---|
[2322] | 39 | import org.openstreetmap.josm.actions.downloadtasks.PostDownloadHandler;
|
---|
[11279] | 40 | import org.openstreetmap.josm.data.preferences.BooleanProperty;
|
---|
[1397] | 41 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
[6032] | 42 | import org.openstreetmap.josm.gui.HelpAwareOptionPane;
|
---|
[2322] | 43 | import org.openstreetmap.josm.gui.progress.PleaseWaitProgressMonitor;
|
---|
[2285] | 44 | import org.openstreetmap.josm.gui.widgets.HistoryComboBox;
|
---|
[11279] | 45 | import org.openstreetmap.josm.tools.GBC;
|
---|
[1146] | 46 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
[5772] | 47 | import org.openstreetmap.josm.tools.Utils;
|
---|
[1146] | 48 |
|
---|
| 49 | /**
|
---|
| 50 | * Open an URL input dialog and load data from the given URL.
|
---|
| 51 | *
|
---|
| 52 | * @author imi
|
---|
| 53 | */
|
---|
| 54 | public class OpenLocationAction extends JosmAction {
|
---|
[11279] | 55 | /**
|
---|
| 56 | * true if the URL needs to be opened in a new layer, false otherwise
|
---|
| 57 | */
|
---|
[11531] | 58 | private static final BooleanProperty USE_NEW_LAYER = new BooleanProperty("download.newlayer", false);
|
---|
[11986] | 59 | /**
|
---|
| 60 | * the list of download tasks
|
---|
| 61 | */
|
---|
[8308] | 62 | protected final transient List<Class<? extends DownloadTask>> downloadTasks;
|
---|
[6069] | 63 |
|
---|
[11343] | 64 | static class WhichTasksToPerformDialog extends ExtendedDialog {
|
---|
| 65 | WhichTasksToPerformDialog(JList<DownloadTask> list) {
|
---|
| 66 | super(Main.parent, tr("Which tasks to perform?"), new String[]{tr("Ok"), tr("Cancel")}, true);
|
---|
| 67 | setButtonIcons(new String[]{"ok", "cancel"});
|
---|
| 68 | final JPanel pane = new JPanel(new GridLayout(2, 1));
|
---|
| 69 | pane.add(new JLabel(tr("Which tasks to perform?")));
|
---|
| 70 | pane.add(list);
|
---|
| 71 | setContent(pane);
|
---|
| 72 | }
|
---|
| 73 | }
|
---|
| 74 |
|
---|
[1169] | 75 | /**
|
---|
| 76 | * Create an open action. The name is "Open a file".
|
---|
| 77 | */
|
---|
| 78 | public OpenLocationAction() {
|
---|
[4045] | 79 | /* I18N: Command to download a specific location/URL */
|
---|
[1195] | 80 | super(tr("Open Location..."), "openlocation", tr("Open an URL."),
|
---|
[7859] | 81 | Shortcut.registerShortcut("system:open_location", tr("File: {0}", tr("Open Location...")),
|
---|
| 82 | KeyEvent.VK_L, Shortcut.CTRL), true);
|
---|
[2323] | 83 | putValue("help", ht("/Action/OpenLocation"));
|
---|
[7005] | 84 | this.downloadTasks = new ArrayList<>();
|
---|
[4521] | 85 | addDownloadTaskClass(DownloadOsmTask.class);
|
---|
| 86 | addDownloadTaskClass(DownloadGpsTask.class);
|
---|
[7531] | 87 | addDownloadTaskClass(DownloadNotesTask.class);
|
---|
[4530] | 88 | addDownloadTaskClass(DownloadOsmChangeTask.class);
|
---|
[4996] | 89 | addDownloadTaskClass(DownloadOsmUrlTask.class);
|
---|
[8240] | 90 | addDownloadTaskClass(DownloadOsmIdTask.class);
|
---|
[5361] | 91 | addDownloadTaskClass(DownloadOsmCompressedTask.class);
|
---|
| 92 | addDownloadTaskClass(DownloadOsmChangeCompressedTask.class);
|
---|
[6245] | 93 | addDownloadTaskClass(DownloadSessionTask.class);
|
---|
[8195] | 94 | addDownloadTaskClass(DownloadNotesUrlBoundsTask.class);
|
---|
| 95 | addDownloadTaskClass(DownloadNotesUrlIdTask.class);
|
---|
[1169] | 96 | }
|
---|
[1146] | 97 |
|
---|
[2285] | 98 | /**
|
---|
| 99 | * Restore the current history from the preferences
|
---|
[2512] | 100 | *
|
---|
[8459] | 101 | * @param cbHistory the history combo box
|
---|
[2285] | 102 | */
|
---|
| 103 | protected void restoreUploadAddressHistory(HistoryComboBox cbHistory) {
|
---|
[7859] | 104 | List<String> cmtHistory = new LinkedList<>(Main.pref.getCollection(getClass().getName() + ".uploadAddressHistory",
|
---|
| 105 | new LinkedList<String>()));
|
---|
[7434] | 106 | // we have to reverse the history, because ComboBoxHistory will reverse it again in addElement()
|
---|
[2285] | 107 | //
|
---|
| 108 | Collections.reverse(cmtHistory);
|
---|
| 109 | cbHistory.setPossibleItems(cmtHistory);
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | /**
|
---|
| 113 | * Remind the current history in the preferences
|
---|
[8459] | 114 | * @param cbHistory the history combo box
|
---|
[2285] | 115 | */
|
---|
| 116 | protected void remindUploadAddressHistory(HistoryComboBox cbHistory) {
|
---|
| 117 | cbHistory.addCurrentItemToHistory();
|
---|
| 118 | Main.pref.putCollection(getClass().getName() + ".uploadAddressHistory", cbHistory.getHistory());
|
---|
| 119 | }
|
---|
| 120 |
|
---|
[6084] | 121 | @Override
|
---|
[1169] | 122 | public void actionPerformed(ActionEvent e) {
|
---|
[11279] | 123 | JPanel all = new JPanel(new GridBagLayout());
|
---|
[1146] | 124 |
|
---|
[11279] | 125 | // download URL selection
|
---|
| 126 | all.add(new JLabel(tr("Enter URL to download:")), GBC.eol());
|
---|
[4310] | 127 | HistoryComboBox uploadAddresses = new HistoryComboBox();
|
---|
| 128 | uploadAddresses.setToolTipText(tr("Enter an URL from where data should be downloaded"));
|
---|
| 129 | restoreUploadAddressHistory(uploadAddresses);
|
---|
[11279] | 130 | all.add(uploadAddresses, GBC.eop().fill(GBC.BOTH));
|
---|
| 131 |
|
---|
| 132 | // use separate layer
|
---|
| 133 | JCheckBox layer = new JCheckBox(tr("Separate Layer"));
|
---|
| 134 | layer.setToolTipText(tr("Select if the data should be downloaded into a new layer"));
|
---|
| 135 | layer.setSelected(USE_NEW_LAYER.get());
|
---|
| 136 | all.add(layer, GBC.eop().fill(GBC.BOTH));
|
---|
| 137 |
|
---|
[2070] | 138 | ExtendedDialog dialog = new ExtendedDialog(Main.parent,
|
---|
| 139 | tr("Download Location"),
|
---|
| 140 | new String[] {tr("Download URL"), tr("Cancel")}
|
---|
| 141 | );
|
---|
[2285] | 142 | dialog.setContent(all, false /* don't embedded content in JScrollpane */);
|
---|
[8061] | 143 | dialog.setButtonIcons(new String[] {"download", "cancel"});
|
---|
[2285] | 144 | dialog.setToolTipTexts(new String[] {
|
---|
| 145 | tr("Start downloading data"),
|
---|
| 146 | tr("Close dialog and cancel downloading")
|
---|
| 147 | });
|
---|
[2308] | 148 | dialog.configureContextsensitiveHelp("/Action/OpenLocation", true /* show help button */);
|
---|
[2080] | 149 | dialog.showDialog();
|
---|
[11279] | 150 | if (dialog.getValue() == 1) {
|
---|
| 151 | USE_NEW_LAYER.put(layer.isSelected());
|
---|
| 152 | remindUploadAddressHistory(uploadAddresses);
|
---|
| 153 | openUrl(Utils.strip(uploadAddresses.getText()));
|
---|
| 154 | }
|
---|
[1169] | 155 | }
|
---|
[6069] | 156 |
|
---|
[1169] | 157 | /**
|
---|
[5691] | 158 | * Replies the list of download tasks accepting the given url.
|
---|
| 159 | * @param url The URL to open
|
---|
[7749] | 160 | * @param isRemotecontrol True if download request comes from remotecontrol.
|
---|
[5691] | 161 | * @return The list of download tasks accepting the given url.
|
---|
| 162 | * @since 5691
|
---|
[1169] | 163 | */
|
---|
[7749] | 164 | public Collection<DownloadTask> findDownloadTasks(final String url, boolean isRemotecontrol) {
|
---|
[11279] | 165 | return downloadTasks.stream()
|
---|
| 166 | .filter(Objects::nonNull)
|
---|
| 167 | .map(taskClass -> {
|
---|
| 168 | try {
|
---|
| 169 | return taskClass.getConstructor().newInstance();
|
---|
| 170 | } catch (ReflectiveOperationException e) {
|
---|
| 171 | Main.error(e);
|
---|
| 172 | return null;
|
---|
[4521] | 173 | }
|
---|
[11279] | 174 | })
|
---|
| 175 | .filter(Objects::nonNull)
|
---|
| 176 | .filter(task -> task.acceptsUrl(url, isRemotecontrol))
|
---|
| 177 | .collect(Collectors.toList());
|
---|
[5691] | 178 | }
|
---|
| 179 |
|
---|
| 180 | /**
|
---|
[6031] | 181 | * Summarizes acceptable urls for error message purposes.
|
---|
[6032] | 182 | * @return The HTML message to be displayed
|
---|
| 183 | * @since 6031
|
---|
[6031] | 184 | */
|
---|
| 185 | public String findSummaryDocumentation() {
|
---|
[6264] | 186 | StringBuilder result = new StringBuilder("<table>");
|
---|
[6040] | 187 | for (Class<? extends DownloadTask> taskClass : downloadTasks) {
|
---|
[6031] | 188 | if (taskClass != null) {
|
---|
| 189 | try {
|
---|
| 190 | DownloadTask task = taskClass.getConstructor().newInstance();
|
---|
[6264] | 191 | result.append(task.acceptsDocumentationSummary());
|
---|
[10212] | 192 | } catch (ReflectiveOperationException e) {
|
---|
[6643] | 193 | Main.error(e);
|
---|
[6031] | 194 | }
|
---|
| 195 | }
|
---|
| 196 | }
|
---|
[6264] | 197 | result.append("</table>");
|
---|
| 198 | return result.toString();
|
---|
[6031] | 199 | }
|
---|
| 200 |
|
---|
| 201 | /**
|
---|
[5691] | 202 | * Open the given URL.
|
---|
[7859] | 203 | * @param newLayer true if the URL needs to be opened in a new layer, false otherwise
|
---|
[5691] | 204 | * @param url The URL to open
|
---|
[11986] | 205 | * @return the list of tasks that have been started successfully (can be empty).
|
---|
| 206 | * @since 11986 (return type)
|
---|
[5691] | 207 | */
|
---|
[11986] | 208 | public List<Future<?>> openUrl(boolean newLayer, String url) {
|
---|
| 209 | return realOpenUrl(newLayer, url);
|
---|
[11279] | 210 | }
|
---|
| 211 |
|
---|
| 212 | /**
|
---|
| 213 | * Open the given URL. This class checks the {@link #USE_NEW_LAYER} preference to check if a new layer should be used.
|
---|
| 214 | * @param url The URL to open
|
---|
[11986] | 215 | * @return the list of tasks that have been started successfully (can be empty).
|
---|
| 216 | * @since 11986 (return type)
|
---|
[11279] | 217 | */
|
---|
[11986] | 218 | public List<Future<?>> openUrl(String url) {
|
---|
[11279] | 219 | return realOpenUrl(USE_NEW_LAYER.get(), url);
|
---|
| 220 | }
|
---|
| 221 |
|
---|
[11986] | 222 | private List<Future<?>> realOpenUrl(boolean newLayer, String url) {
|
---|
[7749] | 223 | Collection<DownloadTask> tasks = findDownloadTasks(url, false);
|
---|
[8195] | 224 |
|
---|
| 225 | if (tasks.size() > 1) {
|
---|
| 226 | tasks = askWhichTasksToLoad(tasks);
|
---|
| 227 | } else if (tasks.isEmpty()) {
|
---|
| 228 | warnNoSuitableTasks(url);
|
---|
[11986] | 229 | return Collections.emptyList();
|
---|
[8195] | 230 | }
|
---|
| 231 |
|
---|
[9062] | 232 | PleaseWaitProgressMonitor monitor = new PleaseWaitProgressMonitor(tr("Download Data"));
|
---|
| 233 |
|
---|
[11986] | 234 | List<Future<?>> result = new ArrayList<>();
|
---|
[8195] | 235 | for (final DownloadTask task : tasks) {
|
---|
[6474] | 236 | try {
|
---|
[11986] | 237 | result.add(Main.worker.submit(new PostDownloadHandler(task, task.loadUrl(newLayer, url, monitor))));
|
---|
[6474] | 238 | } catch (IllegalArgumentException e) {
|
---|
| 239 | Main.error(e);
|
---|
| 240 | }
|
---|
[5691] | 241 | }
|
---|
[11986] | 242 | return result;
|
---|
[1169] | 243 | }
|
---|
[6069] | 244 |
|
---|
[6032] | 245 | /**
|
---|
[8195] | 246 | * Asks the user which of the possible tasks to perform.
|
---|
| 247 | * @param tasks a list of possible tasks
|
---|
| 248 | * @return the selected tasks from the user or an empty list if the dialog has been canceled
|
---|
| 249 | */
|
---|
| 250 | Collection<DownloadTask> askWhichTasksToLoad(final Collection<DownloadTask> tasks) {
|
---|
| 251 | final JList<DownloadTask> list = new JList<>(tasks.toArray(new DownloadTask[tasks.size()]));
|
---|
| 252 | list.addSelectionInterval(0, tasks.size() - 1);
|
---|
[11343] | 253 | final ExtendedDialog dialog = new WhichTasksToPerformDialog(list);
|
---|
[8195] | 254 | dialog.showDialog();
|
---|
| 255 | return dialog.getValue() == 1 ? list.getSelectedValuesList() : Collections.<DownloadTask>emptyList();
|
---|
| 256 | }
|
---|
| 257 |
|
---|
| 258 | /**
|
---|
| 259 | * Displays an error message dialog that no suitable tasks have been found for the given url.
|
---|
| 260 | * @param url the given url
|
---|
| 261 | */
|
---|
[11279] | 262 | protected void warnNoSuitableTasks(final String url) {
|
---|
[8195] | 263 | final String details = findSummaryDocumentation(); // Explain what patterns are supported
|
---|
| 264 | HelpAwareOptionPane.showMessageDialogInEDT(Main.parent, "<html><p>" + tr(
|
---|
| 265 | "Cannot open URL ''{0}''<br>The following download tasks accept the URL patterns shown:<br>{1}",
|
---|
[9972] | 266 | url, details) + "</p></html>", tr("Download Location"), JOptionPane.ERROR_MESSAGE, ht("/Action/OpenLocation"));
|
---|
[8195] | 267 | }
|
---|
| 268 |
|
---|
| 269 | /**
|
---|
[6032] | 270 | * Adds a new download task to the supported ones.
|
---|
| 271 | * @param taskClass The new download task to add
|
---|
| 272 | * @return <tt>true</tt> (as specified by {@link Collection#add})
|
---|
| 273 | */
|
---|
[6890] | 274 | public final boolean addDownloadTaskClass(Class<? extends DownloadTask> taskClass) {
|
---|
[4521] | 275 | return this.downloadTasks.add(taskClass);
|
---|
| 276 | }
|
---|
[1146] | 277 | }
|
---|