Changeset 10714 in josm for trunk


Ignore:
Timestamp:
2016-08-03T14:37:38+02:00 (8 years ago)
Author:
simon04
Message:

see #11390 - Use CompletableFuture for async image loading

Location:
trunk/src/org/openstreetmap/josm
Files:
4 edited

Legend:

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

    r10634 r10714  
    3232import org.openstreetmap.josm.tools.GBC;
    3333import org.openstreetmap.josm.tools.ImageProvider;
    34 import org.openstreetmap.josm.tools.ImageResource;
    3534
    3635/**
     
    5655        String icon = info.getIcon();
    5756        if (icon != null) {
    58             new ImageProvider(icon).setOptional(true).getInBackground((ImageResource result) -> {
     57            new ImageProvider(icon).setOptional(true).getResourceAsync().thenAccept(result -> {
    5958                if (result != null) {
    6059                    GuiHelper.runInEDT(() -> result.attachImageIcon(this));
  • trunk/src/org/openstreetmap/josm/gui/mappaint/styleelement/MapImage.java

    r10634 r10714  
    1919import org.openstreetmap.josm.gui.util.GuiHelper;
    2020import org.openstreetmap.josm.tools.ImageProvider;
    21 import org.openstreetmap.josm.tools.ImageProvider.ImageCallback;
    2221import org.openstreetmap.josm.tools.Utils;
    2322
     
    9998                .setHeight(height)
    10099                .setOptional(true)
    101                 .getInBackground((ImageCallback) result -> {
     100                .getAsync().thenAccept(result -> {
    102101                    synchronized (this) {
    103102                        if (result == null) {
  • trunk/src/org/openstreetmap/josm/gui/tagging/presets/TaggingPreset.java

    r10657 r10714  
    6060import org.openstreetmap.josm.tools.GBC;
    6161import org.openstreetmap.josm.tools.ImageProvider;
    62 import org.openstreetmap.josm.tools.ImageProvider.ImageResourceCallback;
    6362import org.openstreetmap.josm.tools.Utils;
    6463import org.openstreetmap.josm.tools.template_engine.ParseError;
     
    204203        imgProv.setArchive(arch);
    205204        imgProv.setOptional(true);
    206         imgProv.getInBackground((ImageResourceCallback) result -> {
     205        imgProv.getResourceAsync().thenAccept(result -> {
    207206            if (result != null) {
    208207                GuiHelper.runInEDT(() -> result.attachImageIcon(this));
  • trunk/src/org/openstreetmap/josm/tools/ImageProvider.java

    r10680 r10714  
    4040import java.util.Map;
    4141import java.util.TreeSet;
     42import java.util.concurrent.CompletableFuture;
    4243import java.util.concurrent.ExecutorService;
    4344import java.util.concurrent.Executors;
     
    308309
    309310    /**
    310      * Callback interface for asynchronous image loading.
    311      * @since 10600 (functional interface)
    312      */
    313     @FunctionalInterface
    314     public interface ImageCallback {
    315         /**
    316          * Called when image loading has finished.
    317          * @param result the loaded image icon
    318          */
    319         void finished(ImageIcon result);
    320     }
    321 
    322     /**
    323      * Callback interface for asynchronous image loading (with delayed scaling possibility).
    324      * @since 7693
    325      * @since 10600 (functional interface)
    326      */
    327     @FunctionalInterface
    328     public interface ImageResourceCallback {
    329         /**
    330          * Called when image loading has finished.
    331          * @param result the loaded image resource
    332          */
    333         void finished(ImageResource result);
    334     }
    335 
    336     /**
    337311     * Constructs a new {@code ImageProvider} from a filename in a given directory.
    338312     * @param subdir subdirectory the image lies in
     
    653627
    654628    /**
     629     * Load the image in a background thread.
     630     *
     631     * This method returns immediately and runs the image request asynchronously.
     632     *
     633     * @return the future of the requested image
     634     * @since 10714
     635     */
     636    public CompletableFuture<ImageIcon> getAsync() {
     637        return CompletableFuture.supplyAsync(this::get, IMAGE_FETCHER);
     638    }
     639
     640    /**
    655641     * Execute the image request.
    656642     *
     
    687673     * This method returns immediately and runs the image request asynchronously.
    688674     *
    689      * @param callback a callback. It is called, when the image is ready.
    690      * This can happen before the call to this method returns or it may be
    691      * invoked some time (seconds) later. If no image is available, a null
    692      * value is returned to callback (just like {@link #get}).
    693      */
    694     public void getInBackground(final ImageCallback callback) {
    695         if (name.startsWith(HTTP_PROTOCOL) || name.startsWith(WIKI_PROTOCOL)) {
    696             IMAGE_FETCHER.submit(() -> callback.finished(get()));
    697         } else {
    698             callback.finished(get());
    699         }
    700     }
    701 
    702     /**
    703      * Load the image in a background thread.
    704      *
    705      * This method returns immediately and runs the image request asynchronously.
    706      *
    707      * @param callback a callback. It is called, when the image is ready.
    708      * This can happen before the call to this method returns or it may be
    709      * invoked some time (seconds) later. If no image is available, a null
    710      * value is returned to callback (just like {@link #get}).
    711      * @since 7693
    712      */
    713     public void getInBackground(final ImageResourceCallback callback) {
    714         if (name.startsWith(HTTP_PROTOCOL) || name.startsWith(WIKI_PROTOCOL)) {
    715             IMAGE_FETCHER.submit(() -> callback.finished(getResource()));
    716         } else {
    717             callback.finished(getResource());
    718         }
     675     * @return the future of the requested image
     676     * @since 10714
     677     */
     678     public CompletableFuture<ImageResource> getResourceAsync() {
     679        return CompletableFuture.supplyAsync(this::getResource, IMAGE_FETCHER);
    719680    }
    720681
Note: See TracChangeset for help on using the changeset viewer.