Changeset 5494 in josm for trunk/src/org/openstreetmap/josm
- Timestamp:
- 2012-09-01T22:42:19+02:00 (12 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/actions/downloadtasks/DownloadGpsTask.java
r5486 r5494 16 16 import org.openstreetmap.josm.gui.layer.GpxLayer; 17 17 import org.openstreetmap.josm.gui.layer.Layer; 18 import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer; 18 19 import org.openstreetmap.josm.gui.progress.ProgressMonitor; 19 20 import org.openstreetmap.josm.gui.progress.ProgressTaskId; 20 21 import org.openstreetmap.josm.gui.progress.ProgressTaskIds; 21 22 import org.openstreetmap.josm.io.BoundingBoxDownloader; 23 import org.openstreetmap.josm.io.GpxImporter; 24 import org.openstreetmap.josm.io.GpxImporter.GpxImporterData; 22 25 import org.openstreetmap.josm.io.OsmServerLocationReader; 23 26 import org.openstreetmap.josm.io.OsmServerReader; … … 25 28 import org.xml.sax.SAXException; 26 29 30 /** 31 * Task allowing to download GPS data. 32 */ 27 33 public class DownloadGpsTask extends AbstractDownloadTask { 28 34 … … 115 121 return; 116 122 String name = newLayerName != null ? newLayerName : tr("Downloaded GPX Data"); 117 GpxLayer layer = new GpxLayer(rawData, name); 118 Layer x = findMergeLayer(); 119 if (newLayer || x == null) { 123 124 GpxImporterData layers = GpxImporter.loadLayers(rawData, reader.isGpxParsedProperly(), name, tr("Markers from {0}", name)); 125 126 GpxLayer gpxLayer = addOrMergeLayer(layers.gpxLayer, findGpxMergeLayer()); 127 addOrMergeLayer(layers.markerLayer, findMarkerMergeLayer(gpxLayer)); 128 129 layers.postLayerTask.run(); 130 } 131 132 private <L extends Layer> L addOrMergeLayer(L layer, L mergeLayer) { 133 if (layer == null) return null; 134 if (newLayer || mergeLayer == null) { 120 135 Main.main.addLayer(layer); 136 return layer; 121 137 } else { 122 x.mergeFrom(layer);138 mergeLayer.mergeFrom(layer); 123 139 Main.map.repaint(); 140 return mergeLayer; 124 141 } 125 142 } 126 143 127 private Layer findMergeLayer() { 128 boolean merge = Main.pref.getBoolean("download.gps.mergeWithLocal", false); 144 private GpxLayer findGpxMergeLayer() { 129 145 if (!Main.isDisplayingMapView()) 130 146 return null; 147 boolean merge = Main.pref.getBoolean("download.gps.mergeWithLocal", false); 131 148 Layer active = Main.map.mapView.getActiveLayer(); 132 149 if (active != null && active instanceof GpxLayer && (merge || ((GpxLayer)active).data.fromServer)) 133 return active; 134 for (Layer l : Main.map.mapView.getAllLayers()) { 135 if (l instanceof GpxLayer && (merge || ((GpxLayer)l).data.fromServer)) 150 return (GpxLayer) active; 151 for (GpxLayer l : Main.map.mapView.getLayersOfType(GpxLayer.class)) { 152 if (merge || l.data.fromServer) 153 return l; 154 } 155 return null; 156 } 157 158 private MarkerLayer findMarkerMergeLayer(GpxLayer fromLayer) { 159 if (!Main.isDisplayingMapView()) 160 return null; 161 for (MarkerLayer l : Main.map.mapView.getLayersOfType(MarkerLayer.class)) { 162 if (fromLayer != null && l.fromLayer == fromLayer) 136 163 return l; 137 164 } -
trunk/src/org/openstreetmap/josm/io/BoundingBoxDownloader.java
r5097 r5494 43 43 progressMonitor.setTicks(0); 44 44 GpxReader reader = new GpxReader(in); 45 reader.parse(false);45 gpxParsedProperly = reader.parse(false); 46 46 GpxData currentGpx = reader.data; 47 47 if (result == null) { -
trunk/src/org/openstreetmap/josm/io/GpxImporter.java
r5361 r5494 14 14 import org.openstreetmap.josm.Main; 15 15 import org.openstreetmap.josm.actions.ExtensionFileFilter; 16 import org.openstreetmap.josm.data.gpx.GpxData; 16 17 import org.openstreetmap.josm.gui.layer.GpxLayer; 17 18 import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer; … … 20 21 import org.xml.sax.SAXException; 21 22 23 /** 24 * File importer allowing to import GPX files (*.gpx/gpx.gz files). 25 * 26 */ 22 27 public class GpxImporter extends FileImporter { 23 28 29 /** 30 * The GPX file filter (*.gpx and *.gpx.gz files). 31 */ 24 32 public static final ExtensionFileFilter FILE_FILTER = new ExtensionFileFilter( 25 33 "gpx,gpx.gz", "gpx", tr("GPX Files") + " (*.gpx *.gpx.gz)"); 26 34 27 protected static class GpxImporterData { 35 /** 36 * Utility class containing imported GPX and marker layers, and a task to run after they are added to MapView. 37 */ 38 public static class GpxImporterData { 39 /** 40 * The imported GPX layer. May be null if no GPX data. 41 */ 28 42 public GpxLayer gpxLayer; 43 /** 44 * The imported marker layer. May be null if no marker. 45 */ 29 46 public MarkerLayer markerLayer; 47 /** 48 * The task to run after GPX and/or marker layer has been added to MapView. 49 */ 30 50 public Runnable postLayerTask; 31 51 } 32 52 53 /** 54 * Constructs a new {@code GpxImporter}. 55 */ 33 56 public GpxImporter() { 34 57 super(FILE_FILTER); 35 58 } 36 59 37 @Override public void importData(File file, ProgressMonitor progressMonitor) throws IOException { 60 @Override 61 public void importData(File file, ProgressMonitor progressMonitor) throws IOException { 38 62 InputStream is; 39 63 if (file.getName().endsWith(".gpx.gz")) { … … 43 67 } 44 68 String fileName = file.getName(); 45 final GpxImporterData data = loadLayers(is, file, fileName, tr("Markers from {0}", fileName), progressMonitor); 46 69 70 try { 71 GpxReader r = new GpxReader(is); 72 boolean parsedProperly = r.parse(true); 73 r.data.storageFile = file; 74 addLayers(loadLayers(r.data, parsedProperly, fileName, tr("Markers from {0}", fileName))); 75 } catch (SAXException e) { 76 e.printStackTrace(); 77 throw new IOException(tr("Parsing data for layer ''{0}'' failed", fileName)); 78 } 79 } 80 81 /** 82 * Adds the specified GPX and marker layers to Map.main 83 * @param data The layers to add 84 * @see #loadLayers 85 */ 86 public static void addLayers(final GpxImporterData data) { 47 87 // FIXME: remove UI stuff from the IO subsystem 48 88 GuiHelper.runInEDT(new Runnable() { … … 59 99 } 60 100 61 public GpxImporterData loadLayers(InputStream is, final File associatedFile, 62 final String gpxLayerName, String markerLayerName, ProgressMonitor progressMonitor) throws IOException { 63 final GpxImporterData data = new GpxImporterData(); 64 try { 65 final GpxReader r = new GpxReader(is); 66 final boolean parsedProperly = r.parse(true); 67 r.data.storageFile = associatedFile; 68 if (r.data.hasRoutePoints() || r.data.hasTrackPoints()) { 69 data.gpxLayer = new GpxLayer(r.data, gpxLayerName, associatedFile != null); 101 /** 102 * Replies the new GPX and marker layers corresponding to the specified GPX data. 103 * @param data The GPX data 104 * @param parsedProperly True if GPX data has been properly parsed by {@link GpxReader#parse} 105 * @param gpxLayerName The GPX layer name 106 * @param markerLayerName The marker layer name 107 * @return the new GPX and marker layers corresponding to the specified GPX data, to be used with {@link #addLayers} 108 * @see #addLayers 109 */ 110 public static GpxImporterData loadLayers(final GpxData data, final boolean parsedProperly, final String gpxLayerName, String markerLayerName) { 111 final GpxImporterData result = new GpxImporterData(); 112 if (data.hasRoutePoints() || data.hasTrackPoints()) { 113 result.gpxLayer = new GpxLayer(data, gpxLayerName, data.storageFile != null); 114 } 115 if (Main.pref.getBoolean("marker.makeautomarkers", true) && !data.waypoints.isEmpty()) { 116 result.markerLayer = new MarkerLayer(data, markerLayerName, data.storageFile, result.gpxLayer, false); 117 if (result.markerLayer.data.size() == 0) { 118 result.markerLayer = null; 70 119 } 71 if (Main.pref.getBoolean("marker.makeautomarkers", true) && !r.data.waypoints.isEmpty()) { 72 data.markerLayer = new MarkerLayer(r.data, markerLayerName, associatedFile, data.gpxLayer, false); 73 if (data.markerLayer.data.size() == 0) { 74 data.markerLayer = null; 120 } 121 result.postLayerTask = new Runnable() { 122 @Override 123 public void run() { 124 if (result.markerLayer != null) { 125 result.markerLayer.addMouseHandler(); 126 } 127 if (!parsedProperly) { 128 String msg; 129 if (data.storageFile == null) { 130 msg = tr("Error occurred while parsing gpx data for layer ''{0}''. Only a part of the file will be available.", 131 gpxLayerName); 132 } else { 133 msg = tr("Error occurred while parsing gpx file ''{0}''. Only a part of the file will be available.", 134 data.storageFile.getPath()); 135 } 136 JOptionPane.showMessageDialog(null, msg); 75 137 } 76 138 } 77 data.postLayerTask = new Runnable() { 78 @Override 79 public void run() { 80 if (data.markerLayer != null) { 81 data.markerLayer.addMouseHandler(); 82 } 83 if (!parsedProperly) { 84 String msg; 85 if (associatedFile == null) { 86 msg = tr("Error occurred while parsing gpx data for layer ''{0}''. Only a part of the file will be available.", 87 gpxLayerName); 88 } else { 89 msg = tr("Error occurred while parsing gpx file ''{0}''. Only a part of the file will be available.", 90 associatedFile.getPath()); 91 } 92 JOptionPane.showMessageDialog(null, msg); 93 } 94 } 95 }; 96 } catch (SAXException e) { 97 e.printStackTrace(); 98 throw new IOException(tr("Parsing data for layer ''{0}'' failed", gpxLayerName)); 99 } 100 return data; 139 }; 140 return result; 101 141 } 102 142 } -
trunk/src/org/openstreetmap/josm/io/OsmServerLocationReader.java
r5361 r5494 166 166 progressMonitor.subTask(tr("Downloading OSM data...")); 167 167 GpxReader reader = new GpxReader(in); 168 reader.parse(false);168 gpxParsedProperly = reader.parse(false); 169 169 GpxData result = reader.data; 170 170 result.fromServer = true; -
trunk/src/org/openstreetmap/josm/io/OsmServerReader.java
r5361 r5494 32 32 private OsmApi api = OsmApi.getOsmApi(); 33 33 private boolean doAuthenticate = false; 34 protected boolean gpxParsedProperly; 34 35 35 36 /** … … 193 194 this.doAuthenticate = doAuthenticate; 194 195 } 196 197 /** 198 * Determines if the GPX data has been parsed properly. 199 * @return true if the GPX data has been parsed properly, false otherwise 200 * @see GpxReader#parse 201 */ 202 public final boolean isGpxParsedProperly() { 203 return gpxParsedProperly; 204 } 195 205 }
Note:
See TracChangeset
for help on using the changeset viewer.