Class ResourceManager
- java.lang.Object
-
- org.jdesktop.application.AbstractBean
-
- org.jdesktop.application.ResourceManager
-
public class ResourceManager extends AbstractBean
The application'sResourceManagerprovides read-only cached access to resources inResourceBundlesvia theResourceMapclass.ResourceManageris a property of theApplicationContextand most applications look up resources relative to it, like this:ApplicationContext appContext = Application.getInstance().getContext(); ResourceMap resourceMap = appContext.getResourceMap(MyClass.class); String msg = resourceMap.getString("msg"); Icon icon = resourceMap.getIcon("icon"); Color color = resourceMap.getColor("color");ApplicationContext.getResourceMap()just delegates to itsResourceManager. TheResourceMapin this example contains resources from the ResourceBundle namedMyClass, and the rest of the chain contains resources shared by the entire application.Resources for a class are defined by an eponymous
ResourceBundlein aresourcessubpackage. The Application class itself may also provide resources. A complete description of the naming conventions for ResourceBundles is provided by thegetResourceMap()method.The mapping from classes and
Applicationto a list ResourceBundle names is handled by two protected methods:getClassBundleNames,getApplicationBundleNames. Subclasses could override these methods to append additional ResourceBundle names to the default lists.
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedResourceManager(ApplicationContext context)Construct aResourceManager.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected ResourceMapcreateResourceMap(java.lang.ClassLoader classLoader, ResourceMap parent, java.util.List<java.lang.String> bundleNames)Called bygetResourceMap(java.lang.Class, java.lang.Class)to constructResourceMaps.java.util.List<java.lang.String>getApplicationBundleNames()The names of the ResourceBundles to be shared by the entire application.protected java.util.List<java.lang.String>getClassBundleNames(java.lang.Class cls)Map from a class to a list of the names of theResourceBundlesspecific to the class.protected ApplicationContextgetContext()java.lang.StringgetPlatform()The value of the special Application ResourceMap resource named "platform".ResourceMapgetResourceMap()Returns the chain of ResourceMaps that's shared by the entire application, beginning with the resources defined for the application's class, i.e.ResourceMapgetResourceMap(java.lang.Class cls)Return the ResourcedMap chain for the specified class.ResourceMapgetResourceMap(java.lang.Class startClass, java.lang.Class stopClass)Returns achainofResourceMapsthat encapsulate theResourceBundlesfor each class fromstartClassto (including)stopClass.voidsetApplicationBundleNames(java.util.List<java.lang.String> bundleNames)Specify the names of the ResourceBundles to be shared by the entire application.voidsetPlatform(java.lang.String platform)Defines the value of the special Application ResourceMap resource named "platform".-
Methods inherited from class org.jdesktop.application.AbstractBean
addPropertyChangeListener, addPropertyChangeListener, firePropertyChange, firePropertyChange, getPropertyChangeListeners, removePropertyChangeListener, removePropertyChangeListener
-
-
-
-
Constructor Detail
-
ResourceManager
protected ResourceManager(ApplicationContext context)
Construct aResourceManager. Typically applications will not create a ResourceManager directly, they'll retrieve the shared one from theApplicationContextwith:Application.getInstance().getContext().getResourceManager()
Or just look upResourceMapswith the ApplicationContext convenience method:Application.getInstance().getContext().getResourceMap(MyClass.class)
FIXME - @param javadoc
-
-
Method Detail
-
getContext
protected final ApplicationContext getContext()
-
getResourceMap
public ResourceMap getResourceMap(java.lang.Class startClass, java.lang.Class stopClass)
Returns achainofResourceMapsthat encapsulate theResourceBundlesfor each class fromstartClassto (including)stopClass. The final link in the chain is Application ResourceMap chain, i.e. the value ofgetResourceMap().The ResourceBundle names for the chain of ResourceMaps are defined by
getClassBundleNames(java.lang.Class)andgetApplicationBundleNames(). Collectively they define the standard location forResourceBundlesfor a particular class as theresourcessubpackage. For example, the ResourceBundle for the single classcom.myco.MyScreen, would be namedcom.myco.resources.MyScreen. Typical ResourceBundles are ".properties" files, so:com/foo/bar/resources/MyScreen.properties. The following table is a list of the ResourceMaps and their constituent ResourceBundles for the same example:ResourceMap chain for class MyScreen in MyApp ResourceMap ResourceBundle names Typical ResourceBundle files 1 class: com.myco.MyScreen com.myco.resources.MyScreen com/myco/resources/MyScreen.properties 2/td> application: com.myco.MyApp com.myco.resources.MyApp com/myco/resources/MyApp.properties 3 application: javax.swing.application.Application javax.swing.application.resources.Application javax.swing.application.resources.Application.properties None of the ResourceBundles are required to exist. If more than one ResourceBundle contains a resource with the same name then the one earlier in the list has precedence
ResourceMaps are constructed lazily and cached. One ResourceMap is constructed for each sequence of classes in the same package.
- Parameters:
startClass- the first class whose ResourceBundles will be includedstopClass- the last class whose ResourceBundles will be included- Returns:
- a
ResourceMapchain that contains resources loaded fromResourceBundlesfound in the resources subpackage for each class. - See Also:
getClassBundleNames(java.lang.Class),getApplicationBundleNames(),ResourceMap.getParent(),ResourceMap.getBundleNames()
-
getResourceMap
public final ResourceMap getResourceMap(java.lang.Class cls)
Return the ResourcedMap chain for the specified class. This is just a convenince method, it's the same as:getResourceMap(cls, cls).- Parameters:
cls- the class that defines the location of ResourceBundles- Returns:
- a
ResourceMapthat contains resources loaded fromResourceBundlesfound in the resources subpackage of the specified class's package. - See Also:
getResourceMap(Class, Class)
-
getResourceMap
public ResourceMap getResourceMap()
Returns the chain of ResourceMaps that's shared by the entire application, beginning with the resources defined for the application's class, i.e. the value of the ApplicationContextapplicationClassproperty. If theapplicationClassproperty has not been set, e.g. because the application has not beenlaunchedyet, then a ResourceMap for justApplication.classis returned.- Returns:
- the Application's ResourceMap
- See Also:
ApplicationContext.getResourceMap(),ApplicationContext.getApplicationClass()
-
getApplicationBundleNames
public java.util.List<java.lang.String> getApplicationBundleNames()
The names of the ResourceBundles to be shared by the entire application. The list is in priority order: resources defined by the first ResourceBundle shadow resources with the the same name that come later.The default value for this property is a list of
per-classResourceBundle names, beginning with theApplication'sclass and of each of its superclasses, up toApplication.class. For example, if the Application's class wascom.foo.bar.MyApp, and MyApp was a subclass ofSingleFrameApplication.class, then the ResourceBundle names would be:- com.foo.bar.resources.MyApp
- javax.swing.application.resources.SingleFrameApplication
- javax.swing.application.resources.Application
The default value of this property is computed lazily and cached. If it's reset, then all ResourceMaps cached by
getResourceMapwill be updated.
-
setApplicationBundleNames
public void setApplicationBundleNames(java.util.List<java.lang.String> bundleNames)
Specify the names of the ResourceBundles to be shared by the entire application. More information about the property is provided by thegetApplicationBundleNames()method.
-
getClassBundleNames
protected java.util.List<java.lang.String> getClassBundleNames(java.lang.Class cls)
Map from a class to a list of the names of theResourceBundlesspecific to the class. The list is in priority order: resources defined by the first ResourceBundle shadow resources with the the same name that come later.By default this method returns one ResourceBundle whose name is the same as the class's name, but in the
"resources"subpackage.For example, given a class named
com.foo.bar.MyClass, the ResourceBundle name would be"com.foo.bar.resources.MyClass". If MyClass is an inner class, only its "simple name" is used. For example, given an inner class namedcom.foo.bar.OuterClass$InnerClass, the ResourceBundle name would be"com.foo.bar.resources.InnerClass".This method is used by the
getResourceMapmethods to compute the list of ResourceBundle names for a newResourceMap. ResourceManager subclasses can override this method to add additional class-specific ResourceBundle names to the list.- Parameters:
cls- the named ResourceBundles are specific tocls.- Returns:
- the names of the ResourceBundles to be loaded for
cls - See Also:
getResourceMap(java.lang.Class, java.lang.Class),getApplicationBundleNames()
-
createResourceMap
protected ResourceMap createResourceMap(java.lang.ClassLoader classLoader, ResourceMap parent, java.util.List<java.lang.String> bundleNames)
Called bygetResourceMap(java.lang.Class, java.lang.Class)to constructResourceMaps. By default this method is effectively just:return new ResourceMap(parent, classLoader, bundleNames);
Custom ResourceManagers might override this method to construct their own ResourceMap subclasses.
-
getPlatform
public java.lang.String getPlatform()
The value of the special Application ResourceMap resource named "platform". By default the value of this resource is "osx" if the underlying operating environment is Apple OSX or "default".- Returns:
- the value of the platform resource
- See Also:
setPlatform(java.lang.String)
-
setPlatform
public void setPlatform(java.lang.String platform)
Defines the value of the special Application ResourceMap resource named "platform". This resource can be used to define platform specific resources. For example:myLabel.text.osx = A value that's appropriate for OSX myLabel.text.default = A value for other platforms myLabel.text = myLabel.text.${platform}By default the value of this resource is "osx" if the underlying operating environment is Apple OSX or "default". To distinguish other platforms one can reset this property based on the value of the
"os.name"system property.This method should be called as early as possible, typically in the Application
initializemethod.- See Also:
getPlatform(),System.getProperty(java.lang.String)
-
-