1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 |
|
---|
7 | import java.awt.GridBagConstraints;
|
---|
8 | import java.awt.GridBagLayout;
|
---|
9 | import java.awt.event.ActionEvent;
|
---|
10 | import java.awt.event.KeyEvent;
|
---|
11 | import java.util.ArrayList;
|
---|
12 | import java.util.Collection;
|
---|
13 | import java.util.Collections;
|
---|
14 | import java.util.LinkedList;
|
---|
15 | import java.util.List;
|
---|
16 | import java.util.concurrent.Future;
|
---|
17 |
|
---|
18 | import javax.swing.JCheckBox;
|
---|
19 | import javax.swing.JLabel;
|
---|
20 | import javax.swing.JOptionPane;
|
---|
21 | import javax.swing.JPanel;
|
---|
22 |
|
---|
23 | import org.openstreetmap.josm.Main;
|
---|
24 | import org.openstreetmap.josm.actions.downloadtasks.DownloadGpsTask;
|
---|
25 | import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmChangeCompressedTask;
|
---|
26 | import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmChangeTask;
|
---|
27 | import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmCompressedTask;
|
---|
28 | import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmTask;
|
---|
29 | import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmUrlTask;
|
---|
30 | import org.openstreetmap.josm.actions.downloadtasks.DownloadSessionTask;
|
---|
31 | import org.openstreetmap.josm.actions.downloadtasks.DownloadTask;
|
---|
32 | import org.openstreetmap.josm.actions.downloadtasks.PostDownloadHandler;
|
---|
33 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
34 | import org.openstreetmap.josm.gui.HelpAwareOptionPane;
|
---|
35 | import org.openstreetmap.josm.gui.help.HelpUtil;
|
---|
36 | import org.openstreetmap.josm.gui.progress.PleaseWaitProgressMonitor;
|
---|
37 | import org.openstreetmap.josm.gui.widgets.HistoryComboBox;
|
---|
38 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
39 | import 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 | */
|
---|
46 | public 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 | e.printStackTrace();
|
---|
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 | e.printStackTrace();
|
---|
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 | future = tasks.iterator().next().loadUrl(new_layer, url, monitor);
|
---|
186 | }
|
---|
187 | if (future != null) {
|
---|
188 | Main.worker.submit(new PostDownloadHandler(task, future));
|
---|
189 | } else {
|
---|
190 | final String details = findSummaryDocumentation(); // Explain what patterns are supported
|
---|
191 | HelpAwareOptionPane.showMessageDialogInEDT(Main.parent, "<html><p>" + tr(
|
---|
192 | "Cannot open URL ''{0}''<br>The following download tasks accept the URL patterns shown:<br>{1}",
|
---|
193 | url, details) + "</p></html>", tr("Download Location"), JOptionPane.ERROR_MESSAGE, HelpUtil.ht("/Action/OpenLocation"));
|
---|
194 | }
|
---|
195 | }
|
---|
196 |
|
---|
197 | /**
|
---|
198 | * Adds a new download task to the supported ones.
|
---|
199 | * @param taskClass The new download task to add
|
---|
200 | * @return <tt>true</tt> (as specified by {@link Collection#add})
|
---|
201 | */
|
---|
202 | public boolean addDownloadTaskClass(Class<? extends DownloadTask> taskClass) {
|
---|
203 | return this.downloadTasks.add(taskClass);
|
---|
204 | }
|
---|
205 | }
|
---|