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

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

fix various Sonar issues, improve Javadoc

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