Ticket #7193: gpxlayer.patch

File gpxlayer.patch, 3.9 KB (added by stoecker, 12 years ago)

Example solution

  • src/org/openstreetmap/josm/io/GpxImporter.java

     
    2323
    2424public class GpxImporter extends FileImporter {
    2525
    26     protected GpxLayer gpxLayer;
    27     protected MarkerLayer markerLayer;
    28     protected Runnable postLayerTask;
     26    protected class GpxImporterData {
     27        public GpxLayer gpxLayer;
     28        public MarkerLayer markerLayer;
     29        public Runnable postLayerTask;
     30    }
    2931
    3032    public GpxImporter() {
    3133        super(new ExtensionFileFilter("gpx,gpx.gz", "gpx", tr("GPX Files") + " (*.gpx *.gpx.gz)"));
     
    3941            is = new FileInputStream(file);
    4042        }
    4143        String fileName = file.getName();
    42         loadLayers(is, file, fileName, tr("Markers from {0}", fileName), progressMonitor);
     44        final GpxImporterData data = loadLayers(is, file, fileName, tr("Markers from {0}", fileName), progressMonitor);
    4345
    4446        // FIXME: remove UI stuff from the IO subsystem
    4547        GuiHelper.runInEDT(new Runnable() {
    4648            public void run() {
    47                 if (markerLayer != null) {
    48                     Main.main.addLayer(markerLayer);
     49                if (data.markerLayer != null) {
     50                    Main.main.addLayer(data.markerLayer);
    4951                }
    50                 if (gpxLayer != null) {
    51                     Main.main.addLayer(gpxLayer);
     52                if (data.gpxLayer != null) {
     53                    Main.main.addLayer(data.gpxLayer);
    5254                }
    53                 postLayerTask.run();
     55                data.postLayerTask.run();
    5456            }
    5557        });
    5658    }
    5759
    58     public void loadLayers(InputStream is, final File associatedFile,
     60    public GpxImporterData loadLayers(InputStream is, final File associatedFile,
    5961                    final String gpxLayerName, String markerLayerName, ProgressMonitor progressMonitor) throws IOException {
     62        final GpxImporterData data = new GpxImporterData();
    6063        try {
    6164            final GpxReader r = new GpxReader(is);
    6265            final boolean parsedProperly = r.parse(true);
    6366            r.data.storageFile = associatedFile;
    6467            if (r.data.hasRoutePoints() || r.data.hasTrackPoints()) {
    65                 gpxLayer = new GpxLayer(r.data, gpxLayerName, associatedFile != null);
     68                data.gpxLayer = new GpxLayer(r.data, gpxLayerName, associatedFile != null);
    6669            }
    6770            if (Main.pref.getBoolean("marker.makeautomarkers", true) && !r.data.waypoints.isEmpty()) {
    68                 markerLayer = new MarkerLayer(r.data, markerLayerName, associatedFile, gpxLayer, false);
    69                 if (markerLayer.data.size() == 0) {
    70                     markerLayer = null;
     71                data.markerLayer = new MarkerLayer(r.data, markerLayerName, associatedFile, data.gpxLayer, false);
     72                if (data.markerLayer.data.size() == 0) {
     73                    data.markerLayer = null;
    7174                }
    7275            }
    73             postLayerTask = new Runnable() {
     76            data.postLayerTask = new Runnable() {
    7477                @Override
    7578                public void run() {
    76                     if (markerLayer != null) {
    77                         markerLayer.addMouseHandler();
     79                    if (data.markerLayer != null) {
     80                        data.markerLayer.addMouseHandler();
    7881                    }
    7982                    if (!parsedProperly) {
    8083                        String msg;
     
    9396            e.printStackTrace();
    9497            throw new IOException(tr("Parsing data for layer ''{0}'' failed", gpxLayerName));
    9598        }
     99        return data;
    96100    }
    97 
    98     public GpxLayer getGpxLayer() {
    99         return gpxLayer;
    100     }
    101 
    102     public MarkerLayer getMarkerLayer() {
    103         return markerLayer;
    104     }
    105 
    106     public Runnable getPostLayerTask() {
    107         return postLayerTask;
    108     }
    109101}