Class ResourceBundleSupport


  • public class ResourceBundleSupport
    extends java.lang.Object
    An utility class to ease up using property-file resource bundles.

    The class support references within the resource bundle set to minimize the occurence of duplicate keys. References are given in the format:

     a.key.name=@referenced.key
     

    A lookup to a key in an other resource bundle should be written by

     a.key.name=@@resourcebundle_name@referenced.key
     
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.TreeMap cache
      A cache for string values, as looking up the cache is faster than looking up the value in the bundle.
      private java.util.Locale locale
      The locale for this bundle.
      private java.util.TreeSet lookupPath
      The current lookup path when performing non local lookups.
      private java.lang.String resourceBase
      The name of the local resource bundle.
      private java.util.ResourceBundle resources
      The resource bundle that will be used for local lookups.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
        ResourceBundleSupport​(java.lang.String baseName)
      Creates a new instance.
        ResourceBundleSupport​(java.util.Locale locale, java.lang.String baseName)
      Creates a new instance.
        ResourceBundleSupport​(java.util.Locale locale, java.util.ResourceBundle resourceBundle)
      Creates a new instance.
      protected ResourceBundleSupport​(java.util.Locale locale, java.util.ResourceBundle resourceBundle, java.lang.String baseName)
      Creates a new instance.
        ResourceBundleSupport​(java.util.ResourceBundle resourceBundle)
      Creates a new instance.
      protected ResourceBundleSupport​(java.util.ResourceBundle resourceBundle, java.lang.String baseName)
      Creates a new instance.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private javax.swing.ImageIcon createIcon​(java.lang.String resourceName, boolean scale, boolean large)
      Attempts to load an image from classpath.
      javax.swing.JMenu createMenu​(java.lang.String keyPrefix)
      Returns a JMenu created from a resource bundle definition.
      private java.lang.Integer createMnemonic​(java.lang.String keyString)
      Creates the Mnemonic from the given String.
      javax.swing.Icon createTransparentIcon​(int width, int height)
      Creates a transparent icon.
      private java.awt.image.BufferedImage createTransparentImage​(int width, int height)
      Creates a transparent image.
      java.lang.String formatMessage​(java.lang.String key, java.lang.Object parameter)
      Formats the message stored in the resource bundle (using a MessageFormat).
      java.lang.String formatMessage​(java.lang.String key, java.lang.Object[] parameters)
      Formats the message stored in the resource bundle (using a MessageFormat).
      java.lang.String formatMessage​(java.lang.String key, java.lang.Object par1, java.lang.Object par2)
      Formats the message stored in the resource bundle (using a MessageFormat).
      javax.swing.Icon getIcon​(java.lang.String key)
      Returns an unscaled icon.
      javax.swing.Icon getIcon​(java.lang.String key, boolean large)
      Returns an scaled icon suitable for buttons or menus.
      javax.swing.KeyStroke getKeyStroke​(java.lang.String key)
      Returns the keystroke stored at the given resourcebundle key.
      javax.swing.KeyStroke getKeyStroke​(java.lang.String key, int mask)
      Returns the keystroke stored at the given resourcebundle key.
      java.util.Locale getLocale()
      Returns the current locale for this resource bundle.
      private int getMenuKeyMask()
      Returns the plattforms default menu shortcut keymask.
      java.lang.Integer getMnemonic​(java.lang.String key)
      Returns the mnemonic stored at the given resourcebundle key.
      javax.swing.KeyStroke getOptionalKeyStroke​(java.lang.String key)
      Returns an optional key stroke.
      javax.swing.KeyStroke getOptionalKeyStroke​(java.lang.String key, int mask)
      Returns an optional key stroke.
      java.lang.Integer getOptionalMnemonic​(java.lang.String key)
      Returns an optional mnemonic.
      protected java.lang.String getResourceBase()
      The base name of the resource bundle.
      java.net.URL getResourceURL​(java.lang.String key)
      Returns a URL pointing to a resource located in the classpath.
      java.lang.String getString​(java.lang.String key)
      Gets a string for the given key from this resource bundle or one of its parents.
      protected java.lang.String internalGetString​(java.lang.String key)
      Performs the lookup for the given key.
      • Methods inherited from class java.lang.Object

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

      • resources

        private java.util.ResourceBundle resources
        The resource bundle that will be used for local lookups.
      • cache

        private java.util.TreeMap cache
        A cache for string values, as looking up the cache is faster than looking up the value in the bundle.
      • lookupPath

        private java.util.TreeSet lookupPath
        The current lookup path when performing non local lookups. This prevents infinite loops during such lookups.
      • resourceBase

        private java.lang.String resourceBase
        The name of the local resource bundle.
      • locale

        private java.util.Locale locale
        The locale for this bundle.
    • Constructor Detail

      • ResourceBundleSupport

        public ResourceBundleSupport​(java.util.Locale locale,
                                     java.lang.String baseName)
        Creates a new instance.
        Parameters:
        locale - the locale.
        baseName - the base name of the resource bundle, a fully qualified class name
      • ResourceBundleSupport

        protected ResourceBundleSupport​(java.util.Locale locale,
                                        java.util.ResourceBundle resourceBundle,
                                        java.lang.String baseName)
        Creates a new instance.
        Parameters:
        locale - the locale for which this resource bundle is created.
        resourceBundle - the resourcebundle
        baseName - the base name of the resource bundle, a fully qualified class name
      • ResourceBundleSupport

        public ResourceBundleSupport​(java.util.Locale locale,
                                     java.util.ResourceBundle resourceBundle)
        Creates a new instance.
        Parameters:
        locale - the locale for which the resource bundle is created.
        resourceBundle - the resourcebundle
      • ResourceBundleSupport

        public ResourceBundleSupport​(java.lang.String baseName)
        Creates a new instance.
        Parameters:
        baseName - the base name of the resource bundle, a fully qualified class name
      • ResourceBundleSupport

        protected ResourceBundleSupport​(java.util.ResourceBundle resourceBundle,
                                        java.lang.String baseName)
        Creates a new instance.
        Parameters:
        resourceBundle - the resourcebundle
        baseName - the base name of the resource bundle, a fully qualified class name
      • ResourceBundleSupport

        public ResourceBundleSupport​(java.util.ResourceBundle resourceBundle)
        Creates a new instance.
        Parameters:
        resourceBundle - the resourcebundle
    • Method Detail

      • getResourceBase

        protected final java.lang.String getResourceBase()
        The base name of the resource bundle.
        Returns:
        the resource bundle's name.
      • getString

        public java.lang.String getString​(java.lang.String key)
        Gets a string for the given key from this resource bundle or one of its parents. If the key is a link, the link is resolved and the referenced string is returned instead.
        Parameters:
        key - the key for the desired string
        Returns:
        the string for the given key
        Throws:
        java.lang.NullPointerException - if key is null
        java.util.MissingResourceException - if no object for the given key can be found
        java.lang.ClassCastException - if the object found for the given key is not a string
      • internalGetString

        protected java.lang.String internalGetString​(java.lang.String key)
        Performs the lookup for the given key. If the key points to a link the link is resolved and that key is looked up instead.
        Parameters:
        key - the key for the string
        Returns:
        the string for the given key
      • getIcon

        public javax.swing.Icon getIcon​(java.lang.String key,
                                        boolean large)
        Returns an scaled icon suitable for buttons or menus.
        Parameters:
        key - the name of the resource bundle key
        large - true, if the image should be scaled to 24x24, or false for 16x16
        Returns:
        the icon.
      • getIcon

        public javax.swing.Icon getIcon​(java.lang.String key)
        Returns an unscaled icon.
        Parameters:
        key - the name of the resource bundle key
        Returns:
        the icon.
      • getMnemonic

        public java.lang.Integer getMnemonic​(java.lang.String key)
        Returns the mnemonic stored at the given resourcebundle key. The mnemonic should be either the symbolic name of one of the KeyEvent.VK_* constants (without the 'VK_') or the character for that key.

        For the enter key, the resource bundle would therefore either contain "ENTER" or "\n".

         a.resourcebundle.key=ENTER
         an.other.resourcebundle.key=\n
         
        Parameters:
        key - the resourcebundle key
        Returns:
        the mnemonic
      • getOptionalMnemonic

        public java.lang.Integer getOptionalMnemonic​(java.lang.String key)
        Returns an optional mnemonic.
        Parameters:
        key - the key.
        Returns:
        The mnemonic.
      • getKeyStroke

        public javax.swing.KeyStroke getKeyStroke​(java.lang.String key)
        Returns the keystroke stored at the given resourcebundle key.

        The keystroke will be composed of a simple key press and the platform's MenuKeyMask.

        The keystrokes character key should be either the symbolic name of one of the KeyEvent.VK_* constants or the character for that key.

        For the 'A' key, the resource bundle would therefore either contain "VK_A" or "a".

         a.resourcebundle.key=VK_A
         an.other.resourcebundle.key=a
         
        Parameters:
        key - the resourcebundle key
        Returns:
        the mnemonic
        See Also:
        Toolkit.getMenuShortcutKeyMask()
      • getOptionalKeyStroke

        public javax.swing.KeyStroke getOptionalKeyStroke​(java.lang.String key)
        Returns an optional key stroke.
        Parameters:
        key - the key.
        Returns:
        The key stroke.
      • getKeyStroke

        public javax.swing.KeyStroke getKeyStroke​(java.lang.String key,
                                                  int mask)
        Returns the keystroke stored at the given resourcebundle key.

        The keystroke will be composed of a simple key press and the given KeyMask. If the KeyMask is zero, a plain Keystroke is returned.

        The keystrokes character key should be either the symbolic name of one of the KeyEvent.VK_* constants or the character for that key.

        For the 'A' key, the resource bundle would therefore either contain "VK_A" or "a".

         a.resourcebundle.key=VK_A
         an.other.resourcebundle.key=a
         
        Parameters:
        key - the resourcebundle key.
        mask - the mask.
        Returns:
        the mnemonic
        See Also:
        Toolkit.getMenuShortcutKeyMask()
      • getOptionalKeyStroke

        public javax.swing.KeyStroke getOptionalKeyStroke​(java.lang.String key,
                                                          int mask)
        Returns an optional key stroke.
        Parameters:
        key - the key.
        mask - the mask.
        Returns:
        The key stroke.
      • createMenu

        public javax.swing.JMenu createMenu​(java.lang.String keyPrefix)
        Returns a JMenu created from a resource bundle definition.

        The menu definition consists of two keys, the name of the menu and the mnemonic for that menu. Both keys share a common prefix, which is extended by ".name" for the name of the menu and ".mnemonic" for the mnemonic.

         # define the file menu
         menu.file.name=File
         menu.file.mnemonic=F
         
        The menu definition above can be used to create the menu by calling createMenu ("menu.file").
        Parameters:
        keyPrefix - the common prefix for that menu
        Returns:
        the created menu
      • getResourceURL

        public java.net.URL getResourceURL​(java.lang.String key)
        Returns a URL pointing to a resource located in the classpath. The resource is looked up using the given key.

        Example: The load a file named 'logo.gif' which is stored in a java package named 'org.jfree.resources':

         mainmenu.logo=org/jfree/resources/logo.gif
         
        The URL for that file can be queried with: getResource("mainmenu.logo");.
        Parameters:
        key - the key for the resource
        Returns:
        the resource URL
      • createIcon

        private javax.swing.ImageIcon createIcon​(java.lang.String resourceName,
                                                 boolean scale,
                                                 boolean large)
        Attempts to load an image from classpath. If this fails, an empty image icon is returned.
        Parameters:
        resourceName - the name of the image. The name should be a global resource name.
        scale - true, if the image should be scaled, false otherwise
        large - true, if the image should be scaled to 24x24, or false for 16x16
        Returns:
        the image icon.
      • createMnemonic

        private java.lang.Integer createMnemonic​(java.lang.String keyString)
        Creates the Mnemonic from the given String. The String consists of the name of the VK constants of the class KeyEvent without VK_*.
        Parameters:
        keyString - the string
        Returns:
        the mnemonic as integer
      • getMenuKeyMask

        private int getMenuKeyMask()
        Returns the plattforms default menu shortcut keymask.
        Returns:
        the default key mask.
      • createTransparentImage

        private java.awt.image.BufferedImage createTransparentImage​(int width,
                                                                    int height)
        Creates a transparent image. These can be used for aligning menu items.
        Parameters:
        width - the width.
        height - the height.
        Returns:
        the created transparent image.
      • createTransparentIcon

        public javax.swing.Icon createTransparentIcon​(int width,
                                                      int height)
        Creates a transparent icon. The Icon can be used for aligning menu items.
        Parameters:
        width - the width of the new icon
        height - the height of the new icon
        Returns:
        the created transparent icon.
      • formatMessage

        public java.lang.String formatMessage​(java.lang.String key,
                                              java.lang.Object parameter)
        Formats the message stored in the resource bundle (using a MessageFormat).
        Parameters:
        key - the resourcebundle key
        parameter - the parameter for the message
        Returns:
        the formated string
      • formatMessage

        public java.lang.String formatMessage​(java.lang.String key,
                                              java.lang.Object par1,
                                              java.lang.Object par2)
        Formats the message stored in the resource bundle (using a MessageFormat).
        Parameters:
        key - the resourcebundle key
        par1 - the first parameter for the message
        par2 - the second parameter for the message
        Returns:
        the formated string
      • formatMessage

        public java.lang.String formatMessage​(java.lang.String key,
                                              java.lang.Object[] parameters)
        Formats the message stored in the resource bundle (using a MessageFormat).
        Parameters:
        key - the resourcebundle key
        parameters - the parameter collection for the message
        Returns:
        the formated string
      • getLocale

        public java.util.Locale getLocale()
        Returns the current locale for this resource bundle.
        Returns:
        the locale.