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.runtime.config;
020:
021: import java.util.ResourceBundle;
022: import java.util.HashMap;
023: import java.util.Enumeration;
024: import java.text.MessageFormat;
025:
026: import org.apache.beehive.netui.databinding.datagrid.api.DataGridResourceProvider;
027:
028: /**
029: * <p>
030: * Default implementation of the data grid's {@link DataGridResourceProvider} class. This class returns
031: * string resources from the default resource bundle:
032: * <pre>
033: * org.apache.beehive.netui.databinding.datagrid.runtime.util.data-grid-default.properties
034: * </pre>
035: * If resource bundle chaining is enabled, this class will first search the user-provided resource
036: * bundle. If a message matching the key is found, it will be returned. If no message matching a key is found,
037: * the default resource bundle will be searched. To enable chaining, set the property:
038: * {@link DataGridResourceProvider#setEnableChaining(boolean)}.
039: * </p>
040: */
041: class DefaultDataGridResourceProvider extends DataGridResourceProvider {
042:
043: private static final Object VALUE_PLACEHOLDER = new Object();
044: private static final String DEFAULT_RESOURCE_BUNDLE = "org.apache.beehive.netui.databinding.datagrid.runtime.util.data-grid-default";
045:
046: private ResourceBundle _defaultResourceBundle = null;
047: private ResourceBundle _resourceBundle = null;
048: private HashMap _resourceBundleKeys = null;
049:
050: /**
051: * Default, package protected constructor. This class should only be constructed via the
052: * {@link DefaultDataGridResourceProvider}.
053: */
054: DefaultDataGridResourceProvider() {
055: }
056:
057: /**
058: * Get a message from the default properties file given a message key. If chaining is enabled,
059: * this method will first check the specified resource bundle and then fall back to the
060: * default bundle.
061: * @param key the key
062: * @return the message
063: */
064: public String getMessage(String key) {
065: return internalGetMessage(key);
066: }
067:
068: /**
069: * Format a message associated with the given key and with the given message arguments.
070: * @param key the key
071: * @param args the formatting arguments
072: * @return the formatted message
073: */
074: public String formatMessage(String key, Object[] args) {
075: String msg = internalFormatMessage(getMessage(key), args);
076: return msg;
077: }
078:
079: /**
080: * Lookup a message given a message key. If chaining is enabled via
081: * {@link DataGridResourceProvider#setEnableChaining(boolean)}, the outer resource bundle will be searched,
082: * first. If no message matching the message key is found, the default resource bundle is searched.
083: * @param key the key
084: * @return the message.
085: */
086: private final String internalGetMessage(String key) {
087: if (getResourceBundlePath() == null)
088: return getDefaultMessage(key);
089: else {
090: /* ensure that the correct resource bundles are created */
091: if (_resourceBundle == null) {
092: _resourceBundle = createResourceBundle(getResourceBundlePath());
093: }
094:
095: if (isEnableChaining() && _resourceBundleKeys == null) {
096: Enumeration e = _resourceBundle.getKeys();
097: while (e.hasMoreElements())
098: _resourceBundleKeys.put(e.nextElement(),
099: VALUE_PLACEHOLDER);
100: }
101:
102: if (!isEnableChaining()
103: || _resourceBundleKeys.containsKey(key))
104: return _resourceBundle.getString(key);
105: else
106: return _defaultResourceBundle.getString(key);
107: }
108: }
109:
110: /**
111: * Get a message with the given key from the default message bundle.
112: * @param key the key
113: * @return the message
114: */
115: private String getDefaultMessage(String key) {
116: if (_defaultResourceBundle == null)
117: _defaultResourceBundle = createResourceBundle(DEFAULT_RESOURCE_BUNDLE);
118: return _defaultResourceBundle.getString(key);
119: }
120:
121: }
|