Ignore:
Timestamp:
2014-10-20T00:03:35+02:00 (10 years ago)
Author:
Don-vip
Message:

fix #10646 - Remote Control v1.6: new load_data handler to load OSM data directly from URL (modified patch by sanderd17)

File:
1 edited

Legend:

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

    r7575 r7636  
    4545    protected DataSet downloadedData;
    4646    protected DownloadTask downloadTask;
    47 
    48     protected OsmDataLayer targetLayer;
    4947
    5048    protected String newLayerName = null;
     
    171169    }
    172170
    173     protected class DownloadTask extends PleaseWaitRunnable {
    174         protected OsmServerReader reader;
     171    /**
     172     * Superclass of internal download task.
     173     * @since 7635
     174     */
     175    public static abstract class AbstractInternalTask extends PleaseWaitRunnable {
     176
     177        protected final boolean newLayer;
    175178        protected DataSet dataSet;
    176         protected boolean newLayer;
     179
     180        /**
     181         * Constructs a new {@code AbstractInternalTask}.
     182         *
     183         * @param newLayer if {@code true}, force download to a new layer
     184         * @param title message for the user
     185         * @param ignoreException If true, exception will be propagated to calling code. If false then
     186         * exception will be thrown directly in EDT. When this runnable is executed using executor framework
     187         * then use false unless you read result of task (because exception will get lost if you don't)
     188         */
     189        public AbstractInternalTask(boolean newLayer, String title, boolean ignoreException) {
     190            super(title, ignoreException);
     191            this.newLayer = newLayer;
     192        }
     193
     194        /**
     195         * Constructs a new {@code AbstractInternalTask}.
     196         *
     197         * @param newLayer if {@code true}, force download to a new layer
     198         * @param title message for the user
     199         * @param progressMonitor progress monitor
     200         * @param ignoreException If true, exception will be propagated to calling code. If false then
     201         * exception will be thrown directly in EDT. When this runnable is executed using executor framework
     202         * then use false unless you read result of task (because exception will get lost if you don't)
     203         */
     204        public AbstractInternalTask(boolean newLayer, String title, ProgressMonitor progressMonitor, boolean ignoreException) {
     205            super(title, progressMonitor, ignoreException);
     206            this.newLayer = newLayer;
     207        }
     208
     209        protected OsmDataLayer getEditLayer() {
     210            if (!Main.isDisplayingMapView()) return null;
     211            return Main.main.getEditLayer();
     212        }
     213
     214        protected int getNumDataLayers() {
     215            int count = 0;
     216            if (!Main.isDisplayingMapView()) return 0;
     217            Collection<Layer> layers = Main.map.mapView.getAllLayers();
     218            for (Layer layer : layers) {
     219                if (layer instanceof OsmDataLayer) {
     220                    count++;
     221                }
     222            }
     223            return count;
     224        }
     225
     226        protected OsmDataLayer getFirstDataLayer() {
     227            if (!Main.isDisplayingMapView()) return null;
     228            Collection<Layer> layers = Main.map.mapView.getAllLayersAsList();
     229            for (Layer layer : layers) {
     230                if (layer instanceof OsmDataLayer)
     231                    return (OsmDataLayer) layer;
     232            }
     233            return null;
     234        }
     235
     236        protected OsmDataLayer createNewLayer(String layerName) {
     237            if (layerName == null || layerName.isEmpty()) {
     238                layerName = OsmDataLayer.createNewName();
     239            }
     240            return new OsmDataLayer(dataSet, layerName, null);
     241        }
     242
     243        protected OsmDataLayer createNewLayer() {
     244            return createNewLayer(null);
     245        }
     246
     247        protected void computeBboxAndCenterScale(Bounds bounds) {
     248            BoundingXYVisitor v = new BoundingXYVisitor();
     249            if (bounds != null) {
     250                v.visit(bounds);
     251            } else {
     252                v.computeBoundingBox(dataSet.getNodes());
     253            }
     254            Main.map.mapView.recalculateCenterScale(v);
     255        }
     256
     257        protected OsmDataLayer addNewLayerIfRequired(String newLayerName, Bounds bounds) {
     258            int numDataLayers = getNumDataLayers();
     259            if (newLayer || numDataLayers == 0 || (numDataLayers > 1 && getEditLayer() == null)) {
     260                // the user explicitly wants a new layer, we don't have any layer at all
     261                // or it is not clear which layer to merge to
     262                //
     263                final OsmDataLayer layer = createNewLayer(newLayerName);
     264                final boolean isDisplayingMapView = Main.isDisplayingMapView();
     265
     266                Main.main.addLayer(layer);
     267
     268                // If the mapView is not there yet, we cannot calculate the bounds (see constructor of MapView).
     269                // Otherwise jump to the current download.
     270                if (isDisplayingMapView) {
     271                    computeBboxAndCenterScale(bounds);
     272                }
     273                return layer;
     274            }
     275            return null;
     276        }
     277
     278        protected void loadData(String newLayerName, Bounds bounds) {
     279            OsmDataLayer layer = addNewLayerIfRequired(newLayerName, bounds);
     280            if (layer == null) {
     281                layer = getEditLayer();
     282                if (layer == null) {
     283                    layer = getFirstDataLayer();
     284                }
     285                layer.mergeFrom(dataSet);
     286                computeBboxAndCenterScale(bounds);
     287                layer.onPostDownloadFromServer();
     288            }
     289        }
     290    }
     291
     292    protected class DownloadTask extends AbstractInternalTask {
     293        protected final OsmServerReader reader;
    177294
    178295        public DownloadTask(boolean newLayer, OsmServerReader reader, ProgressMonitor progressMonitor) {
    179             super(tr("Downloading data"), progressMonitor, false);
     296            super(newLayer, tr("Downloading data"), progressMonitor, false);
    180297            this.reader = reader;
    181             this.newLayer = newLayer;
    182298        }
    183299
     
    186302        }
    187303
    188         @Override public void realRun() throws IOException, SAXException, OsmTransferException {
     304        @Override
     305        public void realRun() throws IOException, SAXException, OsmTransferException {
    189306            try {
    190307                if (isCanceled())
     
    208325        }
    209326
    210         protected OsmDataLayer getEditLayer() {
    211             if (!Main.isDisplayingMapView()) return null;
    212             return Main.main.getEditLayer();
    213         }
    214 
    215         protected int getNumDataLayers() {
    216             int count = 0;
    217             if (!Main.isDisplayingMapView()) return 0;
    218             Collection<Layer> layers = Main.map.mapView.getAllLayers();
    219             for (Layer layer : layers) {
    220                 if (layer instanceof OsmDataLayer) {
    221                     count++;
    222                 }
    223             }
    224             return count;
    225         }
    226 
    227         protected OsmDataLayer getFirstDataLayer() {
    228             if (!Main.isDisplayingMapView()) return null;
    229             Collection<Layer> layers = Main.map.mapView.getAllLayersAsList();
    230             for (Layer layer : layers) {
    231                 if (layer instanceof OsmDataLayer)
    232                     return (OsmDataLayer) layer;
    233             }
    234             return null;
    235         }
    236 
    237         protected OsmDataLayer createNewLayer(String layerName) {
    238             if (layerName == null || layerName.isEmpty()) {
    239                 layerName = OsmDataLayer.createNewName();
    240             }
    241             return new OsmDataLayer(dataSet, layerName, null);
    242         }
    243 
    244         protected OsmDataLayer createNewLayer() {
    245             return createNewLayer(null);
    246         }
    247 
    248         @Override protected void finish() {
     327        @Override
     328        protected void finish() {
    249329            if (isFailed() || isCanceled())
    250330                return;
     
    259339
    260340            rememberDownloadedData(dataSet);
    261             int numDataLayers = getNumDataLayers();
    262             if (newLayer || numDataLayers == 0 || (numDataLayers > 1 && getEditLayer() == null)) {
    263                 // the user explicitly wants a new layer, we don't have any layer at all
    264                 // or it is not clear which layer to merge to
    265                 //
    266                 targetLayer = createNewLayer(newLayerName);
    267                 final boolean isDisplayingMapView = Main.isDisplayingMapView();
    268 
    269                 Main.main.addLayer(targetLayer);
    270 
    271                 // If the mapView is not there yet, we cannot calculate the bounds (see constructor of MapView).
    272                 // Otherwise jump to the current download.
    273                 if (isDisplayingMapView) {
    274                     computeBboxAndCenterScale();
    275                 }
    276             } else {
    277                 targetLayer = getEditLayer();
    278                 if (targetLayer == null) {
    279                     targetLayer = getFirstDataLayer();
    280                 }
    281                 targetLayer.mergeFrom(dataSet);
    282                 computeBboxAndCenterScale();
    283                 targetLayer.onPostDownloadFromServer();
    284             }
    285         }
    286 
    287         protected void computeBboxAndCenterScale() {
    288             BoundingXYVisitor v = new BoundingXYVisitor();
    289             if (currentBounds != null) {
    290                 v.visit(currentBounds);
    291             } else {
    292                 v.computeBoundingBox(dataSet.getNodes());
    293             }
    294             Main.map.mapView.recalculateCenterScale(v);
    295         }
    296 
    297         @Override protected void cancel() {
     341            loadData(newLayerName, currentBounds);
     342        }
     343
     344        @Override
     345        protected void cancel() {
    298346            setCanceled(true);
    299347            if (reader != null) {
Note: See TracChangeset for help on using the changeset viewer.