Ignore:
Timestamp:
2012-03-08T12:11:38+01:00 (14 years ago)
Author:
bastiK
Message:

see #6797: load map images in background, in case they are loaded over a network. Show temporary image in the meantime.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/ImageProvider.java

    r5033 r5054  
    3232import java.util.HashMap;
    3333import java.util.Map;
     34import java.util.concurrent.Executors;
     35import java.util.concurrent.ExecutorService;
    3436import java.util.regex.Matcher;
    3537import java.util.regex.Pattern;
     
    104106    private static Map<String, ImageResource> cache = new HashMap<String, ImageResource>();
    105107
     108    private final static ExecutorService imageFetcher = Executors.newSingleThreadExecutor();
     109
     110    public interface ImageCallback {
     111        void finished(ImageIcon result);
     112    }
     113
    106114    /**
    107115     * @param subdir    Subdirectory the image lies in.
     
    256264        else
    257265            return ir.getImageIcon(new Dimension(width, height));
     266    }
     267
     268    /**
     269     * Load the image in a background thread.
     270     *
     271     * This method returns immediately and runs the image request
     272     * asynchronously.
     273     *
     274     * @param callback is called, when the image is ready. This can happen
     275     * before the call to getInBackground returns or it may be invoked some
     276     * time (seconds) later.
     277     * If no image is available, a null value is returned to callback (just
     278     * like ImageProvider.get).
     279     */
     280    public void getInBackground(final ImageCallback callback) {
     281        if (name.startsWith("http://") || name.startsWith("wiki://")) {
     282            Runnable fetch = new Runnable() {
     283                @Override
     284                public void run() {
     285                    ImageIcon result = get();
     286                    callback.finished(result);
     287                }
     288            };
     289            imageFetcher.submit(fetch);
     290        } else {
     291            ImageIcon result = get();
     292            callback.finished(result);
     293        }
    258294    }
    259295
Note: See TracChangeset for help on using the changeset viewer.