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.DownloadNotesTask; 026import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmChangeCompressedTask; 027import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmChangeTask; 028import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmCompressedTask; 029import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmTask; 030import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmUrlTask; 031import org.openstreetmap.josm.actions.downloadtasks.DownloadSessionTask; 032import org.openstreetmap.josm.actions.downloadtasks.DownloadTask; 033import org.openstreetmap.josm.actions.downloadtasks.PostDownloadHandler; 034import org.openstreetmap.josm.gui.ExtendedDialog; 035import org.openstreetmap.josm.gui.HelpAwareOptionPane; 036import org.openstreetmap.josm.gui.help.HelpUtil; 037import org.openstreetmap.josm.gui.progress.PleaseWaitProgressMonitor; 038import org.openstreetmap.josm.gui.widgets.HistoryComboBox; 039import org.openstreetmap.josm.tools.Shortcut; 040import org.openstreetmap.josm.tools.Utils; 041 042/** 043 * Open an URL input dialog and load data from the given URL. 044 * 045 * @author imi 046 */ 047public class OpenLocationAction extends JosmAction { 048 049 protected final List<Class<? extends DownloadTask>> downloadTasks; 050 051 /** 052 * Create an open action. The name is "Open a file". 053 */ 054 public OpenLocationAction() { 055 /* I18N: Command to download a specific location/URL */ 056 super(tr("Open Location..."), "openlocation", tr("Open an URL."), 057 Shortcut.registerShortcut("system:open_location", tr("File: {0}", tr("Open Location...")), 058 KeyEvent.VK_L, Shortcut.CTRL), true); 059 putValue("help", ht("/Action/OpenLocation")); 060 this.downloadTasks = new ArrayList<>(); 061 addDownloadTaskClass(DownloadOsmTask.class); 062 addDownloadTaskClass(DownloadGpsTask.class); 063 addDownloadTaskClass(DownloadNotesTask.class); 064 addDownloadTaskClass(DownloadOsmChangeTask.class); 065 addDownloadTaskClass(DownloadOsmUrlTask.class); 066 addDownloadTaskClass(DownloadOsmCompressedTask.class); 067 addDownloadTaskClass(DownloadOsmChangeCompressedTask.class); 068 addDownloadTaskClass(DownloadSessionTask.class); 069 } 070 071 /** 072 * Restore the current history from the preferences 073 * 074 * @param cbHistory 075 */ 076 protected void restoreUploadAddressHistory(HistoryComboBox cbHistory) { 077 List<String> cmtHistory = new LinkedList<>(Main.pref.getCollection(getClass().getName() + ".uploadAddressHistory", 078 new LinkedList<String>())); 079 // we have to reverse the history, because ComboBoxHistory will reverse it again in addElement() 080 // 081 Collections.reverse(cmtHistory); 082 cbHistory.setPossibleItems(cmtHistory); 083 } 084 085 /** 086 * Remind the current history in the preferences 087 * @param cbHistory 088 */ 089 protected void remindUploadAddressHistory(HistoryComboBox cbHistory) { 090 cbHistory.addCurrentItemToHistory(); 091 Main.pref.putCollection(getClass().getName() + ".uploadAddressHistory", cbHistory.getHistory()); 092 } 093 094 @Override 095 public void actionPerformed(ActionEvent e) { 096 097 JCheckBox layer = new JCheckBox(tr("Separate Layer")); 098 layer.setToolTipText(tr("Select if the data should be downloaded into a new layer")); 099 layer.setSelected(Main.pref.getBoolean("download.newlayer")); 100 JPanel all = new JPanel(new GridBagLayout()); 101 GridBagConstraints gc = new GridBagConstraints(); 102 gc.fill = GridBagConstraints.HORIZONTAL; 103 gc.weightx = 1.0; 104 gc.anchor = GridBagConstraints.FIRST_LINE_START; 105 all.add(new JLabel(tr("Enter URL to download:")), gc); 106 HistoryComboBox uploadAddresses = new HistoryComboBox(); 107 uploadAddresses.setToolTipText(tr("Enter an URL from where data should be downloaded")); 108 restoreUploadAddressHistory(uploadAddresses); 109 gc.gridy = 1; 110 all.add(uploadAddresses, gc); 111 gc.gridy = 2; 112 gc.fill = GridBagConstraints.BOTH; 113 gc.weighty = 1.0; 114 all.add(layer, gc); 115 ExtendedDialog dialog = new ExtendedDialog(Main.parent, 116 tr("Download Location"), 117 new String[] {tr("Download URL"), tr("Cancel")} 118 ); 119 dialog.setContent(all, false /* don't embedded content in JScrollpane */); 120 dialog.setButtonIcons(new String[] {"download", "cancel"}); 121 dialog.setToolTipTexts(new String[] { 122 tr("Start downloading data"), 123 tr("Close dialog and cancel downloading") 124 }); 125 dialog.configureContextsensitiveHelp("/Action/OpenLocation", true /* show help button */); 126 dialog.showDialog(); 127 if (dialog.getValue() != 1) return; 128 remindUploadAddressHistory(uploadAddresses); 129 openUrl(layer.isSelected(), Utils.strip(uploadAddresses.getText())); 130 } 131 132 /** 133 * Replies the list of download tasks accepting the given url. 134 * @param url The URL to open 135 * @param isRemotecontrol True if download request comes from remotecontrol. 136 * @return The list of download tasks accepting the given url. 137 * @since 5691 138 */ 139 public Collection<DownloadTask> findDownloadTasks(final String url, boolean isRemotecontrol) { 140 List<DownloadTask> result = new ArrayList<>(); 141 for (Class<? extends DownloadTask> taskClass : downloadTasks) { 142 if (taskClass != null) { 143 try { 144 DownloadTask task = taskClass.getConstructor().newInstance(); 145 if (task.acceptsUrl(url, isRemotecontrol)) { 146 result.add(task); 147 } 148 } catch (Exception e) { 149 Main.error(e); 150 } 151 } 152 } 153 return result; 154 } 155 156 /** 157 * Summarizes acceptable urls for error message purposes. 158 * @return The HTML message to be displayed 159 * @since 6031 160 */ 161 public String findSummaryDocumentation() { 162 StringBuilder result = new StringBuilder("<table>"); 163 for (Class<? extends DownloadTask> taskClass : downloadTasks) { 164 if (taskClass != null) { 165 try { 166 DownloadTask task = taskClass.getConstructor().newInstance(); 167 result.append(task.acceptsDocumentationSummary()); 168 } catch (Exception e) { 169 Main.error(e); 170 } 171 } 172 } 173 result.append("</table>"); 174 return result.toString(); 175 } 176 177 /** 178 * Open the given URL. 179 * @param newLayer true if the URL needs to be opened in a new layer, false otherwise 180 * @param url The URL to open 181 */ 182 public void openUrl(boolean newLayer, final String url) { 183 PleaseWaitProgressMonitor monitor = new PleaseWaitProgressMonitor(tr("Download Data")); 184 Collection<DownloadTask> tasks = findDownloadTasks(url, false); 185 DownloadTask task = null; 186 Future<?> future = null; 187 if (!tasks.isEmpty()) { 188 // TODO: handle multiple suitable tasks ? 189 try { 190 task = tasks.iterator().next(); 191 future = task.loadUrl(newLayer, url, monitor); 192 } catch (IllegalArgumentException e) { 193 Main.error(e); 194 } 195 } 196 if (future != null) { 197 Main.worker.submit(new PostDownloadHandler(task, future)); 198 } else { 199 final String details = findSummaryDocumentation(); // Explain what patterns are supported 200 HelpAwareOptionPane.showMessageDialogInEDT(Main.parent, "<html><p>" + tr( 201 "Cannot open URL ''{0}''<br>The following download tasks accept the URL patterns shown:<br>{1}", 202 url, details) + "</p></html>", tr("Download Location"), JOptionPane.ERROR_MESSAGE, HelpUtil.ht("/Action/OpenLocation")); 203 } 204 } 205 206 /** 207 * Adds a new download task to the supported ones. 208 * @param taskClass The new download task to add 209 * @return <tt>true</tt> (as specified by {@link Collection#add}) 210 */ 211 public final boolean addDownloadTaskClass(Class<? extends DownloadTask> taskClass) { 212 return this.downloadTasks.add(taskClass); 213 } 214}