001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.netui.databinding.datagrid.api;
020:
021: import java.util.Locale;
022: import java.util.ResourceBundle;
023: import java.text.MessageFormat;
024:
025: /**
026: * <p>
027: * This abstract class provides an abstraction used for obtaining messages and strings used
028: * during data grid rendering.
029: * </p>
030: */
031: public abstract class DataGridResourceProvider {
032:
033: private boolean _enableChaining = false;
034: private String _resourceBundlePath = null;
035: private Locale _locale = null;
036:
037: /**
038: * Abstract method used to obtain a message {@link String} given a <code>key</code>
039: *
040: * @param key the key used to lookup a {@link String}
041: * @return the returned String if a message is found matching the key
042: */
043: public abstract String getMessage(String key);
044:
045: /**
046: * Abstract method used to format a pattern given a pattern / message key and an array of
047: * arguments.
048: *
049: * @param key the key to use when looking up a message to format
050: * @param args the arguments to use when formatting a message
051: * @return the formatted message if a message is found matching the key
052: */
053: public abstract String formatMessage(String key, Object[] args);
054:
055: /**
056: * Accessor for determining if implementations are chaining enabled. When chaining
057: * is enabled, subclasses must use any nested DataGridResourceProvider instances to
058: * lookup messages. When chaining is enabled, the default messages for the data grid
059: * will be returned. When chaining is disabled, implementations are free to
060: * hide message keys.
061: *
062: * @return <code>true</code> if chaining is enabled; <code>false</code> otherwise
063: */
064: public boolean isEnableChaining() {
065: return _enableChaining;
066: }
067:
068: /**
069: * Setter for enabling or disabling chaining
070: *
071: * @param enableChaining the new chaining enabled value
072: */
073: public void setEnableChaining(boolean enableChaining) {
074: _enableChaining = enableChaining;
075: }
076:
077: /**
078: * Set the {@link Locale} in which a message {@link String} should be looked up.
079: *
080: * @param locale the {@link Locale} to use
081: */
082: public void setLocale(Locale locale) {
083: _locale = locale;
084: }
085:
086: /**
087: * Accessor for obtaining the {@link Locale} used when looking up messages.
088: *
089: * @return the {@link Locale} used for message lookup or <code>null</code> if no {@link Locale} was set
090: */
091: public Locale getLocale() {
092: return _locale;
093: }
094:
095: /**
096: * Set the path used for creating a {@link ResourceBundle} object.
097: *
098: * @param resourceBundlePath the path to a resource bundle
099: */
100: public void setResourceBundlePath(String resourceBundlePath) {
101: _resourceBundlePath = resourceBundlePath;
102: }
103:
104: /**
105: * Accessor for obtaining the path to the resource bundle used by a DataGridResourceProvider
106: * implementation.
107: *
108: * @return the path to the {@link ResourceBundle}
109: */
110: public String getResourceBundlePath() {
111: return _resourceBundlePath;
112: }
113:
114: protected ResourceBundle createResourceBundle(String path) {
115: ResourceBundle rb = ResourceBundle.getBundle(path, getLocale(),
116: Thread.currentThread().getContextClassLoader());
117: return rb;
118: }
119:
120: /**
121: * Internal convenience method that is used to format a message given a pattern
122: * and a set of arguments.
123: *
124: * @param pattern the pattern to format
125: * @param args the arguments to use when formatting
126: * @return the formatted string
127: */
128: protected String internalFormatMessage(String pattern, Object[] args) {
129: /* todo: perf -- could the MessageFormat objects be cached? */
130: MessageFormat format = new MessageFormat(pattern);
131: String msg = format.format(args).toString();
132: return msg;
133: }
134: }
|