source: josm/trunk/src/org/openstreetmap/josm/actions/OpenLocationAction.java@ 15652

Last change on this file since 15652 was 15441, checked in by Don-vip, 5 years ago

see #10564 - add missing geojson download task
see #13015 - fix presets checkgroup layout for checks without icon

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