Changeset 8176 in josm


Ignore:
Timestamp:
2015-04-07T22:17:42+02:00 (9 years ago)
Author:
stoecker
Message:

fix #11317 - tile flickering during TMS loading - patch by wiktorn

Location:
trunk/src/org/openstreetmap/josm/data
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/cache/ICachedLoaderListener.java

    r8168 r8176  
    33
    44public interface ICachedLoaderListener {
     5
    56    /**
    6      * Will be called when K object was successfully downloaded
    7      *
     7     * Result of download
     8     *
     9     */
     10    enum LoadResult {
     11        SUCCESS,
     12        FAILURE,
     13        REJECTED
     14    }
     15    /**
     16     * Will be called when K object processed. The result might be:
     17     * LoadResult.SUCCESS when object was fetched
     18     * LoadResult.FAILURE when there was a failure during download
     19     * LoadResult.REJECTED when job was rejected because of full queue
     20     *
    821     * @param data
    9      * @param success
     22     * @param result
    1023     */
    11     public void loadingFinished(CacheEntry data, boolean success);
     24    public void loadingFinished(CacheEntry data, LoadResult result);
    1225
    1326}
  • trunk/src/org/openstreetmap/josm/data/cache/JCSCachedTileLoaderJob.java

    r8174 r8176  
    2727import org.apache.commons.jcs.engine.behavior.ICacheElement;
    2828import org.openstreetmap.gui.jmapviewer.FeatureAdapter;
     29import org.openstreetmap.josm.data.cache.ICachedLoaderListener.LoadResult;
    2930import org.openstreetmap.josm.data.preferences.IntegerProperty;
    3031
     
    152153                // we got something in cache, and it's valid, so lets return it
    153154                log.log(Level.FINE, "JCS - Returning object from cache: {0}", getCacheKey());
    154                 finishLoading(true);
     155                finishLoading(LoadResult.SUCCESS);
    155156                return;
    156157            }
     
    162163                // queue was full, try again later
    163164                log.log(Level.FINE, "JCS - rejected job for: {0}", getCacheKey());
    164                 finishLoading(false);
     165                finishLoading(LoadResult.REJECTED);
    165166            }
    166167        }
     
    206207            // try to load object from remote resource
    207208            if (loadObject()) {
    208                 finishLoading(true);
     209                finishLoading(LoadResult.SUCCESS);
    209210            } else {
    210211                // if loading failed - check if we can return stale entry
    211212                if (isObjectLoadable()) {
    212213                    // try to get stale entry in cache
    213                     finishLoading(true);
     214                    finishLoading(LoadResult.SUCCESS);
    214215                    log.log(Level.FINE, "JCS - found stale object in cache: {0}", getUrl());
    215216                } else {
    216217                    // failed completely
    217                     finishLoading(false);
     218                    finishLoading(LoadResult.FAILURE);
    218219                }
    219220            }
     
    224225
    225226
    226     private void finishLoading(boolean success) {
     227    private void finishLoading(LoadResult result) {
    227228        Set<ICachedLoaderListener> listeners = null;
    228229        synchronized (inProgress) {
     
    235236        try {
    236237            for (ICachedLoaderListener l: listeners) {
    237                 l.loadingFinished(cacheData, success);
     238                l.loadingFinished(cacheData, result);
    238239            }
    239240        } catch (Exception e) {
     
    241242            log.log(Level.FINE, "Stacktrace", e);
    242243            for (ICachedLoaderListener l: listeners) {
    243                 l.loadingFinished(cacheData, false);
     244                l.loadingFinished(cacheData, LoadResult.FAILURE);
    244245            }
    245246
  • trunk/src/org/openstreetmap/josm/data/imagery/TMSCachedTileLoaderJob.java

    r8174 r8176  
    133133    }
    134134
     135    private boolean isNoTileAtZoom() {
     136        return attributes != null && attributes.isNoTileAtZoom();
     137    }
     138
    135139    @Override
    136140    protected boolean cacheAsEmpty() {
    137         if (attributes != null && attributes.isNoTileAtZoom()) {
    138             // do not remove file - keep the information, that there is no tile, for further requests
    139             // the code above will check, if this information is still valid
     141        return isNoTileAtZoom();
     142    }
     143
     144    private boolean handleNoTileAtZoom() {
     145        if (isNoTileAtZoom()) {
    140146            log.log(Level.FINE, "JCS TMS - Tile valid, but no file, as no tiles at this level {0}", tile);
    141147            tile.setError("No tile at this zoom level");
     
    143149            return true;
    144150        }
    145         return false; // as there is no other cache to cache the Tile, also cache other empty requests
     151        return false;
    146152    }
    147153
     
    157163
    158164    @Override
    159     public void loadingFinished(CacheEntry object, boolean success) {
     165    public void loadingFinished(CacheEntry object, LoadResult result) {
    160166        try {
    161             loadTile(object, success);
     167            tile.finishLoading(); // whatever happened set that loading has finished
     168            switch(result){
     169            case FAILURE:
     170                tile.setError("Problem loading tile");
     171            case SUCCESS:
     172                handleNoTileAtZoom();
     173                if (object != null) {
     174                    byte[] content = object.getContent();
     175                    if (content != null && content.length > 0) {
     176                        tile.loadImage(new ByteArrayInputStream(content));
     177                    }
     178                }
     179            case REJECTED:
     180                // do not set anything here, leave waiting sign
     181            }
    162182            if (listener != null) {
    163                 listener.tileLoadingFinished(tile, success);
     183                listener.tileLoadingFinished(tile, result.equals(LoadResult.SUCCESS));
    164184            }
    165185        } catch (IOException e) {
     
    181201        if (isObjectLoadable()) {
    182202            try {
    183                 loadTile(data);
     203                if (data != null && data.getImage() != null) {
     204                    tile.setImage(data.getImage());
     205                    tile.finishLoading();
     206                }
     207                if (isNoTileAtZoom()) {
     208                    handleNoTileAtZoom();
     209                    tile.finishLoading();
     210                }
    184211                return tile;
    185212            } catch (IOException e) {
     
    189216
    190217        } else {
    191             return null;
    192         }
    193     }
    194 
    195     // loads tile when calling back from cache
    196     private void loadTile(CacheEntry object, boolean success) throws IOException {
    197         tile.finishLoading();
    198         if (object != null) {
    199             byte[] content = object.getContent();
    200             if (content != null && content.length > 0) {
    201                 tile.loadImage(new ByteArrayInputStream(content));
    202             }
    203         }
    204         if (!success) {
    205             tile.setError("Problem loading tile");
    206         }
    207     }
    208 
    209     // loads tile when geting stright from cache
    210     private void loadTile(BufferedImageCacheEntry object) throws IOException {
    211         tile.finishLoading();
    212         if (cacheAsEmpty() || object != null) { // if cache as empty object, do not try to load image
    213             if (object.getImage() != null) {
    214                 tile.setImage(object.getImage());
    215             }
     218            return tile;
    216219        }
    217220    }
Note: See TracChangeset for help on using the changeset viewer.