View Javadoc

1   /*
2    * Copyright (c) 2003-2008 by Cosylab d. d.
3    *
4    * This file is part of Java-Common.
5    *
6    * Java-Common is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * Java-Common is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with Java-Common.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  
20  package com.cosylab.util;
21  
22  import java.awt.Image;
23  import java.beans.BeanDescriptor;
24  import java.beans.BeanInfo;
25  import java.beans.EventSetDescriptor;
26  import java.beans.IntrospectionException;
27  import java.beans.Introspector;
28  import java.beans.MethodDescriptor;
29  import java.beans.PropertyDescriptor;
30  import java.beans.SimpleBeanInfo;
31  import java.lang.reflect.Method;
32  import java.util.HashMap;
33  import java.util.Map;
34  
35  /**
36   * @author <a href="mailto:jernej.kamenik@cosylab.com">Jernej Kamenik</a>
37   * @version $id$
38   *
39   */
40  public class BeanInfoSupport extends SimpleBeanInfo implements BeanInfo {
41  
42  	private static HashMap delegates = new HashMap();
43  
44  	public static BeanInfo getBeanInfo(Class beanType) {
45  		BeanInfo retVal = null;
46  		if (delegates.containsKey(beanType)) {
47  			retVal = (BeanInfo)delegates.get(beanType);
48  		} else {
49  			try {
50  				retVal = Introspector.getBeanInfo(beanType,Introspector.IGNORE_IMMEDIATE_BEANINFO);
51  				delegates.put(beanType,retVal);
52  			} catch (IntrospectionException e) {
53  				e.printStackTrace();
54  			}
55  		}	
56  		return retVal;
57  	}
58  
59  	public static BeanDescriptor getBeanDescriptor(Class beanType) {
60  		BeanInfo delegate = getBeanInfo(beanType);
61  		if (delegate==null) return null;
62  		return delegate.getBeanDescriptor();
63  	}
64  
65  	public static EventSetDescriptor[] getEventSetDescriptors(Class beanType) {
66  		BeanInfo delegate = getBeanInfo(beanType);
67  		if (delegate==null) return null;
68  		return delegate.getEventSetDescriptors();
69  	}
70  
71  	/**
72  	 * @see BeanInfo#getDefaultEventIndex()
73  	 */
74  	public static int getDefaultEventIndex(Class beanType) {
75  		BeanInfo delegate = getBeanInfo(beanType);
76  		if (delegate==null) return -1;
77  		return delegate.getDefaultEventIndex();
78  	}
79  
80  	/**
81  	 * @see BeanInfo#getPropertyDescriptors()
82  	 */
83  	public static PropertyDescriptor[] getPropertyDescriptors(Class beanType) {
84  		BeanInfo delegate = getBeanInfo(beanType);
85  		if (delegate==null) return null;
86  		return delegate.getPropertyDescriptors();
87  	}
88  
89  	/**
90  	 * @see BeanInfo#getDefaultPropertyIndex()
91  	 */
92  	public static int getDefaultPropertyIndex(Class beanType) {
93  		BeanInfo delegate = getBeanInfo(beanType);
94  		if (delegate==null) return -1;
95  		return delegate.getDefaultPropertyIndex();
96  	}
97  
98  	/**
99  	 * @see BeanInfo#getMethodDescriptors()
100 	 */
101 	public static MethodDescriptor[] getMethodDescriptors(Class beanType) {
102 		BeanInfo delegate = getBeanInfo(beanType);
103 		if (delegate==null) return null;
104 		return delegate.getMethodDescriptors();
105 	}
106 
107 	/**
108 	 * @see BeanInfo#getAdditionalBeanInfo()
109 	 */
110 	public static BeanInfo[] getAdditionalBeanInfo(Class beanType) {
111 		BeanInfo delegate = getBeanInfo(beanType);
112 		if (delegate==null) return null;
113 		return delegate.getAdditionalBeanInfo();
114 	}
115 
116 	/**
117 	 * @see BeanInfo#getIcon(int)
118 	 */
119 	public static Image getIcon(int arg0, Class beanType) {
120 		BeanInfo delegate = getBeanInfo(beanType);
121 		if (delegate==null) return null;
122 		return delegate.getIcon(arg0);
123 	}
124 	
125 	private Class beanClass;
126 	private Map<String,PropertyDescriptor> propertyDescriptors = new HashMap<String,PropertyDescriptor>();
127 	private BeanDescriptor beanDescriptor;
128 	
129 	/**
130 	 * Creates new instance of BeanInfoSupport.
131 	 */
132 	protected BeanInfoSupport() {
133 		beanClass= getBeanClass();
134 	}
135 	/**
136 	 * Override this method, if default implementation is not suitable for you. Default implementation
137 	 * calculates bean class from bean info class name by stripping BeanInfo part.
138 	 * @return calculates bean class from bean info class name by stripping BeanInfo part
139 	 */
140 	protected Class getBeanClass() {
141 		String s= this.getClass().getName();
142 		s= s.substring(0,s.length()-"BeanInfo".length());
143 		try {
144 			return Class.forName(s);
145 		} catch (ClassNotFoundException e) {
146 			e.printStackTrace();
147 		}
148 		return null;
149 	}
150 	
151 	protected void setPropertyEditor(String property, Class editor) throws IntrospectionException {
152 		PropertyDescriptor p = getPropertyDescriptor(property);
153 		p.setPropertyEditorClass(editor);
154 	}
155 	
156 	protected PropertyDescriptor getPropertyDescriptor(String name) throws IntrospectionException {
157 		PropertyDescriptor p = propertyDescriptors.get(name);
158 		if (p==null) {
159 			p= new PropertyDescriptor(name,beanClass);
160 			propertyDescriptors.put(name,p);
161 		}
162 		return p;
163 	}
164 
165 	protected PropertyDescriptor removePropertyDescriptor(String name) {
166 		return propertyDescriptors.remove(name);
167 	}
168 	
169 	protected void loadDefaultPropertyDescriptors() {
170 		PropertyDescriptor[] pd= getPropertyDescriptors(beanClass);
171 		
172 		for (PropertyDescriptor p : pd) {
173 			if (p.getWriteMethod() == null)
174 				continue;
175 			if (p.getReadMethod() == null)
176 				continue;
177 			if (!propertyDescriptors.containsKey(p.getName())) propertyDescriptors.put(p.getName(),p);
178 		}
179 		
180 	}
181 	
182 	protected void loadPropertyDescriptors(String... names) throws IntrospectionException {
183 		for (String s : names) {
184 			if (!propertyDescriptors.containsKey(s)) propertyDescriptors.put(s,new PropertyDescriptor(s,beanClass));
185 		}
186 	}
187 
188 	protected void addPropertyDescriptor(PropertyDescriptor... descriptors) {
189 		for (PropertyDescriptor p : descriptors) {
190 			propertyDescriptors.put(p.getName(),p);
191 		}
192 	}
193 
194 	protected void removePropertyDescriptors(String... names) {
195 		for (String s : names) {
196 			propertyDescriptors.remove(s);
197 		}
198 	}
199 	
200 	protected void loadPropertyDescriptor(String name, Class editor) throws IntrospectionException {
201 		PropertyDescriptor p= propertyDescriptors.get(name);
202 		if (p == null) {
203 			p= new PropertyDescriptor(name,beanClass);
204 		}
205 		p.setPropertyEditorClass(editor);
206 		if (!propertyDescriptors.containsKey(name)) propertyDescriptors.put(name,p);
207 	}
208 	
209 	protected void loadPropertyDescriptor(String name, Class editor, Method readMethod, Method writeMethod) throws IntrospectionException {
210 		PropertyDescriptor p= propertyDescriptors.get(name);
211 		if (p == null) {
212 			p= new PropertyDescriptor(name,beanClass, readMethod.getName(), writeMethod.getName());
213 		}
214 		p.setPropertyEditorClass(editor);
215 		p.setReadMethod(readMethod);
216 		p.setWriteMethod(writeMethod);
217 		if (!propertyDescriptors.containsKey(name)) propertyDescriptors.put(name,p);
218 	}
219 	
220 	protected void overridePropertyDescriptor(String name, Class editor, String readMethod, String writeMethod) throws IntrospectionException {
221 		PropertyDescriptor p = new PropertyDescriptor(name,beanClass, readMethod, writeMethod);
222 		p.setPropertyEditorClass(editor);
223 		propertyDescriptors.put(name,p);
224 	}
225 	
226 	protected void clearPropertyDescriptors() {
227 		propertyDescriptors.clear();
228 	}
229 	
230 	/**
231 	 * Sets new beanDescriptor.
232 	 */
233 	protected void setBeanDescriptor(Class customizer) {
234 		this.beanDescriptor = new BeanDescriptor(beanClass,customizer);
235 	}
236 	
237 	/* (non-Javadoc)
238 	 * @see java.beans.SimpleBeanInfo#getPropertyDescriptors()
239 	 */
240 	@Override
241 	public PropertyDescriptor[] getPropertyDescriptors() {
242 		if (propertyDescriptors.size()>0) {
243 			return propertyDescriptors.values().toArray(new PropertyDescriptor[propertyDescriptors.size()]);
244 		}
245 		return super.getPropertyDescriptors();
246 	}
247 	
248 	/* (non-Javadoc)
249 	 * @see java.beans.SimpleBeanInfo#getBeanDescriptor()
250 	 */
251 	@Override
252 	public BeanDescriptor getBeanDescriptor() {
253 		if (beanDescriptor!=null) {
254 			return beanDescriptor;
255 		}
256 		return super.getBeanDescriptor();
257 	}
258 
259 }