| | 1 | // License: GPL. See LICENSE file for details. |
| | 2 | package org.openstreetmap.josm.gui.download; |
| | 3 | |
| | 4 | import static org.openstreetmap.josm.tools.I18n.tr; |
| | 5 | |
| | 6 | import java.awt.GridBagLayout; |
| | 7 | import java.awt.event.ActionEvent; |
| | 8 | import java.awt.event.ActionListener; |
| | 9 | import java.awt.event.FocusAdapter; |
| | 10 | import java.awt.event.FocusEvent; |
| | 11 | import java.io.IOException; |
| | 12 | import java.util.logging.Logger; |
| | 13 | |
| | 14 | import javax.swing.JLabel; |
| | 15 | import javax.swing.JPanel; |
| | 16 | import javax.swing.JTextField; |
| | 17 | import javax.xml.parsers.DocumentBuilder; |
| | 18 | import javax.xml.parsers.DocumentBuilderFactory; |
| | 19 | import javax.xml.parsers.ParserConfigurationException; |
| | 20 | |
| | 21 | import org.openstreetmap.josm.data.Bounds; |
| | 22 | import org.openstreetmap.josm.tools.GBC; |
| | 23 | import org.w3c.dom.Document; |
| | 24 | import org.w3c.dom.NamedNodeMap; |
| | 25 | import org.w3c.dom.Node; |
| | 26 | import org.xml.sax.SAXException; |
| | 27 | /** |
| | 28 | * ID Number Selector |
| | 29 | * |
| | 30 | * Input method for the "Download from OSM" dialog box. Given an OSM element ID number, |
| | 31 | * it finds the center point and loads small additional area on each side. |
| | 32 | * Very handy if you've found a problem element in an XML or database dump. |
| | 33 | * |
| | 34 | * Example: enter node id 1, to see the first node ever added to OSM |
| | 35 | * TODO: support ways/relations |
| | 36 | * |
| | 37 | * @author Bryce Nesbitt, April 2011 |
| | 38 | * |
| | 39 | */ |
| | 40 | public class IdNumberSelection implements DownloadSelection { |
| | 41 | private static final Logger logger = Logger.getLogger(IdNumberSelection.class.getName()); |
| | 42 | |
| | 43 | private DownloadDialog parent; |
| | 44 | private JTextField osmid = new JTextField(11); |
| | 45 | private int lastid = 0; |
| | 46 | |
| | 47 | public void addGui(final DownloadDialog gui) { |
| | 48 | BoundingBoxBuilder bboxbuilder = new BoundingBoxBuilder(); |
| | 49 | osmid.addFocusListener (bboxbuilder); |
| | 50 | osmid.addActionListener(bboxbuilder); |
| | 51 | |
| | 52 | final JPanel dlg = new JPanel(new GridBagLayout()); |
| | 53 | dlg.add(new JLabel(tr("OSM Node ID")), GBC.std().insets(10,20,5,0)); |
| | 54 | dlg.add(osmid, GBC.std().insets(0,20,0,0)); |
| | 55 | gui.addDownloadAreaSelector(dlg, tr("Node ID")); |
| | 56 | parent = gui; |
| | 57 | } |
| | 58 | |
| | 59 | public void setDownloadArea(Bounds area) { |
| | 60 | } |
| | 61 | |
| | 62 | class BoundingBoxBuilder extends FocusAdapter implements ActionListener { |
| | 63 | |
| | 64 | protected void refreshBounds() { |
| | 65 | double fudgelon=.001, fudgelat=.001; |
| | 66 | double mylon=0, mylat=0; |
| | 67 | |
| | 68 | // Extract the OSM element ID the user wants as the center point |
| | 69 | int id; |
| | 70 | try { |
| | 71 | id = Integer.parseInt(osmid.getText().trim()); |
| | 72 | } catch (NumberFormatException e1) { |
| | 73 | Bounds b = new Bounds(0,0,0,0); |
| | 74 | parent.boundingBoxChanged(b, IdNumberSelection.this); |
| | 75 | return; |
| | 76 | } |
| | 77 | if( id == lastid ) { |
| | 78 | return; |
| | 79 | } |
| | 80 | lastid = id; |
| | 81 | |
| | 82 | // Use the DOM to load an element from the API, and parse out the lat/lon. |
| | 83 | String url = "http://api.openstreetmap.org/api/0.6/node/"+id; |
| | 84 | try { |
| | 85 | DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); |
| | 86 | DocumentBuilder db = dbf.newDocumentBuilder(); |
| | 87 | logger.info(url); |
| | 88 | Document doc = db.parse(url); |
| | 89 | Node node = doc.getElementsByTagName("node").item(0); |
| | 90 | NamedNodeMap nmap = node.getAttributes(); |
| | 91 | mylat = Double.parseDouble(nmap.getNamedItem("lat").getNodeValue()); |
| | 92 | mylon = Double.parseDouble(nmap.getNamedItem("lon").getNodeValue()); |
| | 93 | } catch (ParserConfigurationException e) { |
| | 94 | e.printStackTrace(); |
| | 95 | } catch (SAXException e) { |
| | 96 | e.printStackTrace(); |
| | 97 | } catch (IOException e) { |
| | 98 | mylat = mylon = 0; |
| | 99 | } |
| | 100 | |
| | 101 | Bounds b = new Bounds(mylat-fudgelon,mylon-fudgelat,mylat+fudgelon,mylon+fudgelat); |
| | 102 | parent.boundingBoxChanged(b, IdNumberSelection.this); |
| | 103 | } |
| | 104 | public void focusLost(FocusEvent e) { |
| | 105 | refreshBounds(); |
| | 106 | } |
| | 107 | public void actionPerformed(ActionEvent e) { |
| | 108 | refreshBounds(); |
| | 109 | } |
| | 110 | } |
| | 111 | } |
| | 112 | |