Package resources
Class ResourceManager
- java.lang.Object
-
- resources.ResourceManager
-
public class ResourceManager extends java.lang.Object
General resource management class that provides a convenient way of accessing external resources used in Ghidra.There is a known problem with Java's
MediaTracker
that can cause deadlocks. The various methods of this class that createImageIcon
s will do so by loading image bytes directly, as opposed to using the flawed constructorImageIcon(Image)
.
-
-
Constructor Summary
Constructors Constructor Description ResourceManager()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static javax.swing.ImageIcon
getDefaultIcon()
static javax.swing.ImageIcon
getDisabledIcon(javax.swing.Icon icon)
Get the disabled rendering of the given icon.static javax.swing.ImageIcon
getDisabledIcon(javax.swing.Icon icon, int brightnessPercent)
Returns a disabled icon while allowing the caller to control the brightness of the icon returnedstatic javax.swing.ImageIcon
getDisabledIcon(javax.swing.ImageIcon icon)
Get the disabled rendering of the given imageIcon.static java.lang.String
getIconName(javax.swing.Icon icon)
Get the name of this icon.static javax.swing.ImageIcon
getImageIcon(javax.swing.Icon icon)
Returns anImageIcon
for the given icon.static javax.swing.ImageIcon
getImageIconFromImage(java.lang.String imageName, java.awt.Image image)
Creates an image icon from the given image.static java.net.URL
getResource(java.lang.String filename)
Finds a resource with a given name.static java.io.InputStream
getResourceAsStream(java.lang.String filename)
Finds a resource with a given name.static java.io.File
getResourceFile(java.lang.String filename)
Locates a File resource by the given namestatic java.util.Set<java.lang.String>
getResourceNames(java.lang.String dirName, java.lang.String extension)
Search the classpath for files in the <classpath entry>/dirName
location that have the given extension.static java.util.Set<java.net.URL>
getResources(java.lang.String dirName, java.lang.String extension)
Search the classpath for files in the <classpath entry>/dirName
location that have the given extension.static javax.swing.ImageIcon
getScaledIcon(javax.swing.Icon icon, int width, int height)
Creates a scaled ImageIcon from the given icon with scaling ofImage.SCALE_AREA_AVERAGING
static javax.swing.ImageIcon
getScaledIcon(javax.swing.Icon icon, int width, int height, int hints)
Creates a scaled ImageIcon from the given icon.static java.util.Set<java.lang.String>
getToolImages()
static javax.swing.ImageIcon
loadImage(java.lang.String filename)
Load the image specified by filename; returns the default bomb icon if problems occur trying to load the file.static javax.swing.ImageIcon
loadImage(java.lang.String imageName, byte[] imageBytes)
Load the image using the specified bytes.static javax.swing.ImageIcon
loadImage(java.lang.String filename, int width, int height)
Load and scale the image specified by filename; returns null if problems occur trying to load the file.static java.util.List<javax.swing.ImageIcon>
loadImages(java.lang.String... filenames)
Load the images specified by filenames; substitutes the default bomb icon if problems occur trying to load an individual file.static javax.swing.ImageIcon
reloadImage(java.lang.String filename)
A convenience method to force the image denoted byfilename
to be read from disk and to not use the cached version
-
-
-
Method Detail
-
getResource
public static java.net.URL getResource(java.lang.String filename)
Finds a resource with a given name. This method returns null if no resource with this name is found. The rules for searching resources associated with a given class are implemented by the defining class loader of the class.- Parameters:
filename
- "partially" qualified resource filename to get, e.g., "images/go-home.png" would look for the file named 'home.gif' in the 'images' subdirectory of the 'resources' package, following the search rules defined by your CLASSPATH and return an InputStream if found; null if it cannot load the resource.- Returns:
- the URL
-
getResourceAsStream
public static java.io.InputStream getResourceAsStream(java.lang.String filename)
Finds a resource with a given name. This method returns null if no resource with this name is found. The rules for searching resources associated with a given class are implemented by the defining class loader of the class.- Parameters:
filename
- "partially" qualified resource filename to get, e.g., "images/home.gif" would look for the file named 'home.gif' in the 'images' subdirectory of the 'resources' package, following the search rules defined by your CLASSPATH and return an InputStream if found; null if it cannot load the resource.- Returns:
- the input stream
-
getResourceFile
public static java.io.File getResourceFile(java.lang.String filename)
Locates a File resource by the given name- Parameters:
filename
- the filename- Returns:
- the File for the given resource; null if there is no such file
-
getResources
public static java.util.Set<java.net.URL> getResources(java.lang.String dirName, java.lang.String extension)
Search the classpath for files in the <classpath entry>/dirName
location that have the given extension. Innull
is passed for the extension, then all files found in the given dir names will be returned. In this way,null
is a wildcard.This method differs from
getResource(String)
in that this method finds multiple matches.- Parameters:
dirName
- the name of the sub-directory under which to searchextension
- the extension that matching files must possess- Returns:
- set of URLs in the given directory that end with the given extension
-
getResourceNames
public static java.util.Set<java.lang.String> getResourceNames(java.lang.String dirName, java.lang.String extension)
Search the classpath for files in the <classpath entry>/dirName
location that have the given extension. Innull
is passed for the extension, then all files found in the given dir names will be returned. In this way,null
is a wildcard.The names returned from this method are relative and are meant to be used in a later callback to this class for methods such as
loadImage(String)
orgetResource(String)
.- Parameters:
dirName
- the name of the directory under which to searchextension
- the extension that matching files must possess- Returns:
- set of filenames in the given directory that end with the given extension
-
getScaledIcon
public static javax.swing.ImageIcon getScaledIcon(javax.swing.Icon icon, int width, int height, int hints)
Creates a scaled ImageIcon from the given icon.- Parameters:
icon
- the icon to scalewidth
- the width of the new iconheight
- the height of the new iconhints
- scaling hints (seeImage.getScaledInstance(int, int, int)
- Returns:
- A new, scaled ImageIcon
-
getScaledIcon
public static javax.swing.ImageIcon getScaledIcon(javax.swing.Icon icon, int width, int height)
Creates a scaled ImageIcon from the given icon with scaling ofImage.SCALE_AREA_AVERAGING
- Parameters:
icon
- the icon to scalewidth
- the width of the new iconheight
- the height of the new icon- Returns:
- A new, scaled ImageIcon
-
getDisabledIcon
public static javax.swing.ImageIcon getDisabledIcon(javax.swing.Icon icon)
Get the disabled rendering of the given icon.- Parameters:
icon
- The icon to disable.- Returns:
- disabled icon
-
getDisabledIcon
public static javax.swing.ImageIcon getDisabledIcon(javax.swing.ImageIcon icon)
Get the disabled rendering of the given imageIcon.- Parameters:
icon
- The icon to disable.- Returns:
- disabled icon
-
getDisabledIcon
public static javax.swing.ImageIcon getDisabledIcon(javax.swing.Icon icon, int brightnessPercent)
Returns a disabled icon while allowing the caller to control the brightness of the icon returned- Parameters:
icon
- The icon to disable.brightnessPercent
- The level of brightness (0-100, where 100 is the brightest).- Returns:
- a disabled version of the original icon.
-
getImageIconFromImage
public static javax.swing.ImageIcon getImageIconFromImage(java.lang.String imageName, java.awt.Image image)
Creates an image icon from the given image. This method will create anImageIcon
the "safe" way by avoiding the constructorImageIcon(Image)
, which can trigger problems with Java'sMediaTracker
.- Parameters:
imageName
- A textual description of the image; may be nullimage
- The image to use for creating an ImageIcon.- Returns:
- the new icon
-
getImageIcon
public static javax.swing.ImageIcon getImageIcon(javax.swing.Icon icon)
Returns anImageIcon
for the given icon. If the value is already an ImageIcon, then that object is returned; otherwise, an ImageIcon will be created the safe way.- Parameters:
icon
- The icon to convert- Returns:
- the new icon
-
getIconName
public static java.lang.String getIconName(javax.swing.Icon icon)
Get the name of this icon. The value is usually going to be the URL from which the icon was loaded- Parameters:
icon
- the icon for which the name is desired- Returns:
- the name
-
loadImage
public static javax.swing.ImageIcon loadImage(java.lang.String imageName, byte[] imageBytes)
Load the image using the specified bytes. The image icon will be cached using the image name. The bytes must have been read from an image file containing a supported image format, such as GIF, JPEG, or (as of 1.3) PNG.- Parameters:
imageName
- the name of the imageimageBytes
- the bytes of the image- Returns:
- the image icon stored in the bytes
-
loadImage
public static javax.swing.ImageIcon loadImage(java.lang.String filename, int width, int height)
Load and scale the image specified by filename; returns null if problems occur trying to load the file.- Parameters:
filename
- name of file to load, e.g., "images/home.gif"width
- - the width to scale the image toheight
- - the height to scale the image to- Returns:
- the scaled image.
-
loadImage
public static javax.swing.ImageIcon loadImage(java.lang.String filename)
Load the image specified by filename; returns the default bomb icon if problems occur trying to load the file.- Parameters:
filename
- name of file to load, e.g., "images/home.gif"- Returns:
- the image icon stored in the bytes
-
loadImages
public static java.util.List<javax.swing.ImageIcon> loadImages(java.lang.String... filenames)
Load the images specified by filenames; substitutes the default bomb icon if problems occur trying to load an individual file.- Parameters:
filenames
- vararg list of string filenames (ie. "images/home.gif")- Returns:
- list of ImageIcons with each image, problem / missing images replaced with the default icon.
-
reloadImage
public static javax.swing.ImageIcon reloadImage(java.lang.String filename)
A convenience method to force the image denoted byfilename
to be read from disk and to not use the cached version- Parameters:
filename
- name of file to load, e.g., "images/home.gif"- Returns:
- the image icon stored in the bytes
- See Also:
loadImage(String)
-
getDefaultIcon
public static javax.swing.ImageIcon getDefaultIcon()
-
getToolImages
public static java.util.Set<java.lang.String> getToolImages()
-
-