Ticket #18494: 18494.1.patch

File 18494.1.patch, 4.3 KB (added by taylor.smock, 3 months ago)

Move newLayerName logic to getLayerName function. Also, remove a pointless call in GpxLayer (super(nameInFile); setName(name);, where the super call essentially sets the name is pointless).

  • src/org/openstreetmap/josm/actions/downloadtasks/DownloadGpsTask.java

     
    1515import org.openstreetmap.josm.data.Bounds;
    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;
    2021import org.openstreetmap.josm.gui.PleaseWaitRunnable;
     
    4344    private DownloadTask downloadTask;
    4445    private GpxLayer gpxLayer;
    4546
    46     protected String newLayerName;
     47    protected String url;
    4748
    4849    @Override
    4950    public String[] getPatterns() {
     
    6768    @Override
    6869    public Future<?> loadUrl(DownloadParams settings, String url, ProgressMonitor progressMonitor) {
    6970        CheckParameterUtil.ensureParameterNotNull(url, "url");
     71        this.url = url;
    7072        final Optional<String> mappedUrl = Stream.of(GpxUrlPattern.USER_TRACE_ID, GpxUrlPattern.EDIT_TRACE_ID)
    7173                .map(p -> Pattern.compile(p.pattern()).matcher(url))
    7274                .filter(Matcher::matches)
     
    8082                .anyMatch(p -> url.matches(p.pattern()))) {
    8183            downloadTask = new DownloadTask(settings,
    8284                    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;
    8685            // We need submit instead of execute so we can wait for it to finish and get the error
    8786            // message if necessary. If no one calls getErrorMessage() it just behaves like execute.
    8887            return MainApplication.worker.submit(downloadTask);
     
    138137            rememberDownloadedData(rawData);
    139138            if (rawData == null)
    140139                return;
    141             String name = newLayerName != null ? newLayerName : tr("Downloaded GPX Data");
     140            String name = getLayerName();
    142141
    143142            GpxImporterData layers = GpxImporter.loadLayers(rawData, reader.isGpxParsedProperly(), name,
    144143                    tr("Markers from {0}", name));
     
    150149            layers.getPostLayerTask().run();
    151150        }
    152151
     152        private String getLayerName() {
     153            final String name;
     154            // Extract .gpx filename from URL to set the new layer name
     155            Matcher matcher = Pattern.compile(GpxUrlPattern.EXTERNAL_GPX_FILE.pattern()).matcher(url);
     156            final String newLayerName = matcher.matches() ? matcher.group(1) : null;
     157
     158            if (newLayerName != null) {
     159                name = newLayerName;
     160            } else if (rawData != null && rawData.getString(GpxConstants.META_NAME) != null
     161                    && !rawData.getString(GpxConstants.META_NAME).trim().isEmpty()) {
     162                name = rawData.getString(GpxConstants.META_NAME);
     163            } else {
     164                name = tr("Downloaded GPX Data");
     165            }
     166            return name;
     167        }
     168
    153169        private <L extends Layer> L addOrMergeLayer(L layer, L mergeLayer) {
    154170            if (layer == null) return null;
    155171            if (newLayer || mergeLayer == null) {
  • src/org/openstreetmap/josm/gui/layer/GpxLayer.java

     
    106106     * @param isLocal whether data is attached to a local file
    107107     */
    108108    public GpxLayer(GpxData d, String name, boolean isLocal) {
    109         super(d.getString(GpxConstants.META_NAME));
     109        super(name);
    110110        data = d;
    111111        data.addWeakChangeListener(dataChangeListener);
    112112        trackVisibility = new boolean[data.getTracks().size()];
    113113        Arrays.fill(trackVisibility, true);
    114         setName(name);
    115114        isLocalFile = isLocal;
    116115        ExpertToggleAction.addExpertModeChangeListener(this, true);
    117116    }