001: package net.sourceforge.squirrel_sql.fw.datasetviewer;
002:
003: /*
004: * Copyright (C) 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.IntrospectionException;
023: import java.beans.Introspector;
024: import java.beans.PropertyDescriptor;
025: import java.lang.reflect.InvocationTargetException;
026: import java.lang.reflect.Method;
027: import java.util.ArrayList;
028: import java.util.Iterator;
029: import java.util.List;
030:
031: import net.sourceforge.squirrel_sql.fw.util.IMessageHandler;
032: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
033: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
034:
035: public class JavabeanArrayDataSet implements IDataSet {
036: @SuppressWarnings("unused")
037: private final ILogger s_log = LoggerController
038: .createLogger(JavabeanArrayDataSet.class);
039:
040: private Object[] _currentRow;
041:
042: private List<Object[]> _data;
043: private Iterator<Object[]> _dataIter;
044:
045: private int _columnCount;
046: private DataSetDefinition _dataSetDefinition;
047:
048: /**
049: * @throws IllegalArgumentException
050: * Thrrown if all objects in <TT>beans</TT> are not the same class.
051: */
052: public JavabeanArrayDataSet(Object[] beans) throws DataSetException {
053: super ();
054: setJavabeanArray(beans);
055: }
056:
057: /**
058: * Retrieve the number of columns in this <TT>DataSet</TT>.
059: *
060: * @return Number of columns.
061: */
062: public final int getColumnCount() {
063: return _columnCount;
064: }
065:
066: public DataSetDefinition getDataSetDefinition() {
067: return _dataSetDefinition;
068: }
069:
070: public synchronized boolean next(IMessageHandler msgHandler)
071: throws DataSetException {
072: if (_dataIter.hasNext()) {
073: _currentRow = _dataIter.next();
074: return true;
075: }
076: return false;
077: }
078:
079: public synchronized Object get(int columnIndex) {
080: return _currentRow[columnIndex];
081: }
082:
083: protected Object executeGetter(Object bean, Method getter)
084: throws InvocationTargetException, IllegalAccessException {
085: return getter.invoke(bean, (Object[]) null);
086: }
087:
088: /**
089: * @throws IllegalArgumentException
090: * Thrrown if all objects in <TT>beans</TT> are not the same class.
091: */
092: private void setJavabeanArray(Object[] beans)
093: throws DataSetException {
094: if (beans == null) {
095: beans = new Object[0];
096: }
097:
098: if (beans.length > 0) {
099: BeanInfo info = null;
100: try {
101: info = Introspector.getBeanInfo(beans[0].getClass(),
102: Introspector.USE_ALL_BEANINFO);
103: validateBeans(beans);
104: initialize(info);
105: } catch (IntrospectionException ex) {
106: throw new DataSetException(ex);
107: }
108:
109: try {
110: for (int i = 0; i < beans.length; ++i) {
111: processBeanInfo(beans[i], info);
112: }
113: } catch (Exception ex) {
114: throw new DataSetException(ex);
115: }
116: _dataIter = _data.iterator();
117: }
118: }
119:
120: private void processBeanInfo(Object bean, BeanInfo info)
121: throws InvocationTargetException, IllegalAccessException {
122: // BeanInfo[] extra = info.getAdditionalBeanInfo();
123: // if (extra != null)
124: // {
125: // for (int i = 0; i < extra.length; ++i)
126: // {
127: // processBeanInfo(bean, extra[i]);
128: // }
129: // }
130:
131: PropertyDescriptor[] propDesc = info.getPropertyDescriptors();
132: Object[] line = new Object[propDesc.length];
133: for (int i = 0; i < propDesc.length; ++i) {
134: final Method getter = propDesc[i].getReadMethod();
135: if (getter != null) {
136: line[i] = executeGetter(bean, getter);
137: }
138: }
139:
140: if (line != null) {
141: _data.add(line);
142: }
143: }
144:
145: private void validateBeans(Object[] beans)
146: throws IllegalArgumentException {
147: if (beans.length > 0) {
148: final String className = beans[0].getClass().getName();
149: for (int i = 1; i < beans.length; ++i) {
150: if (!beans[i].getClass().getName().equals(className)) {
151: throw new IllegalArgumentException(
152: "All beans must be the same Class");
153: }
154: }
155: }
156:
157: }
158:
159: private void initialize(BeanInfo info) {
160: _data = new ArrayList<Object[]>();
161: ColumnDisplayDefinition[] colDefs = createColumnDefinitions(
162: info, null);
163: _dataSetDefinition = new DataSetDefinition(colDefs);
164: _columnCount = _dataSetDefinition.getColumnDefinitions().length;
165: }
166:
167: private ColumnDisplayDefinition[] createColumnDefinitions(
168: BeanInfo info, int[] columnWidths) {
169: PropertyDescriptor[] propDesc = info.getPropertyDescriptors();
170: ColumnDisplayDefinition[] columnDefs = new ColumnDisplayDefinition[propDesc.length];
171: for (int i = 0; i < propDesc.length; ++i) {
172: int colWidth = columnWidths != null
173: && columnWidths.length >= i ? columnWidths[i] : 200;
174: columnDefs[i] = new ColumnDisplayDefinition(colWidth,
175: propDesc[i].getDisplayName());
176: }
177: return columnDefs;
178: }
179: }
|