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