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

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

fix #8885 (see #4614) - add offline mode with new command line argument --offline which can take one of several of these values (comma separated):

  • josm_website: to disable all accesses to JOSM website (when not cached, disables Getting Started page, help, plugin list, styles, imagery, presets, rules)
  • osm_api: to disable all accesses to OSM API (disables download, upload, changeset queries, history, user message notification)
  • all: alias to disable all values. Currently equivalent to "josm_website,osm_api"

Plus improved javadoc, fixed EDT violations, and fixed a bug with HTTP redirection sent without "Location" header

  • 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<>();
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<>(Main.pref.getCollection(getClass().getName() + ".uploadAddressHistory", new LinkedList<String>()));
75 // we have to reverse the history, because ComboBoxHistory will reverse it again in addElement()
76 //
77 Collections.reverse(cmtHistory);
78 cbHistory.setPossibleItems(cmtHistory);
79 }
80
81 /**
82 * Remind the current history in the preferences
83 * @param cbHistory
84 */
85 protected void remindUploadAddressHistory(HistoryComboBox cbHistory) {
86 cbHistory.addCurrentItemToHistory();
87 Main.pref.putCollection(getClass().getName() + ".uploadAddressHistory", cbHistory.getHistory());
88 }
89
90 @Override
91 public void actionPerformed(ActionEvent e) {
92
93 JCheckBox layer = new JCheckBox(tr("Separate Layer"));
94 layer.setToolTipText(tr("Select if the data should be downloaded into a new layer"));
95 layer.setSelected(Main.pref.getBoolean("download.newlayer"));
96 JPanel all = new JPanel(new GridBagLayout());
97 GridBagConstraints gc = new GridBagConstraints();
98 gc.fill = GridBagConstraints.HORIZONTAL;
99 gc.weightx = 1.0;
100 gc.anchor = GridBagConstraints.FIRST_LINE_START;
101 all.add(new JLabel(tr("Enter URL to download:")), gc);
102 HistoryComboBox uploadAddresses = new HistoryComboBox();
103 uploadAddresses.setToolTipText(tr("Enter an URL from where data should be downloaded"));
104 restoreUploadAddressHistory(uploadAddresses);
105 gc.gridy = 1;
106 all.add(uploadAddresses, gc);
107 gc.gridy = 2;
108 gc.fill = GridBagConstraints.BOTH;
109 gc.weighty = 1.0;
110 all.add(layer, gc);
111 ExtendedDialog dialog = new ExtendedDialog(Main.parent,
112 tr("Download Location"),
113 new String[] {tr("Download URL"), tr("Cancel")}
114 );
115 dialog.setContent(all, false /* don't embedded content in JScrollpane */);
116 dialog.setButtonIcons(new String[] {"download.png", "cancel.png"});
117 dialog.setToolTipTexts(new String[] {
118 tr("Start downloading data"),
119 tr("Close dialog and cancel downloading")
120 });
121 dialog.configureContextsensitiveHelp("/Action/OpenLocation", true /* show help button */);
122 dialog.showDialog();
123 if (dialog.getValue() != 1) return;
124 remindUploadAddressHistory(uploadAddresses);
125 openUrl(layer.isSelected(), Utils.strip(uploadAddresses.getText()));
126 }
127
128 /**
129 * Replies the list of download tasks accepting the given url.
130 * @param url The URL to open
131 * @return The list of download tasks accepting the given url.
132 * @since 5691
133 */
134 public Collection<DownloadTask> findDownloadTasks(final String url) {
135 List<DownloadTask> result = new ArrayList<>();
136 for (Class<? extends DownloadTask> taskClass : downloadTasks) {
137 if (taskClass != null) {
138 try {
139 DownloadTask task = taskClass.getConstructor().newInstance();
140 if (task.acceptsUrl(url)) {
141 result.add(task);
142 }
143 } catch (Exception e) {
144 Main.error(e);
145 }
146 }
147 }
148 return result;
149 }
150
151 /**
152 * Summarizes acceptable urls for error message purposes.
153 * @return The HTML message to be displayed
154 * @since 6031
155 */
156 public String findSummaryDocumentation() {
157 StringBuilder result = new StringBuilder("<table>");
158 for (Class<? extends DownloadTask> taskClass : downloadTasks) {
159 if (taskClass != null) {
160 try {
161 DownloadTask task = taskClass.getConstructor().newInstance();
162 result.append(task.acceptsDocumentationSummary());
163 } catch (Exception e) {
164 Main.error(e);
165 }
166 }
167 }
168 result.append("</table>");
169 return result.toString();
170 }
171
172 /**
173 * Open the given URL.
174 * @param new_layer true if the URL needs to be opened in a new layer, false otherwise
175 * @param url The URL to open
176 */
177 public void openUrl(boolean new_layer, final String url) {
178 PleaseWaitProgressMonitor monitor = new PleaseWaitProgressMonitor(tr("Download Data"));
179 Collection<DownloadTask> tasks = findDownloadTasks(url);
180 DownloadTask task = null;
181 Future<?> future = null;
182 if (!tasks.isEmpty()) {
183 // TODO: handle multiple suitable tasks ?
184 try {
185 task = tasks.iterator().next();
186 future = task.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 final boolean addDownloadTaskClass(Class<? extends DownloadTask> taskClass) {
207 return this.downloadTasks.add(taskClass);
208 }
209}
Note: See TracBrowser for help on using the repository browser.