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
createDisabledIcon(javax.swing.Icon icon, int brightnessPercent)
Algorithm for filtering an image to make it appear disabledstatic javax.swing.ImageIcon
createScaledIcon(javax.swing.Icon icon, int width, int height)
This is really a package-level method.static javax.swing.ImageIcon
createScaledIcon(javax.swing.Icon icon, int width, int height, int hints)
This is really a package-level method.static java.awt.Image
createScaledImage(java.awt.Image image, int width, int height, int hints)
Creates a scaled image based upon the given image.static javax.swing.ImageIcon
getDefaultIcon()
static javax.swing.Icon
getDisabledIcon(javax.swing.Icon icon)
Get the disabled rendering of the given icon.static javax.swing.Icon
getDisabledIcon(javax.swing.ImageIcon icon)
Get the disabled rendering of the given imageIcon.static javax.swing.Icon
getDisabledIcon(javax.swing.ImageIcon icon, int brightnessPercent)
Returns a disabled icon while allowing the caller to control the brightness of the icon returnedstatic 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 null if problems occur trying to load the filestatic javax.swing.ImageIcon
loadImage(java.lang.String imageName, byte[] imageBytes)
Load the image using the specified bytes.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. In null 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. In null 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
-
createScaledIcon
public static javax.swing.ImageIcon createScaledIcon(javax.swing.Icon icon, int width, int height)
This is really a package-level method. From outside of this package you should instead be callinggetScaledIcon(Icon, int, int)
.- Parameters:
icon
- the icon to scalewidth
- the new widthheight
- the new height- Returns:
- the new icon
-
createScaledImage
public static java.awt.Image createScaledImage(java.awt.Image image, int width, int height, int hints)
Creates a scaled image based upon the given image.- Parameters:
image
- the image to scalewidth
- the new widthheight
- the new heighthints
-RenderingHints
used byGraphics2D
- Returns:
- a scaled version of the given image
-
createScaledIcon
public static javax.swing.ImageIcon createScaledIcon(javax.swing.Icon icon, int width, int height, int hints)
This is really a package-level method. From outside of this package you should instead be callinggetScaledIcon(Icon, int, int, int)
.- Parameters:
icon
- the icon to scalewidth
- the new widthheight
- the new heighthints
- any hints to apply to the scaling operation- Returns:
- the new icon
-
getDisabledIcon
public static javax.swing.Icon 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.Icon 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.Icon getDisabledIcon(javax.swing.ImageIcon 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.
-
createDisabledIcon
public static javax.swing.ImageIcon createDisabledIcon(javax.swing.Icon icon, int brightnessPercent)
Algorithm for filtering an image to make it appear disabledNote: you should use one of the
getDisabledIcon(Icon)
methods, as this is an internal API method.- Parameters:
icon
- the iconbrightnessPercent
- the percentage of brightness, 0-100, with 100 being the brightest possible value- Returns:
- the new icon
- See Also:
getDisabledIcon(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 an ImageIcon 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. If icon is an ImageIcon, its getDescription() is called to get the name- 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)
Load 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"- Returns:
- the image icon stored in the bytes
-
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()
-
-