1 /*
2 * Copyright (c) 2003-2008 by Cosylab d. d.
3 *
4 * This file is part of CosyBeans.
5 *
6 * CosyBeans 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 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. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 package com.cosylab.gui.util;
21
22 import java.beans.PropertyDescriptor;
23 import java.util.ArrayList;
24
25 import com.cosylab.gui.displayers.Displayer;
26 import com.cosylab.introspection.BeanIntrospector;
27
28 /**
29 * <code>GuiIntrospector</code> offers utility methods for inspecting beans
30 * properties.
31 *
32 * @author <a href="mailto:jaka.bobnar@cosylab.com">Jaka Bobnar</a>
33 * @version $Id: GuiIntrospector.java,v 1.4 2008-04-22 12:31:02 jbobnar Exp $
34 *
35 */
36 public class GuiIntrospector {
37
38 /**
39 * Returns all static writable properties of the specified class.
40 *
41 * @param displayerClass
42 * @return property descriptors for all writable properties
43 * @throws java.beans.IntrospectionException
44 */
45 public static PropertyDescriptor[] getDisplayerWritableStaticProperties(Class displayerClass)
46 throws java.beans.IntrospectionException {
47 PropertyDescriptor[] desc = BeanIntrospector.getWritable(BeanIntrospector.getInterfacePropertyDescriptors(
48 displayerClass, Displayer.class));
49 ArrayList<PropertyDescriptor> list = new ArrayList<PropertyDescriptor>();
50 for (int i = 0; i < desc.length; i++) {
51 if (!desc[i].getName().equals("value"))
52 list.add(desc[i]);
53 }
54 PropertyDescriptor[] ret = new PropertyDescriptor[list.size()];
55 list.toArray(ret);
56 return ret;
57 }
58
59 /**
60 * Returns dynamic properties of the displayer.
61 *
62 * @param displayerClass
63 * @return
64 * @throws java.beans.IntrospectionException
65 */
66 public static PropertyDescriptor getDisplayerDynamicProperty(Class displayerClass)
67 throws java.beans.IntrospectionException {
68 return BeanIntrospector.getPropertyDescriptor(displayerClass, "value");
69 }
70
71 /**
72 * Returns all static readable properties of the specified class.
73 *
74 * @param displayerClass
75 * @return property descriptors for all writable properties
76 * @throws java.beans.IntrospectionException
77 */
78 public static PropertyDescriptor[] getDisplayerReadableStaticProperties(Class displayerClass)
79 throws java.beans.IntrospectionException {
80 PropertyDescriptor[] desc = BeanIntrospector.getReadable(BeanIntrospector.getInterfacePropertyDescriptors(
81 displayerClass, Displayer.class));
82 ArrayList<PropertyDescriptor> list = new ArrayList<PropertyDescriptor>();
83 for (int i = 0; i < desc.length; i++) {
84 if (!desc[i].getName().equals("value"))
85 list.add(desc[i]);
86 }
87 PropertyDescriptor[] ret = new PropertyDescriptor[list.size()];
88 list.toArray(ret);
89 return ret;
90 }
91 }