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

Last change on this file since 6031 was 6031, checked in by akks, 11 years ago

see #8809: [patch by brycenesbitt, reworked] detailed documentation for "no suitable download task is available" error (Ctrl-L)

  • Property svn:eol-style set to native
File size: 8.2 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
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.event.ActionEvent;
10import java.awt.event.KeyEvent;
11import java.util.ArrayList;
12import java.util.Collection;
13import java.util.Collections;
14import java.util.LinkedList;
15import java.util.List;
16import java.util.concurrent.Future;
17
18import javax.swing.JCheckBox;
19import javax.swing.JLabel;
20import javax.swing.JOptionPane;
21import javax.swing.JPanel;
22import javax.swing.SwingUtilities;
23
24import org.openstreetmap.josm.Main;
25import org.openstreetmap.josm.actions.downloadtasks.DownloadGpsTask;
26import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmChangeCompressedTask;
27import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmCompressedTask;
28import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmChangeTask;
29import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmTask;
30import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmUrlTask;
31import org.openstreetmap.josm.actions.downloadtasks.DownloadTask;
32import org.openstreetmap.josm.actions.downloadtasks.PostDownloadHandler;
33import org.openstreetmap.josm.gui.ExtendedDialog;
34import org.openstreetmap.josm.gui.progress.PleaseWaitProgressMonitor;
35import org.openstreetmap.josm.gui.util.GuiHelper;
36import org.openstreetmap.josm.gui.widgets.HistoryComboBox;
37import org.openstreetmap.josm.tools.Shortcut;
38import org.openstreetmap.josm.tools.Utils;
39
40/**
41 * Open an URL input dialog and load data from the given URL.
42 *
43 * @author imi
44 */
45public class OpenLocationAction extends JosmAction {
46
47 protected final List<Class<? extends DownloadTask>> downloadTasks;
48
49 /**
50 * Create an open action. The name is "Open a file".
51 */
52 public OpenLocationAction() {
53 /* I18N: Command to download a specific location/URL */
54 super(tr("Open Location..."), "openlocation", tr("Open an URL."),
55 Shortcut.registerShortcut("system:open_location", tr("File: {0}", tr("Open Location...")), KeyEvent.VK_L, Shortcut.CTRL), true);
56 putValue("help", ht("/Action/OpenLocation"));
57 this.downloadTasks = new ArrayList<Class<? extends DownloadTask>>();
58 addDownloadTaskClass(DownloadOsmTask.class);
59 addDownloadTaskClass(DownloadGpsTask.class);
60 addDownloadTaskClass(DownloadOsmChangeTask.class);
61 addDownloadTaskClass(DownloadOsmUrlTask.class);
62 addDownloadTaskClass(DownloadOsmCompressedTask.class);
63 addDownloadTaskClass(DownloadOsmChangeCompressedTask.class);
64 }
65
66 /**
67 * Restore the current history from the preferences
68 *
69 * @param cbHistory
70 */
71 protected void restoreUploadAddressHistory(HistoryComboBox cbHistory) {
72 List<String> cmtHistory = new LinkedList<String>(Main.pref.getCollection(getClass().getName() + ".uploadAddressHistory", new LinkedList<String>()));
73 // we have to reverse the history, because ComboBoxHistory will reverse it again
74 // in addElement()
75 //
76 Collections.reverse(cmtHistory);
77 cbHistory.setPossibleItems(cmtHistory);
78 }
79
80 /**
81 * Remind the current history in the preferences
82 * @param cbHistory
83 */
84 protected void remindUploadAddressHistory(HistoryComboBox cbHistory) {
85 cbHistory.addCurrentItemToHistory();
86 Main.pref.putCollection(getClass().getName() + ".uploadAddressHistory", cbHistory.getHistory());
87 }
88
89 public void actionPerformed(ActionEvent e) {
90
91 JCheckBox layer = new JCheckBox(tr("Separate Layer"));
92 layer.setToolTipText(tr("Select if the data should be downloaded into a new layer"));
93 layer.setSelected(Main.pref.getBoolean("download.newlayer"));
94 JPanel all = new JPanel(new GridBagLayout());
95 GridBagConstraints gc = new GridBagConstraints();
96 gc.fill = GridBagConstraints.HORIZONTAL;
97 gc.weightx = 1.0;
98 gc.anchor = GridBagConstraints.FIRST_LINE_START;
99 all.add(new JLabel(tr("Enter URL to download:")), gc);
100 HistoryComboBox uploadAddresses = new HistoryComboBox();
101 uploadAddresses.setToolTipText(tr("Enter an URL from where data should be downloaded"));
102 restoreUploadAddressHistory(uploadAddresses);
103 gc.gridy = 1;
104 all.add(uploadAddresses, gc);
105 gc.gridy = 2;
106 gc.fill = GridBagConstraints.BOTH;
107 gc.weighty = 1.0;
108 all.add(layer, gc);
109 ExtendedDialog dialog = new ExtendedDialog(Main.parent,
110 tr("Download Location"),
111 new String[] {tr("Download URL"), tr("Cancel")}
112 );
113 dialog.setContent(all, false /* don't embedded content in JScrollpane */);
114 dialog.setButtonIcons(new String[] {"download.png", "cancel.png"});
115 dialog.setToolTipTexts(new String[] {
116 tr("Start downloading data"),
117 tr("Close dialog and cancel downloading")
118 });
119 dialog.configureContextsensitiveHelp("/Action/OpenLocation", true /* show help button */);
120 dialog.showDialog();
121 if (dialog.getValue() != 1) return;
122 remindUploadAddressHistory(uploadAddresses);
123 openUrl(layer.isSelected(), Utils.strip(uploadAddresses.getText()));
124 }
125
126 /**
127 * Replies the list of download tasks accepting the given url.
128 * @param url The URL to open
129 * @return The list of download tasks accepting the given url.
130 * @since 5691
131 */
132 public Collection<DownloadTask> findDownloadTasks(final String url) {
133 List<DownloadTask> result = new ArrayList<DownloadTask>();
134 for (int i = 0; i < downloadTasks.size(); i++) {
135 Class<? extends DownloadTask> taskClass = downloadTasks.get(i);
136 if (taskClass != null) {
137 try {
138 DownloadTask task = taskClass.getConstructor().newInstance();
139 if (task.acceptsUrl(url)) {
140 result.add(task);
141 }
142 } catch (Exception e) {
143 e.printStackTrace();
144 }
145 }
146 }
147 return result;
148 }
149
150 /**
151 * Summarizes acceptable urls for error message purposes.
152 * @since 6030
153 */
154 public String findSummaryDocumentation() {
155 String result = "";
156 for (int i = 0; i < downloadTasks.size(); i++) {
157 Class<? extends DownloadTask> taskClass = downloadTasks.get(i);
158 if (taskClass != null) {
159 try {
160 DownloadTask task = taskClass.getConstructor().newInstance();
161 result += "<br/>" + task.acceptsDocumentationSummary();
162 } catch (Exception e) {
163 e.printStackTrace();
164 }
165 }
166 }
167 return result;
168 }
169
170 /**
171 * Open the given URL.
172 * @param new_layer true if the URL needs to be opened in a new layer, false otherwise
173 * @param url The URL to open
174 */
175 public void openUrl(boolean new_layer, final String url) {
176 PleaseWaitProgressMonitor monitor = new PleaseWaitProgressMonitor(tr("Download Data"));
177 Collection<DownloadTask> tasks = findDownloadTasks(url);
178 DownloadTask task = null;
179 Future<?> future = null;
180 if (!tasks.isEmpty()) {
181 // TODO: handle multiple suitable tasks ?
182 future = tasks.iterator().next().loadUrl(new_layer, url, monitor);
183 }
184 if (future != null) {
185 Main.worker.submit(new PostDownloadHandler(task, future));
186 } else {
187 final String details = findSummaryDocumentation(); // Explain what patterns are supported
188 SwingUtilities.invokeLater(new Runnable() {
189 @Override
190 public void run() {
191 JOptionPane.showMessageDialog(Main.parent, tr(
192 "<html><p>Cannot open URL ''{0}''<br/>The following load tasks accept the URL patterns shown:<br/>{1}</p></html>",
193 url, details), tr("Download Location"), JOptionPane.ERROR_MESSAGE);
194 }
195 });
196 }
197 }
198
199 public boolean addDownloadTaskClass(Class<? extends DownloadTask> taskClass) {
200 return this.downloadTasks.add(taskClass);
201 }
202}
Note: See TracBrowser for help on using the repository browser.