| 1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others |
|---|
| 2 | package org.openstreetmap.josm.actions; |
|---|
| 3 | |
|---|
| 4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht; |
|---|
| 5 | import static org.openstreetmap.josm.tools.I18n.tr; |
|---|
| 6 | |
|---|
| 7 | import java.awt.GridBagConstraints; |
|---|
| 8 | import java.awt.GridBagLayout; |
|---|
| 9 | import java.awt.event.ActionEvent; |
|---|
| 10 | import java.awt.event.KeyEvent; |
|---|
| 11 | import java.util.ArrayList; |
|---|
| 12 | import java.util.Collections; |
|---|
| 13 | import java.util.LinkedList; |
|---|
| 14 | import java.util.List; |
|---|
| 15 | import java.util.concurrent.Future; |
|---|
| 16 | |
|---|
| 17 | import javax.swing.JCheckBox; |
|---|
| 18 | import javax.swing.JLabel; |
|---|
| 19 | import javax.swing.JOptionPane; |
|---|
| 20 | import javax.swing.JPanel; |
|---|
| 21 | import javax.swing.SwingUtilities; |
|---|
| 22 | |
|---|
| 23 | import org.openstreetmap.josm.Main; |
|---|
| 24 | import org.openstreetmap.josm.actions.downloadtasks.DownloadGpsTask; |
|---|
| 25 | import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmChangeTask; |
|---|
| 26 | import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmTask; |
|---|
| 27 | import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmUrlTask; |
|---|
| 28 | import org.openstreetmap.josm.actions.downloadtasks.DownloadTask; |
|---|
| 29 | import org.openstreetmap.josm.actions.downloadtasks.PostDownloadHandler; |
|---|
| 30 | import org.openstreetmap.josm.gui.ExtendedDialog; |
|---|
| 31 | import org.openstreetmap.josm.gui.progress.PleaseWaitProgressMonitor; |
|---|
| 32 | import org.openstreetmap.josm.gui.widgets.HistoryComboBox; |
|---|
| 33 | import org.openstreetmap.josm.tools.Shortcut; |
|---|
| 34 | |
|---|
| 35 | /** |
|---|
| 36 | * Open an URL input dialog and load data from the given URL. |
|---|
| 37 | * |
|---|
| 38 | * @author imi |
|---|
| 39 | */ |
|---|
| 40 | public class OpenLocationAction extends JosmAction { |
|---|
| 41 | |
|---|
| 42 | protected final List<Class<? extends DownloadTask>> downloadTasks; |
|---|
| 43 | |
|---|
| 44 | /** |
|---|
| 45 | * Create an open action. The name is "Open a file". |
|---|
| 46 | */ |
|---|
| 47 | public OpenLocationAction() { |
|---|
| 48 | /* I18N: Command to download a specific location/URL */ |
|---|
| 49 | super(tr("Open Location..."), "openlocation", tr("Open an URL."), |
|---|
| 50 | Shortcut.registerShortcut("system:open_location", tr("File: {0}", tr("Open Location...")), KeyEvent.VK_L, Shortcut.CTRL), true); |
|---|
| 51 | putValue("help", ht("/Action/OpenLocation")); |
|---|
| 52 | this.downloadTasks = new ArrayList<Class<? extends DownloadTask>>(); |
|---|
| 53 | addDownloadTaskClass(DownloadOsmTask.class); |
|---|
| 54 | addDownloadTaskClass(DownloadGpsTask.class); |
|---|
| 55 | addDownloadTaskClass(DownloadOsmChangeTask.class); |
|---|
| 56 | addDownloadTaskClass(DownloadOsmUrlTask.class); |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | /** |
|---|
| 60 | * Restore the current history from the preferences |
|---|
| 61 | * |
|---|
| 62 | * @param cbHistory |
|---|
| 63 | */ |
|---|
| 64 | protected void restoreUploadAddressHistory(HistoryComboBox cbHistory) { |
|---|
| 65 | List<String> cmtHistory = new LinkedList<String>(Main.pref.getCollection(getClass().getName() + ".uploadAddressHistory", new LinkedList<String>())); |
|---|
| 66 | // we have to reverse the history, because ComboBoxHistory will reverse it again |
|---|
| 67 | // in addElement() |
|---|
| 68 | // |
|---|
| 69 | Collections.reverse(cmtHistory); |
|---|
| 70 | cbHistory.setPossibleItems(cmtHistory); |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | /** |
|---|
| 74 | * Remind the current history in the preferences |
|---|
| 75 | * @param cbHistory |
|---|
| 76 | */ |
|---|
| 77 | protected void remindUploadAddressHistory(HistoryComboBox cbHistory) { |
|---|
| 78 | cbHistory.addCurrentItemToHistory(); |
|---|
| 79 | Main.pref.putCollection(getClass().getName() + ".uploadAddressHistory", cbHistory.getHistory()); |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | public void actionPerformed(ActionEvent e) { |
|---|
| 83 | |
|---|
| 84 | JCheckBox layer = new JCheckBox(tr("Separate Layer")); |
|---|
| 85 | layer.setToolTipText(tr("Select if the data should be downloaded into a new layer")); |
|---|
| 86 | layer.setSelected(Main.pref.getBoolean("download.newlayer")); |
|---|
| 87 | JPanel all = new JPanel(new GridBagLayout()); |
|---|
| 88 | GridBagConstraints gc = new GridBagConstraints(); |
|---|
| 89 | gc.fill = GridBagConstraints.HORIZONTAL; |
|---|
| 90 | gc.weightx = 1.0; |
|---|
| 91 | gc.anchor = GridBagConstraints.FIRST_LINE_START; |
|---|
| 92 | all.add(new JLabel(tr("Enter URL to download:")), gc); |
|---|
| 93 | HistoryComboBox uploadAddresses = new HistoryComboBox(); |
|---|
| 94 | uploadAddresses.setToolTipText(tr("Enter an URL from where data should be downloaded")); |
|---|
| 95 | restoreUploadAddressHistory(uploadAddresses); |
|---|
| 96 | gc.gridy = 1; |
|---|
| 97 | all.add(uploadAddresses, gc); |
|---|
| 98 | gc.gridy = 2; |
|---|
| 99 | gc.fill = GridBagConstraints.BOTH; |
|---|
| 100 | gc.weighty = 1.0; |
|---|
| 101 | all.add(layer, gc); |
|---|
| 102 | ExtendedDialog dialog = new ExtendedDialog(Main.parent, |
|---|
| 103 | tr("Download Location"), |
|---|
| 104 | new String[] {tr("Download URL"), tr("Cancel")} |
|---|
| 105 | ); |
|---|
| 106 | dialog.setContent(all, false /* don't embedded content in JScrollpane */); |
|---|
| 107 | dialog.setButtonIcons(new String[] {"download.png", "cancel.png"}); |
|---|
| 108 | dialog.setToolTipTexts(new String[] { |
|---|
| 109 | tr("Start downloading data"), |
|---|
| 110 | tr("Close dialog and cancel downloading") |
|---|
| 111 | }); |
|---|
| 112 | dialog.configureContextsensitiveHelp("/Action/OpenLocation", true /* show help button */); |
|---|
| 113 | dialog.showDialog(); |
|---|
| 114 | if (dialog.getValue() != 1) return; |
|---|
| 115 | remindUploadAddressHistory(uploadAddresses); |
|---|
| 116 | openUrl(layer.isSelected(), uploadAddresses.getText()); |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | /** |
|---|
| 120 | * Open the given URL. |
|---|
| 121 | */ |
|---|
| 122 | public void openUrl(boolean new_layer, final String url) { |
|---|
| 123 | PleaseWaitProgressMonitor monitor = new PleaseWaitProgressMonitor(tr("Download Data")); |
|---|
| 124 | DownloadTask task = null; |
|---|
| 125 | Future<?> future = null; |
|---|
| 126 | for (int i = 0; future == null && i < downloadTasks.size(); i++) { |
|---|
| 127 | Class<? extends DownloadTask> taskClass = downloadTasks.get(i); |
|---|
| 128 | if (taskClass != null) { |
|---|
| 129 | try { |
|---|
| 130 | task = taskClass.getConstructor().newInstance(); |
|---|
| 131 | if (task.acceptsUrl(url)) { |
|---|
| 132 | future = task.loadUrl(new_layer, url, monitor); |
|---|
| 133 | } |
|---|
| 134 | } catch (Exception e) { |
|---|
| 135 | e.printStackTrace(); |
|---|
| 136 | } |
|---|
| 137 | } |
|---|
| 138 | } |
|---|
| 139 | if (future != null) { |
|---|
| 140 | Main.worker.submit(new PostDownloadHandler(task, future)); |
|---|
| 141 | } else { |
|---|
| 142 | SwingUtilities.invokeLater(new Runnable() { |
|---|
| 143 | public void run() { |
|---|
| 144 | JOptionPane.showMessageDialog(Main.parent, tr( |
|---|
| 145 | "<html>Cannot open URL ''{0}'' because no suitable download task is available.</html>", |
|---|
| 146 | url), tr("Download Location"), JOptionPane.ERROR_MESSAGE); |
|---|
| 147 | } |
|---|
| 148 | }); |
|---|
| 149 | } |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | public boolean addDownloadTaskClass(Class<? extends DownloadTask> taskClass) { |
|---|
| 153 | return this.downloadTasks.add(taskClass); |
|---|
| 154 | } |
|---|
| 155 | } |
|---|