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