Class ResourceLoader
- Direct Known Subclasses:
SwingFormsResources
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, by using the methods that do NOT accept a class loader argument. These methods use this class's class loader, which will load resources from the application's jar file. For example:
// Loads application resources:
BufferedImage logo = ResourceLoader.getImage("images/app-logo.png");
String helpText = ResourceLoader.getTextResource("docs/app-help.txt");
// And so on...
If an extension wishes to use this class to load resources from ITS OWN jar file, then it must supply its own class loader to the overloads that accept a class loader argument. For example:
// Loads extension resources:
BufferedImage logo = ResourceLoader.getImage("images/extension-logo.png",
extensionClassLoader);
Remember that application extensions only have access to their own class loader either in the constructor of the main extension class, or in the loadJarResources() method! After the extension is fully instantiated, its class loader is closed. Refer to the swing-extras documentation for more details on application extensions and class loaders.
- Since:
- swing-extras 2.7
- Author:
- scorbo2
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanextractResourceToFile(String resourcePath, File outFile) Attempts to extract the given jar resource to the specified output file.static booleanextractResourceToFile(String resourcePath, File outFile, ClassLoader classLoader) Attempts to extract the given jar resource to the specified output file.static ImageIconLoads the named image resource, scales it to the specified size, and returns it as an ImageIcon.static ImageIcongetIcon(String imagePath, int iconSize, ClassLoader classLoader) Loads the named image resource, scales it to the specified size, and returns it as an ImageIcon.static BufferedImageLoads the given image resource as a BufferedImage and returns it at its natural size.static BufferedImagegetImage(String imagePath, ClassLoader classLoader) Loads the given image resource as a BufferedImage and returns it at its natural size.static StringReturns the current resource path prefix.static StringgetTextResource(String resourcePath) Attempts to read the given text resource from the jar and return it as a String.static StringgetTextResource(String resourcePath, ClassLoader classLoader) Attempts to read the given text resource from the jar and return it as a String.getTextResourceAsLines(String resourcePath) Attempts to read the given text resource from the jar and return it as a List of lines.getTextResourceAsLines(String resourcePath, ClassLoader classLoader) Attempts to read the given text resource from the jar and return it as a List of lines.protected static ImageIconLoads the given image resource as an ImageIcon and returns it at its natural size.protected static ImageIconLoads the named image resource, scales it to the specified size, and returns it as an ImageIcon.protected static BufferedImageLoads and optionally scales the given image resource.static voidYou can optionally set a prefix that will be prepended to all resource paths.
-
Field Details
-
prefix
-
-
Constructor Details
-
ResourceLoader
protected ResourceLoader()
-
-
Method Details
-
setPrefix
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
Returns the current resource path prefix. -
getTextResourceAsLines
Attempts to read the given text resource from the jar and return it as a List of lines. Uses this class's class loader to locate the resource. 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. -
getTextResourceAsLines
Attempts to read the given text resource from the jar and return it as a List of lines. Uses the specified class loader to locate the resource. 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.- Parameters:
resourcePath- the path to the resource within the jarclassLoader- the class loader to use for locating the resource- Returns:
- a list of lines from the resource, or null if the resource cannot be found
-
getTextResource
Attempts to read the given text resource from the jar and return it as a String. Uses this class's class loader to locate the resource. 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.
-
getTextResource
Attempts to read the given text resource from the jar and return it as a String. Uses the specified class loader to locate the resource. 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.
- Parameters:
resourcePath- the path to the resource within the jarclassLoader- the class loader to use for locating the resource- Returns:
- the text resource as a String, or null if the resource cannot be found
-
extractResourceToFile
Attempts to extract the given jar resource to the specified output file. Uses this class's class loader to locate the resource. 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. -
extractResourceToFile
public static boolean extractResourceToFile(String resourcePath, File outFile, ClassLoader classLoader) Attempts to extract the given jar resource to the specified output file. Uses the specified class loader to locate the resource. 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.- Parameters:
resourcePath- the path to the resource within the jaroutFile- the file to write the resource toclassLoader- the class loader to use for locating the resource- Returns:
- true if extraction was successful, false otherwise
-
getImage
Loads the given image resource as a BufferedImage and returns it at its natural size. Uses this class's class loader to locate the resource. 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. -
getImage
Loads the given image resource as a BufferedImage and returns it at its natural size. Uses the specified class loader to locate the resource. 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.- Parameters:
imagePath- the path to the image resource within the jarclassLoader- the class loader to use for locating the resource- Returns:
- the image as a BufferedImage, or null if the resource cannot be found
-
getIcon
Loads the named image resource, scales it to the specified size, and returns it as an ImageIcon. Uses this class's class loader to locate the resource. 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
Loads the named image resource, scales it to the specified size, and returns it as an ImageIcon. Uses the specified class loader to locate the resource. 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.- Parameters:
imagePath- the path to the image resource within the jariconSize- the desired size of the iconclassLoader- the class loader to use for locating the resource- Returns:
- the image as an ImageIcon, or null if the resource cannot be found
-
loadIcon
-
loadIcon
-
loadImage
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.
-