1 17 package org.apache.jmeter.testbeans; 18 19 import java.awt.Image ; 20 import java.beans.BeanDescriptor ; 21 import java.beans.BeanInfo ; 22 import java.beans.EventSetDescriptor ; 23 import java.beans.IntrospectionException ; 24 import java.beans.Introspector ; 25 import java.beans.MethodDescriptor ; 26 import java.beans.PropertyDescriptor ; 27 import java.beans.SimpleBeanInfo ; 28 import java.util.MissingResourceException ; 29 import java.util.ResourceBundle ; 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 66 public abstract class BeanInfoSupport extends SimpleBeanInfo 67 { 68 69 private static transient Logger log = LoggingManager.getLoggerForClass(); 70 71 public static final String TAGS= 73 GenericTestBeanCustomizer.TAGS; 74 public static final String NOT_UNDEFINED= 75 GenericTestBeanCustomizer.NOT_UNDEFINED; 76 public static final String NOT_EXPRESSION= 77 GenericTestBeanCustomizer.NOT_EXPRESSION; 78 public static final String NOT_OTHER= 79 GenericTestBeanCustomizer.NOT_OTHER; 80 public static final String DEFAULT= 81 GenericTestBeanCustomizer.DEFAULT; 82 public static final String RESOURCE_BUNDLE= 83 GenericTestBeanCustomizer.RESOURCE_BUNDLE; 84 85 86 private Class beanClass; 87 88 89 private BeanInfo rootBeanInfo; 90 91 92 private Image [] icons= new Image [5]; 93 94 97 protected BeanInfoSupport(Class beanClass) { 98 99 this.beanClass= beanClass; 100 101 try { 102 rootBeanInfo= Introspector.getBeanInfo( 103 beanClass, 104 Introspector.IGNORE_IMMEDIATE_BEANINFO); 105 } catch (IntrospectionException e) { 106 log.error("Can't introspect.", e); 107 throw new Error (e.toString()); } 109 110 try{ 111 ResourceBundle resourceBundle= ResourceBundle.getBundle( 112 beanClass.getName()+"Resources", 113 JMeterUtils.getLocale()); 114 115 getBeanDescriptor().setValue(RESOURCE_BUNDLE, resourceBundle); 117 118 try 120 { 121 getBeanDescriptor().setDisplayName( 122 resourceBundle.getString("displayName")); 123 } 124 catch (MissingResourceException e) 125 { 126 log.debug( 127 "Localized display name not available for bean " 128 +beanClass.getName()); 129 } 130 131 PropertyDescriptor [] properties= getPropertyDescriptors(); 133 134 for (int i=0; i<properties.length; i++) 135 { 136 String name= properties[i].getName(); 137 138 try 139 { 140 properties[i].setDisplayName( 141 resourceBundle.getString(name+".displayName")); 142 } 143 catch (MissingResourceException 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 e) 156 { 157 log.debug( 158 "Localized short description not available for property " 159 +name); 160 } 161 } 162 } 163 catch (MissingResourceException e) 164 { 165 log.warn("Localized strings not available for bean "+beanClass); 166 } 167 } 168 169 175 protected PropertyDescriptor property(String name) { 176 PropertyDescriptor [] 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 192 protected void setIcon(String resourceName) 193 { 194 icons[ICON_COLOR_16x16]= loadImage(resourceName); 195 } 196 197 198 private int numCreatedGroups= 0; 199 200 212 protected void createPropertyGroup(String group, String [] names) 213 { 214 for (int i=0; i<names.length; i++) 215 { 216 PropertyDescriptor p= property(names[i]); 217 p.setValue(GenericTestBeanCustomizer.GROUP, group); 218 p.setValue(GenericTestBeanCustomizer.ORDER, new Integer (i)); 219 } 220 numCreatedGroups++; 221 getBeanDescriptor().setValue( 222 GenericTestBeanCustomizer.ORDER(group), 223 new Integer (numCreatedGroups)); 224 } 225 226 public BeanInfo [] getAdditionalBeanInfo() { 227 return rootBeanInfo.getAdditionalBeanInfo(); 228 } 229 230 public BeanDescriptor 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 [] getEventSetDescriptors() { 243 return rootBeanInfo.getEventSetDescriptors(); 244 } 245 246 public Image getIcon(int iconKind) { 247 return icons[iconKind]; 248 } 249 250 public MethodDescriptor [] getMethodDescriptors() { 251 return rootBeanInfo.getMethodDescriptors(); 252 } 253 254 public PropertyDescriptor [] getPropertyDescriptors() { 255 return rootBeanInfo.getPropertyDescriptors(); 256 } 257 } 258 | Popular Tags |