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

Last change on this file since 2474 was 2323, checked in by Gubaer, 15 years ago

Added explicit help topics
See also current list of help topics with links to source files and to help pages

File size: 4.4 KB
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.Collections;
12import java.util.LinkedList;
13import java.util.List;
14import java.util.concurrent.Future;
15
16import javax.swing.JCheckBox;
17import javax.swing.JLabel;
18import javax.swing.JPanel;
19
20import org.openstreetmap.josm.Main;
21import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmTask;
22import org.openstreetmap.josm.actions.downloadtasks.PostDownloadHandler;
23import org.openstreetmap.josm.gui.ExtendedDialog;
24import org.openstreetmap.josm.gui.progress.PleaseWaitProgressMonitor;
25import org.openstreetmap.josm.gui.widgets.HistoryComboBox;
26import 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 */
33public 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 in 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}
Note: See TracBrowser for help on using the repository browser.