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