Ignore:
Timestamp:
2009-12-31T00:49:20+01:00 (14 years ago)
Author:
stoecker
Message:

close #4128 - new name search server

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/download/PlaceSelection.java

    r2676 r2717  
    22
    33import static org.openstreetmap.josm.tools.I18n.tr;
     4import static org.openstreetmap.josm.tools.I18n.trc;
    45
    56import java.awt.BorderLayout;
    67import java.awt.Component;
    78import java.awt.Dimension;
    8 import java.awt.GridBagConstraints;
    99import java.awt.GridBagLayout;
     10import java.awt.GridLayout;
    1011import java.awt.Insets;
    1112import java.awt.event.ActionEvent;
     
    1718import java.net.HttpURLConnection;
    1819import java.net.URL;
     20import java.text.DecimalFormat;
    1921import java.util.ArrayList;
    2022import java.util.Collections;
     
    2729import javax.swing.DefaultListSelectionModel;
    2830import javax.swing.JButton;
     31import javax.swing.JComboBox;
    2932import javax.swing.JLabel;
    3033import javax.swing.JPanel;
     
    5154import org.openstreetmap.josm.gui.widgets.HistoryComboBox;
    5255import org.openstreetmap.josm.io.OsmTransferException;
     56import org.openstreetmap.josm.tools.GBC;
    5357import org.openstreetmap.josm.tools.ImageProvider;
     58import org.openstreetmap.josm.tools.OsmUrlToBounds;
    5459import org.xml.sax.Attributes;
    5560import org.xml.sax.InputSource;
     
    6368    private JButton btnSearch;
    6469    private NamedResultTableModel model;
     70    private NamedResultTableColumnModel columnmodel;
    6571    private JTable tblSearchResults;
    6672    private DownloadDialog parent;
     73    private final static Server[] servers = new Server[]{
     74        new Server("Nominatim","http://nominatim.openstreetmap.org/search?format=xml&q=",tr("Class Type"),tr("Bounds")),
     75        new Server("Gazetter","http://gazetteer.openstreetmap.org/namefinder/search.xml?find=",tr("Near"),trc("placeselection", "Zoom"))
     76    };
     77    private final JComboBox server = new JComboBox(servers);
     78
     79    private static class Server {
     80        public String name;
     81        public String url;
     82        public String thirdcol;
     83        public String fourthcol;
     84        public String toString() {
     85            return name;
     86        }
     87        public Server(String n, String u, String t, String f) {
     88            name = n;
     89            url = u;
     90            thirdcol = t;
     91            fourthcol = f;
     92        }
     93    }
    6794
    6895    protected JPanel buildSearchPanel() {
     96        JPanel lpanel = new JPanel();
     97        lpanel.setLayout(new GridLayout(2,2));
    6998        JPanel panel = new JPanel();
    7099        panel.setLayout(new GridBagLayout());
    71         GridBagConstraints gc = new GridBagConstraints();
    72 
    73         // the label for the search field
    74         //
    75         gc.gridwidth = 2;
    76         gc.fill = GridBagConstraints.HORIZONTAL;
    77         gc.weightx  =1.0;
    78         gc.insets = new Insets(5, 5, 0, 5);
    79         panel.add(new JLabel(tr("Enter a place name to search for:")), gc);
    80 
    81         // the search expression field
    82         //
     100
     101        lpanel.add(new JLabel(tr("Choose the server for searching:")));
     102        lpanel.add(server);
     103        String s = Main.pref.get("namefinder.server", servers[0].name);
     104        for(int i = 0; i < servers.length; ++i) {
     105            if(servers[i].name.equals(s))
     106                server.setSelectedIndex(i);
     107        }
     108        lpanel.add(new JLabel(tr("Enter a place name to search for:")));
     109
    83110        cbSearchExpression = new HistoryComboBox();
    84111        cbSearchExpression.setToolTipText(tr("Enter a place name to search for"));
     
    86113        Collections.reverse(cmtHistory);
    87114        cbSearchExpression.setPossibleItems(cmtHistory);
    88         gc.gridx = 0;
    89         gc.gridy = 1;
    90         gc.gridwidth = 1;
    91         panel.add(cbSearchExpression,  gc);
    92 
    93         // the search button
    94         //
     115        lpanel.add(cbSearchExpression);
     116
     117        panel.add(lpanel, GBC.std().fill(GBC.HORIZONTAL).insets(5, 5, 0, 5));
    95118        SearchAction searchAction = new SearchAction();
    96119        btnSearch = new JButton(searchAction);
     
    98121        ((JTextField)cbSearchExpression.getEditor().getEditorComponent()).addActionListener(searchAction);
    99122
    100         gc.gridx = 1;
    101         gc.gridy = 1;
    102         gc.fill = GridBagConstraints.HORIZONTAL;
    103         gc.weightx = 0.0;
    104         panel.add(btnSearch,  gc);
     123        panel.add(btnSearch, GBC.eol().insets(5, 5, 0, 5));
    105124
    106125        return panel;
     
    119138        DefaultListSelectionModel selectionModel = new DefaultListSelectionModel();
    120139        model = new NamedResultTableModel(selectionModel);
    121         tblSearchResults = new JTable(model, new NamedResultTableColumnModel());
     140        columnmodel = new NamedResultTableColumnModel();
     141        tblSearchResults = new JTable(model, columnmodel);
    122142        tblSearchResults.setSelectionModel(selectionModel);
    123143        JScrollPane scrollPane = new JScrollPane(tblSearchResults);
     
    156176        public double lat;
    157177        public double lon;
    158         public int zoom;
     178        public int zoom = 0;
     179        public Bounds bounds = null;
    159180
    160181        public Bounds getDownloadArea() {
    161             double size = 180.0 / Math.pow(2, zoom);
    162             Bounds b = new Bounds(
    163                     new LatLon(lat - size / 2, lon - size),
    164                     new LatLon(lat + size / 2, lon+ size)
    165             );
    166             return b;
     182            return bounds != null ? bounds : OsmUrlToBounds.positionToBounds(lat, lon, zoom);
    167183        }
    168184    }
     
    194210                    currentResult.name = atts.getValue("name");
    195211                    currentResult.info = atts.getValue("info");
     212                    if(currentResult.info != null)
     213                        currentResult.info = tr(currentResult.info);
    196214                    currentResult.lat = Double.parseDouble(atts.getValue("lat"));
    197215                    currentResult.lon = Double.parseDouble(atts.getValue("lon"));
    198216                    currentResult.zoom = Integer.parseInt(atts.getValue("zoom"));
    199                     //currentResult.osmId = Integer.parseInt(atts.getValue("id"));
    200                     //currentResult.type = OsmPrimitiveType.from(atts.getValue("type"));
    201217                    data.add(currentResult);
    202218                } else if (qName.equals("description") && (depth == 3)) {
     
    208224                        currentResult.nearestPlace = atts.getValue("name");
    209225                    }
     226                } else if (qName.equals("place")) {
     227                    currentResult = new PlaceSelection.SearchResult();
     228                    currentResult.name = atts.getValue("display_name");
     229                    currentResult.description = currentResult.name;
     230                    currentResult.info = tr(atts.getValue("class"));
     231                    currentResult.nearestPlace = tr(atts.getValue("type"));
     232                    currentResult.lat = Double.parseDouble(atts.getValue("lat"));
     233                    currentResult.lon = Double.parseDouble(atts.getValue("lon"));
     234                    String[] bbox = atts.getValue("boundingbox").split(",");
     235                    currentResult.bounds = new Bounds(
     236                        new LatLon(Double.parseDouble(bbox[0]), Double.parseDouble(bbox[2])),
     237                        new LatLon(Double.parseDouble(bbox[1]), Double.parseDouble(bbox[3])));
     238                    data.add(currentResult);
    210239                }
    211240            } catch (NumberFormatException x) {
     
    288317        private List<SearchResult> data;
    289318        private boolean canceled = false;
     319        private Server useserver;
    290320        private Exception lastException;
    291321
     
    293323            super(tr("Querying name server"),false /* don't ignore exceptions */);
    294324            this.searchExpression = searchExpression;
     325            useserver = (Server)server.getSelectedItem();
     326            Main.pref.put("namefinder.server", useserver.name);
    295327        }
    296328
     
    313345                return;
    314346            }
     347            columnmodel.setHeadlines(useserver.thirdcol, useserver.fourthcol);
    315348            model.setData(this.data);
    316349        }
     
    320353            try {
    321354                getProgressMonitor().indeterminateSubTask(tr("Querying name server ..."));
    322                 URL url = new URL("http://gazetteer.openstreetmap.org/namefinder/search.xml?find="
    323                         +java.net.URLEncoder.encode(searchExpression, "UTF-8"));
     355                URL url = new URL(useserver.url+java.net.URLEncoder.encode(searchExpression, "UTF-8"));
    324356                synchronized(this) {
    325357                    connection = (HttpURLConnection)url.openConnection();
     
    381413
    382414    static class NamedResultTableColumnModel extends DefaultTableColumnModel {
     415        TableColumn col3 = null;
     416        TableColumn col4 = null;
    383417        protected void createColumns() {
    384418            TableColumn col = null;
     
    402436
    403437            // column 2 - Near
    404             col = new TableColumn(2);
    405             col.setHeaderValue(tr("Near"));
    406             col.setResizable(true);
    407             col.setPreferredWidth(100);
    408             col.setCellRenderer(renderer);
    409             addColumn(col);
     438            col3 = new TableColumn(2);
     439            col3.setHeaderValue(servers[0].thirdcol);
     440            col3.setResizable(true);
     441            col3.setPreferredWidth(100);
     442            col3.setCellRenderer(renderer);
     443            addColumn(col3);
    410444
    411445            // column 3 - Zoom
    412             col = new TableColumn(3);
    413             col.setHeaderValue(tr("Zoom"));
    414             col.setResizable(true);
    415             col.setPreferredWidth(50);
    416             col.setCellRenderer(renderer);
    417             addColumn(col);
     446            col4 = new TableColumn(3);
     447            col4.setHeaderValue(servers[0].fourthcol);
     448            col4.setResizable(true);
     449            col4.setPreferredWidth(50);
     450            col4.setCellRenderer(renderer);
     451            addColumn(col4);
     452        }
     453        public void setHeadlines(String third, String fourth) {
     454            col3.setHeaderValue(third);
     455            col4.setHeaderValue(fourth);
     456            fireColumnMarginChanged();
    418457        }
    419458
     
    499538                break;
    500539            case 3:
    501                 setText(Integer.toString(sr.zoom));
     540                if(sr.bounds != null)
     541                    setText(sr.bounds.toShortString(new DecimalFormat("0.000")));
     542                else
     543                    setText(sr.zoom != 0 ? Integer.toString(sr.zoom) : tr("unknown"));
    502544                break;
    503545            }
     
    506548        }
    507549    }
    508 
    509550}
Note: See TracChangeset for help on using the changeset viewer.