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

Last change on this file since 11108 was 10630, checked in by simon04, 8 years ago

fix #13201 - Support Geo URLs on command line and open location

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