01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: *
17: * $Header:$
18: */
19: package org.apache.beehive.controls.runtime.bean;
20:
21: import java.util.Locale;
22: import java.util.MissingResourceException;
23: import java.util.ResourceBundle;
24:
25: /**
26: * The ControlBeanInfo class is an abstract base class for the JavaBean BeanInfo classes generated
27: * to support Beehive controls. It is used to bundle helper code common across all generated
28: * BeanInfo classes.
29: */
30: abstract public class ControlBeanInfo extends java.beans.SimpleBeanInfo {
31: /**
32: * Protected constructor that is called from generated BeanInfo subclasses.
33: * @param beanClass the JavaBean class for which BeanInfo is being provided.
34: */
35: protected ControlBeanInfo(Class beanClass) {
36: super ();
37: _beanClass = beanClass;
38: }
39:
40: /*
41: * Gets a possibly-localized string for the given input string.
42: * @param input This the the string that may be localizable. If it is of the form
43: * "%foo.bar.Baz%", then the resource Baz will be looked up in the foo.bar bundle using
44: * standard ResourceBundle rules for the default Locale using the control's classloader.
45: * If the input does not start and end with '%', or if the bundle is not located, the string
46: * will be returned verbatim.
47: * @return the string to be displayed, or the input string if no resource is found.
48: */
49: final protected String localizeString(String input) {
50: if (input == null || !input.startsWith("%")
51: || !input.endsWith("%"))
52: return input;
53: String bundleName = input.substring(1, input.length() - 1);
54: String resourceName = null;
55: int lastDot = bundleName.lastIndexOf('.');
56: while (lastDot != -1 && lastDot != 0
57: && (lastDot + 1 < bundleName.length())) {
58: // move last element from bundle to resource. foo.bar.Baz could be the
59: // Baz property in foo.bar, or the bar.Baz property in foo.
60: if (resourceName == null)
61: resourceName = bundleName.substring(lastDot + 1);
62: else
63: resourceName = bundleName.substring(lastDot + 1) + '.'
64: + resourceName;
65: bundleName = bundleName.substring(0, lastDot);
66:
67: try {
68: ResourceBundle bundle = ResourceBundle.getBundle(
69: bundleName, Locale.getDefault(), _beanClass
70: .getClassLoader());
71: if (bundle != null) {
72: String lookup = bundle.getString(resourceName);
73: if (lookup != null)
74: return lookup;
75: }
76: } catch (MissingResourceException mre) {
77: }
78:
79: lastDot = bundleName.lastIndexOf('.');
80: }
81:
82: return input;
83: }
84:
85: Class _beanClass;
86: }
|