Ignore:
Timestamp:
2009-08-02T14:36:40+02:00 (15 years ago)
Author:
Gubaer
Message:

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

Location:
trunk/src/org/openstreetmap/josm/tools
Files:
2 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}
  • trunk/src/org/openstreetmap/josm/tools/WikiReader.java

    r1755 r1879  
    44import java.io.BufferedReader;
    55import java.io.IOException;
     6import java.io.InputStream;
    67import java.io.InputStreamReader;
    78import java.net.URL;
     
    1011import org.openstreetmap.josm.tools.LanguageInfo;
    1112
     13import static org.openstreetmap.josm.tools.I18n.tr;
     14
    1215/**
    1316 * Read a trac-wiki page.
    14  *
     17 * 
    1518 * @author imi
    1619 */
     
    2932    /**
    3033     * Read the page specified by the url and return the content.
    31      *
    32      * If the url is within the baseurl path, parse it as an trac wikipage and
    33      * replace relative pathes etc..
    34      *
     34     * 
     35     * If the url is within the baseurl path, parse it as an trac wikipage and replace relative
     36     * pathes etc..
     37     * 
    3538     * @return Either the string of the content of the wiki page.
    3639     * @throws IOException Throws, if the page could not be loaded.
     
    4548    public String readLang(String text) {
    4649        String languageCode = LanguageInfo.getLanguageCodeWiki();
    47         String url = baseurl + "/wiki/"+languageCode+text;
     50        String url = baseurl + "/wiki/" + languageCode + text;
    4851        String res = "";
     52        InputStream in = null;
    4953        try {
    50             res = readFromTrac(new BufferedReader(new InputStreamReader(new URL(url).openStream(), "utf-8")));
    51         } catch (IOException ioe) {}
    52         if(res.length() == 0 && languageCode.length() != 0)
    53         {
    54             url = baseurl + "/wiki/"+text;
     54            in = new URL(url).openStream();
     55            res = readFromTrac(new BufferedReader(new InputStreamReader(in, "utf-8")));
     56        } catch (IOException ioe) {
     57            System.out.println(tr("Warning: failed to read MOTD from ''{0}''. Exception was: {1}", url, ioe
     58                    .toString()));
     59        } catch(SecurityException e) {
     60            System.out.println(tr(
     61                    "Warning: failed to read MOTD from ''{0}'' for security reasons. Exception was: {1}", url, e
     62                    .toString()));
     63        } finally {
     64            if (in != null) {
     65                try {
     66                    in.close();
     67                } catch (IOException e) {
     68                }
     69            }
     70        }
     71        if (res.length() == 0 && languageCode.length() != 0) {
     72            url = baseurl + "/wiki/" + text;
    5573            try {
    56                 res = readFromTrac(new BufferedReader(new InputStreamReader(new URL(url).openStream(), "utf-8")));
    57             } catch (IOException ioe) {}
     74                in = new URL(url).openStream();
     75            } catch (IOException e) {
     76                System.out.println(tr("Warning: failed to read MOTD from ''{0}''. Exception was: {1}", url, e
     77                        .toString()));
     78                return res;
     79            } catch (SecurityException e) {
     80                System.out.println(tr(
     81                        "Warning: failed to read MOTD from ''{0}'' for security reasons. Exception was: {1}", url, e
     82                        .toString()));
     83                return res;
     84            }
     85            try {
     86                res = readFromTrac(new BufferedReader(new InputStreamReader(in, "utf-8")));
     87            } catch (IOException ioe) {
     88                System.out.println(tr("Warning: failed to read MOTD from ''{0}''. Exception was: {1}", url, ioe
     89                        .toString()));
     90                return res;
     91            } finally {
     92                if (in != null) {
     93                    try {
     94                        in.close();
     95                    } catch (IOException e) {
     96                    }
     97                }
     98            }
    5899        }
    59100        return res;
     
    63104        String b = "";
    64105        for (String line = in.readLine(); line != null; line = in.readLine()) {
    65             if(!line.contains("[[TranslatedPages]]"))
     106            if (!line.contains("[[TranslatedPages]]")) {
    66107                b += line.replaceAll(" />", ">") + "\n";
     108            }
    67109        }
    68110        return "<html>" + b + "</html>";
     
    74116        String b = "";
    75117        for (String line = in.readLine(); line != null; line = in.readLine()) {
    76             if (line.contains("<div id=\"searchable\">"))
     118            if (line.contains("<div id=\"searchable\">")) {
    77119                inside = true;
    78             else if (line.contains("<div class=\"wiki-toc trac-nav\""))
     120            } else if (line.contains("<div class=\"wiki-toc trac-nav\"")) {
    79121                transl = true;
    80             else if (line.contains("<div class=\"wikipage searchable\">"))
     122            } else if (line.contains("<div class=\"wikipage searchable\">")) {
    81123                inside = true;
    82             else if (line.contains("<div class=\"buttons\">"))
     124            } else if (line.contains("<div class=\"buttons\">")) {
    83125                inside = false;
     126            }
    84127            if (inside && !transl) {
    85                 b += line.replaceAll("<img src=\"/", "<img src=\""+baseurl+"/")
    86                          .replaceAll("href=\"/", "href=\""+baseurl+"/")
    87                          .replaceAll(" />", ">") + "\n";
     128                b += line.replaceAll("<img src=\"/", "<img src=\"" + baseurl + "/").replaceAll("href=\"/",
     129                        "href=\"" + baseurl + "/").replaceAll(" />", ">")
     130                        + "\n";
     131            } else if (transl && line.contains("</div>")) {
     132                transl = false;
    88133            }
    89             else if (transl && line.contains("</div>"))
    90                 transl = false;
    91134        }
    92         if(b.indexOf("      Describe ") >= 0)
     135        if (b.indexOf("      Describe ") >= 0)
    93136            return "";
    94137        return "<html>" + b + "</html>";
Note: See TracChangeset for help on using the changeset viewer.