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

Revision 4530, 6.2 KB checked in by Don-vip, 4 months ago (diff)

see #6653 and #6960 - Allow JOSM to download osmChange files again

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