Package resources

Class ResourceManager

java.lang.Object
resources.ResourceManager

public class ResourceManager extends 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 create ImageIcons will do so by loading image bytes directly, as opposed to using the flawed constructor ImageIcon(Image).

  • Field Details

  • Constructor Details

    • ResourceManager

      public ResourceManager()
  • Method Details

    • getResource

      public static URL getResource(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 InputStream getResourceAsStream(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 File getResourceFile(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 Set<URL> getResources(String dirName, 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 search
      extension - the extension that matching files must possess
      Returns:
      set of URLs in the given directory that end with the given extension
    • getResourceNames

      public static Set<String> getResourceNames(String dirName, 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) or getResource(String).

      Parameters:
      dirName - the name of the directory under which to search
      extension - the extension that matching files must possess
      Returns:
      set of filenames in the given directory that end with the given extension
    • getScaledIcon

      public static ImageIcon getScaledIcon(Icon icon, int width, int height, int hints)
      Creates a scaled ImageIcon from the given icon.
      Parameters:
      icon - the icon to scale
      width - the width of the new icon
      height - the height of the new icon
      hints - scaling hints (see Image.getScaledInstance(int, int, int)
      Returns:
      A new, scaled ImageIcon
    • getScaledIcon

      public static ImageIcon getScaledIcon(ImageIcon icon, int width, int height)
      Creates a scaled ImageIcon from the given icon with scaling of Image.SCALE_AREA_AVERAGING
      Parameters:
      icon - the icon to scale
      width - the width of the new icon
      height - the height of the new icon
      Returns:
      A new, scaled ImageIcon
    • getScaledIcon

      public static Icon getScaledIcon(Icon icon, int width, int height)
      Creates a scaled Icon from the given icon with scaling of Image.SCALE_AREA_AVERAGING. If an EmptyIcon is passed, a new EmptyIcon is returned with the new dimensions.
      Parameters:
      icon - the icon to scale
      width - the width of the new icon
      height - the height of the new icon
      Returns:
      A new, scaled ImageIcon
    • getDisabledIcon

      public static ImageIcon getDisabledIcon(Icon icon)
      Get the disabled rendering of the given icon.
      Parameters:
      icon - The icon to disable.
      Returns:
      disabled icon
    • getDisabledIcon

      public static ImageIcon getDisabledIcon(ImageIcon icon)
      Get the disabled rendering of the given imageIcon.
      Parameters:
      icon - The icon to disable.
      Returns:
      disabled icon
    • getDisabledIcon

      public static ImageIcon getDisabledIcon(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 ImageIcon getImageIconFromImage(String imageName, Image image)
      Creates an image icon from the given image. This method will create an ImageIcon the "safe" way by avoiding the constructor ImageIcon(Image), which can trigger problems with Java's MediaTracker.
      Parameters:
      imageName - A textual description of the image; may be null
      image - The image to use for creating an ImageIcon.
      Returns:
      the new icon
    • getImageIcon

      public static ImageIcon getImageIcon(Icon icon)
      Returns an ImageIcon 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 String getIconName(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 ImageIcon loadImage(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 image
      imageBytes - the bytes of the image
      Returns:
      the image icon stored in the bytes
    • loadImage

      public static ImageIcon loadImage(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 to
      height - - the height to scale the image to
      Returns:
      the scaled image.
    • findIcon

      public static ImageIcon findIcon(String path)
      Attempts to load an icon from the given path. Returns the icon or null if no icon was found from the given path. This differs from loadImage(String) in that loadImage will return the default Icon if one can't be found. Further, loadImage will cache even the default value, while findIcon only caches resolved icons.

      Parameters:
      path - the icon to load, e.g., "images/home.gif"
      Returns:
      the ImageIcon if it exists or null
    • loadIcon

      public static Icon loadIcon(String iconPath)
      Load the icon specified by iconPath. The iconPath can be either a path to a resource on the classpath or a relative or absolute path to an icon on the file system. If the iconPath is a path to a classpath resource, then it will be searched directly or also with an "images/" prepended to the path. For example, if there exists an icon "home.gif" on the classpath that was stored in the standard "images" resource directory, then it exists on the classpath as "images/home.gif". That icon will be found if the iconPath is either "images/home.gif" or just as "home.gif".
      Parameters:
      iconPath - name of file to load, e.g., "images/home.gif"
      Returns:
      an Icon from the given iconPath or null, if no such icon can be found
    • loadImage

      public static ImageIcon loadImage(String iconPath)
      Load the image specified by filename; returns the default bomb icon if problems occur trying to load the file.
      Parameters:
      iconPath - name of file to load, e.g., "images/home.gif"
      Returns:
      the image icon stored in the bytes
    • getLoadedIcons

      public static Set<Icon> getLoadedIcons()
      Returns a list of all loaded icons.
      Returns:
      a list of all loaded icons
    • loadImages

      public static List<ImageIcon> loadImages(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 ImageIcon reloadImage(String filename)
      A convenience method to force the image denoted by filename 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:
    • getDefaultIcon

      public static ImageIcon getDefaultIcon()
    • getToolImages

      public static Set<String> getToolImages()