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

Last change on this file since 8231 was 8203, checked in by simon04, 9 years ago

see #9907 - Open Location, Which tasks to perform?: use standard icons

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