Resources can be loaded form the classpath using the standard mechanism provided by the Java runtime, that is, ask a ClassLoader
instance to load a resource URL
or obtain an InputStream
that points to the resource.But the code can get quite verbose, take for example the following view code that locates a text file and displays it on a text componentscrollPane {
textArea(columns: 40, rows: 20,
text: this.class.classLoader.getResource('someTextFile.txt').text)
}
In order to reduce visual clutter, also to provide an abstraction over resource location, both GriffonApplication
and GriffonArtifact
have a new pair of methods that simply working with resources. Those methods are provided by ResourceHandler
:
URL getResourceAsURL(String resourceName)
InputStream getResourceAsStream(String resourceName)
List<URL> getResources(String resourceName)
Thus, the previous example can be rewritten this wayscrollPane {
textArea(columns: 40, rows: 20,
text: getResourceAsURL('someTextFile.txt').text)
}
In the future Griffon may switch to a modularized runtime, this abstraction will shield you from any problems when the underlying mechanism changes.These methods can be attached to any non-artifact class at compile time if you apply the @griffon.transform.ResourcesAware AST transformation.