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

Revision 4996, 6.3 KB checked in by Don-vip, 3 months ago (diff)

fix #2012 - Download from URL dropped on main window

  • Property svn:eol-style set to native
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.Collections;
13import java.util.LinkedList;
14import java.util.List;
15import java.util.concurrent.Future;
16
17import javax.swing.JCheckBox;
18import javax.swing.JLabel;
19import javax.swing.JOptionPane;
20import javax.swing.JPanel;
21import javax.swing.SwingUtilities;
22
23import org.openstreetmap.josm.Main;
24import org.openstreetmap.josm.actions.downloadtasks.DownloadGpsTask;
25import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmChangeTask;
26import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmTask;
27import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmUrlTask;
28import org.openstreetmap.josm.actions.downloadtasks.DownloadTask;
29import org.openstreetmap.josm.actions.downloadtasks.PostDownloadHandler;
30import org.openstreetmap.josm.gui.ExtendedDialog;
31import org.openstreetmap.josm.gui.progress.PleaseWaitProgressMonitor;
32import org.openstreetmap.josm.gui.widgets.HistoryComboBox;
33import 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 */
40public 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}
Note: See TracBrowser for help on using the repository browser.