KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > databinding > datagrid > runtime > config > DefaultDataGridResourceProvider


1 /*
2  * Copyright 2004 The Apache Software Foundation.
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  * $Header:$
17  */

18 package org.apache.beehive.netui.databinding.datagrid.runtime.config;
19
20 import java.util.ResourceBundle JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Enumeration JavaDoc;
23 import java.text.MessageFormat JavaDoc;
24
25 import org.apache.beehive.netui.databinding.datagrid.api.DataGridResourceProvider;
26
27 /**
28  * <p>
29  * Default implementation of the data grid's {@link DataGridResourceProvider} class. This class returns
30  * string resources from the default resource bundle:
31  * <pre>
32  * org.apache.beehive.netui.databinding.datagrid.runtime.util.data-grid-default.properties
33  * </pre>
34  * If resource bundle chaining is enabled, this class will first search the user-provided resource
35  * bundle. If a message matching the key is found, it will be returned. If no message matching a key is found,
36  * the default resource bundle will be searched. To enable chaining, set the property:
37  * {@link DataGridResourceProvider#setEnableChaining(boolean)}.
38  * </p>
39  */

40 class DefaultDataGridResourceProvider
41     extends DataGridResourceProvider {
42
43     private static final Object JavaDoc VALUE_PLACEHOLDER = new Object JavaDoc();
44     private static final String JavaDoc DEFAULT_RESOURCE_BUNDLE = "org.apache.beehive.netui.databinding.datagrid.runtime.util.data-grid-default";
45
46     private ResourceBundle JavaDoc _defaultResourceBundle = null;
47     private ResourceBundle JavaDoc _resourceBundle = null;
48     private HashMap JavaDoc _resourceBundleKeys = null;
49
50     /**
51      * Default, package protected constructor. This class should only be constructed via the
52      * {@link DefaultDataGridResourceProvider}.
53      */

54     DefaultDataGridResourceProvider() {
55     }
56
57     /**
58      * Get a message from the default properties file given a message key. If chaining is enabled,
59      * this method will first check the specified resource bundle and then fall back to the
60      * default bundle.
61      * @param key the key
62      * @return the message
63      */

64     public String JavaDoc getMessage(String JavaDoc key) {
65         return internalGetMessage(key);
66     }
67
68     /**
69      * Format a message associated with the given key and with the given message arguments.
70      * @param key the key
71      * @param args the formatting arguments
72      * @return the formatted message
73      */

74     public String JavaDoc formatMessage(String JavaDoc key, Object JavaDoc[] args) {
75         String JavaDoc msg = internalFormatMessage(getMessage(key), args);
76         return msg;
77     }
78
79     /**
80      * Lookup a message given a message key. If chaining is enabled via
81      * {@link DataGridResourceProvider#setEnableChaining(boolean)}, the outer resource bundle will be searched,
82      * first. If no message matching the message key is found, the default resource bundle is searched.
83      * @param key the key
84      * @return the message.
85      */

86     private final String JavaDoc internalGetMessage(String JavaDoc key) {
87         if(getResourceBundlePath() == null)
88             return getDefaultMessage(key);
89         else {
90             /* ensure that the correct resource bundles are created */
91             if(_resourceBundle == null) {
92                 _resourceBundle = createResourceBundle(getResourceBundlePath());
93             }
94
95             if(isEnableChaining() && _resourceBundleKeys == null) {
96                 Enumeration JavaDoc e = _resourceBundle.getKeys();
97                 while(e.hasMoreElements())
98                     _resourceBundleKeys.put(e.nextElement(), VALUE_PLACEHOLDER);
99             }
100
101             if(!isEnableChaining() || _resourceBundleKeys.containsKey(key))
102                 return _resourceBundle.getString(key);
103             else
104                 return _defaultResourceBundle.getString(key);
105         }
106     }
107
108     /**
109      * Get a message with the given key from the default message bundle.
110      * @param key the key
111      * @return the message
112      */

113     private String JavaDoc getDefaultMessage(String JavaDoc key) {
114         if(_defaultResourceBundle == null)
115             _defaultResourceBundle = createResourceBundle(DEFAULT_RESOURCE_BUNDLE);
116         return _defaultResourceBundle.getString(key);
117     }
118
119 }
120
Popular Tags