001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.actions; 003 004import static org.openstreetmap.josm.gui.help.HelpUtil.ht; 005import static org.openstreetmap.josm.tools.I18n.tr; 006 007import java.awt.GridBagConstraints; 008import java.awt.GridBagLayout; 009import java.awt.event.ActionEvent; 010import java.awt.event.KeyEvent; 011import java.util.ArrayList; 012import java.util.Collection; 013import java.util.Collections; 014import java.util.LinkedList; 015import java.util.List; 016import java.util.concurrent.Future; 017 018import javax.swing.JCheckBox; 019import javax.swing.JLabel; 020import javax.swing.JOptionPane; 021import javax.swing.JPanel; 022 023import org.openstreetmap.josm.Main; 024import org.openstreetmap.josm.actions.downloadtasks.DownloadGpsTask; 025import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmChangeCompressedTask; 026import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmChangeTask; 027import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmCompressedTask; 028import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmTask; 029import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmUrlTask; 030import org.openstreetmap.josm.actions.downloadtasks.DownloadSessionTask; 031import org.openstreetmap.josm.actions.downloadtasks.DownloadTask; 032import org.openstreetmap.josm.actions.downloadtasks.PostDownloadHandler; 033import org.openstreetmap.josm.gui.ExtendedDialog; 034import org.openstreetmap.josm.gui.HelpAwareOptionPane; 035import org.openstreetmap.josm.gui.help.HelpUtil; 036import org.openstreetmap.josm.gui.progress.PleaseWaitProgressMonitor; 037import org.openstreetmap.josm.gui.widgets.HistoryComboBox; 038import org.openstreetmap.josm.tools.Shortcut; 039import org.openstreetmap.josm.tools.Utils; 040 041/** 042 * Open an URL input dialog and load data from the given URL. 043 * 044 * @author imi 045 */ 046public class OpenLocationAction extends JosmAction { 047 048 protected final List<Class<? extends DownloadTask>> downloadTasks; 049 050 /** 051 * Create an open action. The name is "Open a file". 052 */ 053 public OpenLocationAction() { 054 /* I18N: Command to download a specific location/URL */ 055 super(tr("Open Location..."), "openlocation", tr("Open an URL."), 056 Shortcut.registerShortcut("system:open_location", tr("File: {0}", tr("Open Location...")), KeyEvent.VK_L, Shortcut.CTRL), true); 057 putValue("help", ht("/Action/OpenLocation")); 058 this.downloadTasks = new ArrayList<>(); 059 addDownloadTaskClass(DownloadOsmTask.class); 060 addDownloadTaskClass(DownloadGpsTask.class); 061 addDownloadTaskClass(DownloadOsmChangeTask.class); 062 addDownloadTaskClass(DownloadOsmUrlTask.class); 063 addDownloadTaskClass(DownloadOsmCompressedTask.class); 064 addDownloadTaskClass(DownloadOsmChangeCompressedTask.class); 065 addDownloadTaskClass(DownloadSessionTask.class); 066 } 067 068 /** 069 * Restore the current history from the preferences 070 * 071 * @param cbHistory 072 */ 073 protected void restoreUploadAddressHistory(HistoryComboBox cbHistory) { 074 List<String> cmtHistory = new LinkedList<>(Main.pref.getCollection(getClass().getName() + ".uploadAddressHistory", new LinkedList<String>())); 075 // we have to reverse the history, because ComboBoxHistory will reverse it again 076 // in addElement() 077 // 078 Collections.reverse(cmtHistory); 079 cbHistory.setPossibleItems(cmtHistory); 080 } 081 082 /** 083 * Remind the current history in the preferences 084 * @param cbHistory 085 */ 086 protected void remindUploadAddressHistory(HistoryComboBox cbHistory) { 087 cbHistory.addCurrentItemToHistory(); 088 Main.pref.putCollection(getClass().getName() + ".uploadAddressHistory", cbHistory.getHistory()); 089 } 090 091 @Override 092 public void actionPerformed(ActionEvent e) { 093 094 JCheckBox layer = new JCheckBox(tr("Separate Layer")); 095 layer.setToolTipText(tr("Select if the data should be downloaded into a new layer")); 096 layer.setSelected(Main.pref.getBoolean("download.newlayer")); 097 JPanel all = new JPanel(new GridBagLayout()); 098 GridBagConstraints gc = new GridBagConstraints(); 099 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<>(); 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 task = tasks.iterator().next(); 187 future = task.loadUrl(new_layer, url, monitor); 188 } catch (IllegalArgumentException e) { 189 Main.error(e); 190 } 191 } 192 if (future != null) { 193 Main.worker.submit(new PostDownloadHandler(task, future)); 194 } else { 195 final String details = findSummaryDocumentation(); // Explain what patterns are supported 196 HelpAwareOptionPane.showMessageDialogInEDT(Main.parent, "<html><p>" + tr( 197 "Cannot open URL ''{0}''<br>The following download tasks accept the URL patterns shown:<br>{1}", 198 url, details) + "</p></html>", tr("Download Location"), JOptionPane.ERROR_MESSAGE, HelpUtil.ht("/Action/OpenLocation")); 199 } 200 } 201 202 /** 203 * Adds a new download task to the supported ones. 204 * @param taskClass The new download task to add 205 * @return <tt>true</tt> (as specified by {@link Collection#add}) 206 */ 207 public final boolean addDownloadTaskClass(Class<? extends DownloadTask> taskClass) { 208 return this.downloadTasks.add(taskClass); 209 } 210}