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.Collections;
|
---|
12 | import java.util.LinkedList;
|
---|
13 | import java.util.List;
|
---|
14 | import java.util.concurrent.Future;
|
---|
15 |
|
---|
16 | import javax.swing.JCheckBox;
|
---|
17 | import javax.swing.JLabel;
|
---|
18 | import javax.swing.JPanel;
|
---|
19 |
|
---|
20 | import org.openstreetmap.josm.Main;
|
---|
21 | import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmTask;
|
---|
22 | import org.openstreetmap.josm.actions.downloadtasks.PostDownloadHandler;
|
---|
23 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
24 | import org.openstreetmap.josm.gui.progress.PleaseWaitProgressMonitor;
|
---|
25 | import org.openstreetmap.josm.gui.widgets.HistoryComboBox;
|
---|
26 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Open an URL input dialog and load data from the given URL.
|
---|
30 | *
|
---|
31 | * @author imi
|
---|
32 | */
|
---|
33 | public class OpenLocationAction extends JosmAction {
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Create an open action. The name is "Open a file".
|
---|
37 | */
|
---|
38 | public OpenLocationAction() {
|
---|
39 | super(tr("Open Location..."), "openlocation", tr("Open an URL."),
|
---|
40 | Shortcut.registerShortcut("system:open_location", tr("File: {0}", tr("Open Location...")), KeyEvent.VK_L, Shortcut.GROUP_MENU), true);
|
---|
41 | putValue("help", ht("/Action/OpenLocation"));
|
---|
42 | }
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Restore the current history from the preferences
|
---|
46 | *
|
---|
47 | * @param cbHistory
|
---|
48 | */
|
---|
49 | protected void restoreUploadAddressHistory(HistoryComboBox cbHistory) {
|
---|
50 | List<String> cmtHistory = new LinkedList<String>(Main.pref.getCollection(getClass().getName() + ".uploadAddressHistory", new LinkedList<String>()));
|
---|
51 | // we have to reverse the history, because ComboBoxHistory will reverse it again
|
---|
52 | // in addElement()
|
---|
53 | //
|
---|
54 | Collections.reverse(cmtHistory);
|
---|
55 | cbHistory.setPossibleItems(cmtHistory);
|
---|
56 | }
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Remind the current history in the preferences
|
---|
60 | * @param cbHistory
|
---|
61 | */
|
---|
62 | protected void remindUploadAddressHistory(HistoryComboBox cbHistory) {
|
---|
63 | cbHistory.addCurrentItemToHistory();
|
---|
64 | Main.pref.putCollection(getClass().getName() + ".uploadAddressHistory", cbHistory.getHistory());
|
---|
65 | }
|
---|
66 |
|
---|
67 | public void actionPerformed(ActionEvent e) {
|
---|
68 |
|
---|
69 | JCheckBox layer = new JCheckBox(tr("Separate Layer"));
|
---|
70 | layer.setToolTipText(tr("Select if the data should be downloaded into a new layer"));
|
---|
71 | layer.setSelected(Main.pref.getBoolean("download.newlayer"));
|
---|
72 | JPanel all = new JPanel(new GridBagLayout());
|
---|
73 | GridBagConstraints gc = new GridBagConstraints();
|
---|
74 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
75 | gc.weightx = 1.0;
|
---|
76 | gc.anchor = GridBagConstraints.FIRST_LINE_START;
|
---|
77 | all.add(new JLabel(tr("Enter URL to download:")), gc);
|
---|
78 | HistoryComboBox uploadAdresses = new HistoryComboBox();
|
---|
79 | uploadAdresses.setToolTipText(tr("Enter an URL from where data should be downloaded"));
|
---|
80 | restoreUploadAddressHistory(uploadAdresses);
|
---|
81 | gc.gridy = 1;
|
---|
82 | all.add(uploadAdresses, gc);
|
---|
83 | gc.gridy = 2;
|
---|
84 | gc.fill = GridBagConstraints.BOTH;
|
---|
85 | gc.weighty = 1.0;
|
---|
86 | all.add(layer, gc);
|
---|
87 | ExtendedDialog dialog = new ExtendedDialog(Main.parent,
|
---|
88 | tr("Download Location"),
|
---|
89 | new String[] {tr("Download URL"), tr("Cancel")}
|
---|
90 | );
|
---|
91 | dialog.setContent(all, false /* don't embedded content in JScrollpane */);
|
---|
92 | dialog.setButtonIcons(new String[] {"download.png", "cancel.png"});
|
---|
93 | dialog.setToolTipTexts(new String[] {
|
---|
94 | tr("Start downloading data"),
|
---|
95 | tr("Close dialog and cancel downloading")
|
---|
96 | });
|
---|
97 | dialog.configureContextsensitiveHelp("/Action/OpenLocation", true /* show help button */);
|
---|
98 | dialog.showDialog();
|
---|
99 | if (dialog.getValue() != 1) return;
|
---|
100 | remindUploadAddressHistory(uploadAdresses);
|
---|
101 | openUrl(layer.isSelected(), uploadAdresses.getText());
|
---|
102 | }
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * Open the given file.
|
---|
106 | */
|
---|
107 | public void openUrl(boolean new_layer, String url) {
|
---|
108 | DownloadOsmTask task = new DownloadOsmTask();
|
---|
109 | PleaseWaitProgressMonitor monitor = new PleaseWaitProgressMonitor(tr("Download Data"));
|
---|
110 | Future<?> future = task.loadUrl(new_layer, url, monitor);
|
---|
111 | Main.worker.submit(new PostDownloadHandler(task, future));
|
---|
112 | }
|
---|
113 | }
|
---|