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

Last change on this file since 5361 was 5361, checked in by Don-vip, 12 years ago

fix #7879 - Allow to open local and remote gzipped/bzipped osmChange files + remote osm.gz files + make some public constants of File filters to share between same importers/exporters

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