View Javadoc

1   /*
2    * Copyright (c) 2003-2008 by Cosylab d. d.
3    *
4    * This file is part of CosyBeans-Common.
5    *
6    * CosyBeans-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   * CosyBeans-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 CosyBeans-Common.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  
20  package com.cosylab.gui.components.customizer;
21  
22  import java.beans.BeanInfo;
23  import java.beans.Customizer;
24  import java.beans.IntrospectionException;
25  import java.beans.Introspector;
26  import java.beans.PropertyDescriptor;
27  import java.util.ArrayList;
28  import java.util.HashMap;
29  
30  /**
31   * Provides methods to simplifly creation of Customizers.
32   * 
33   * @author Alen Vrecko
34   * 
35   */
36  public class CustomizerFactory {
37  
38  	/**
39  	 * Returns a simple customizer for the provided properties.
40  	 * 
41  	 * @param properties
42  	 * @param type
43  	 * @return
44  	 */
45  	public static Customizer createCustomizer(String[] properties) {
46  		return new PropertiesTableCustomizer(properties);
47  	}
48  
49  	/**
50  	 * Returns a simple customizer for the provided properties.
51  	 * 
52  	 * @param properties
53  	 * @param type
54  	 * @return
55  	 */
56  	public static Customizer createCustomizer(PropertyDescriptor[] properties) {
57  		return new PropertiesTableCustomizer(properties);
58  	}
59  
60  	/**
61  	 * Method to convert a set of string descriptior properties
62  	 * 
63  	 * @param properties
64  	 * @param type
65  	 * @return
66  	 */
67  	public static PropertyDescriptor[] propertyStringsToDescriptors(String[] properties,
68  			Class type) {
69  		if (properties == null || properties.length == 0) {
70  
71  			return null;
72  		}
73  
74  		HashMap<String, PropertyDescriptor> nameToDescriptor = new HashMap<String, PropertyDescriptor>();
75  
76  		// populate the map with names <-> propertydscriptors
77  		try {
78  			BeanInfo beanInfo = Introspector.getBeanInfo(type);
79  
80  			PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
81  
82  			for (PropertyDescriptor descriptor : propertyDescriptors) {
83  				nameToDescriptor.put(descriptor.getName(), descriptor);
84  			}
85  		} catch (IntrospectionException e) {
86  
87  			return null;
88  		}
89  
90  		// create array of PropertyDescriptors
91  
92  		ArrayList<PropertyDescriptor> descriptors = new ArrayList<PropertyDescriptor>();
93  
94  		PropertyDescriptor temp = null;
95  
96  		for (String property : properties) {
97  			temp = nameToDescriptor.get(property);
98  			if (temp != null) {
99  				descriptors.add(temp);
100 			}
101 		}
102 
103 		return descriptors.toArray(new PropertyDescriptor[] {});
104 	}
105 }