Changeset 5494 in josm for trunk/src/org


Ignore:
Timestamp:
2012-09-01T22:42:19+02:00 (12 years ago)
Author:
Don-vip
Message:

fix #8006 - Download remote marker layer

Location:
trunk/src/org/openstreetmap/josm
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/downloadtasks/DownloadGpsTask.java

    r5486 r5494  
    1616import org.openstreetmap.josm.gui.layer.GpxLayer;
    1717import org.openstreetmap.josm.gui.layer.Layer;
     18import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
    1819import org.openstreetmap.josm.gui.progress.ProgressMonitor;
    1920import org.openstreetmap.josm.gui.progress.ProgressTaskId;
    2021import org.openstreetmap.josm.gui.progress.ProgressTaskIds;
    2122import org.openstreetmap.josm.io.BoundingBoxDownloader;
     23import org.openstreetmap.josm.io.GpxImporter;
     24import org.openstreetmap.josm.io.GpxImporter.GpxImporterData;
    2225import org.openstreetmap.josm.io.OsmServerLocationReader;
    2326import org.openstreetmap.josm.io.OsmServerReader;
     
    2528import org.xml.sax.SAXException;
    2629
     30/**
     31 * Task allowing to download GPS data.
     32 */
    2733public class DownloadGpsTask extends AbstractDownloadTask {
    2834
     
    115121                return;
    116122            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) {
    120135                Main.main.addLayer(layer);
     136                return layer;
    121137            } else {
    122                 x.mergeFrom(layer);
     138                mergeLayer.mergeFrom(layer);
    123139                Main.map.repaint();
     140                return mergeLayer;
    124141            }
    125142        }
    126143
    127         private Layer findMergeLayer() {
    128             boolean merge = Main.pref.getBoolean("download.gps.mergeWithLocal", false);
     144        private GpxLayer findGpxMergeLayer() {
    129145            if (!Main.isDisplayingMapView())
    130146                return null;
     147            boolean merge = Main.pref.getBoolean("download.gps.mergeWithLocal", false);
    131148            Layer active = Main.map.mapView.getActiveLayer();
    132149            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)
    136163                    return l;
    137164            }
  • trunk/src/org/openstreetmap/josm/io/BoundingBoxDownloader.java

    r5097 r5494  
    4343            progressMonitor.setTicks(0);
    4444            GpxReader reader = new GpxReader(in);
    45             reader.parse(false);
     45            gpxParsedProperly = reader.parse(false);
    4646            GpxData currentGpx = reader.data;
    4747            if (result == null) {
  • trunk/src/org/openstreetmap/josm/io/GpxImporter.java

    r5361 r5494  
    1414import org.openstreetmap.josm.Main;
    1515import org.openstreetmap.josm.actions.ExtensionFileFilter;
     16import org.openstreetmap.josm.data.gpx.GpxData;
    1617import org.openstreetmap.josm.gui.layer.GpxLayer;
    1718import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
     
    2021import org.xml.sax.SAXException;
    2122
     23/**
     24 * File importer allowing to import GPX files (*.gpx/gpx.gz files).
     25 *
     26 */
    2227public class GpxImporter extends FileImporter {
    2328
     29    /**
     30     * The GPX file filter (*.gpx and *.gpx.gz files).
     31     */
    2432    public static final ExtensionFileFilter FILE_FILTER = new ExtensionFileFilter(
    2533            "gpx,gpx.gz", "gpx", tr("GPX Files") + " (*.gpx *.gpx.gz)");
    2634   
    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         */
    2842        public GpxLayer gpxLayer;
     43        /**
     44         * The imported marker layer. May be null if no marker.
     45         */
    2946        public MarkerLayer markerLayer;
     47        /**
     48         * The task to run after GPX and/or marker layer has been added to MapView.
     49         */
    3050        public Runnable postLayerTask;
    3151    }
    3252
     53    /**
     54     * Constructs a new {@code GpxImporter}.
     55     */
    3356    public GpxImporter() {
    3457        super(FILE_FILTER);
    3558    }
    3659
    37     @Override public void importData(File file, ProgressMonitor progressMonitor) throws IOException {
     60    @Override
     61    public void importData(File file, ProgressMonitor progressMonitor) throws IOException {
    3862        InputStream is;
    3963        if (file.getName().endsWith(".gpx.gz")) {
     
    4367        }
    4468        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) {
    4787        // FIXME: remove UI stuff from the IO subsystem
    4888        GuiHelper.runInEDT(new Runnable() {
     
    5999    }
    60100
    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;
    70119            }
    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);
    75137                }
    76138            }
    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;
    101141    }
    102142}
  • trunk/src/org/openstreetmap/josm/io/OsmServerLocationReader.java

    r5361 r5494  
    166166                progressMonitor.subTask(tr("Downloading OSM data..."));
    167167                GpxReader reader = new GpxReader(in);
    168                 reader.parse(false);
     168                gpxParsedProperly = reader.parse(false);
    169169                GpxData result = reader.data;
    170170                result.fromServer = true;
  • trunk/src/org/openstreetmap/josm/io/OsmServerReader.java

    r5361 r5494  
    3232    private OsmApi api = OsmApi.getOsmApi();
    3333    private boolean doAuthenticate = false;
     34    protected boolean gpxParsedProperly;
    3435
    3536    /**
     
    193194        this.doAuthenticate = doAuthenticate;
    194195    }
     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    }
    195205}
Note: See TracChangeset for help on using the changeset viewer.