Ignore:
Timestamp:
2020-01-06T20:05:34+01:00 (5 years ago)
Author:
Don-vip
Message:

fix #18494 - GPX layers should use the name from the file instead of Downloaded GPX Data whenever possible (patch by taylor.smock, modified)

File:
1 edited

Legend:

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

    r14761 r15646  
    1616import org.openstreetmap.josm.data.Bounds.ParseMethod;
    1717import org.openstreetmap.josm.data.ProjectionBounds;
     18import org.openstreetmap.josm.data.gpx.GpxConstants;
    1819import org.openstreetmap.josm.data.gpx.GpxData;
    1920import org.openstreetmap.josm.gui.MainApplication;
     
    3435import org.openstreetmap.josm.spi.preferences.Config;
    3536import org.openstreetmap.josm.tools.CheckParameterUtil;
     37import org.openstreetmap.josm.tools.Utils;
    3638import org.xml.sax.SAXException;
    3739
     
    4446    private GpxLayer gpxLayer;
    4547
    46     protected String newLayerName;
     48    protected String url;
    4749
    4850    @Override
     
    6870    public Future<?> loadUrl(DownloadParams settings, String url, ProgressMonitor progressMonitor) {
    6971        CheckParameterUtil.ensureParameterNotNull(url, "url");
     72        this.url = url;
    7073        final Optional<String> mappedUrl = Stream.of(GpxUrlPattern.USER_TRACE_ID, GpxUrlPattern.EDIT_TRACE_ID)
    7174                .map(p -> Pattern.compile(p.pattern()).matcher(url))
     
    8184            downloadTask = new DownloadTask(settings,
    8285                    new OsmServerLocationReader(url), progressMonitor);
    83             // Extract .gpx filename from URL to set the new layer name
    84             Matcher matcher = Pattern.compile(GpxUrlPattern.EXTERNAL_GPX_FILE.pattern()).matcher(url);
    85             newLayerName = matcher.matches() ? matcher.group(1) : null;
    8686            // We need submit instead of execute so we can wait for it to finish and get the error
    8787            // message if necessary. If no one calls getErrorMessage() it just behaves like execute.
     
    139139            if (rawData == null)
    140140                return;
    141             String name = newLayerName != null ? newLayerName : tr("Downloaded GPX Data");
     141            String name = getLayerName();
    142142
    143143            GpxImporterData layers = GpxImporter.loadLayers(rawData, reader.isGpxParsedProperly(), name,
     
    149149
    150150            layers.getPostLayerTask().run();
     151        }
     152
     153        private String getLayerName() {
     154            // Extract .gpx filename from URL to set the new layer name
     155            final Matcher matcher = Pattern.compile(GpxUrlPattern.EXTERNAL_GPX_FILE.pattern()).matcher(url);
     156            final String newLayerName = matcher.matches() ? matcher.group(1) : null;
     157            final String metadataName = rawData != null ? rawData.getString(GpxConstants.META_NAME) : null;
     158            final String defaultName = tr("Downloaded GPX Data");
     159
     160            if (Config.getPref().getBoolean("gpx.prefermetadataname", false)) {
     161                return Utils.firstNotEmptyString(defaultName, metadataName, newLayerName);
     162            } else {
     163                return Utils.firstNotEmptyString(defaultName, newLayerName, metadataName);
     164            }
    151165        }
    152166
Note: See TracChangeset for help on using the changeset viewer.