KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > util > JiveBeanInfo


1 /**
2  * $RCSfile: JiveBeanInfo.java,v $
3  * $Revision: 1.3 $
4  * $Date: 2005/04/11 21:05:19 $
5  *
6  * Copyright (C) 2004 Jive Software. All rights reserved.
7  *
8  * This software is published under the terms of the GNU Public License (GPL),
9  * a copy of which is included in this distribution.
10  */

11
12 package org.jivesoftware.util;
13
14 import java.beans.*;
15 import java.util.Enumeration JavaDoc;
16 import java.util.Locale JavaDoc;
17 import java.util.ResourceBundle JavaDoc;
18
19 /**
20  * An abstract BeanInfo implementation that automatically constructs
21  * PropertyDescriptors and handles i18n through ResourceBundles.
22  *
23  * @author Jive Software
24  * @see java.beans.BeanInfo
25  */

26 public abstract class JiveBeanInfo implements BeanInfo {
27
28     private ResourceBundle JavaDoc bundle;
29
30     public JiveBeanInfo() {
31         //Get the locale that should be used, then load the resource bundle.
32
Locale JavaDoc currentLocale = JiveGlobals.getLocale();
33         try {
34             bundle = ResourceBundle.getBundle("bean_" + getName(),
35                     currentLocale);
36         }
37         catch (Exception JavaDoc e) {
38             // Ignore any exception when trying to load bundle.
39
}
40     }
41
42     /**
43      * Returns the names of the properties of the bean that should be exposed.
44      *
45      * @return the names of the properties that should be exposed.
46      */

47     public abstract String JavaDoc[] getPropertyNames();
48
49     /**
50      * Returns the bean Class.
51      *
52      * @return the Class of the JavaBean that the BeanInfo is for.
53      */

54     public abstract Class JavaDoc getBeanClass();
55
56     /**
57      * Returns the name of the class that the bean info applies to (which
58      * corresponds to the resource bundle that will be loaded). For example,
59      * for the class <tt>com.foo.ExampleClass</tt>, the name would be
60      * <tt>ExampleClass</tt>.
61      *
62      * @return the name of the JavaBean that the BeanInfo is for.
63      */

64     public abstract String JavaDoc getName();
65
66     // BeanInfo Interface
67

68     public BeanDescriptor getBeanDescriptor() {
69         BeanDescriptor descriptor = new BeanDescriptor(getBeanClass());
70         try {
71             // Attempt to load the displayName and shortDescription explicitly.
72
String JavaDoc displayName = bundle.getString("displayName");
73             if (displayName != null) {
74                 descriptor.setDisplayName(displayName);
75             }
76             String JavaDoc shortDescription = bundle.getString("shortDescription");
77             if (shortDescription != null) {
78                 descriptor.setShortDescription(shortDescription);
79             }
80             // Add any other properties that are specified.
81
Enumeration JavaDoc enumeration = bundle.getKeys();
82             while (enumeration.hasMoreElements()) {
83                 String JavaDoc key = (String JavaDoc)enumeration.nextElement();
84                 String JavaDoc value = bundle.getString(key);
85                 if (value != null) {
86                     descriptor.setValue(key, value);
87                 }
88             }
89         }
90         catch (Exception JavaDoc e) {
91             // Ignore any exceptions. We may get some if we try to load a
92
// a property that doesn't appear in the resource bundle.
93
}
94         return descriptor;
95     }
96
97     public PropertyDescriptor[] getPropertyDescriptors() {
98         Class JavaDoc beanClass = getBeanClass();
99         String JavaDoc[] properties = getPropertyNames();
100
101         PropertyDescriptor[] descriptors = new PropertyDescriptor[properties.length];
102         try {
103             // For each property, create a property descriptor and set the
104
// name and description using the localized data.
105
for (int i = 0; i < descriptors.length; i++) {
106                 PropertyDescriptor newDescriptor =
107                         new PropertyDescriptor(properties[i], beanClass);
108                 if (bundle != null) {
109                     newDescriptor.setDisplayName(bundle.getString(properties[i] + ".displayName"));
110                     newDescriptor.setShortDescription(bundle.getString(properties[i] + ".shortDescription"));
111                 }
112                 descriptors[i] = newDescriptor;
113             }
114             return descriptors;
115         }
116         catch (IntrospectionException ie) {
117             Log.error(ie);
118             throw new Error JavaDoc(ie.toString());
119         }
120     }
121
122     public int getDefaultPropertyIndex() {
123         return -1;
124     }
125
126     public EventSetDescriptor[] getEventSetDescriptors() {
127         return null;
128     }
129
130     public int getDefaultEventIndex() {
131         return -1;
132     }
133
134     public MethodDescriptor[] getMethodDescriptors() {
135         return null;
136     }
137
138     public BeanInfo[] getAdditionalBeanInfo() {
139         return null;
140     }
141
142     public java.awt.Image JavaDoc getIcon(int iconKind) {
143         return null;
144     }
145 }
Popular Tags