Ignore:
Timestamp:
02.08.2009 14:36:40 (3 years ago)
Author:
Gubaer
Message:

towards a fix for #3142: JOSM applet class no longer functional

File:
1 edited

Legend:

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

    r1814 r1879  
    4343     * @author imi 
    4444     */ 
    45     public static enum OverlayPosition {NORTHWEST, NORTHEAST, SOUTHWEST, SOUTHEAST} 
     45    public static enum OverlayPosition { 
     46        NORTHWEST, NORTHEAST, SOUTHWEST, SOUTHEAST 
     47    } 
    4648 
    4749    /** 
     
    5153 
    5254    /** 
    53      * Add here all ClassLoader whose ressource should be searched. 
    54      * Plugin's class loaders are added by main. 
     55     * Add here all ClassLoader whose ressource should be searched. Plugin's class loaders are added 
     56     * by main. 
    5557     */ 
    5658    public static final List<ClassLoader> sources = new LinkedList<ClassLoader>(); 
     
    5860    /** 
    5961     * Return an image from the specified location. 
    60      * 
    61      * @param subdir    The position of the directory, e.g. "layer" 
    62      * @param name      The icons name (without the ending of ".png") 
     62     *  
     63     * @param subdir The position of the directory, e.g. "layer" 
     64     * @param name The icons name (without the ending of ".png") 
    6365     * @return The requested Image. 
    6466     */ 
     
    6769        if (icon == null) { 
    6870            String ext = name.indexOf('.') != -1 ? "" : ".png"; 
    69             throw new NullPointerException("/images/"+subdir+"/"+name+ext+" not found"); 
     71            throw new NullPointerException("/images/" + subdir + "/" + name + ext + " not found"); 
    7072        } 
    7173        return icon; 
    7274    } 
    7375 
    74     public static ImageIcon getIfAvailable(String subdir, String name) 
    75     { 
    76         return getIfAvailable((Collection<String>)null, null, subdir, name); 
    77     } 
    78     public static final ImageIcon getIfAvailable(String[] dirs, String id, String subdir, String name) 
    79     { 
     76    public static ImageIcon getIfAvailable(String subdir, String name) { 
     77        return getIfAvailable((Collection<String>) null, null, subdir, name); 
     78    } 
     79 
     80    public static final ImageIcon getIfAvailable(String[] dirs, String id, String subdir, String name) { 
    8081        return getIfAvailable(Arrays.asList(dirs), id, subdir, name); 
    8182    } 
    8283 
    8384    /** 
    84      * Like {@link #get(String)}, but does not throw and return <code>null</code> 
    85      * in case of nothing is found. Use this, if the image to retrieve is optional. 
    86      */ 
    87     public static ImageIcon getIfAvailable(Collection<String> dirs, String id, String subdir, String name) 
    88     { 
     85     * Like {@link #get(String)}, but does not throw and return <code>null</code> in case of nothing 
     86     * is found. Use this, if the image to retrieve is optional. 
     87     */ 
     88    public static ImageIcon getIfAvailable(Collection<String> dirs, String id, String subdir, String name) { 
    8989        if (name == null) 
    9090            return null; 
    91         if (name.startsWith("http://")) 
    92         { 
     91        if (name.startsWith("http://")) { 
    9392            Image img = cache.get(name); 
    94             if(img == null) 
    95             { 
    96                 try 
    97                 { 
    98                     MirroredInputStream is = new MirroredInputStream(name, 
    99                             new File(Main.pref.getPreferencesDir(), "images").toString()); 
    100                     if(is != null) 
    101                     { 
     93            if (img == null) { 
     94                try { 
     95                    MirroredInputStream is = new MirroredInputStream(name, new File(Main.pref.getPreferencesDir(), 
     96                    "images").toString()); 
     97                    if (is != null) { 
    10298                        img = Toolkit.getDefaultToolkit().createImage(is.getFile().toURI().toURL()); 
    10399                        cache.put(name, img); 
    104100                    } 
    105                 } 
    106                 catch(IOException e) { 
     101                } catch (IOException e) { 
    107102                } 
    108103            } 
     
    115110        } 
    116111        String ext = name.indexOf('.') != -1 ? "" : ".png"; 
    117         String full_name = subdir+name+ext; 
     112        String full_name = subdir + name + ext; 
    118113        String cache_name = full_name; 
    119114        /* cache separately */ 
    120         if(dirs != null && dirs.size() > 0) { 
    121             cache_name = "id:"+id+":"+full_name; 
     115        if (dirs != null && dirs.size() > 0) { 
     116            cache_name = "id:" + id + ":" + full_name; 
    122117        } 
    123118 
     
    125120        if (img == null) { 
    126121            // getImageUrl() does a ton of "stat()" calls and gets expensive 
    127             // and redundant when you have a whole ton of objects.  So, 
     122            // and redundant when you have a whole ton of objects. So, 
    128123            // index the cache by the name of the icon we're looking for 
    129124            // and don't bother to create a URL unless we're actually 
     
    139134    } 
    140135 
    141     private static URL getImageUrl(String path, String name) 
    142     { 
    143         if(path.startsWith("resource://")) 
    144         { 
     136    private static URL getImageUrl(String path, String name) { 
     137        if (path.startsWith("resource://")) { 
    145138            String p = path.substring("resource://".length()); 
    146             for (ClassLoader source : sources) 
    147             { 
     139            for (ClassLoader source : sources) { 
    148140                URL res; 
    149                 if ((res = source.getResource(p+name)) != null) 
     141                if ((res = source.getResource(p + name)) != null) 
    150142                    return res; 
    151143            } 
    152         } 
    153         else 
    154         { 
     144        } else { 
    155145            try { 
    156146                File f = new File(path, name); 
    157                 if(f.exists()) 
     147                if (f.exists()) 
    158148                    return f.toURI().toURL(); 
    159             } catch (MalformedURLException e) {} 
     149            } catch (MalformedURLException e) { 
     150            } 
    160151        } 
    161152        return null; 
    162153    } 
    163154 
    164     private static URL getImageUrl(String imageName, Collection<String> dirs) 
    165     { 
    166         URL u; 
     155    private static URL getImageUrl(String imageName, Collection<String> dirs) { 
     156        URL u = null; 
     157 
    167158        // Try passed directories first 
    168         if(dirs != null) 
    169         { 
    170             for (String name : dirs) 
    171             { 
    172                 u = getImageUrl(name, imageName); 
    173                 if(u != null) return u; 
     159        if (dirs != null) { 
     160            for (String name : dirs) { 
     161                try { 
     162                    u = getImageUrl(name, imageName); 
     163                    if (u != null) 
     164                        return u; 
     165                } catch (SecurityException e) { 
     166                    System.out.println(tr( 
     167                            "Warning: failed to acccess directory ''{0}'' for security reasons. Exception was: {1}", 
     168                            name, e.toString())); 
     169                } 
     170 
    174171            } 
    175172        } 
    176173        // Try user-preference directory 
    177         u = getImageUrl(Main.pref.getPreferencesDir()+"images", imageName); 
    178         if(u != null) return u; 
     174        String dir = Main.pref.getPreferencesDir() + "images"; 
     175        try { 
     176            u = getImageUrl(dir, imageName); 
     177            if (u != null) 
     178                return u; 
     179        } catch (SecurityException e) { 
     180            System.out.println(tr( 
     181                    "Warning: failed to acccess directory ''{0}'' for security reasons. Exception was: {1}", dir, e 
     182                    .toString())); 
     183        } 
    179184 
    180185        // Try plugins and josm classloader 
    181186        u = getImageUrl("resource://images/", imageName); 
    182         if(u != null) return u; 
     187        if (u != null) 
     188            return u; 
    183189 
    184190        // Try all other ressource directories 
    185         for (String location : Main.pref.getAllPossiblePreferenceDirs()) 
    186         { 
    187             u = getImageUrl(location+"images", imageName); 
    188             if(u != null) return u; 
     191        for (String location : Main.pref.getAllPossiblePreferenceDirs()) { 
     192            u = getImageUrl(location + "images", imageName); 
     193            if (u != null) 
     194                return u; 
    189195            u = getImageUrl(location, imageName); 
    190             if(u != null) return u; 
    191         } 
     196            if (u != null) 
     197                return u; 
     198        } 
     199        System.out 
     200        .println(tr( 
     201                "Fatal: failed to locate image ''{0}''. This is a serious configuration problem. JOSM will stop working.", 
     202                imageName)); 
    192203        return null; 
    193204    } 
     
    201212 
    202213    public static Cursor getCursor(String name, String overlay) { 
    203         ImageIcon img = get("cursor",name); 
     214        ImageIcon img = get("cursor", name); 
    204215        if (overlay != null) { 
    205             img = overlay(img, "cursor/modifier/"+overlay, OverlayPosition.SOUTHEAST); 
     216            img = overlay(img, "cursor/modifier/" + overlay, OverlayPosition.SOUTHEAST); 
    206217        } 
    207218        Cursor c = Toolkit.getDefaultToolkit().createCustomCursor(img.getImage(), 
    208                 name.equals("crosshair") ? new Point(10,10) : new Point(3,2), "Cursor"); 
     219                name.equals("crosshair") ? new Point(10, 10) : new Point(3, 2), "Cursor"); 
    209220        return c; 
    210221    } 
    211222 
    212223    /** 
    213      * @return an icon that represent the overlay of the two given icons. The 
    214      * second icon is layed on the first relative to the given position. 
     224     * @return an icon that represent the overlay of the two given icons. The second icon is layed 
     225     * on the first relative to the given position. 
    215226     */ 
    216227    public static ImageIcon overlay(Icon ground, String overlayImage, OverlayPosition pos) { 
    217         GraphicsConfiguration conf = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration(); 
     228        GraphicsConfiguration conf = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice() 
     229        .getDefaultConfiguration(); 
    218230        int w = ground.getIconWidth(); 
    219231        int h = ground.getIconHeight(); 
     
    221233        int wo = overlay.getIconWidth(); 
    222234        int ho = overlay.getIconHeight(); 
    223         BufferedImage img = conf.createCompatibleImage(w,h, Transparency.TRANSLUCENT); 
     235        BufferedImage img = conf.createCompatibleImage(w, h, Transparency.TRANSLUCENT); 
    224236        Graphics g = img.createGraphics(); 
    225237        ground.paintIcon(null, g, 0, 0); 
     
    231243            break; 
    232244        case NORTHEAST: 
    233             x = w-wo; 
     245            x = w - wo; 
    234246            y = 0; 
    235247            break; 
    236248        case SOUTHWEST: 
    237249            x = 0; 
    238             y = h-ho; 
     250            y = h - ho; 
    239251            break; 
    240252        case SOUTHEAST: 
    241             x = w-wo; 
    242             y = h-ho; 
     253            x = w - wo; 
     254            y = h - ho; 
    243255            break; 
    244256        } 
     
    256268    } 
    257269 
    258     /* from: http://www.jidesoft.com/blog/2008/02/29/rotate-an-icon-in-java/ 
    259      * License: "feel free to use" 
     270    /* 
     271     * from: http://www.jidesoft.com/blog/2008/02/29/rotate-an-icon-in-java/ License: 
     272     * "feel free to use" 
    260273     */ 
    261274    final static double DEGREE_90 = 90.0 * Math.PI / 180.0; 
     
    263276    /** 
    264277     * Creates a rotated version of the input image. 
    265      * 
    266      * @param c            The component to get properties useful for painting, e.g. the foreground 
    267      *                     or background color. 
    268      * @param icon         the image to be rotated. 
     278     *  
     279     * @param c The component to get properties useful for painting, e.g. the foreground or 
     280     * background color. 
     281     * @param icon the image to be rotated. 
    269282     * @param rotatedAngle the rotated angle, in degree, clockwise. It could be any double but we 
    270      *                     will mod it with 360 before using it. 
    271      * 
     283     * will mod it with 360 before using it. 
     284     *  
    272285     * @return the image after rotating. 
    273286     */ 
     
    295308            w = (int) (iw * Math.sin(DEGREE_90 - radian) + ih * Math.sin(radian)); 
    296309            h = (int) (iw * Math.sin(radian) + ih * Math.sin(DEGREE_90 - radian)); 
    297         } 
    298         else { 
     310        } else { 
    299311            w = (int) (ih * Math.sin(DEGREE_90 - radian) + iw * Math.sin(radian)); 
    300312            h = (int) (ih * Math.sin(radian) + iw * Math.sin(DEGREE_90 - radian)); 
     
    309321 
    310322        // move the graphics center point to the center of the icon. 
    311         g2d.translate(w/2, h/2); 
     323        g2d.translate(w / 2, h / 2); 
    312324 
    313325        // rotate the graphics about the center point of the icon 
     
    329341        if (type == null) 
    330342            throw new IllegalArgumentException(tr("parameter ''{0}'' must not be null", "type")); 
    331         return get("data",type.getAPIName()); 
     343        return get("data", type.getAPIName()); 
    332344    } 
    333345} 
Note: See TracChangeset for help on using the changeset viewer.