KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > databinding > datagrid > api > DataGridResourceProvider


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.api;
19
20 import java.util.Locale JavaDoc;
21 import java.util.ResourceBundle JavaDoc;
22 import java.text.MessageFormat JavaDoc;
23
24 /**
25  * <p>
26  * This abstract class provides an abstraction used for obtaining messages and strings used
27  * during data grid rendering.
28  * </p>
29  */

30 public abstract class DataGridResourceProvider {
31
32     private boolean _enableChaining = false;
33     private String JavaDoc _resourceBundlePath = null;
34     private Locale JavaDoc _locale = null;
35
36     /**
37      * Abstract method used to obtain a message {@link String} given a <code>key</code>
38      *
39      * @param key the key used to lookup a {@link String}
40      * @return the returned String if a message is found matching the key
41      */

42     public abstract String JavaDoc getMessage(String JavaDoc key);
43
44     /**
45      * Abstract method used to format a pattern given a pattern / message key and an array of
46      * arguments.
47      *
48      * @param key the key to use when looking up a message to format
49      * @param args the arguments to use when formatting a message
50      * @return the formatted message if a message is found matching the key
51      */

52     public abstract String JavaDoc formatMessage(String JavaDoc key, Object JavaDoc[] args);
53
54     /**
55      * Accessor for determining if implementations are chaining enabled. When chaining
56      * is enabled, subclasses must use any nested DataGridResourceProvider instances to
57      * lookup messages. When chaining is enabled, the default messages for the data grid
58      * will be returned. When chaining is disabled, implementations are free to
59      * hide message keys.
60      *
61      * @return <code>true</code> if chaining is enabled; <code>false</code> otherwise
62      */

63     public boolean isEnableChaining() {
64         return _enableChaining;
65     }
66
67     /**
68      * Setter for enabling or disabling chaining
69      *
70      * @param enableChaining the new chaining enabled value
71      */

72     public void setEnableChaining(boolean enableChaining) {
73         _enableChaining = enableChaining;
74     }
75
76     /**
77      * Set the {@link Locale} in which a message {@link String} should be looked up.
78      *
79      * @param locale the {@link Locale} to use
80      */

81     public void setLocale(Locale JavaDoc locale) {
82         _locale = locale;
83     }
84
85     /**
86      * Accessor for obtaining the {@link Locale} used when looking up messages.
87      *
88      * @return the {@link Locale} used for message lookup or <code>null</code> if no {@link Locale} was set
89      */

90     public Locale JavaDoc getLocale() {
91         return _locale;
92     }
93
94     /**
95      * Set the path used for creating a {@link ResourceBundle} object.
96      *
97      * @param resourceBundlePath the path to a resource bundle
98      */

99     public void setResourceBundlePath(String JavaDoc resourceBundlePath) {
100         _resourceBundlePath = resourceBundlePath;
101     }
102
103     /**
104      * Accessor for obtaining the path to the resource bundle used by a DataGridResourceProvider
105      * implementation.
106      *
107      * @return the path to the {@link ResourceBundle}
108      */

109     public String JavaDoc getResourceBundlePath() {
110         return _resourceBundlePath;
111     }
112
113     protected ResourceBundle JavaDoc createResourceBundle(String JavaDoc path) {
114         ResourceBundle JavaDoc rb = ResourceBundle.getBundle(path, getLocale(), Thread.currentThread().getContextClassLoader());
115         return rb;
116     }
117
118     /**
119      * Internal convenience method that is used to format a message given a pattern
120      * and a set of arguments.
121      *
122      * @param pattern the pattern to format
123      * @param args the arguments to use when formatting
124      * @return the formatted string
125      */

126     protected String JavaDoc internalFormatMessage(String JavaDoc pattern, Object JavaDoc[] args) {
127         /* todo: perf -- could the MessageFormat objects be cached? */
128         MessageFormat JavaDoc format = new MessageFormat JavaDoc(pattern);
129         String JavaDoc msg = format.format(args).toString();
130         return msg;
131     }
132 }
133
Popular Tags