Changeset 3874 in josm


Ignore:
Timestamp:
Feb 9, 2011 9:53:35 AM (2 years ago)
Author:
bastiK
Message:

use classloader to find projections from plugins

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

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/projection/Projections.java

    r3872 r3874  
    3434    } 
    3535 
     36    /** 
     37     * Adds a new projection to the list of known projections. 
     38     *  
     39     * For Plugins authors: make sure your plugin is an early plugin, i.e. put 
     40     * Plugin-Early=true in your Manifest. 
     41     */ 
    3642    public static void addProjection(Projection proj) { 
    3743        allProjections.add(proj); 
  • trunk/src/org/openstreetmap/josm/gui/preferences/ProjectionPreference.java

    r3873 r3874  
    3030import org.openstreetmap.josm.data.projection.ProjectionSubPrefs; 
    3131import org.openstreetmap.josm.gui.NavigatableComponent; 
     32import org.openstreetmap.josm.plugins.PluginHandler; 
    3233import org.openstreetmap.josm.tools.GBC; 
    3334 
     
    203204        Projection oldProj = Main.proj; 
    204205 
    205         try { 
    206             Main.proj = (Projection)Class.forName(name).newInstance(); 
    207         } catch (final Exception e) { 
    208             // backup plan: if we cannot instantiate this, maybe we have an instance already. 
    209             Main.proj = null; 
    210             for (Projection p : Projections.getProjections()) { 
    211                 if (p.getClass().getName().equals(name)) { 
    212                     Main.proj = p; break; 
    213                 }  
    214             } 
    215             if (Main.proj == null) { 
    216                 JOptionPane.showMessageDialog( 
    217                         Main.parent, 
    218                         tr("The projection {0} could not be activated. Using Mercator", name), 
    219                         tr("Error"), 
    220                         JOptionPane.ERROR_MESSAGE 
    221                 ); 
    222                 coll = null; 
    223                 Main.proj = new Mercator(); 
    224                 name = Main.proj.getClass().getName(); 
    225             } 
     206        Projection p = null; 
     207        for (ClassLoader cl : PluginHandler.getResourceClassLoaders()) { 
     208            try { 
     209                p = (Projection) Class.forName(name, true, cl).newInstance(); 
     210            } catch (final Exception e) { 
     211            } 
     212            if (p != null) { 
     213                Main.proj = p; 
     214                break; 
     215            } 
     216        } 
     217        if (p == null) { 
     218            JOptionPane.showMessageDialog( 
     219                    Main.parent, 
     220                    tr("The projection {0} could not be activated. Using Mercator", name), 
     221                    tr("Error"), 
     222                    JOptionPane.ERROR_MESSAGE 
     223            ); 
     224            coll = null; 
     225            Main.proj = new Mercator(); 
     226            name = Main.proj.getClass().getName(); 
    226227        } 
    227228        PROP_SUB_PROJECTION.put(coll); 
     
    285286     */ 
    286287    private void setupProjectionCombo() { 
     288        boolean found = false; 
    287289        for (int i = 0; i < projectionCombo.getItemCount(); ++i) { 
    288290            Projection proj = (Projection)projectionCombo.getItemAt(i); 
     
    294296                projectionCombo.setSelectedIndex(i); 
    295297                selectedProjectionChanged(proj); 
     298                found = true; 
    296299                break; 
    297300            } 
    298301        } 
     302        if (!found) 
     303            throw new RuntimeException("Couldn't find the current projection in the list of available projections!"); 
    299304 
    300305        projectionCombo.addActionListener(new ActionListener() { 
  • trunk/src/org/openstreetmap/josm/plugins/PluginHandler.java

    r3730 r3874  
    159159 
    160160    /** 
     161     * Add here all ClassLoader whose resource should be searched. 
     162     */ 
     163    private static final List<ClassLoader> sources = new LinkedList<ClassLoader>(); 
     164 
     165    static { 
     166        try { 
     167            sources.add(ClassLoader.getSystemClassLoader()); 
     168            sources.add(org.openstreetmap.josm.gui.MainApplication.class.getClassLoader()); 
     169        } catch (SecurityException ex) { 
     170            sources.add(ImageProvider.class.getClassLoader()); 
     171        } 
     172    } 
     173 
     174    public static Collection<ClassLoader> getResourceClassLoaders() { 
     175        return Collections.unmodifiableCollection(sources); 
     176    } 
     177 
     178    /** 
    161179     * Removes deprecated plugins from a collection of plugins. Modifies the 
    162180     * collection <code>plugins</code>. 
     
    516534 
    517535            ClassLoader pluginClassLoader = createClassLoader(toLoad); 
    518             ImageProvider.sources.add(0, pluginClassLoader); 
     536            sources.add(0, pluginClassLoader); 
    519537            monitor.setTicksCount(toLoad.size()); 
    520538            for (PluginInformation info : toLoad) { 
  • trunk/src/org/openstreetmap/josm/plugins/ReadLocalPluginInformationTask.java

    r3730 r3874  
    6565            availablePlugins.put(info.getName(), info); 
    6666        } else { 
    67             availablePlugins.get(info.getName()).localversion = info.version; 
     67            PluginInformation current = availablePlugins.get(info.getName()); 
     68            current.localversion = info.version; 
    6869            if (info.icon != null) { 
    69                 availablePlugins.get(info.getName()).icon = info.icon; 
    70             } 
     70                current.icon = info.icon; 
     71            } 
     72            current.early = info.early; 
     73            current.className = info.className; 
     74            current.libraries = info.libraries; 
     75            current.stage = info.stage; 
     76            current.requires = info.requires; 
    7177        } 
    7278    } 
  • trunk/src/org/openstreetmap/josm/tools/ImageProvider.java

    r3862 r3874  
    2424import java.util.Collection; 
    2525import java.util.HashMap; 
    26 import java.util.LinkedList; 
    27 import java.util.List; 
    2826import java.util.Map; 
    2927import java.util.zip.ZipEntry; 
     
    3634import org.openstreetmap.josm.data.osm.OsmPrimitiveType; 
    3735import org.openstreetmap.josm.io.MirroredInputStream; 
     36import org.openstreetmap.josm.plugins.PluginHandler; 
    3837 
    3938/** 
     
    6867     */ 
    6968    private static Map<String, ImageWrapper> cache = new HashMap<String, ImageWrapper>(); 
    70  
    71     /** 
    72      * Add here all ClassLoader whose resource should be searched. Plugin's class loaders are added 
    73      * by main. 
    74      */ 
    75     public static final List<ClassLoader> sources = new LinkedList<ClassLoader>(); 
    7669 
    7770    /** 
     
    234227        if (path.startsWith("resource://")) { 
    235228            String p = path.substring("resource://".length()); 
    236             for (ClassLoader source : sources) { 
     229            for (ClassLoader source : PluginHandler.getResourceClassLoaders()) { 
    237230                URL res; 
    238231                if ((res = source.getResource(p + name)) != null) 
     
    355348    } 
    356349 
    357     static { 
    358         try { 
    359             sources.add(ClassLoader.getSystemClassLoader()); 
    360             sources.add(org.openstreetmap.josm.gui.MainApplication.class.getClassLoader()); 
    361         } catch (SecurityException ex) { 
    362             sources.add(ImageProvider.class.getClassLoader()); 
    363         } 
    364     } 
    365  
    366350    /* 
    367351     * from: http://www.jidesoft.com/blog/2008/02/29/rotate-an-icon-in-java/ License: 
Note: See TracChangeset for help on using the changeset viewer.