flamingo
  1. flamingo
  2. FLAMINGO-54

IcoWrapperResizableIcon and SvgBatikResizableIcon not loading icon from resources when in jar

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Incomplete
    • Affects Version/s: 4.1
    • Fix Version/s: milestone 1
    • Component/s: common components
    • Labels:
      None
    • Environment:

      Operating System: Linux
      Platform: All

    • Issuezilla Id:
      54

      Description

      This is bizarre. I'm sure I'm doing something wrong, but I don't know what!

      Using IcoWrapperResizableIcon.getIcon(InputStream, Dimension) when the
      inputstream is from a resource (getClass().getResourceAsStream(path)) works as
      expected until the project is bundled up into one jar file.

      In the ribbon UI I have created, the icons showed up correctly when running
      using "java -cp build packagepath.RibbonThingy".

      When I packaged up the same files into a jar file, the ico icons don't show up
      using "java -cp packaged.jar packagePath.RibbonThingy"

      (png icons show up just fine)

      $ java -version
      java version "1.6.0_0"
      OpenJDK Runtime Environment (IcedTea6 1.4.1) (6b14-1.4.1-0ubuntu11)
      OpenJDK 64-Bit Server VM (build 14.0-b08, mixed mode)

      I've tried to eliminate as many variables as I can think of. I've tried running
      with OpenJDK under linux as well. Also tried OS X Java 6 and MS Windows Java 6.
      All of those show the PNG icons just fine but not ICO nor SVG icons.

        Activity

        Hide
        scherpbier added a comment -

        Follow up...
        I'm using the code below to load icons. Maybe I'm just doing something really
        stupid.

        /**

        • Takes care of loading icons in an efficient manner.
          */
          public class IconManager {

        private final static Object mAnchor = new Object();
        private static Map<String, ResizableIcon> mCachedIcons = new HashMap<String,
        ResizableIcon>();

        /**

        • Loads an icon from the resources. For every path, only one icon object
        • will ever be created.
        • @param resourcePath
        • the path to the icon.
        • @param size
        • the horizontal size of the icon (icons are assumed to be square)
        • @return the icon or null if the resource could not be found.
          */
          public static ResizableIcon getIcon(String resourcePath, int size) {
          ResizableIcon icon = mCachedIcons.get(resourcePath + size);
          if (icon == null) {
          InputStream resourceStream = mAnchor.getClass()
          .getResourceAsStream(resourcePath);
          if (resourceStream != null)
          Unknown macro: { byte[] contents = null; try { contents = getContents(resourceStream); } catch (IOException e) { e.printStackTrace(); } ByteArrayInputStream in = new ByteArrayInputStream(contents); if (resourcePath.endsWith(".png")) { icon = ImageWrapperResizableIcon.getIcon(in, new Dimension( size, size)); } else if (resourcePath.endsWith(".svg")) { icon = SvgBatikResizableIcon.getSvgIcon(in, new Dimension( size, size)); } else if (resourcePath.endsWith(".ico")) { icon = IcoWrapperResizableIcon.getIcon(in, new Dimension( size, size)); } }

          else

          { System.out.format("Resource %s not found\n", resourcePath); }

          if (icon != null)

          { mCachedIcons.put(resourcePath + size, icon); }

          else

          { System.out.format("No icon loaded for resource %s\n", resourcePath); }

          }
          System.out.format("Icon for %s and size %d: %s\n", resourcePath, size,
          icon);
          return icon;
          }

        /**

        • Reads the contents of a stream without closing it.
        • @param in
        • the stream to read from.
        • @return the contents.
        • @throws IOException
          */
          private static byte[] getContents(InputStream in) throws IOException {
          ByteArrayOutputStream bout = new ByteArrayOutputStream();
          byte[] buffer = new byte[40000];
          while (true)
          Unknown macro: { int bytesRead = in.read(buffer); if (bytesRead <= 0) { break; } bout.write(buffer, 0, bytesRead); }

          System.out.format("Got contents: %d bytes\n", bout.size());
          return bout.toByteArray();
          }
          }

        Show
        scherpbier added a comment - Follow up... I'm using the code below to load icons. Maybe I'm just doing something really stupid. /** Takes care of loading icons in an efficient manner. */ public class IconManager { private final static Object mAnchor = new Object(); private static Map<String, ResizableIcon> mCachedIcons = new HashMap<String, ResizableIcon>(); /** Loads an icon from the resources. For every path, only one icon object will ever be created. @param resourcePath the path to the icon. @param size the horizontal size of the icon (icons are assumed to be square) @return the icon or null if the resource could not be found. */ public static ResizableIcon getIcon(String resourcePath, int size) { ResizableIcon icon = mCachedIcons.get(resourcePath + size); if (icon == null) { InputStream resourceStream = mAnchor.getClass() .getResourceAsStream(resourcePath); if (resourceStream != null) Unknown macro: { byte[] contents = null; try { contents = getContents(resourceStream); } catch (IOException e) { e.printStackTrace(); } ByteArrayInputStream in = new ByteArrayInputStream(contents); if (resourcePath.endsWith(".png")) { icon = ImageWrapperResizableIcon.getIcon(in, new Dimension( size, size)); } else if (resourcePath.endsWith(".svg")) { icon = SvgBatikResizableIcon.getSvgIcon(in, new Dimension( size, size)); } else if (resourcePath.endsWith(".ico")) { icon = IcoWrapperResizableIcon.getIcon(in, new Dimension( size, size)); } } else { System.out.format("Resource %s not found\n", resourcePath); } if (icon != null) { mCachedIcons.put(resourcePath + size, icon); } else { System.out.format("No icon loaded for resource %s\n", resourcePath); } } System.out.format("Icon for %s and size %d: %s\n", resourcePath, size, icon); return icon; } /** Reads the contents of a stream without closing it. @param in the stream to read from. @return the contents. @throws IOException */ private static byte[] getContents(InputStream in) throws IOException { ByteArrayOutputStream bout = new ByteArrayOutputStream(); byte[] buffer = new byte [40000] ; while (true) Unknown macro: { int bytesRead = in.read(buffer); if (bytesRead <= 0) { break; } bout.write(buffer, 0, bytesRead); } System.out.format("Got contents: %d bytes\n", bout.size()); return bout.toByteArray(); } }
        Hide
        scherpbier added a comment -

        The output produced during UI creation in my app is as follows:
        Got contents: 1660 bytes
        Icon for /images/x32/location.png and size 32:
        org.jvnet.flamingo.common.icon.ImageWrapperResizableIcon@53077fc9
        Got contents: 1881 bytes
        Icon for /images/x32/search.png and size 32:
        org.jvnet.flamingo.common.icon.ImageWrapperResizableIcon@52c00025
        Got contents: 717 bytes
        Icon for /images/x16/0204-scroll_search.png and size 16:
        org.jvnet.flamingo.common.icon.ImageWrapperResizableIcon@70e434d
        Got contents: 702 bytes
        Icon for /images/x16/2034-left2.png and size 16:
        org.jvnet.flamingo.common.icon.ImageWrapperResizableIcon@26789869
        Got contents: 670 bytes
        Icon for /images/x16/2035-right2.png and size 16:
        org.jvnet.flamingo.common.icon.ImageWrapperResizableIcon@56e2ecc7
        Got contents: 65643 bytes
        Icon for /images/tomcat.svg and size 16:
        org.jvnet.flamingo.svg.SvgBatikResizableIcon@288b567c
        Got contents: 1893 bytes
        Icon for /images/x32/filters.png and size 32:
        org.jvnet.flamingo.common.icon.ImageWrapperResizableIcon@10987197
        Got contents: 537 bytes
        Icon for /images/x16/0181-text_tool.png and size 16:
        org.jvnet.flamingo.common.icon.ImageWrapperResizableIcon@45c3e9ba
        Got contents: 462 bytes
        Icon for /images/x16/0200-scroll.png and size 16:
        org.jvnet.flamingo.common.icon.ImageWrapperResizableIcon@259a8416
        Got contents: 687 bytes
        Icon for /images/x16/0177-calendar.png and size 16:
        org.jvnet.flamingo.common.icon.ImageWrapperResizableIcon@7df1bd98
        Got contents: 2141 bytes
        Icon for /images/x32/savedsearch.png and size 32:
        org.jvnet.flamingo.common.icon.ImageWrapperResizableIcon@25071521
        Got contents: 1726 bytes
        Icon for /images/x32/0025-search.png and size 32:
        org.jvnet.flamingo.common.icon.ImageWrapperResizableIcon@5d2a73d9
        Icon for /images/x32/0025-search.png and size 32:
        org.jvnet.flamingo.common.icon.ImageWrapperResizableIcon@5d2a73d9
        Icon for /images/x32/0025-search.png and size 32:
        org.jvnet.flamingo.common.icon.ImageWrapperResizableIcon@5d2a73d9

        Note that /images/tomcat.svg is loaded (the contents is 65643 bytes)
        I added the getContents() method to see if this had anything to do with possible
        problems with either closing the resource stream or other loading from a jar issues.

        Show
        scherpbier added a comment - The output produced during UI creation in my app is as follows: Got contents: 1660 bytes Icon for /images/x32/location.png and size 32: org.jvnet.flamingo.common.icon.ImageWrapperResizableIcon@53077fc9 Got contents: 1881 bytes Icon for /images/x32/search.png and size 32: org.jvnet.flamingo.common.icon.ImageWrapperResizableIcon@52c00025 Got contents: 717 bytes Icon for /images/x16/0204-scroll_search.png and size 16: org.jvnet.flamingo.common.icon.ImageWrapperResizableIcon@70e434d Got contents: 702 bytes Icon for /images/x16/2034-left2.png and size 16: org.jvnet.flamingo.common.icon.ImageWrapperResizableIcon@26789869 Got contents: 670 bytes Icon for /images/x16/2035-right2.png and size 16: org.jvnet.flamingo.common.icon.ImageWrapperResizableIcon@56e2ecc7 Got contents: 65643 bytes Icon for /images/tomcat.svg and size 16: org.jvnet.flamingo.svg.SvgBatikResizableIcon@288b567c Got contents: 1893 bytes Icon for /images/x32/filters.png and size 32: org.jvnet.flamingo.common.icon.ImageWrapperResizableIcon@10987197 Got contents: 537 bytes Icon for /images/x16/0181-text_tool.png and size 16: org.jvnet.flamingo.common.icon.ImageWrapperResizableIcon@45c3e9ba Got contents: 462 bytes Icon for /images/x16/0200-scroll.png and size 16: org.jvnet.flamingo.common.icon.ImageWrapperResizableIcon@259a8416 Got contents: 687 bytes Icon for /images/x16/0177-calendar.png and size 16: org.jvnet.flamingo.common.icon.ImageWrapperResizableIcon@7df1bd98 Got contents: 2141 bytes Icon for /images/x32/savedsearch.png and size 32: org.jvnet.flamingo.common.icon.ImageWrapperResizableIcon@25071521 Got contents: 1726 bytes Icon for /images/x32/0025-search.png and size 32: org.jvnet.flamingo.common.icon.ImageWrapperResizableIcon@5d2a73d9 Icon for /images/x32/0025-search.png and size 32: org.jvnet.flamingo.common.icon.ImageWrapperResizableIcon@5d2a73d9 Icon for /images/x32/0025-search.png and size 32: org.jvnet.flamingo.common.icon.ImageWrapperResizableIcon@5d2a73d9 Note that /images/tomcat.svg is loaded (the contents is 65643 bytes) I added the getContents() method to see if this had anything to do with possible problems with either closing the resource stream or other loading from a jar issues.
        Hide
        kirillcool added a comment -

        From the description i'm not sure what is the environment. What is in the jar?
        The flamingo classes, your classes, or images?

        Please create a small (<100 lines) test application that illustrates the issue
        (along with a main() method). Eliminate everything that is not necessary - for
        example, your code handles three types of icon sources. Also, if there is
        special deployment configuration (images in jars or what not), i'll need that
        information too.

        Thanks
        Kirill

        Show
        kirillcool added a comment - From the description i'm not sure what is the environment. What is in the jar? The flamingo classes, your classes, or images? Please create a small (<100 lines) test application that illustrates the issue (along with a main() method). Eliminate everything that is not necessary - for example, your code handles three types of icon sources. Also, if there is special deployment configuration (images in jars or what not), i'll need that information too. Thanks Kirill
        Hide
        scherpbier added a comment -

        My sincere apologies! The problem was between the screen and the back of my head!

        Turns out I wasn't packaging up the "xml-apis-ext.jar" in my final jar file.
        Adding it made both the ICO and SVG images work as expected.

        Could you add a blurb in the docs about packaging up an app using flamingo and
        which additional dependencies need to be included? (By process of elimination,
        all the other jars in the "lib" directory except the batik and xml-apis-ext jars
        weren't required...)

        I see no way to mark this issue as invalid, so could you do that please?

        Show
        scherpbier added a comment - My sincere apologies! The problem was between the screen and the back of my head! Turns out I wasn't packaging up the "xml-apis-ext.jar" in my final jar file. Adding it made both the ICO and SVG images work as expected. Could you add a blurb in the docs about packaging up an app using flamingo and which additional dependencies need to be included? (By process of elimination, all the other jars in the "lib" directory except the batik and xml-apis-ext jars weren't required...) I see no way to mark this issue as invalid, so could you do that please?
        Hide
        kirillcool added a comment -

        There is no documentation on Flamingo components - see [1] for the reasons why.

        Closing this issue as INVALID since it was confirmed to be the deployment
        configuration.

        Thanks
        Kirill

        [1] http://www.pushing-pixels.org/?p=1043

        Show
        kirillcool added a comment - There is no documentation on Flamingo components - see [1] for the reasons why. Closing this issue as INVALID since it was confirmed to be the deployment configuration. Thanks Kirill [1] http://www.pushing-pixels.org/?p=1043

          People

          • Assignee:
            kirillcool
            Reporter:
            scherpbier
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: