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