Class ResourceLoader

java.lang.Object
ca.corbett.extras.ResourceLoader
Direct Known Subclasses:
SwingFormsResources

public class ResourceLoader extends Object
A generic utility class for managing access to resources packaged within a jar file. You can optionally extend this class to provide static convenience methods for specific resources used by your application. For example:
     public class MyAppResourceLoader extends ResourceLoader {
         public static final String LOGO_IMG
                  = "com/mycompany/myapp/images/logo.png";
         public static final String RELEASE_NOTES
                  = "com/mycompany/myapp/docs/release-notes.txt";

         public static BufferedImage getLogo() {
             return getImage(LOGO_IMG);
         }

         public static String getReleaseNotes() {
             return getTextResource(RELEASE_NOTES);
         }

         // And so on for your other resources...
     }
 

You can optionally use setPrefix() to make it easier to load multiple resources from a common path. For example:

     ResourceLoader.setPrefix("com/mycompany/myapp/");
     BufferedImage logo = ResourceLoader.getImage("images/logo.png");
     String helpText = ResourceLoader.getTextResource("docs/help.txt");
     // And so on...
 

Application extensions

Application extensions can use this class to load resources from the application's jar file, but they cannot use this to load resources from their own extension jar files. Extension developers need to implement the loadJarResources() method in their AppExtension subclass and use that class's class loader to load resources from the extension jar. Refer to the javadocs in AppExtension and ExtensionManager for more details, or refer to the swing-extras book for a complete example.

Since:
swing-extras 2.7
Author:
scorbo2
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected static String
     
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean
    extractResourceToFile(String resourcePath, File outFile)
    Attempts to extract the given jar resource to the specified output file.
    static ImageIcon
    getIcon(String imagePath, int iconSize)
    Loads the named image resource, scales it to the specified size, and returns it as an ImageIcon.
    getImage(String imagePath)
    Loads the given image resource as a BufferedImage and returns it at its natural size.
    static String
    Returns the current resource path prefix.
    static String
    getTextResource(String resourcePath)
    Attempts to read the given text resource from the jar and return it as a String.
    static List<String>
    Attempts to read the given text resource from the jar and return it as a List of lines.
    protected static ImageIcon
    Loads the given image resource as an ImageIcon and returns it at its natural size.
    protected static ImageIcon
    loadIcon(URL url, int size)
    Loads the named image resource, scales it to the specified size, and returns it as an ImageIcon.
    protected static BufferedImage
    loadImage(URL url, int size)
    Loads and optionally scales the given image resource.
    static void
    setPrefix(String prefix)
    You can optionally set a prefix that will be prepended to all resource paths.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • prefix

      protected static String prefix
  • Constructor Details

    • ResourceLoader

      protected ResourceLoader()
  • Method Details

    • setPrefix

      public static void setPrefix(String prefix)
      You can optionally set a prefix that will be prepended to all resource paths. That way, your calling code can do getImage("icon.png") instead of getImage("my/app/prefix/images/icon.png").

      The default prefix is "", meaning all calls must specify the full resource path.

      Parameters:
      prefix - A resource path prefix common to all your resources, e.g. "/my/app/prefix/"
    • getPrefix

      public static String getPrefix()
      Returns the current resource path prefix.
    • getTextResourceAsLines

      public static List<String> getTextResourceAsLines(String resourcePath)
      Attempts to read the given text resource from the jar and return it as a List of lines. You can use setPrefix() to avoid having to specify a full path each time. Otherwise, the given path is expected to be the full resource path within the jar. Will return null if the resource cannot be found. If you request a resource that is not text, the result will be garbage.
    • getTextResource

      public static String getTextResource(String resourcePath)
      Attempts to read the given text resource from the jar and return it as a String. You can use setPrefix() to avoid having to specify a full path each time. Otherwise, the given path is expected to be the full resource path within the jar. Will return null if the resource cannot be found. If you request a resource that is not text, the result will be garbage.

      Note: This method joins lines using the system line separator, regardless of what line endings are present in the original resource. If you'd rather process the lines separately, use getTextResourceAsLines() instead.

    • extractResourceToFile

      public static boolean extractResourceToFile(String resourcePath, File outFile)
      Attempts to extract the given jar resource to the specified output file. You can use setPrefix() to avoid having to specify a full path each time. Otherwise, the given path is expected to be the full resource path within the jar. Returns true if successful, false otherwise.
    • getImage

      public static BufferedImage getImage(String imagePath)
      Loads the given image resource as a BufferedImage and returns it at its natural size. You can use setPrefix() to avoid having to specify a full path each time. Otherwise, the given path is expected to be the full resource path within the jar. Will return null if the resource cannot be found.
    • getIcon

      public static ImageIcon getIcon(String imagePath, int iconSize)
      Loads the named image resource, scales it to the specified size, and returns it as an ImageIcon. You can use setPrefix() to avoid having to specify a full path each time. Otherwise, the given path is expected to be the full resource path within the jar. Will return null if the resource cannot be found.
    • loadIcon

      protected static ImageIcon loadIcon(URL url)
      Loads the given image resource as an ImageIcon and returns it at its natural size. Will return null if the resource cannot be found, or if the given URL is null.
    • loadIcon

      protected static ImageIcon loadIcon(URL url, int size)
      Loads the named image resource, scales it to the specified size, and returns it as an ImageIcon. Will return null if the resource cannot be found, or if the given URL is null.
    • loadImage

      protected static BufferedImage loadImage(URL url, int size)
      Loads and optionally scales the given image resource. Pass 0 for size to load at natural size. Will return null if the resource cannot be found, or if the given URL is null.