View Javadoc

1   /*
2    * Copyright 2006-2016 The JGUIraffe Team.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License")
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.sf.jguiraffe.resources.impl;
17  
18  import java.util.Locale;
19  import java.util.MissingResourceException;
20  
21  import net.sf.jguiraffe.resources.ResourceGroup;
22  import net.sf.jguiraffe.resources.ResourceLoader;
23  import net.sf.jguiraffe.resources.ResourceManager;
24  
25  /**
26   * <p>
27   * A default implementation of the <code>ResourceManager</code> interface.
28   * </p>
29   * <p>
30   * This class provides a fully functional <code>ResourceManager</code>
31   * implementation that can be used as is. There is usually no need to subclass
32   * this class or use a different implementation.
33   * </p>
34   * <p>
35   * The class uses the associated <code>ResourceLoader</code> to retrieve
36   * requested resources or resource groups. No caching is performed, this can be
37   * done in the <code>ResourceLoader</code>.
38   * </p>
39   * <p>Implementation note: This class is thread-safe.</p>
40   *
41   * @author Oliver Heger
42   * @version $Id: ResourceManagerImpl.java 205 2012-01-29 18:29:57Z oheger $
43   */
44  public class ResourceManagerImpl implements ResourceManager
45  {
46      /** Stores the associated resource loader object. */
47      private volatile ResourceLoader resourceLoader;
48  
49      /** Stores the name of the default resource group. */
50      private volatile Object defaultResourceGroup;
51  
52      /**
53       * Creates a new instance of <code>ResourceManagerImpl</code>.
54       */
55      public ResourceManagerImpl()
56      {
57          this(null);
58      }
59  
60      /**
61       * Creates a new instance of <code>ResourceManagerImpl</code> and
62       * initializes the associated resource loader.
63       *
64       * @param loader the resource loader to use
65       */
66      public ResourceManagerImpl(ResourceLoader loader)
67      {
68          setResourceLoader(loader);
69      }
70  
71      /**
72       * Returns the specified resource.
73       *
74       * @param locale the <code>Locale</code>
75       * @param group the owning resource group's name
76       * @param key the resource key
77       * @return the found resource
78       * @throws MissingResourceException if the resource cannot be found
79       */
80      public Object getResource(Locale locale, Object group, Object key)
81      {
82          return getResourceGroup(locale,
83                  (group != null) ? group : getDefaultResourceGroup())
84                  .getResource(key);
85      }
86  
87      /**
88       * Returns the specified resource group.
89       *
90       * @param locale the <code>Locale</code> of the group
91       * @param group the group's name
92       * @return the specified resource group
93       * @throws MissingResourceException if the group cannot be found
94       */
95      public ResourceGroup getResourceGroup(Locale locale, Object group)
96      {
97          return fetchLoader().loadGroup(locale, group);
98      }
99  
100     /**
101      * Returns the text of the specified resource.
102      *
103      * @param locale the <code>Locale</code>
104      * @param group the name of the resource group
105      * @param key the resource key
106      * @return the text of the specified resource
107      * @throws MissingResourceException if the resource cannot be found
108      */
109     public String getText(Locale locale, Object group, Object key)
110             throws MissingResourceException
111     {
112         return getResource(locale, group, key).toString();
113     }
114 
115     /**
116      * Returns the associated <code>ResourceLoader</code> object.
117      *
118      * @return the <code>ResourceLoader</code>
119      */
120     public ResourceLoader getResourceLoader()
121     {
122         return resourceLoader;
123     }
124 
125     /**
126      * Sets the <code>ResourceLoader</code> to use. Requests for resource
127      * groups are delegated to this object.
128      *
129      * @param resourceLoader the <code>ResourceLoader</code> to use
130      */
131     public void setResourceLoader(ResourceLoader resourceLoader)
132     {
133         this.resourceLoader = resourceLoader;
134     }
135 
136     /**
137      * Fetches the resource loader. This method is called whenever access to a
138      * resource loader is needed. It checks if a resource loader is defined and
139      * if not, throws an exception.
140      *
141      * @return the resource loader to use
142      */
143     protected ResourceLoader fetchLoader()
144     {
145         ResourceLoader loader = getResourceLoader();
146         if (loader == null)
147         {
148             throw new IllegalStateException("No ResourceLoader defined!");
149         }
150         return getResourceLoader();
151     }
152 
153     /**
154      * Returns the name of the default resource group.
155      *
156      * @return the name of the default resource group
157      */
158     public Object getDefaultResourceGroup()
159     {
160         return defaultResourceGroup;
161     }
162 
163     /**
164      * Sets the name of the default resource group.
165      *
166      * @param grp the name of the default resource group
167      */
168     public void setDefaultResourceGroup(Object grp)
169     {
170         defaultResourceGroup = grp;
171     }
172 }