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

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

fix #8827 - HTML rendering differ if run before or after having launched Help Browser

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