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

Last change on this file since 5851 was 5772, checked in by Don-vip, 11 years ago

see #8505 - strip of trailing and leading whitespace characters of input URL (proper solution than String.trim(), see Utils.strip()) + unit test

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