Ticket #7511: 7511b.patch

File 7511b.patch, 18.2 KB (added by simon04, 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 UrlSelectionDialog
    73 {
    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 Tab
    88     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/MirroredDownloadInfo
    133     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

     
    66
    77import java.awt.Component;
    88import java.awt.GridBagConstraints;
    9 
    109import java.awt.event.ActionEvent;
    1110import java.awt.event.KeyEvent;
     11import java.util.Arrays;
    1212import java.util.LinkedList;
    1313import java.util.concurrent.Future;
    14 import java.util.regex.Pattern;
    1514import javax.swing.JComboBox;
    1615import javax.swing.JLabel;
    1716import javax.swing.JPanel;
    18 import javax.swing.text.JTextComponent;
    1917
    2018import org.openstreetmap.josm.actions.JosmAction;
    21 
    2219import org.openstreetmap.josm.Main;
    2320import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmTask;
    2421import org.openstreetmap.josm.actions.downloadtasks.PostDownloadHandler;
     
    2724import org.openstreetmap.josm.data.osm.DataSource;
    2825import org.openstreetmap.josm.gui.download.DownloadDialog;
    2926import org.openstreetmap.josm.gui.progress.ProgressMonitor;
    30 import org.openstreetmap.josm.gui.widgets.AbstractTextComponentValidator;
     27import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionItemPritority;
     28import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionListItem;
    3129import org.openstreetmap.josm.gui.widgets.HistoryComboBox;
    3230import org.openstreetmap.josm.io.BoundingBoxDownloader;
    3331import org.openstreetmap.josm.io.OsmTransferException;
     
    3634
    3735public class MirroredDownloadAction extends JosmAction {
    3836
     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";
    3939    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]''");
    4140
    4241    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."),
    4443                Shortcut.registerShortcut("mirror:download", tr("File: {0}", tr("Download from OSM mirror...")), KeyEvent.VK_DOWN, Shortcut.ALT_SHIFT),
    4544                true, "mirroreddownload/download", true);
    4645        putValue("help", ht("/Action/MirroredDownload"));
     
    5655            Bounds area = dialog.getSelectedDownloadArea();
    5756            DownloadOsmTask task = new DownloadOsmTask();
    5857            Future<?> future = task.download(
    59                     new MirroredDownloadReader(area, dialog.getOverpassType(), dialog.getOverpassQuery()),
     58                    new MirroredDownloadReader(area, dialog.getServerUrl(), dialog.getOverpassType(), dialog.getOverpassQuery()),
    6059                    dialog.isNewLayerRequired(), area, null);
    6160            Main.worker.submit(new PostDownloadHandler(task, future));
    6261        }
    6362    }
    6463
    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         @Override
    74         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         @Override
    83         public boolean isValid() {
    84             return pattern.matcher(getComponent().getText().trim()).matches();
    85         }
    86     }
    87 
    8864    static class MirroredDownloadDialog extends DownloadDialog {
    8965
    90         protected JComboBox/*<String>*/ overpassType;
     66        protected HistoryComboBox serverUrl;
     67        protected JComboBox overpassType;
    9168        protected HistoryComboBox overpassQuery;
    9269        private static MirroredDownloadDialog instance;
    9370
     
    9673            cbDownloadOsmData.setEnabled(false);
    9774            cbDownloadGpxData.setEnabled(false);
    9875            cbStartup.setEnabled(false);
     76            sizeCheck.setVisible(false);
    9977        }
    10078
    10179        static public MirroredDownloadDialog getInstance() {
     
    10785
    10886        @Override
    10987        protected void buildMainPanelAboveDownloadSelections(JPanel pnl) {
    110             overpassType = new JComboBox/*<String>*/(new String[]{"*", "node", "way", "relation"});
     88            overpassType = new JComboBox(new String[]{"*", "node", "way", "relation"});
    11189            pnl.add(new JLabel(tr("Object type: ")), GBC.std().insets(5, 5, 5, 5));
    11290            pnl.add(overpassType, GBC.eol().fill(GridBagConstraints.HORIZONTAL));
    11391            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"));
    11496            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));
    11798            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]''"));
    119100        }
    120101
     102        public String getServerUrl() {
     103            return serverUrl.getText();
     104        }
     105
    121106        public String getOverpassQuery() {
    122107            return overpassQuery.getText();
    123108        }
    124109
    125110        public String getOverpassType() {
    126             return (String)overpassType.getItemAt(overpassType.getSelectedIndex());
     111            return (String) overpassType.getItemAt(overpassType.getSelectedIndex());
    127112        }
    128113
    129114        @Override
    130115        public void restoreSettings() {
    131116            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);
    132126            overpassQuery.setPossibleItems(
    133127                    Main.pref.getCollection(XAPI_QUERY_HISTORY_KEY, new LinkedList<String>()));
    134128        }
     
    136130        @Override
    137131        public void rememberSettings() {
    138132            super.rememberSettings();
     133            serverUrl.addCurrentItemToHistory();
     134            Main.pref.putCollection(XAPI_SERVER_HISTORY_KEY, serverUrl.getHistory());
    139135            overpassQuery.addCurrentItemToHistory();
    140136            Main.pref.putCollection(XAPI_QUERY_HISTORY_KEY, overpassQuery.getHistory());
    141137        }
     
    143139
    144140    static class MirroredDownloadReader extends BoundingBoxDownloader {
    145141
     142        final String serverUrl;
    146143        final String overpassType;
    147144        final String overpassQuery;
    148145
    149         public MirroredDownloadReader(Bounds downloadArea, String overpassType, String overpassQuery) {
     146        public MirroredDownloadReader(Bounds downloadArea, String serverUrl, String overpassType, String overpassQuery) {
    150147            super(downloadArea);
     148            this.serverUrl = serverUrl;
    151149            this.overpassType = overpassType;
    152150            this.overpassQuery = overpassQuery.trim();
    153151        }
    154152
    155153        @Override
    156154        protected String getBaseUrl() {
    157             return MirroredDownloadPlugin.getDownloadUrl();
     155            return null;
    158156        }
    159157
    160158        @Override
    161159        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));
    165168        }
    166169
    167170        @Override
     
    173176            if (ds != null && ds.dataSources.isEmpty()) {
    174177                if (crosses180th) {
    175178                    Bounds bounds = new Bounds(lat1, lon1, lat2, 180.0);
    176                     DataSource src = new DataSource(bounds, MirroredDownloadPlugin.getDownloadUrl());
     179                    DataSource src = new DataSource(bounds, serverUrl);
    177180                    ds.dataSources.add(src);
    178181
    179182                    bounds = new Bounds(lat1, -180.0, lat2, lon2);
    180                     src = new DataSource(bounds, MirroredDownloadPlugin.getDownloadUrl());
     183                    src = new DataSource(bounds, serverUrl);
    181184                    ds.dataSources.add(src);
    182185                } else {
    183186                    Bounds bounds = new Bounds(lat1, lon1, lat2, lon2);
    184                     DataSource src = new DataSource(bounds, MirroredDownloadPlugin.getDownloadUrl());
     187                    DataSource src = new DataSource(bounds, serverUrl);
    185188                    ds.dataSources.add(src);
    186189                }
    187190            }
  • src/mirrored_download/MirroredDownloadPlugin.java

     
    1111    public MirroredDownloadPlugin(PluginInformation info) {
    1212        super(info);
    1313        MainMenu.addAfter(Main.main.menu.fileMenu, new MirroredDownloadAction(), false, Main.main.menu.download);
    14         MainMenu.add(Main.main.menu.editMenu, new UrlSelectionAction());
    1514    }
    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     }
    2515}