Changeset 5402 in josm


Ignore:
Timestamp:
Aug 7, 2012 1:31:18 AM (11 months ago)
Author:
Don-vip
Message:

see #7910 - resolve NPE when trying to suggest imagery layers for a dataset without download bounds

Location:
trunk/src/org/openstreetmap/josm/actions/downloadtasks
Files:
2 edited

Legend:

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

    r5388 r5402  
    2929import org.openstreetmap.josm.gui.layer.Layer; 
    3030import org.openstreetmap.josm.gui.layer.OsmDataLayer; 
     31import org.openstreetmap.josm.gui.progress.NullProgressMonitor; 
    3132import org.openstreetmap.josm.gui.progress.ProgressMonitor; 
    3233import org.openstreetmap.josm.io.BoundingBoxDownloader; 
     
    5556    } 
    5657 
     58    /** 
     59     * Replies the {@link DataSet} containing the downloaded OSM data. 
     60     * @return The {@link DataSet} containing the downloaded OSM data. 
     61     */ 
    5762    public DataSet getDownloadedData() { 
    5863        return downloadedData; 
     
    6469    } 
    6570 
     71    /** 
     72     * Asynchronously launches the download task for a given bounding box. 
     73     * 
     74     * Set <code>progressMonitor</code> to null, if the task should create, open, and close a progress monitor. 
     75     * Set progressMonitor to {@link NullProgressMonitor#INSTANCE} if progress information is to 
     76     * be discarded. 
     77     * 
     78     * You can wait for the asynchronous download task to finish by synchronizing on the returned 
     79     * {@link Future}, but make sure not to freeze up JOSM. Example: 
     80     * <pre> 
     81     *    Future<?> future = task.download(...); 
     82     *    // DON'T run this on the Swing EDT or JOSM will freeze 
     83     *    future.get(); // waits for the dowload task to complete 
     84     * </pre> 
     85     * 
     86     * The following example uses a pattern which is better suited if a task is launched from 
     87     * the Swing EDT: 
     88     * <pre> 
     89     *    final Future<?> future = task.download(...); 
     90     *    Runnable runAfterTask = new Runnable() { 
     91     *       public void run() { 
     92     *           // this is not strictly necessary because of the type of executor service 
     93     *           // Main.worker is initialized with, but it doesn't harm either 
     94     *           // 
     95     *           future.get(); // wait for the download task to complete 
     96     *           doSomethingAfterTheTaskCompleted(); 
     97     *       } 
     98     *    } 
     99     *    Main.worker.submit(runAfterTask); 
     100     * </pre> 
     101     * @param reader the reader used to parse OSM data (see {@link OsmServerReader#parseOsm}) 
     102     * @param newLayer true, if the data is to be downloaded into a new layer. If false, the task 
     103     *                 selects one of the existing layers as download layer, preferably the active layer. 
     104     * @param downloadArea the area to download 
     105     * @param progressMonitor the progressMonitor 
     106     * @return the future representing the asynchronous task 
     107     */ 
    66108    public Future<?> download(OsmServerReader reader, boolean newLayer, Bounds downloadArea, ProgressMonitor progressMonitor) { 
    67109        return download(new DownloadTask(newLayer, reader, progressMonitor), downloadArea); 
     
    249291 
    250292        protected void suggestImageryLayers() { 
    251             final LatLon center = currentBounds.getCenter(); 
    252             final Set<ImageryInfo> layers = new HashSet<ImageryInfo>(); 
    253  
    254             for (ImageryInfo i : ImageryLayerInfo.instance.getDefaultLayers()) { 
    255                 if (i.getBounds() != null && i.getBounds().contains(center)) { 
    256                     layers.add(i); 
    257                 } 
    258             } 
    259             // Do not suggest layers already in use 
    260             layers.removeAll(ImageryLayerInfo.instance.getLayers()); 
    261             // For layers containing complex shapes, check that center is in one of its shapes (fix #7910) 
    262             for (Iterator<ImageryInfo> iti = layers.iterator(); iti.hasNext(); ) { 
    263                 List<Shape> shapes = iti.next().getBounds().getShapes(); 
    264                 if (shapes != null && !shapes.isEmpty()) { 
    265                     boolean found = false; 
    266                     for (Iterator<Shape> its = shapes.iterator(); its.hasNext() && !found; ) { 
    267                         found = its.next().contains(center); 
     293            if (currentBounds != null) { 
     294                final LatLon center = currentBounds.getCenter(); 
     295                final Set<ImageryInfo> layers = new HashSet<ImageryInfo>(); 
     296     
     297                for (ImageryInfo i : ImageryLayerInfo.instance.getDefaultLayers()) { 
     298                    if (i.getBounds() != null && i.getBounds().contains(center)) { 
     299                        layers.add(i); 
    268300                    } 
    269                     if (!found) { 
    270                         iti.remove(); 
     301                } 
     302                // Do not suggest layers already in use 
     303                layers.removeAll(ImageryLayerInfo.instance.getLayers()); 
     304                // For layers containing complex shapes, check that center is in one of its shapes (fix #7910) 
     305                for (Iterator<ImageryInfo> iti = layers.iterator(); iti.hasNext(); ) { 
     306                    List<Shape> shapes = iti.next().getBounds().getShapes(); 
     307                    if (shapes != null && !shapes.isEmpty()) { 
     308                        boolean found = false; 
     309                        for (Iterator<Shape> its = shapes.iterator(); its.hasNext() && !found; ) { 
     310                            found = its.next().contains(center); 
     311                        } 
     312                        if (!found) { 
     313                            iti.remove(); 
     314                        } 
    271315                    } 
    272316                } 
    273             } 
    274  
    275             if (layers.isEmpty()) { 
    276                 return; 
    277             } 
    278  
    279             final List<String> layerNames = new ArrayList<String>(); 
    280             for (ImageryInfo i : layers) { 
    281                 layerNames.add(i.getName()); 
    282             } 
    283  
    284             if (!ConditionalOptionPaneUtil.showConfirmationDialog( 
    285                     "download.suggest-imagery-layer", 
    286                     Main.parent, 
    287                     tr("<html>For the downloaded area, the following additional imagery layers are available: {0}" + 
    288                             "Do you want to add those layers to the <em>Imagery</em> menu?" + 
    289                             "<br>(If needed, you can remove those entries in the <em>Preferences</em>.)", 
    290                             Utils.joinAsHtmlUnorderedList(layerNames)), 
    291                     tr("Add imagery layers?"), 
    292                     JOptionPane.YES_NO_OPTION, 
    293                     JOptionPane.QUESTION_MESSAGE, 
    294                     JOptionPane.YES_OPTION)) { 
    295                 return; 
    296             } 
    297  
    298             ImageryLayerInfo.addLayers(layers); 
    299         } 
    300  
     317     
     318                if (layers.isEmpty()) { 
     319                    return; 
     320                } 
     321     
     322                final List<String> layerNames = new ArrayList<String>(); 
     323                for (ImageryInfo i : layers) { 
     324                    layerNames.add(i.getName()); 
     325                } 
     326     
     327                if (!ConditionalOptionPaneUtil.showConfirmationDialog( 
     328                        "download.suggest-imagery-layer", 
     329                        Main.parent, 
     330                        tr("<html>For the downloaded area, the following additional imagery layers are available: {0}" + 
     331                                "Do you want to add those layers to the <em>Imagery</em> menu?" + 
     332                                "<br>(If needed, you can remove those entries in the <em>Preferences</em>.)", 
     333                                Utils.joinAsHtmlUnorderedList(layerNames)), 
     334                        tr("Add imagery layers?"), 
     335                        JOptionPane.YES_NO_OPTION, 
     336                        JOptionPane.QUESTION_MESSAGE, 
     337                        JOptionPane.YES_OPTION)) { 
     338                    return; 
     339                } 
     340     
     341                ImageryLayerInfo.addLayers(layers); 
     342            } 
     343        } 
    301344    } 
    302345} 
  • trunk/src/org/openstreetmap/josm/actions/downloadtasks/DownloadTask.java

    r5266 r5402  
    66 
    77import org.openstreetmap.josm.data.Bounds; 
     8import org.openstreetmap.josm.gui.progress.NullProgressMonitor; 
    89import org.openstreetmap.josm.gui.progress.ProgressMonitor; 
    910 
     11/** 
     12 * Interface defining a general download task used to download geographic data (OSM data, GPX tracks, etc.) for a given URL or geographic area. 
     13 */ 
    1014public interface DownloadTask { 
     15     
    1116    /** 
    1217     * Asynchronously launches the download task for a given bounding box. 
Note: See TracChangeset for help on using the changeset viewer.