Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * Copyright (C) 2006 Sun Microsystems, Inc. All rights reserved. Use is
   * subject to license terms.
  package org.jdesktop.application;
 import java.util.List;
 import java.util.Map;
The application's ResourceManager provides read-only cached access to resources in ResourceBundles via the ResourceMap class. ResourceManager is a property of the ApplicationContext and 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 its ResourceManager. The ResourceMap in this example contains resources from the ResourceBundle named MyClass, and the rest of the chain contains resources shared by the entire application.

Resources for a class are defined by an eponymous ResourceBundle in a resources subpackage. The Application class itself may also provide resources. A complete description of the naming conventions for ResourceBundles is provided by the getResourceMap() method.

The mapping from classes and Application to 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.

 public class ResourceManager extends AbstractBean {
     private static final Logger logger = Logger.getLogger(ResourceManager.class.getName());
     private final Map<StringResourceMapresourceMaps;
     private final ApplicationContext context;
     private List<StringapplicationBundleNames = null;
     private ResourceMap appResourceMap = null;

Construct a ResourceManager. Typically applications will not create a ResourceManager directly, they'll retrieve the shared one from the ApplicationContext with:
Or just look up ResourceMaps with the ApplicationContext convenience method:

     protected ResourceManager(ApplicationContext context) {
         if (context == null) {
             throw new IllegalArgumentException("null context");
         this. = context;
     // FIXME - documentation
     protected final ApplicationContext getContext() {
         return ;
     /* Returns a read-only list of the ResourceBundle names for all of
      * the classes from startClass to (including) stopClass.  The
      * bundle names for each class are #getClassBundleNames(Class).
      * The list is in priority order: resources defined in bundles
      * earlier in the list shadow resources with the same name that
      * appear bundles that come later.
     private List<StringallBundleNames(Class startClassClass stopClass) {
	List<StringbundleNames = new ArrayList<String>();
        Class limitClass = stopClass.getSuperclass(); // could be null
	for(Class c = startClassc != limitClassc = c.getSuperclass()) {
        return Collections.unmodifiableList(bundleNames);
    private String bundlePackageName(String bundleName) {
	int i = bundleName.lastIndexOf(".");
	return (i == -1) ? "" : bundleName.substring(0, i);
    /* Creates a parent chain of ResourceMaps for the specfied
     * ResourceBundle names.  One ResourceMap is created for each
     * subsequence of ResourceBundle names with a common bundle
     * package name, i.e. with a common resourcesDir.  The parent 
     * of the final ResourceMap in the chain is root.
	if (!names.hasNext()) {
	    return root;
	else {
	    String bundleName0 =;
	    String rmBundlePackage = bundlePackageName(bundleName0);
	    List<StringrmNames = new ArrayList<String>();
            while(names.hasNext()) {
                String bundleName =;
                if (rmBundlePackage.equals(bundlePackageName(bundleName))) {
                else {
	    ResourceMap parent = createResourceMapChain(clrootnames);
	    return createResourceMap(clparentrmNames);
    /* Lazily creates the Application ResourceMap chain,
     * appResourceMap.  If the Application hasn't been launched yet,
     * i.e. if the ApplicationContext applicationClass property hasn't
     * been set yet, then the ResourceMap just corresponds to
     * Application.class.
	if ( == null) {
            List<StringappBundleNames = getApplicationBundleNames();
	    Class appClass = getContext().getApplicationClass();
	    if (appClass == null) {
		.warning("getApplicationResourceMap(): no Application class");
		appClass = Application.class;
	    ClassLoader classLoader = appClass.getClassLoader();
	     = createResourceMapChain(classLoadernullappBundleNames.listIterator());
    /* Lazily creates the ResourceMap chain for the the class from 
     * startClass to stopClass.
    private ResourceMap getClassResourceMap(Class startClassClass stopClass) {
	String classResourceMapKey = startClass.getName() + stopClass.getName();
	ResourceMap classResourceMap = .get(classResourceMapKey);
	if (classResourceMap == null) {
            List<StringclassBundleNames = allBundleNames(startClassstopClass);
            ClassLoader classLoader = startClass.getClassLoader();
            ResourceMap appRM = getResourceMap();
	    classResourceMap = createResourceMapChain(classLoaderappRMclassBundleNames.listIterator());
	return classResourceMap;
Returns a chain of ResourceMaps that encapsulate the ResourceBundles for each class from startClass to (including) stopClass. The final link in the chain is Application ResourceMap chain, i.e. the value of getResourceMap().

The ResourceBundle names for the chain of ResourceMaps are defined by getClassBundleNames(java.lang.Class) and getApplicationBundleNames(). Collectively they define the standard location for ResourceBundles for a particular class as the resources subpackage. For example, the ResourceBundle for the single class com.myco.MyScreen, would be named com.myco.resources.MyScreen. Typical ResourceBundles are ".properties" files, so: com/foo/bar/resources/ The following table is a list of the ResourceMaps and their constituent ResourceBundles for the same example:

ResourceMap chain for class MyScreen in MyApp
ResourceMapResourceBundle namesTypical ResourceBundle files
1class: com.myco.MyScreencom.myco.resources.MyScreencom/myco/resources/
2/td> application: com.myco.MyAppcom.myco.resources.MyAppcom/myco/resources/

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.

startClass the first class whose ResourceBundles will be included
stopClass the last class whose ResourceBundles will be included
a ResourceMap chain that contains resources loaded from ResourceBundles found in the resources subpackage for each class.
See also:
    public ResourceMap getResourceMap(Class startClassClass stopClass) {
	if (startClass == null) {
	    throw new IllegalArgumentException("null startClass");
	if (stopClass == null) {
	    throw new IllegalArgumentException("null stopClass");
        if (!stopClass.isAssignableFrom(startClass)) {
	    throw new IllegalArgumentException("startClass is not a subclass, or the same as, stopClass");
        return getClassResourceMap(startClassstopClass);

Return the ResourcedMap chain for the specified class. This is just a convenince method, it's the same as: getResourceMap(cls, cls).

cls the class that defines the location of ResourceBundles
a ResourceMap that contains resources loaded from ResourceBundles found in the resources subpackage of the specified class's package.
See also:
    public final ResourceMap getResourceMap(Class cls) {
	if (cls == null) {
	    throw new IllegalArgumentException("null class");
        return getResourceMap(clscls);

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 ApplicationContext applicationClass property. If the applicationClass property has not been set, e.g. because the application has not been launched yet, then a ResourceMap for just Application.class is returned.

    public ResourceMap getResourceMap() {

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 getClassBundleNames(java.lang.Class) ResourceBundle names, beginning with the Application's class and of each of its superclasses, up to Application.class. For example, if the Application's class was, and MyApp was a subclass of SingleFrameApplication.class, then the ResourceBundle names would be:

  2. javax.swing.application.resources.SingleFrameApplication
  3. 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 getResourceMap will be updated.

	/* Lazily compute an initial value for this property, unless the
	 * application's class hasn't been specified yet.  In that case
	 * we just return a placeholder based on Application.class.
	if ( == null) {
	    Class appClass = getContext().getApplicationClass();
	    if (appClass == null) {
		return allBundleNames(Application.classApplication.class); // placeholder
	    else {

Specify the names of the ResourceBundles to be shared by the entire application. More information about the property is provided by the getApplicationBundleNames() method.

    public void setApplicationBundleNames(List<StringbundleNames) {
	if (bundleNames != null) {
	    for(String bundleName : bundleNames) {
		if ((bundleName == null) || (bundleNames.size() == 0)) {
		    throw new IllegalArgumentException("invalid bundle name \"" + bundleName + "\"");
	if (bundleNames != null) {
	     = Collections.unmodifiableList(new ArrayList(bundleNames));
	else {
	     = null;
    /* Convert a class name to an eponymous resource bundle in the 
     * resources subpackage.  For example, given a class named
     *, the ResourceBundle name would be
     * ""  If MyClass is an inner class,
     * only its "simple name" is used.  For example, given an
     * inner class named$InnerClass, the
     * ResourceBundle name would be "".
     * Although this could result in a collision, creating more
     * complex rules for inner classes would be a burden for
     * developers.
    private String classBundleBaseName(Class cls) {
	String className = cls.getName();
	StringBuffer sb = new StringBuffer();
	int i = className.lastIndexOf('.');
	if (i > 0) {
	    sb.append(className.substring(0, i));
	else {
	return sb.toString();

Map from a class to a list of the names of the ResourceBundles specific 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, the ResourceBundle name would be "". If MyClass is an inner class, only its "simple name" is used. For example, given an inner class named$InnerClass, the ResourceBundle name would be "".

This method is used by the getResourceMap methods to compute the list of ResourceBundle names for a new ResourceMap. ResourceManager subclasses can override this method to add additional class-specific ResourceBundle names to the list.

cls the named ResourceBundles are specific to cls.
the names of the ResourceBundles to be loaded for cls
See also:
    protected List<StringgetClassBundleNames(Class cls) {
	String bundleName = classBundleBaseName(cls);
	return Collections.singletonList(bundleName);

Called by getResourceMap() to construct ResourceMaps. 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.
    protected ResourceMap createResourceMap(ClassLoader classLoaderResourceMap parentList<StringbundleNames) {
	return new ResourceMap(parentclassLoaderbundleNames);

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".

the value of the platform resource
See also:
    public String getPlatform() {
        return getResourceMap().getString("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 "" system property.

This method should be called as early as possible, typically in the Application initialize method.

    public void setPlatform(String platform) {
        if (platform == null) {
            throw new IllegalArgumentException("null platform");
New to GrepCode? Check out our FAQ X