Ticket #7511: 7511b.patch
| File 7511b.patch, 18.2 KB (added by , 14 years ago) |
|---|
-
src/mirrored_download/UrlSelectionAction.java
1 // License: GPL. For details, see LICENSE file.2 package mirrored_download;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.event.ActionEvent;8 9 import org.openstreetmap.josm.actions.JosmAction;10 11 12 /**13 * TODO: Write comment.14 */15 public class UrlSelectionAction extends JosmAction {16 17 public UrlSelectionAction() {18 super(tr("Select OSM mirror URL"), (String)null, tr("Select OSM mirror URL to download from."),19 null, true, "mirroreddownload/urlselection", true);20 putValue("help", ht("/Action/SelectUrl"));21 }22 23 public void actionPerformed(ActionEvent e) {24 UrlSelectionDialog dialog = UrlSelectionDialog.getInstance();25 dialog.setVisible(true);26 }27 } -
src/mirrored_download/UrlSelectionDialog.java
1 // License: GPL. For details, see LICENSE file.2 package mirrored_download;3 4 import static org.openstreetmap.josm.tools.I18n.marktr;5 import static org.openstreetmap.josm.tools.I18n.tr;6 7 import java.awt.Container;8 import java.awt.Frame;9 import java.awt.GridBagConstraints;10 import java.awt.GridBagLayout;11 import java.awt.event.ActionEvent;12 import java.awt.event.ActionListener;13 import java.io.BufferedReader;14 import java.io.File;15 import java.io.FileInputStream;16 import java.io.FileNotFoundException;17 import java.io.InputStream;18 import java.io.InputStreamReader;19 import java.io.IOException;20 import java.text.DecimalFormat;21 import java.text.Format;22 import java.util.ArrayList;23 import java.util.Collection;24 import java.util.Collections;25 import java.util.Iterator;26 import java.util.Vector;27 import java.util.zip.GZIPInputStream;28 29 import javax.swing.DefaultCellEditor;30 import javax.swing.DefaultListModel;31 import javax.swing.JButton;32 import javax.swing.JComboBox;33 import javax.swing.JComponent;34 import javax.swing.JDialog;35 import javax.swing.JFileChooser;36 import javax.swing.JLabel;37 import javax.swing.JList;38 import javax.swing.JOptionPane;39 import javax.swing.JPanel;40 import javax.swing.JScrollPane;41 import javax.swing.JTabbedPane;42 import javax.swing.JTable;43 import javax.swing.JTextField;44 import javax.swing.KeyStroke;45 import javax.swing.ListSelectionModel;46 import javax.swing.event.ListSelectionEvent;47 import javax.swing.event.ListSelectionListener;48 import javax.swing.event.TableModelEvent;49 import javax.swing.event.TableModelListener;50 import javax.swing.table.DefaultTableModel;51 52 import org.openstreetmap.josm.Main;53 import org.openstreetmap.josm.actions.JosmAction;54 import org.openstreetmap.josm.command.Command;55 import org.openstreetmap.josm.command.ChangeCommand;56 import org.openstreetmap.josm.command.DeleteCommand;57 import org.openstreetmap.josm.data.coor.LatLon;58 import org.openstreetmap.josm.data.gpx.GpxData;59 import org.openstreetmap.josm.data.gpx.GpxTrack;60 import org.openstreetmap.josm.data.gpx.GpxTrackSegment;61 import org.openstreetmap.josm.data.gpx.WayPoint;62 import org.openstreetmap.josm.data.osm.DataSet;63 import org.openstreetmap.josm.data.osm.Node;64 import org.openstreetmap.josm.data.osm.OsmPrimitive;65 import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;66 import org.openstreetmap.josm.io.GpxReader;67 import org.openstreetmap.josm.io.MirroredInputStream;68 import org.openstreetmap.josm.tools.Utils;69 70 import org.xml.sax.SAXException;71 72 public class UrlSelectionDialog73 {74 private JDialog jDialog = null;75 private JTabbedPane tabbedPane = null;76 private JComboBox cbSelectUrl = null;77 78 public UrlSelectionDialog() {79 Frame frame = JOptionPane.getFrameForComponent(Main.parent);80 jDialog = new JDialog(frame, tr("Select OSM mirror URL"), false);81 tabbedPane = new JTabbedPane();82 JPanel tabSettings = new JPanel();83 tabbedPane.addTab(tr("Settings"), tabSettings);84 tabbedPane.setEnabledAt(0, true);85 jDialog.add(tabbedPane);86 87 //Settings Tab88 JPanel contentPane = tabSettings;89 GridBagLayout gridbag = new GridBagLayout();90 GridBagConstraints layoutCons = new GridBagConstraints();91 contentPane.setLayout(gridbag);92 93 JLabel label = new JLabel(tr("Base URL"));94 95 layoutCons.gridx = 0;96 layoutCons.gridy = 0;97 layoutCons.gridwidth = 2;98 layoutCons.weightx = 0.0;99 layoutCons.weighty = 0.0;100 layoutCons.fill = GridBagConstraints.BOTH;101 gridbag.setConstraints(label, layoutCons);102 contentPane.add(label);103 104 cbSelectUrl = new JComboBox();105 cbSelectUrl.setEditable(true);106 107 String preferredUrl = Main.pref.get("plugin.mirrored_download.preferred-url");108 if (preferredUrl != null && !"".equals(preferredUrl))109 cbSelectUrl.addItem(preferredUrl);110 111 for (String url: getURLs()) {112 cbSelectUrl.addItem(url);113 }114 115 cbSelectUrl.setActionCommand("selectURL");116 cbSelectUrl.addActionListener(new UrlChangedAction());117 118 layoutCons.gridx = 0;119 layoutCons.gridy = 1;120 layoutCons.gridwidth = 1;121 layoutCons.weightx = 0.0;122 layoutCons.weighty = 0.0;123 layoutCons.fill = GridBagConstraints.BOTH;124 gridbag.setConstraints(cbSelectUrl, layoutCons);125 contentPane.add(cbSelectUrl);126 127 jDialog.pack();128 jDialog.setLocationRelativeTo(frame);129 }130 131 private Collection<String> getURLs() {132 // List can be edited at http://josm.openstreetmap.de/wiki/MirroredDownloadInfo133 String src = Main.pref.get("plugin.mirrored_download.url-src", "http://josm.openstreetmap.de/mirrored_download_info");134 Collection<String> urls = new ArrayList<String>();135 InputStream in = null;136 try {137 in = new MirroredInputStream(src, 24*60*60);138 BufferedReader reader = new BufferedReader(new InputStreamReader(in));139 String line = null;140 while ((line = reader.readLine()) != null) {141 line = line.trim();142 if (!line.equals("")) {143 urls.add(line);144 }145 }146 } catch (IOException e) {147 e.printStackTrace();148 }149 Utils.close(in);150 for (String url : Main.pref.getCollection("plugin.mirrored_download.custom-urls")) {151 urls.add(url);152 }153 return urls;154 }155 156 public class UrlChangedAction implements ActionListener {157 158 public void actionPerformed(ActionEvent e) {159 MirroredDownloadPlugin.setDownloadUrl(cbSelectUrl.getSelectedItem().toString());160 Main.pref.put("plugin.mirrored_download.preferred-url",161 cbSelectUrl.getSelectedItem().toString());162 }163 164 }165 166 public void setVisible(boolean visible) {167 jDialog.setVisible(visible);168 }169 170 private static UrlSelectionDialog singleton = null;171 172 public static UrlSelectionDialog getInstance() {173 174 if (singleton == null)175 singleton = new UrlSelectionDialog();176 177 return singleton;178 }179 } -
src/mirrored_download/MirroredDownloadAction.java
6 6 7 7 import java.awt.Component; 8 8 import java.awt.GridBagConstraints; 9 10 9 import java.awt.event.ActionEvent; 11 10 import java.awt.event.KeyEvent; 11 import java.util.Arrays; 12 12 import java.util.LinkedList; 13 13 import java.util.concurrent.Future; 14 import java.util.regex.Pattern;15 14 import javax.swing.JComboBox; 16 15 import javax.swing.JLabel; 17 16 import javax.swing.JPanel; 18 import javax.swing.text.JTextComponent;19 17 20 18 import org.openstreetmap.josm.actions.JosmAction; 21 22 19 import org.openstreetmap.josm.Main; 23 20 import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmTask; 24 21 import org.openstreetmap.josm.actions.downloadtasks.PostDownloadHandler; … … 27 24 import org.openstreetmap.josm.data.osm.DataSource; 28 25 import org.openstreetmap.josm.gui.download.DownloadDialog; 29 26 import org.openstreetmap.josm.gui.progress.ProgressMonitor; 30 import org.openstreetmap.josm.gui.widgets.AbstractTextComponentValidator; 27 import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionItemPritority; 28 import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionListItem; 31 29 import org.openstreetmap.josm.gui.widgets.HistoryComboBox; 32 30 import org.openstreetmap.josm.io.BoundingBoxDownloader; 33 31 import org.openstreetmap.josm.io.OsmTransferException; … … 36 34 37 35 public class MirroredDownloadAction extends JosmAction { 38 36 37 static final String XAPI_SERVER_HISTORY_KEY = "plugin.mirrored_download.server-history"; 38 static final String XAPI_DEFAULT_SERVER_KEY = "plugin.mirrored_download.server-default"; 39 39 static final String XAPI_QUERY_HISTORY_KEY = "plugin.mirrored_download.query-history"; 40 static final String XAPI_QUERY_TOOLTIP = tr("XAPI query, e.g., '''' (to download all data), ''[highway=*]'', or ''[[network=VRR][ref=603|613]''");41 40 42 41 public MirroredDownloadAction() { 43 super(tr("Download from OSM mirror..."), (String) null, tr("Download map data from the OSM server."),42 super(tr("Download from OSM mirror..."), (String) null, tr("Download map data from the OSM server."), 44 43 Shortcut.registerShortcut("mirror:download", tr("File: {0}", tr("Download from OSM mirror...")), KeyEvent.VK_DOWN, Shortcut.ALT_SHIFT), 45 44 true, "mirroreddownload/download", true); 46 45 putValue("help", ht("/Action/MirroredDownload")); … … 56 55 Bounds area = dialog.getSelectedDownloadArea(); 57 56 DownloadOsmTask task = new DownloadOsmTask(); 58 57 Future<?> future = task.download( 59 new MirroredDownloadReader(area, dialog.get OverpassType(), dialog.getOverpassQuery()),58 new MirroredDownloadReader(area, dialog.getServerUrl(), dialog.getOverpassType(), dialog.getOverpassQuery()), 60 59 dialog.isNewLayerRequired(), area, null); 61 60 Main.worker.submit(new PostDownloadHandler(task, future)); 62 61 } 63 62 } 64 63 65 static class XAPIQueryValidator extends AbstractTextComponentValidator {66 67 static final Pattern pattern = Pattern.compile("^(\\[([^=]+=[^=]*)\\])*$");68 69 public XAPIQueryValidator(JTextComponent tc) throws IllegalArgumentException {70 super(tc);71 }72 73 @Override74 public void validate() {75 if (pattern.matcher(getComponent().getText().trim()).matches()) {76 feedbackValid(XAPI_QUERY_TOOLTIP);77 } else {78 feedbackInvalid(tr("This XAPI query seems to be invalid, please doublecheck"));79 }80 }81 82 @Override83 public boolean isValid() {84 return pattern.matcher(getComponent().getText().trim()).matches();85 }86 }87 88 64 static class MirroredDownloadDialog extends DownloadDialog { 89 65 90 protected JComboBox/*<String>*/ overpassType; 66 protected HistoryComboBox serverUrl; 67 protected JComboBox overpassType; 91 68 protected HistoryComboBox overpassQuery; 92 69 private static MirroredDownloadDialog instance; 93 70 … … 96 73 cbDownloadOsmData.setEnabled(false); 97 74 cbDownloadGpxData.setEnabled(false); 98 75 cbStartup.setEnabled(false); 76 sizeCheck.setVisible(false); 99 77 } 100 78 101 79 static public MirroredDownloadDialog getInstance() { … … 107 85 108 86 @Override 109 87 protected void buildMainPanelAboveDownloadSelections(JPanel pnl) { 110 overpassType = new JComboBox /*<String>*/(new String[]{"*", "node", "way", "relation"});88 overpassType = new JComboBox(new String[]{"*", "node", "way", "relation"}); 111 89 pnl.add(new JLabel(tr("Object type: ")), GBC.std().insets(5, 5, 5, 5)); 112 90 pnl.add(overpassType, GBC.eol().fill(GridBagConstraints.HORIZONTAL)); 113 91 overpassType.setToolTipText(tr("OSM object type to download (''*'' stands for any)")); 92 serverUrl = new HistoryComboBox(); 93 pnl.add(new JLabel(tr("Server URL: ")), GBC.std().insets(5, 5, 5, 5)); 94 pnl.add(serverUrl, GBC.eol().fill(GridBagConstraints.HORIZONTAL)); 95 serverUrl.setToolTipText(tr("OSM mirror URL to download from")); 114 96 overpassQuery = new HistoryComboBox(); 115 pnl.add(new JLabel(tr("XAPI query: ")), GBC.std().insets(5, 5, 5, 5)); 116 new XAPIQueryValidator((JTextComponent) overpassQuery.getEditor().getEditorComponent()); 97 pnl.add(new JLabel(tr("Query: ")), GBC.std().insets(5, 5, 5, 5)); 117 98 pnl.add(overpassQuery, GBC.eol().fill(GridBagConstraints.HORIZONTAL)); 118 overpassQuery.setToolTipText( XAPI_QUERY_TOOLTIP);99 overpassQuery.setToolTipText(tr("XAPI query, e.g., '''' (to download all data), ''[highway=*]'', or ''[[network=VRR][ref=603|613]''")); 119 100 } 120 101 102 public String getServerUrl() { 103 return serverUrl.getText(); 104 } 105 121 106 public String getOverpassQuery() { 122 107 return overpassQuery.getText(); 123 108 } 124 109 125 110 public String getOverpassType() { 126 return (String) overpassType.getItemAt(overpassType.getSelectedIndex());111 return (String) overpassType.getItemAt(overpassType.getSelectedIndex()); 127 112 } 128 113 129 114 @Override 130 115 public void restoreSettings() { 131 116 super.restoreSettings(); 117 serverUrl.setPossibleItems( 118 Main.pref.getCollection(XAPI_SERVER_HISTORY_KEY, new LinkedList<String>())); 119 for (String s : Main.pref.getCollection(XAPI_DEFAULT_SERVER_KEY, Arrays.asList( 120 "http://ovehttp://rpass.osm.rambler.ru/cgi/xapi?{type}[bbox={xapi_bbox}][@meta]", 121 "http://overpass-api.de/api/xapi?{type}[bbox={xapi_bbox}][@meta]"))) { 122 serverUrl.addItem(new AutoCompletionListItem(s, AutoCompletionItemPritority.IS_IN_STANDARD)); 123 } 124 serverUrl.setSelectedIndex(1); 125 serverUrl.setSelectedIndex(0); 132 126 overpassQuery.setPossibleItems( 133 127 Main.pref.getCollection(XAPI_QUERY_HISTORY_KEY, new LinkedList<String>())); 134 128 } … … 136 130 @Override 137 131 public void rememberSettings() { 138 132 super.rememberSettings(); 133 serverUrl.addCurrentItemToHistory(); 134 Main.pref.putCollection(XAPI_SERVER_HISTORY_KEY, serverUrl.getHistory()); 139 135 overpassQuery.addCurrentItemToHistory(); 140 136 Main.pref.putCollection(XAPI_QUERY_HISTORY_KEY, overpassQuery.getHistory()); 141 137 } … … 143 139 144 140 static class MirroredDownloadReader extends BoundingBoxDownloader { 145 141 142 final String serverUrl; 146 143 final String overpassType; 147 144 final String overpassQuery; 148 145 149 public MirroredDownloadReader(Bounds downloadArea, String overpassType, String overpassQuery) {146 public MirroredDownloadReader(Bounds downloadArea, String serverUrl, String overpassType, String overpassQuery) { 150 147 super(downloadArea); 148 this.serverUrl = serverUrl; 151 149 this.overpassType = overpassType; 152 150 this.overpassQuery = overpassQuery.trim(); 153 151 } 154 152 155 153 @Override 156 154 protected String getBaseUrl() { 157 return MirroredDownloadPlugin.getDownloadUrl();155 return null; 158 156 } 159 157 160 158 @Override 161 159 protected String getRequestForBbox(double lon1, double lat1, double lon2, double lat2) { 162 return overpassQuery.isEmpty() && "*".equals(overpassType) 163 ? super.getRequestForBbox(lon1, lat1, lon2, lat2) 164 : overpassType + "[bbox=" + lon1 + "," + lat1 + "," + lon2 + "," + lat2 + "][@meta]" + overpassQuery; 160 return ((serverUrl + overpassQuery) 161 .replace("{type}", overpassType) 162 .replace("{xapi_bbox}", "{minlon},{minlat},{maxlon},{maxlat}") 163 .replace("{overpass_bbox}", "{minlat},{minlon},{maxlat},{maxlon}")) 164 .replace("{minlon}", Double.toString(lon1)) 165 .replace("{minlat}", Double.toString(lat1)) 166 .replace("{maxlon}", Double.toString(lon2)) 167 .replace("{maxlat}", Double.toString(lat2)); 165 168 } 166 169 167 170 @Override … … 173 176 if (ds != null && ds.dataSources.isEmpty()) { 174 177 if (crosses180th) { 175 178 Bounds bounds = new Bounds(lat1, lon1, lat2, 180.0); 176 DataSource src = new DataSource(bounds, MirroredDownloadPlugin.getDownloadUrl());179 DataSource src = new DataSource(bounds, serverUrl); 177 180 ds.dataSources.add(src); 178 181 179 182 bounds = new Bounds(lat1, -180.0, lat2, lon2); 180 src = new DataSource(bounds, MirroredDownloadPlugin.getDownloadUrl());183 src = new DataSource(bounds, serverUrl); 181 184 ds.dataSources.add(src); 182 185 } else { 183 186 Bounds bounds = new Bounds(lat1, lon1, lat2, lon2); 184 DataSource src = new DataSource(bounds, MirroredDownloadPlugin.getDownloadUrl());187 DataSource src = new DataSource(bounds, serverUrl); 185 188 ds.dataSources.add(src); 186 189 } 187 190 } -
src/mirrored_download/MirroredDownloadPlugin.java
11 11 public MirroredDownloadPlugin(PluginInformation info) { 12 12 super(info); 13 13 MainMenu.addAfter(Main.main.menu.fileMenu, new MirroredDownloadAction(), false, Main.main.menu.download); 14 MainMenu.add(Main.main.menu.editMenu, new UrlSelectionAction());15 14 } 16 private static String downloadUrl = "http://overpass.osm.rambler.ru/cgi/xapi?";//"http://overpass-api.de/api/xapi?";17 18 public static String getDownloadUrl() {19 return downloadUrl;20 }21 22 public static void setDownloadUrl(String downloadUrl_) {23 downloadUrl = downloadUrl_;24 }25 15 }
