001: /**
002: * $RCSfile$
003: * $Revision: 1217 $
004: * $Date: 2005-04-11 14:11:06 -0700 (Mon, 11 Apr 2005) $
005: *
006: * Copyright (C) 2004 Jive Software. All rights reserved.
007: *
008: * This software is published under the terms of the GNU Public License (GPL),
009: * a copy of which is included in this distribution.
010: */package org.jivesoftware.util;
011:
012: import java.beans.*;
013: import java.util.Enumeration;
014: import java.util.Locale;
015: import java.util.ResourceBundle;
016:
017: /**
018: * An abstract BeanInfo implementation that automatically constructs
019: * PropertyDescriptors and handles i18n through ResourceBundles.
020: *
021: * @author Jive Software
022: * @see java.beans.BeanInfo
023: */
024: public abstract class JiveBeanInfo implements BeanInfo {
025:
026: private ResourceBundle bundle;
027:
028: public JiveBeanInfo() {
029: //Get the locale that should be used, then load the resource bundle.
030: Locale currentLocale = JiveGlobals.getLocale();
031: try {
032: bundle = ResourceBundle.getBundle("bean_" + getName(),
033: currentLocale);
034: } catch (Exception e) {
035: // Ignore any exception when trying to load bundle.
036: }
037: }
038:
039: /**
040: * Returns the names of the properties of the bean that should be exposed.
041: *
042: * @return the names of the properties that should be exposed.
043: */
044: public abstract String[] getPropertyNames();
045:
046: /**
047: * Returns the bean Class.
048: *
049: * @return the Class of the JavaBean that the BeanInfo is for.
050: */
051: public abstract Class getBeanClass();
052:
053: /**
054: * Returns the name of the class that the bean info applies to (which
055: * corresponds to the resource bundle that will be loaded). For example,
056: * for the class <tt>com.foo.ExampleClass</tt>, the name would be
057: * <tt>ExampleClass</tt>.
058: *
059: * @return the name of the JavaBean that the BeanInfo is for.
060: */
061: public abstract String getName();
062:
063: // BeanInfo Interface
064:
065: public BeanDescriptor getBeanDescriptor() {
066: BeanDescriptor descriptor = new BeanDescriptor(getBeanClass());
067: try {
068: // Attempt to load the displayName and shortDescription explicitly.
069: String displayName = bundle.getString("displayName");
070: if (displayName != null) {
071: descriptor.setDisplayName(displayName);
072: }
073: String shortDescription = bundle
074: .getString("shortDescription");
075: if (shortDescription != null) {
076: descriptor.setShortDescription(shortDescription);
077: }
078: // Add any other properties that are specified.
079: Enumeration enumeration = bundle.getKeys();
080: while (enumeration.hasMoreElements()) {
081: String key = (String) enumeration.nextElement();
082: String value = bundle.getString(key);
083: if (value != null) {
084: descriptor.setValue(key, value);
085: }
086: }
087: } catch (Exception e) {
088: // Ignore any exceptions. We may get some if we try to load a
089: // a property that doesn't appear in the resource bundle.
090: }
091: return descriptor;
092: }
093:
094: public PropertyDescriptor[] getPropertyDescriptors() {
095: Class beanClass = getBeanClass();
096: String[] properties = getPropertyNames();
097:
098: PropertyDescriptor[] descriptors = new PropertyDescriptor[properties.length];
099: try {
100: // For each property, create a property descriptor and set the
101: // name and description using the localized data.
102: for (int i = 0; i < descriptors.length; i++) {
103: PropertyDescriptor newDescriptor = new PropertyDescriptor(
104: properties[i], beanClass);
105: if (bundle != null) {
106: newDescriptor.setDisplayName(bundle
107: .getString(properties[i] + ".displayName"));
108: newDescriptor.setShortDescription(bundle
109: .getString(properties[i]
110: + ".shortDescription"));
111: }
112: descriptors[i] = newDescriptor;
113: }
114: return descriptors;
115: } catch (IntrospectionException ie) {
116: Log.error(ie);
117: throw new Error(ie.toString());
118: }
119: }
120:
121: public int getDefaultPropertyIndex() {
122: return -1;
123: }
124:
125: public EventSetDescriptor[] getEventSetDescriptors() {
126: return null;
127: }
128:
129: public int getDefaultEventIndex() {
130: return -1;
131: }
132:
133: public MethodDescriptor[] getMethodDescriptors() {
134: return null;
135: }
136:
137: public BeanInfo[] getAdditionalBeanInfo() {
138: return null;
139: }
140:
141: public java.awt.Image getIcon(int iconKind) {
142: return null;
143: }
144: }
|