Changeset 12778 in osm for applications/editors/josm/plugins/waypoints
- Timestamp:
- 2009-01-01T18:28:53+01:00 (17 years ago)
- Location:
- applications/editors/josm/plugins/waypoints/src
- Files:
-
- 3 edited
-
WaypointOpenAction.java (modified) (1 diff)
-
WaypointPlugin.java (modified) (1 diff)
-
WaypointReader.java (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/waypoints/src/WaypointOpenAction.java
r12575 r12778 29 29 */ 30 30 public class WaypointOpenAction extends DiskAccessAction { 31 32 /**33 * Create an open action. The name is "Open a file".34 */35 public WaypointOpenAction() {36 super(tr("Open waypoints file"), "open", tr("Open a waypoints file."),37 Shortcut.registerShortcut("tools:waypoints", tr("Menu: {0}",38 tr("Open waypoints file")), KeyEvent.VK_W, Shortcut.GROUP_MENU, Shortcut.SHIFT_DEFAULT));39 }31 32 /** 33 * Create an open action. The name is "Open a file". 34 */ 35 public WaypointOpenAction() { 36 super(tr("Open waypoints file"), "open", tr("Open a waypoints file."), 37 Shortcut.registerShortcut("tools:waypoints", tr("Menu: {0}", 38 tr("Open waypoints file")), KeyEvent.VK_W, Shortcut.GROUP_MENU, Shortcut.SHIFT_DEFAULT)); 39 } 40 40 41 public void actionPerformed(ActionEvent e) {42 JFileChooser fc = createAndOpenFileChooser(true, true, null);43 if (fc == null)44 return;45 File[] files = fc.getSelectedFiles();46 for (int i = files.length; i > 0; --i)47 openFile(files[i-1]);48 }41 public void actionPerformed(ActionEvent e) { 42 JFileChooser fc = createAndOpenFileChooser(true, true, null); 43 if (fc == null) 44 return; 45 File[] files = fc.getSelectedFiles(); 46 for (int i = files.length; i > 0; --i) 47 openFile(files[i-1]); 48 } 49 49 50 /**51 * Open the given file.52 */53 public void openFile(File file) {54 String fn = file.getName();55 try {56 DataSet dataSet =57 WaypointReader.parse(new FileInputStream(file));58 Main.main.addLayer(new OsmDataLayer(dataSet, file.getName(),59 file));60 } catch (SAXException x) {61 x.printStackTrace();62 JOptionPane.showMessageDialog(Main.parent,63 tr("Error while parsing {0}",fn)+": "+x.getMessage());64 } catch (ParserConfigurationException x) {65 x.printStackTrace(); // broken SAXException chaining66 JOptionPane.showMessageDialog(Main.parent,67 tr("Error while parsing {0}",fn)+": "+x.getMessage());68 } catch (IOException x) {69 x.printStackTrace();70 JOptionPane.showMessageDialog(Main.parent,71 tr("Could not read \"{0}\"",fn)+"\n"+x.getMessage());72 }73 }50 /** 51 * Open the given file. 52 */ 53 public void openFile(File file) { 54 String fn = file.getName(); 55 try { 56 DataSet dataSet = 57 WaypointReader.parse(new FileInputStream(file)); 58 Main.main.addLayer(new OsmDataLayer(dataSet, file.getName(), 59 file)); 60 } catch (SAXException x) { 61 x.printStackTrace(); 62 JOptionPane.showMessageDialog(Main.parent, 63 tr("Error while parsing {0}",fn)+": "+x.getMessage()); 64 } catch (ParserConfigurationException x) { 65 x.printStackTrace(); // broken SAXException chaining 66 JOptionPane.showMessageDialog(Main.parent, 67 tr("Error while parsing {0}",fn)+": "+x.getMessage()); 68 } catch (IOException x) { 69 x.printStackTrace(); 70 JOptionPane.showMessageDialog(Main.parent, 71 tr("Could not read \"{0}\"",fn)+"\n"+x.getMessage()); 72 } 73 } 74 74 } -
applications/editors/josm/plugins/waypoints/src/WaypointPlugin.java
r1495 r12778 11 11 12 12 13 public WaypointPlugin() {14 JMenuItem waypointItem = new JMenuItem(new WaypointOpenAction());15 int index = findFirstSeparator();16 Main.main.menu.fileMenu.add(waypointItem,index<0 ? 0: index);17 }13 public WaypointPlugin() { 14 JMenuItem waypointItem = new JMenuItem(new WaypointOpenAction()); 15 int index = findFirstSeparator(); 16 Main.main.menu.fileMenu.add(waypointItem,index<0 ? 0: index); 17 } 18 18 19 private int findFirstSeparator()20 {21 Component[] components = Main.main.menu.fileMenu.getMenuComponents();22 for(int count=0; count<components.length; count++)23 {24 if(components[count] instanceof JSeparator)25 return count;26 }27 return -1;28 }19 private int findFirstSeparator() 20 { 21 Component[] components = Main.main.menu.fileMenu.getMenuComponents(); 22 for(int count=0; count<components.length; count++) 23 { 24 if(components[count] instanceof JSeparator) 25 return count; 26 } 27 return -1; 28 } 29 29 } -
applications/editors/josm/plugins/waypoints/src/WaypointReader.java
r9949 r12778 1 package waypoints; 1 package waypoints; 2 2 3 3 import static org.openstreetmap.josm.tools.I18n.tr; … … 23 23 public class WaypointReader { 24 24 25 private static class Parser extends DefaultHandler {26 /**27 * Current track to be read. The last entry is the current trkpt.28 * If in wpt-mode, it contain only one GpsPoint.29 */30 private DataSet dataSet;31 private LatLon currentLatLon;32 private String curWptName;33 private boolean inName = false;25 private static class Parser extends DefaultHandler { 26 /** 27 * Current track to be read. The last entry is the current trkpt. 28 * If in wpt-mode, it contain only one GpsPoint. 29 */ 30 private DataSet dataSet; 31 private LatLon currentLatLon; 32 private String curWptName; 33 private boolean inName = false; 34 34 35 // NW start36 // data now has two components: the GPS points and an OsmDataLayer.37 // This is to allow us to convert waypoints straight to nodes.38 // The way this works is that waypoints with a name not beginning39 // with 0 - i.e. waypoints specially named - will be loaded in as40 // nodes, in addition to going into the gpx layer. Other waypoints will41 // only go into the gpx layer.42 public Parser()43 {44 dataSet = new DataSet();45 }46 // NW end35 // NW start 36 // data now has two components: the GPS points and an OsmDataLayer. 37 // This is to allow us to convert waypoints straight to nodes. 38 // The way this works is that waypoints with a name not beginning 39 // with 0 - i.e. waypoints specially named - will be loaded in as 40 // nodes, in addition to going into the gpx layer. Other waypoints will 41 // only go into the gpx layer. 42 public Parser() 43 { 44 dataSet = new DataSet(); 45 } 46 // NW end 47 47 48 @Override public void startElement(String namespaceURI,49 String localName, String qName, Attributes atts)50 throws SAXException {51 if (qName.equals("wpt")) {52 try {53 double lat = Double.parseDouble(atts.getValue("lat"));54 double lon = Double.parseDouble(atts.getValue("lon"));55 if (Math.abs(lat) > 90)56 throw new SAXException57 (tr("Data error: lat value \"{0}\" is out of bound.",58 lat));59 if (Math.abs(lon) > 180)60 throw new SAXException61 (tr("Data error: lon value \"{0}\" is out of bound.",62 lon));63 currentLatLon = new LatLon(lat, lon);48 @Override public void startElement(String namespaceURI, 49 String localName, String qName, Attributes atts) 50 throws SAXException { 51 if (qName.equals("wpt")) { 52 try { 53 double lat = Double.parseDouble(atts.getValue("lat")); 54 double lon = Double.parseDouble(atts.getValue("lon")); 55 if (Math.abs(lat) > 90) 56 throw new SAXException 57 (tr("Data error: lat value \"{0}\" is out of bound.", 58 lat)); 59 if (Math.abs(lon) > 180) 60 throw new SAXException 61 (tr("Data error: lon value \"{0}\" is out of bound.", 62 lon)); 63 currentLatLon = new LatLon(lat, lon); 64 64 } catch (NumberFormatException e) { 65 e.printStackTrace();66 throw new SAXException(e);65 e.printStackTrace(); 66 throw new SAXException(e); 67 67 } 68 }69 else if (qName.equals("name")) {70 inName = true;71 curWptName = "";72 } 73 }68 } 69 else if (qName.equals("name")) { 70 inName = true; 71 curWptName = ""; 72 } 73 } 74 74 75 @Override public void characters(char[] ch, int start, int length) {76 // NW start77 if (inName) {78 curWptName = new String (ch,start,length);79 }80 // NW end81 }75 @Override public void characters(char[] ch, int start, int length) { 76 // NW start 77 if (inName) { 78 curWptName = new String (ch,start,length); 79 } 80 // NW end 81 } 82 82 83 @Override public void endElement(String namespaceURI, String localName,84 String qName) {85 if (qName.equals("wpt") && curWptName!="") {86 // create a new node from the latitude and longitude87 System.out.println("Found a waypoint to convert to a node: "88 + curWptName);89 Node node = new Node(currentLatLon);90 node.put("name",curWptName);91 dataSet.nodes.add(node);92 }93 else if (qName.equals("name")) {94 inName = false;95 }83 @Override public void endElement(String namespaceURI, String localName, 84 String qName) { 85 if (qName.equals("wpt") && curWptName!="") { 86 // create a new node from the latitude and longitude 87 System.out.println("Found a waypoint to convert to a node: " 88 + curWptName); 89 Node node = new Node(currentLatLon); 90 node.put("name",curWptName); 91 dataSet.nodes.add(node); 92 } 93 else if (qName.equals("name")) { 94 inName = false; 95 } 96 96 } 97 }97 } 98 98 99 /**100 * Parse and return the read data101 */102 public static DataSet parse(InputStream source)103 throws SAXException, IOException, ParserConfigurationException {104 Parser parser = new Parser();105 SAXParserFactory.newInstance().newSAXParser().parse(new InputSource(new InputStreamReader(source, "UTF-8")), parser);106 return parser.dataSet;107 }99 /** 100 * Parse and return the read data 101 */ 102 public static DataSet parse(InputStream source) 103 throws SAXException, IOException, ParserConfigurationException { 104 Parser parser = new Parser(); 105 SAXParserFactory.newInstance().newSAXParser().parse(new InputSource(new InputStreamReader(source, "UTF-8")), parser); 106 return parser.dataSet; 107 } 108 108 }
Note:
See TracChangeset
for help on using the changeset viewer.
