Changeset 29776 in osm for applications/editors/josm/plugins/DirectDownload/src
- Timestamp:
- 2013-07-25T03:26:37+02:00 (11 years ago)
- Location:
- applications/editors/josm/plugins/DirectDownload/src/org/openstreetmap/josm/plugins/directdownload
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/DirectDownload/src/org/openstreetmap/josm/plugins/directdownload/DirectDownload.java
r29222 r29776 3 3 import static org.openstreetmap.josm.tools.I18n.tr; 4 4 5 import java.awt.event.ActionEvent; 5 6 import java.io.InputStream; 6 import java.io.IOException;7 8 7 import java.net.URL; 9 8 10 import java.util.List; 11 12 import java.awt.event.ActionEvent; 9 import javax.swing.JOptionPane; 13 10 14 11 import org.openstreetmap.josm.Main; 15 12 import org.openstreetmap.josm.actions.JosmAction; 16 13 import org.openstreetmap.josm.gui.MainMenu; 14 import org.openstreetmap.josm.gui.layer.GpxLayer; 15 import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer; 16 import org.openstreetmap.josm.io.GpxReader; 17 import org.openstreetmap.josm.io.OsmApi; 17 18 import org.openstreetmap.josm.plugins.Plugin; 18 19 import org.openstreetmap.josm.plugins.PluginInformation; 19 20 import org.openstreetmap.josm.gui.download.DownloadSelection;21 22 import org.openstreetmap.josm.gui.layer.GpxLayer;23 import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;24 25 import org.openstreetmap.josm.io.GpxReader;26 27 import javax.swing.JOptionPane;28 29 20 import org.xml.sax.SAXException; 30 21 31 22 public class DirectDownload extends Plugin { 32 DownloadAction openaction;23 private DownloadAction openaction; 33 24 34 35 36 37 38 39 40 25 /** 26 * Will be invoked by JOSM to bootstrap the plugin 27 * 28 * @param info information about the plugin and its local installation 29 */ 30 public DirectDownload(PluginInformation info) { 31 super(info); 41 32 42 43 Main.main.menu.fileMenu.add(openaction);44 33 openaction = new DownloadAction(); 34 MainMenu.add(Main.main.menu.gpsMenu, openaction); 35 } 45 36 46 class DownloadAction extends JosmAction {47 public DownloadAction() {48 super(tr("Download Track ..."), "DownloadAction", tr("Download GPX track from openstreetmap.org"),49 null, false);37 class DownloadAction extends JosmAction { 38 public DownloadAction() { 39 super(tr("Download Track ..."), "DownloadAction", 40 tr("Download GPX track from openstreetmap.org"), null, false); 50 41 } 42 51 43 public void actionPerformed(ActionEvent event) { 52 44 DownloadDataGui go = new DownloadDataGui(); 53 45 go.setVisible(true); 54 46 55 47 UserTrack track = go.getSelectedUserTrack(); 56 48 57 if ((go.getValue() == 1) && (track != null)) { 58 String apiBaseUrl = "http://api.openstreetmap.org/api/0.6/"; 59 String urlString = apiBaseUrl + "gpx/" + track.id + "/data"; 49 if ((go.getValue() == 1) && (track != null)) { 50 String urlString = OsmApi.getOsmApi().getBaseUrl() + "gpx/" + track.id + "/data"; 60 51 61 62 63 52 try { 53 URL trackDataUrl = new URL(urlString); 54 InputStream is = trackDataUrl.openStream(); 64 55 65 66 67 56 GpxReader r = new GpxReader(is); 57 boolean parsedProperly = r.parse(true); 58 GpxLayer gpxLayer = new GpxLayer(r.getGpxData(), track.filename, true); 68 59 69 60 if (r.getGpxData().hasRoutePoints() || r.getGpxData().hasTrackPoints()) { … … 78 69 } 79 70 if (!parsedProperly) { 80 JOptionPane.showMessageDialog(null, tr("Error occurred while parsing gpx file {0}. Only a part of the file will be available.", urlString)); 71 JOptionPane.showMessageDialog(Main.parent, 72 tr("Error occurred while parsing gpx file {0}. Only a part of the file will be available.", urlString)); 81 73 } 82 74 83 } catch (java.net.MalformedURLException e) { 84 JOptionPane.showMessageDialog(null, tr("Invalid URL {0}", urlString)); 85 } catch (java.io.IOException e) { 86 JOptionPane.showMessageDialog(null, tr("Error fetching URL {0}", urlString)); 87 } catch (SAXException e) { 88 JOptionPane.showMessageDialog(null, tr("Error parsing data from URL {0}", urlString)); 89 } 90 } 75 } catch (java.net.MalformedURLException e) { 76 JOptionPane.showMessageDialog(Main.parent, 77 tr("Invalid URL {0}", urlString)); 78 } catch (java.io.IOException e) { 79 JOptionPane.showMessageDialog(Main.parent, 80 tr("Error fetching URL {0}", urlString)); 81 } catch (SAXException e) { 82 JOptionPane.showMessageDialog(Main.parent, 83 tr("Error parsing data from URL {0}", urlString)); 84 } 85 } 91 86 } 92 87 } 93 94 88 } 95 -
applications/editors/josm/plugins/DirectDownload/src/org/openstreetmap/josm/plugins/directdownload/DownloadDataGui.java
r23585 r29776 3 3 import static org.openstreetmap.josm.tools.I18n.tr; 4 4 5 import java. io.InputStream;6 import java. io.IOException;7 5 import java.awt.BorderLayout; 6 import java.awt.Component; 7 import java.awt.Dimension; 8 8 import java.net.URL; 9 10 9 import java.util.ArrayList; 11 10 import java.util.LinkedList; 12 11 import java.util.List; 13 import java.util.StringTokenizer;14 15 import org.openstreetmap.josm.Main;16 import org.openstreetmap.josm.gui.ExtendedDialog;17 18 import java.awt.BorderLayout;19 import java.awt.Component;20 import java.awt.Dimension;21 12 22 13 import javax.swing.BorderFactory; 23 import javax.swing.DefaultListModel;24 14 import javax.swing.DefaultListSelectionModel; 25 15 import javax.swing.JLabel; 26 import javax.swing.JList;27 16 import javax.swing.JOptionPane; 28 17 import javax.swing.JPanel; … … 31 20 import javax.swing.ListSelectionModel; 32 21 import javax.swing.UIManager; 33 34 22 import javax.swing.event.ListSelectionEvent; 35 23 import javax.swing.event.ListSelectionListener; 36 37 24 import javax.swing.table.DefaultTableColumnModel; 38 25 import javax.swing.table.DefaultTableModel; 39 26 import javax.swing.table.TableCellRenderer; 40 27 import javax.swing.table.TableColumn; 41 42 import org.xml.sax.*;43 import org.xml.sax.helpers.*;44 45 import javax.xml.parsers.SAXParserFactory;46 28 import javax.xml.parsers.ParserConfigurationException; 47 29 import javax.xml.parsers.SAXParser; 48 30 import javax.xml.parsers.SAXParserFactory; 31 32 import org.openstreetmap.josm.Main; 33 import org.openstreetmap.josm.gui.ExtendedDialog; 34 import org.openstreetmap.josm.io.OsmApi; 35 import org.xml.sax.Attributes; 36 import org.xml.sax.SAXException; 37 import org.xml.sax.helpers.DefaultHandler; 49 38 50 39 public class DownloadDataGui extends ExtendedDialog { 51 private String apiBaseUrl = "http://api.openstreetmap.org/api/0.6/";52 53 // User for log in when downloading trace54 private String username = Main.pref.get("osm-server.username");55 private String password = Main.pref.get("osm-server.password");56 40 57 41 private NamedResultTableModel model; … … 62 46 // Initalizes ExtendedDialog 63 47 super(Main.parent, 64 65 66 67 68 69 70 71 48 tr("Download Track"), 49 new String[] {tr("Download Track"), tr("Cancel")}, 50 true 51 ); 52 53 JPanel panel = new JPanel(); 54 panel.setLayout(new BorderLayout()); 55 72 56 DefaultListSelectionModel selectionModel = new DefaultListSelectionModel(); 73 57 model = new NamedResultTableModel(selectionModel); … … 80 64 panel.add(scrollPane, BorderLayout.CENTER); 81 65 82 83 84 85 66 model.setData(getTrackList()); 67 68 setContent(panel); 69 setupDialog(); 86 70 } 87 71 88 72 private static class TrackListHandler extends DefaultHandler { 89 73 private LinkedList<UserTrack> data = new LinkedList<UserTrack>(); 90 private String cdata = new String(); 91 92 @Override 93 public void startElement(String namespaceURI, String localName, String qName, Attributes atts) 94 throws SAXException { 95 if (qName.equals("gpx_file")) { 96 UserTrack track = new UserTrack(); 97 98 track.id = atts.getValue("id"); 99 track.filename = atts.getValue("name"); 100 track.datetime = atts.getValue("timestamp").replaceAll("[TZ]", " "); // TODO: do real parsing and time zone conversion 101 102 data.addFirst(track); 103 } 104 cdata = new String(); 105 } 106 107 public void characters(char ch[], int start, int length) 108 throws SAXException { 109 cdata += new String(ch, start, length); 110 } 111 112 public void endElement(String uri, String localName, 113 String qName) 114 throws SAXException { 115 if (qName.equals("description")) { 116 data.getFirst().description = cdata; 117 } 118 /* 119 else if (qName.equals("tag")) { 120 data.getFirst().tags = cdata; 121 cdata = new String(); 122 } 123 */ 124 } 125 74 75 private String cdata = new String(); 76 77 @Override 78 public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { 79 if (qName.equals("gpx_file")) { 80 UserTrack track = new UserTrack(); 81 82 track.id = atts.getValue("id"); 83 track.filename = atts.getValue("name"); 84 track.datetime = atts.getValue("timestamp").replaceAll("[TZ]", " "); // TODO: do real parsing and time zone conversion 85 86 data.addFirst(track); 87 } 88 cdata = new String(); 89 } 90 91 public void characters(char ch[], int start, int length) 92 throws SAXException { 93 cdata += new String(ch, start, length); 94 } 95 96 public void endElement(String uri, String localName, String qName) throws SAXException { 97 if (qName.equals("description")) { 98 data.getFirst().description = cdata; 99 } 100 /* 101 else if (qName.equals("tag")) { 102 data.getFirst().tags = cdata; 103 cdata = new String(); 104 } 105 */ 106 } 126 107 127 108 public List<UserTrack> getResult() { … … 131 112 132 113 private List<UserTrack> getTrackList() { 133 String urlString = apiBaseUrl+ "user/gpx_files";134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 114 String urlString = OsmApi.getOsmApi().getBaseUrl() + "user/gpx_files"; 115 116 try { 117 URL userTracksUrl = new URL(urlString); 118 119 SAXParserFactory spf = SAXParserFactory.newInstance(); 120 TrackListHandler handler = new TrackListHandler(); 121 122 //get a new instance of parser 123 SAXParser sp = spf.newSAXParser(); 124 125 //parse the file and also register this class for call backs 126 sp.parse(userTracksUrl.openStream(), handler); 127 128 return handler.getResult(); 129 } catch (java.net.MalformedURLException e) { 130 JOptionPane.showMessageDialog(null, tr("Invalid URL {0}", urlString)); 131 } catch (java.io.IOException e) { 132 JOptionPane.showMessageDialog(null, tr("Error fetching URL {0}", urlString)); 133 } catch (SAXException e) { 134 JOptionPane.showMessageDialog(null, tr("Error parsing data from URL {0}", urlString)); 135 } catch(ParserConfigurationException pce) { 136 JOptionPane.showMessageDialog(null, tr("Error parsing data from URL {0}", urlString)); 137 } 138 139 return new LinkedList<UserTrack>(); 159 140 } 160 141 … … 167 148 this.selectionModel = selectionModel; 168 149 } 150 169 151 @Override 170 152 public int getRowCount() { … … 187 169 fireTableDataChanged(); 188 170 } 171 189 172 @Override 190 173 public boolean isCellEditable(int row, int column) { … … 200 183 201 184 public UserTrack getSelectedUserTrack() { 202 return model.getSelectedUserTrack(); 203 } 204 185 return model.getSelectedUserTrack(); 186 } 205 187 206 188 static class NamedResultTableColumnModel extends DefaultTableColumnModel { … … 234 216 235 217 // column 3 - tags 236 218 /* 237 219 col = new TableColumn(3); 238 220 col.setHeaderValue(tr("Tags")); … … 241 223 col.setCellRenderer(renderer); 242 224 addColumn(col); 243 225 */ 244 226 } 245 227 … … 286 268 switch(column) { 287 269 case 0: 288 270 setText(sr.datetime); 289 271 break; 290 272 case 1: 291 273 setText(sr.filename); 292 break; 293 274 break; 275 case 2: 294 276 setText(sr.description); 295 277 break; 296 278 /* 297 279 case 3: 298 280 setText(sr.tags); 299 281 break; 300 282 */ 301 283 } 302 284 return this; 303 285 } 304 286 } 305 306 287 }
Note:
See TracChangeset
for help on using the changeset viewer.