Using Resources
Resources are a central part of the Genuine framework. They are collected in property files and used both to configure action, buttons, messages, etc, and to identify the corresponding objects at runtime.
Every resource has a name and a type. Several resources with the same name are bundled into a resource item. A resource item may consist of resources of any of the following types:
- Text (text)
- Image (image)
- Large image (largeimage)
- Tooltip (tooltip)
- Keyboard accelerator (key)
- Title (title)
- Type (type)
- Choices (choices)
Depending on what a resource item is used for, different kinds of resources may be combined into a resource item. Here are some of examples that are defined in Genuine's default property file:
cancel.text=Cancel cancel.image=crystal/16/actions/no.png copy.text=&Copy copy.image=crystal/16/actions/editcopy.png copy.key=control C copy.tooltip=Copy exit.confirm.type=warning exit.confirm.title=Exit the application exit.confirm.text=Would you really like to exit the application? exit.confirm.choices=yes, no yes.text=Yes no.text=No
The resource item cancel is mainly used for configuring a button with both a text and an image. The resource item copy is typically used for a menu item or a toolbar button and additionally has a keyboard accelerator and a tooltip. The resource item exit.confirm is used to show a message to the user. It has the message type warning, a title for the message box, the message text, and a list of other resource items that identify the buttons the user may choose from to close the message box.
Using properties, an internationalized application is easy to achieve. For each language, a property file needs to be created that contains the properties for this language. The naming of the files is defined by the Java plattform. Example: The default resources are kept in the property file default.properties, the German translations of all texts are kept in the property file default_de.properties.
As it is common in Java, a property file for a specific language only has to contain those resource values that differ. In particular, resource for images and keyboard shortcut don't have to be repeated. The following resources are part of the German default property file and redefine the resource items from the example above:
cancel.text=Abbrechen copy.text=&Kopieren copy.tooltip=Kopiert Daten exit.confirm.title=Anwendung beenden exit.confirm.text=Wollen Sie die Anwendung wirklich beenden?
To include a property file in an application, you need to add it to the classpath of the application and mention the file in the application configuration data. The following example is taken from the example application:
<application resourceitem="application"> <size width="800" height="600"/> <resources> <resource>crm</resource> </resources> <!-- Many more... --> </application>
The default resources are always read in by the framework. The example application provides an additional property file called crm.properties. You may add arbitrary many resource files. It is your own responsibility that there are no name clashes between the resource items from different property files.
To access a resource item in the application, you use the Resource Manager, a singleton class. Its most important method is getResourceItem by which you may fetch a resource item that may be defined in any property file. Note the following example:
ResourceItem resourceItem = resourceManager.getResourceItem("exit.confirm");
You should always consider to define static constants instead of passing strings directly as in the example shown.