001: package net.sourceforge.squirrel_sql.fw.gui;
002:
003: /*
004: * Copyright (C) 2001-2003 Colin Bell
005: * colbell@users.sourceforge.net
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021: import java.beans.BeanInfo;
022: import java.beans.Introspector;
023: import java.beans.PropertyDescriptor;
024: import java.lang.reflect.InvocationTargetException;
025: import java.lang.reflect.Method;
026: import java.util.Collections;
027: import java.util.Comparator;
028: import java.util.Vector;
029:
030: import javax.swing.table.DefaultTableModel;
031:
032: import net.sourceforge.squirrel_sql.fw.util.BaseException;
033: import net.sourceforge.squirrel_sql.fw.util.StringManager;
034: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
035:
036: public class BeanPropertyTableModel extends DefaultTableModel {
037: private static final long serialVersionUID = 1L;
038:
039: /** Internationalized strings for this class. */
040: private static final StringManager s_stringMgr = StringManagerFactory
041: .getStringManager(BeanPropertyTableModel.class);
042:
043: private Object _bean;
044:
045: private String _nameColumnName = s_stringMgr
046: .getString("BeanPropertyTableModel.namecolumn");
047: private String _valueColumnName = s_stringMgr
048: .getString("BeanPropertyTableModel.valuecolumn");
049:
050: public BeanPropertyTableModel() {
051: super ();
052: }
053:
054: public void setBean(Object bean) throws BaseException {
055: _bean = bean;
056: refresh();
057: }
058:
059: public void refresh() throws BaseException {
060: final Vector<Object> columnNames = new Vector<Object>();
061: columnNames.add(_nameColumnName);
062: columnNames.add(_valueColumnName);
063: final Vector<Object> columnData = new Vector<Object>();
064: if (_bean != null) {
065: try {
066: BeanInfo info = Introspector.getBeanInfo(_bean
067: .getClass(), Introspector.USE_ALL_BEANINFO);
068: processBeanInfo(info, columnData);
069: } catch (Exception ex) {
070: throw new BaseException(ex);
071: }
072: }
073:
074: // Sort the rows by the property name.
075: Collections.sort(columnData, new DataSorter());
076:
077: setDataVector(columnData, columnNames);
078: }
079:
080: private void processBeanInfo(BeanInfo info,
081: Vector<Object> columnData)
082: throws InvocationTargetException, IllegalAccessException {
083: BeanInfo[] extra = info.getAdditionalBeanInfo();
084: if (extra != null) {
085: for (int i = 0; i < extra.length; ++i) {
086: processBeanInfo(extra[i], columnData);
087: }
088: }
089:
090: PropertyDescriptor[] propDesc = info.getPropertyDescriptors();
091: for (int i = 0; i < propDesc.length; ++i) {
092: final String propName = propDesc[i].getName();
093: final Method getter = propDesc[i].getReadMethod();
094: if (propName != null && getter != null) {
095: Vector<Object> line = generateLine(propName, _bean,
096: getter);
097: if (line != null) {
098: columnData.add(line);
099: }
100: }
101: }
102: }
103:
104: /**
105: * Generate a line for the passed property.
106: *
107: * @param propName Name of the property.
108: * @param bean Bean containg the property.
109: * @param getter The "getter" function to retrieve the
110: * properties value.
111: *
112: * @return A <CODE>Vector</CODE> containing the cells for the line in
113: * the table. Element zero the first cell etc. Return
114: * <CODE>null</CODE> if this property is <B>not</B> to be added
115: * to the table.
116: */
117: protected Vector<Object> generateLine(String propName, Object bean,
118: Method getter) throws InvocationTargetException,
119: IllegalAccessException {
120: final Vector<Object> line = new Vector<Object>();
121: line.add(propName);
122: line.add(executeGetter(bean, getter));
123: return line;
124: }
125:
126: protected Object executeGetter(Object bean, Method getter)
127: throws InvocationTargetException, IllegalAccessException {
128: return getter.invoke(bean, (Object[]) null);
129: }
130:
131: public void setNameColumnName(String value) {
132: _nameColumnName = value;
133: }
134:
135: public void setValueColumnName(String value) {
136: _valueColumnName = value;
137: }
138:
139: /**
140: * This comparator is compatible with the strange use of lists in this
141: * class. This classes lists are Vectors with Strings as the first element
142: * and any object as the other objects.
143: */
144: private static final class DataSorter implements Comparator<Object> {
145: public int compare(Object o1, Object o2) {
146: Vector<Object> v1 = (Vector<Object>) o1;
147: Vector<Object> v2 = (Vector<Object>) o2;
148: String lhs = (String) v1.get(0);
149: String rhs = (String) v2.get(0);
150: return lhs.compareToIgnoreCase(rhs);
151: }
152: }
153: }
|