KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > testbeans > BeanInfoSupport


1 // $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/testbeans/BeanInfoSupport.java,v 1.4 2004/02/11 14:40:51 jsalvata Exp $
2
/*
3  * Copyright 2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * 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 package org.apache.jmeter.testbeans;
18
19 import java.awt.Image JavaDoc;
20 import java.beans.BeanDescriptor JavaDoc;
21 import java.beans.BeanInfo JavaDoc;
22 import java.beans.EventSetDescriptor JavaDoc;
23 import java.beans.IntrospectionException JavaDoc;
24 import java.beans.Introspector JavaDoc;
25 import java.beans.MethodDescriptor JavaDoc;
26 import java.beans.PropertyDescriptor JavaDoc;
27 import java.beans.SimpleBeanInfo JavaDoc;
28 import java.util.MissingResourceException JavaDoc;
29 import java.util.ResourceBundle JavaDoc;
30
31 import org.apache.jmeter.testbeans.gui.GenericTestBeanCustomizer;
32 import org.apache.jmeter.util.JMeterUtils;
33 import org.apache.jorphan.logging.LoggingManager;
34 import org.apache.log.Logger;
35
36 /**
37  * Support class for test bean beanInfo objects. It will help using the
38  * introspector to get most of the information, to then modify it at will.
39  * <p>
40  * To use, subclass it, create a subclass with a parameter-less constructor
41  * that:
42  * <ol>
43  * <li>Calls super(beanClass)
44  * <li>Modifies the property descriptors, bean descriptor, etc. at will.
45  * </ol>
46  * <p>
47  * Even before any such modifications, a resource bundle named xxxResources
48  * (where xxx is the fully qualified bean class name) will be obtained if
49  * available and used to localize the following:
50  * <ul>
51  * <li>Bean's display name -- from property <b>displayName</b>.
52  * <li>Properties' display names -- from properties
53  * <b><i>propertyName</i>.displayName</b>.
54  * <li>Properties' short descriptions -- from properties
55  * <b><i>propertyName</i>.shortDescription</b>.
56  * </ul>
57  * <p>
58  * The resource bundle will be stored as the bean descriptor's "resourceBundle"
59  * attribute, so that it can be used for further localization. TestBeanGUI, for
60  * example, uses it to obtain the group's display names from properties
61  * <b><i>groupName</i>.displayName</b>.
62  *
63  * @author <a HREF="mailto:jsalvata@apache.org">Jordi Salvat i Alabart</a>
64  * @version $Revision: 1.4 $ updated on $Date: 2004/02/11 14:40:51 $
65  */

