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

Last change on this file since 8855 was 8510, checked in by Don-vip, 9 years ago

checkstyle: enable relevant whitespace checks and fix them

  • Property svn:eol-style set to native
File size: 10.5 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.DownloadOsmIdTask;
34import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmTask;
35import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmUrlTask;
36import org.openstreetmap.josm.actions.downloadtasks.DownloadSessionTask;
37import org.openstreetmap.josm.actions.downloadtasks.DownloadTask;
38import org.openstreetmap.josm.actions.downloadtasks.PostDownloadHandler;
39import org.openstreetmap.josm.gui.ExtendedDialog;
40import org.openstreetmap.josm.gui.HelpAwareOptionPane;
41import org.openstreetmap.josm.gui.help.HelpUtil;
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(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 the history combo box
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 the history combo box
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,
221 tr("Which tasks to perform?"), new String[]{tr("Ok"), tr("Cancel")}, true) { {
222 setButtonIcons(new String[]{"ok", "cancel"});
223 final JPanel pane = new JPanel(new GridLayout(2, 1));
224 pane.add(new JLabel(tr("Which tasks to perform?")));
225 pane.add(list);
226 setContent(pane);
227 } };
228 dialog.showDialog();
229 return dialog.getValue() == 1 ? list.getSelectedValuesList() : Collections.<DownloadTask>emptyList();
230 }
231
232 /**
233 * Displays an error message dialog that no suitable tasks have been found for the given url.
234 * @param url the given url
235 */
236 void warnNoSuitableTasks(final String url) {
237 final String details = findSummaryDocumentation(); // Explain what patterns are supported
238 HelpAwareOptionPane.showMessageDialogInEDT(Main.parent, "<html><p>" + tr(
239 "Cannot open URL ''{0}''<br>The following download tasks accept the URL patterns shown:<br>{1}",
240 url, details) + "</p></html>", tr("Download Location"), JOptionPane.ERROR_MESSAGE, HelpUtil.ht("/Action/OpenLocation"));
241 }
242
243 /**
244 * Adds a new download task to the supported ones.
245 * @param taskClass The new download task to add
246 * @return <tt>true</tt> (as specified by {@link Collection#add})
247 */
248 public final boolean addDownloadTaskClass(Class<? extends DownloadTask> taskClass) {
249 return this.downloadTasks.add(taskClass);
250 }
251}
Note: See TracBrowser for help on using the repository browser.