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.introspection;
21
22 import com.cosylab.introspection.BeanIntrospector;
23
24 import java.beans.IntrospectionException;
25
26 import java.lang.reflect.InvocationTargetException;
27
28 import java.util.ArrayList;
29
30
31 /**
32 * <code>BeanPropertiesTableModel</code> is table model which displayes
33 * properties of a bean and allows property manipulation based on
34 * introspection. This implementationis build around
35 * <code>PropertiesTableModel</code> with help of
36 * <code>BeanIntrospector</code>.
37 *
38 * <p>
39 * When value in table is changed, new value is only stored in model. To set
40 * all property values to bean use <code>applyProperties</code> method.
41 * </p>
42 *
43 * @author <a href="mailto:igor.kriznar@cosylab.com">Igor Kriznar</a>
44 * @version $Id: BeanPropertiesTableModel.java,v 1.4 2008-04-22 12:28:41 jbobnar Exp $
45 *
46 * @see com.cosylab.gui.components.introspection.PropertiesTableModel
47 * @see com.cosylab.introspection.BeanIntrospector
48 * @since May 19, 2004.
49 */
50 public class BeanPropertiesTableModel extends PropertiesTableModel
51 {
52 private static final long serialVersionUID = 1L;
53 private Object bean;
54
55 /**
56 * Default constructor creates empty model.
57 */
58 public BeanPropertiesTableModel()
59 {
60 super();
61 }
62
63 /**
64 * Creates new model with specifies bean and property names, which model
65 * will display in table and manipulate
66 *
67 * @param bean
68 * @param names
69 */
70 public BeanPropertiesTableModel(Object bean, String[] names)
71 {
72 super();
73
74 this.bean = bean;
75
76 ArrayList<Class> typesList = new ArrayList<Class>();
77 ArrayList<Object> valuesList = new ArrayList<Object>();
78
79 for (int i = 0; i < names.length; i++) {
80 try {
81 Object value = BeanIntrospector.getPropertyValue(bean, names[i]);
82 Class type = BeanIntrospector.getPropertyType(bean.getClass(),
83 names[i]);
84 valuesList.add(value);
85 typesList.add(type);
86 } catch (Exception e) {
87 e.printStackTrace();
88 }
89 }
90
91 Class[] types = new Class[typesList.size()];
92 typesList.toArray(types);
93
94 Object[] values = valuesList.toArray();
95
96 setPropertyNames(names);
97 setPropertyTypes(types);
98 setPropertyValues(values);
99 }
100
101 /**
102 * Sets all in model stored property values to the bean.
103 *
104 * @throws IllegalArgumentException if introspection manipulation failed
105 * @throws IllegalAccessException if introspection manipulation failed
106 * @throws InvocationTargetException if introspection manipulation failed
107 * @throws NoSuchMethodException if introspection manipulation failed
108 * @throws IntrospectionException if introspection manipulation failed
109 * @throws NullPointerException if bean was not set
110 */
111 public void applyProperties()
112 throws IllegalArgumentException, IllegalAccessException,
113 InvocationTargetException, NoSuchMethodException,
114 IntrospectionException
115 {
116 if (bean == null) {
117 throw new NullPointerException("No bean object specified in model.");
118 }
119
120 String[] names = getPropertyNames();
121 Object[] values = getPropertyValues();
122
123 for (int j = 0; j < names.length; j++) {
124 BeanIntrospector.setPropertyValue(bean, names[j], values[j]);
125
126 values[j] = BeanIntrospector.getPropertyValue(bean, names[j]);
127 }
128
129 setPropertyValues(values);
130 fireTableDataChanged();
131 }
132
133 /**
134 * Returns bean for which properties apply.
135 *
136 * @return bean for which properties apply
137 */
138 public Object getBean()
139 {
140 return bean;
141 }
142
143 /**
144 * Sets the bean for which properties apply.
145 *
146 * @param bean new bean object
147 */
148 public void setBean(Object bean)
149 {
150 this.bean = bean;
151 }
152 }
153
154 /* __oOo__ */