Changeset 5033 in josm


Ignore:
Timestamp:
2012-03-02T13:58:16+01:00 (13 years ago)
Author:
Don-vip
Message:

Allow plugins to specify additional classLoaders when loading an image

File:
1 edited

Legend:

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

    r5027 r5033  
    2727import java.net.URL;
    2828import java.net.URLDecoder;
     29import java.util.ArrayList;
    2930import java.util.Arrays;
    3031import java.util.Collection;
     
    216217        return get(true);
    217218    }
    218    
     219
    219220    /**
    220221     * Execute the image request.
     222     * @param warn If the requested image has been set as optional and is not found, prints an error message on System.err.
    221223     */
    222224    public ImageIcon get(boolean warn) {
    223         ImageResource ir = getIfAvailableImpl();
     225        return get(warn, null);
     226    }
     227
     228    /**
     229     * Execute the image request.
     230     * @param additionalClassLoaders A collection of additional class loaders to search image for.
     231     */
     232    public ImageIcon get(Collection<ClassLoader> additionalClassLoaders) {
     233        return get(true, additionalClassLoaders);
     234    }
     235
     236    /**
     237     * Execute the image request.
     238     * @param warn If the requested image has been set as optional and is not found, prints an error message on System.err.
     239     * @param additionalClassLoaders A collection of additional class loaders to search image for.
     240     */
     241    public ImageIcon get(boolean warn, Collection<ClassLoader> additionalClassLoaders) {
     242        ImageResource ir = getIfAvailableImpl(additionalClassLoaders);
    224243        if (ir == null) {
    225244            if (!optional) {
     
    228247            } else {
    229248                if (warn) {
    230                     System.out.println(tr("Failed to locate image ''{0}''", name));
     249                    System.err.println(tr("Failed to locate image ''{0}''", name));
    231250                }
    232251                return null;
     
    270289            "^data:([a-zA-Z]+/[a-zA-Z+]+)?(;base64)?,(.+)$");
    271290
    272     private ImageResource getIfAvailableImpl() {
     291    private ImageResource getIfAvailableImpl(Collection<ClassLoader> additionalClassLoaders) {
    273292        if (name == null)
    274293            return null;
     
    371390                        // and don't bother to create a URL unless we're actually
    372391                        // creating the image.
    373                         URL path = getImageUrl(full_name, dirs);
     392                        URL path = getImageUrl(full_name, dirs, additionalClassLoaders);
    374393                        if (path == null) {
    375394                            continue;
     
    510529    }
    511530
    512     private static URL getImageUrl(String path, String name) {
     531    private static URL getImageUrl(String path, String name, Collection<ClassLoader> additionalClassLoaders) {
    513532        if (path != null && path.startsWith("resource://")) {
    514533            String p = path.substring("resource://".length());
    515             for (ClassLoader source : PluginHandler.getResourceClassLoaders()) {
     534            Collection<ClassLoader> classLoaders = new ArrayList<ClassLoader>(PluginHandler.getResourceClassLoaders());
     535            if (additionalClassLoaders != null) {
     536                classLoaders.addAll(additionalClassLoaders);
     537            }
     538            for (ClassLoader source : classLoaders) {
    516539                URL res;
    517540                if ((res = source.getResource(p + name)) != null)
     
    529552    }
    530553
    531     private static URL getImageUrl(String imageName, Collection<String> dirs) {
     554    private static URL getImageUrl(String imageName, Collection<String> dirs, Collection<ClassLoader> additionalClassLoaders) {
    532555        URL u = null;
    533556
     
    536559            for (String name : dirs) {
    537560                try {
    538                     u = getImageUrl(name, imageName);
     561                    u = getImageUrl(name, imageName, additionalClassLoaders);
    539562                    if (u != null)
    540563                        return u;
     
    550573        String dir = Main.pref.getPreferencesDir() + "images";
    551574        try {
    552             u = getImageUrl(dir, imageName);
     575            u = getImageUrl(dir, imageName, additionalClassLoaders);
    553576            if (u != null)
    554577                return u;
     
    560583
    561584        // Absolute path?
    562         u = getImageUrl(null, imageName);
     585        u = getImageUrl(null, imageName, additionalClassLoaders);
    563586        if (u != null)
    564587            return u;
    565588
    566589        // Try plugins and josm classloader
    567         u = getImageUrl("resource://images/", imageName);
     590        u = getImageUrl("resource://images/", imageName, additionalClassLoaders);
    568591        if (u != null)
    569592            return u;
     
    571594        // Try all other resource directories
    572595        for (String location : Main.pref.getAllPossiblePreferenceDirs()) {
    573             u = getImageUrl(location + "images", imageName);
     596            u = getImageUrl(location + "images", imageName, additionalClassLoaders);
    574597            if (u != null)
    575598                return u;
    576             u = getImageUrl(location, imageName);
     599            u = getImageUrl(location, imageName, additionalClassLoaders);
    577600            if (u != null)
    578601                return u;
Note: See TracChangeset for help on using the changeset viewer.