11.2 Resolving Configured Resources - Reference Documentation
Authors: Andres Almiray
Version: 1.2.0
11.2 Resolving Configured Resources
Parameterized resources may be resolved by an application by leveraging the behavior exposed bygriffon.core.resources.ResourceResolver
which every single griffon.core.GriffonApplication
implements. This interface exposes the following methods:
- Object resolveResource(String key)
- Object resolveResource(String key, Locale locale)
- Object resolveResource(String key, Object[] args)
- Object resolveResource(String key, Object[] args, Locale locale)
- Object resolveResource(String key, List args)
- Object resolveResource(String key, List args, Locale locale)
- Object resolveResource(String key, Map args)
- Object resolveResource(String key, Map args, Locale locale)
NoSuchResourceException
if a resource could not be resolved given the key sent as argument. The following methods take and additional defaultValue
parameter that may be used if no configured resource is found. If this optional parameter were to be null then the key
is used as the literal value of the resource; in other words, these methods never throw NoSuchResourceException
nor return null
unless the passed in key
is null.
- Object resolveResource(String key, Object defaultValue)
- Object resolveResource(String key, Object defaultValue, Locale locale)
- Object resolveResource(String key, Object[] args, Object defaultValue)
- Object resolveResource(String key, Object[] args, Object defaultValue, Locale locale)
- Object resolveResource(String key, List args, Object defaultValue)
- Object resolveResource(String key, List args, Object defaultValue, Locale locale)
- Object resolveResource(String key, Map args, Object defaultValue)
- Object resolveResource(String key, Map args, Object defaultValue, Locale locale)
app.resolveResource('menu.icon')
List
as arguments are meant to be used from Groovy code whereas those that take an Object[]
are meant for Java code; this leads to better idiomatic code as the following examples revealapp.resolveResource('groovy.icon.resource', ['small']) app.resolveResource("java.icon.resource", new Object[]{"large"});
List
versions in Java, like thisimport static java.util.Arrays.asList; … app.resolveResource("hybrid.icon.resource", asList("medium"));
Resource Formats
There are three types of resource formats supported by default. Additional formats may be supported if the right plugins are installed. Resources may be configured using either properties files or Groovy scripts, please refer to the configuration section.Standard FormatThe first set of resource formats are those supported by the JDK'sMessageFormat
facilities. These formats work with all versions of the resolveResource()
method that take a List
or an Object[]
as arguments. Examples follow. First the resource definitions stored in a properties filemenu.icon = /img/icons/menu-{0}.png
griffon-app/resources/img/icons
whose filenames are menu-small.png
, menu-medium.png
and menu-large.png
a component may resolve any of them withObject largeIcon = app.resolveResource('menu.icon', ['large'])
MessageFormat
) and can only be resolved by Griffon. This format uses symbols instead of numbers as placeholders for arguments. Thus the previous messages can be rewritten as followsmenu.icon = /img/icons/menu-{:size}.png
Object largeIcon = app.resolveResource('menu.icon', [size: 'large'])
java.awt.Rectangle
resourcesimport java.awt.Rectangledirect.instance = new Rectangle(10i, 20i, 30i, 40i) computed.instance = { x, y, w, h -> new Rectangle(x, y, w, h) }
resolveResource
is marked as Object
but you'll get a String
from the first two formats. You'll have to make use of property editors in order to transform the value into the correct type. Injected resources are automatically transformed to the expected type.Here's how it can be doneimport javax.swing.Icon import java.beans.PropertyEditor import java.beans.PropertyEditorManager … Object iconValue = app.resolveResource('menu.icon', ['large']) PropertyEditor propertyEditor = PropertyEditorManager.findEditor(Icon) propertyEditor.setAsText(String.valueOf(iconValue)) Icon icon = propertyEditor.getValue()