66 public abstract class BeanInfoSupport extends SimpleBeanInfo JavaDoc
67 {
68
69     private static transient Logger log = LoggingManager.getLoggerForClass();
70
71     // Some known attribute names, just for convenience:
72
public static final String JavaDoc TAGS=
73             GenericTestBeanCustomizer.TAGS;
74     public static final String JavaDoc NOT_UNDEFINED=
75             GenericTestBeanCustomizer.NOT_UNDEFINED;
76     public static final String JavaDoc NOT_EXPRESSION=
77             GenericTestBeanCustomizer.NOT_EXPRESSION;
78     public static final String JavaDoc NOT_OTHER=
79             GenericTestBeanCustomizer.NOT_OTHER;
80     public static final String JavaDoc DEFAULT=
81             GenericTestBeanCustomizer.DEFAULT;
82     public static final String JavaDoc RESOURCE_BUNDLE=
83             GenericTestBeanCustomizer.RESOURCE_BUNDLE;
84
85     /** The class for which we're providing the bean info. */
86     private Class JavaDoc beanClass;
87
88     /** The BeanInfo for our class as obtained by the introspector. */
89     private BeanInfo JavaDoc rootBeanInfo;
90
91     /** The icons for this bean. */
92     private Image JavaDoc[] icons= new Image JavaDoc[5];
93
94     /**
95      * Construct a BeanInfo for the given class.
96      */

97     protected BeanInfoSupport(Class JavaDoc beanClass) {
98         
99         this.beanClass= beanClass;
100
101         try {
102             rootBeanInfo= Introspector.getBeanInfo(
103                 beanClass,
104                 Introspector.IGNORE_IMMEDIATE_BEANINFO);
105         } catch (IntrospectionException JavaDoc e) {
106             log.error("Can't introspect.", e);
107             throw new Error JavaDoc(e.toString()); // Programming error: bail out.
108
}
109     
110         try{
111             ResourceBundle JavaDoc resourceBundle= ResourceBundle.getBundle(
112                 beanClass.getName()+"Resources",
113                 JMeterUtils.getLocale());
114
115             // Store the resource bundle as an attribute of the BeanDescriptor:
116
getBeanDescriptor().setValue(RESOURCE_BUNDLE, resourceBundle);
117
118             // Localize the bean name
119
try
120             {
121                 getBeanDescriptor().setDisplayName(
122                     resourceBundle.getString("displayName"));
123             }
124             catch (MissingResourceException JavaDoc e)
125             {
126                 log.debug(
127                     "Localized display name not available for bean "
128                     +beanClass.getName());
129             }
130             
131             // Localize the property names and descriptions:
132
PropertyDescriptor JavaDoc[] properties= getPropertyDescriptors();
133
134             for (int i=0; i<properties.length; i++)
135             {
136                 String JavaDoc name= properties[i].getName();
137             
138                 try
139                 {
140                     properties[i].setDisplayName(
141                         resourceBundle.getString(name+".displayName"));
142                 }
143                 catch (MissingResourceException JavaDoc e)
144                 {
145                     log.debug(
146                         "Localized display name not available for property "
147                         +name);
148                 }
149             
150                 try
151                 {
152                     properties[i].setShortDescription(
153                         resourceBundle.getString(name+".shortDescription"));
154                 }
155                 catch (MissingResourceException JavaDoc e)
156                 {
157                     log.debug(
158                         "Localized short description not available for property "
159                         +name);
160                 }
161             }
162         }
163         catch (MissingResourceException JavaDoc e)
164         {
165             log.warn("Localized strings not available for bean "+beanClass);
166         }
167     }
168     
169     /**
170      * Get the property descriptor for the property of the given name.
171      *
172      * @param name property name
173      * @return descriptor for a property of that name, or null if there's none
174      */

175     protected PropertyDescriptor JavaDoc property(String JavaDoc name) {
176         PropertyDescriptor JavaDoc[] properties= getPropertyDescriptors();
177         for (int i=0; i<properties.length; i++)
178         {
179             if (properties[i].getName().equals(name)) {
180                 return properties[i];
181             }
182         }
183         return null;
184     }
185
186     /**
187      * Set the bean's 16x16 colour icon.
188      *
189      * @param resourceName A pathname relative to the directory holding the
190      * class file of the current class.
191      */

192     protected void setIcon(String JavaDoc resourceName)
193     {
194         icons[ICON_COLOR_16x16]= loadImage(resourceName);
195     }
196     
197     /** Number of groups created so far by createPropertyGroup. */
198     private int numCreatedGroups= 0;
199     
200     /**
201      * Utility method to group and order properties.
202      * <p>
203      * It will assing the given group name to each of the named properties,
204      * and set their order attribute so that they are shown in the given order.
205      * <p>
206      * The created groups will get order 1, 2, 3,... in the order in which they
207      * are created.
208      *
209      * @param group name of the group
210      * @param names property names in the desired order
211      */

212     protected void createPropertyGroup(String JavaDoc group, String JavaDoc[] names)
213     {
214         for (int i=0; i<names.length; i++)
215         {
216             PropertyDescriptor JavaDoc p= property(names[i]);
217             p.setValue(GenericTestBeanCustomizer.GROUP, group);
218             p.setValue(GenericTestBeanCustomizer.ORDER, new Integer JavaDoc(i));
219         }
220         numCreatedGroups++;
221         getBeanDescriptor().setValue(
222             GenericTestBeanCustomizer.ORDER(group),
223             new Integer JavaDoc(numCreatedGroups));
224     }
225
226     public BeanInfo JavaDoc[] getAdditionalBeanInfo() {
227         return rootBeanInfo.getAdditionalBeanInfo();
228     }
229
230     public BeanDescriptor JavaDoc getBeanDescriptor() {
231         return rootBeanInfo.getBeanDescriptor();
232     }
233
234     public int getDefaultEventIndex() {
235         return rootBeanInfo.getDefaultEventIndex();
236     }
237
238     public int getDefaultPropertyIndex() {
239         return rootBeanInfo.getDefaultPropertyIndex();
240     }
241
242     public EventSetDescriptor JavaDoc[] getEventSetDescriptors() {
243         return rootBeanInfo.getEventSetDescriptors();
244     }
245
246     public Image JavaDoc getIcon(int iconKind) {
247         return icons[iconKind];
248     }
249
250     public MethodDescriptor JavaDoc[] getMethodDescriptors() {
251         return rootBeanInfo.getMethodDescriptors();
252     }
253
254     public PropertyDescriptor JavaDoc[] getPropertyDescriptors() {
255         return rootBeanInfo.getPropertyDescriptors();
256     }
257 }
258
Popular Tags