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

Last change on this file since 6706 was 6643, checked in by Don-vip, 10 years ago

global replacement of e.printStackTrace() by Main.error(e)

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