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