001: package net.sourceforge.squirrel_sql.fw.datasetviewer;
002:
003: /*
004: * Copyright (C) 2002 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.ArrayList;
027: import java.util.List;
028:
029: import net.sourceforge.squirrel_sql.fw.util.IMessageHandler;
030: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
031: import net.sourceforge.squirrel_sql.fw.util.StringManager;
032: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
033: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
034:
035: public class JavabeanDataSet implements IDataSet {
036: private static final StringManager s_stringMgr = StringManagerFactory
037: .getStringManager(JavabeanDataSet.class);
038:
039: @SuppressWarnings("unused")
040: private ILogger s_log = LoggerController
041: .createLogger(JavabeanDataSet.class);
042:
043: // i18n[javaBeanDataSet.name=Property Name]
044: private static final String _nameColumnName = s_stringMgr
045: .getString("javaBeanDataSet.name");
046: // i18n[javaBeanDataSet.value=Value]
047: private static final String _valueColumnName = s_stringMgr
048: .getString("javaBeanDataSet.value");
049:
050: // TODO: These 2 should be handled with an Iterator!!!
051: private int _iCurrent = -1;
052: private Object[] _currentRow;
053:
054: private List<Object[]> _data;
055:
056: private DataSetDefinition _dataSetDefinition;
057:
058: public JavabeanDataSet() {
059: super ();
060: commonCtor();
061: }
062:
063: public JavabeanDataSet(Object bean) throws DataSetException {
064: super ();
065: setJavabean(bean);
066: }
067:
068: public void setJavabean(Object bean) throws DataSetException {
069: commonCtor();
070: if (bean != null) {
071: try {
072: BeanInfo info = Introspector.getBeanInfo(bean
073: .getClass(), Introspector.USE_ALL_BEANINFO);
074: processBeanInfo(bean, info);
075: } catch (Exception ex) {
076: throw new DataSetException(ex);
077: }
078: }
079: }
080:
081: private void processBeanInfo(Object bean, BeanInfo info)
082: throws InvocationTargetException, IllegalAccessException {
083: BeanInfo[] extra = info.getAdditionalBeanInfo();
084: if (extra != null) {
085: for (int i = 0; i < extra.length; ++i) {
086: processBeanInfo(bean, extra[i]);
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: String displayName = propDesc[i].getDisplayName();
096: if (displayName == null) {
097: displayName = propName;
098: }
099: final Object[] line = generateLine(displayName, bean,
100: getter);
101: if (line != null) {
102: _data.add(line);
103: }
104: }
105: }
106: }
107:
108: /**
109: * Generate a line for the passed property.
110: *
111: * @param propTitle Descriptive name for the property.
112: * @param bean Bean containg the property.
113: * @param getter The "getter" function to retrieve the
114: * properties value.
115: *
116: * @return An <CODE>Object[]</CODE> containing the cells for the line in
117: * the table. Element zero the first cell etc. Return
118: * <CODE>null</CODE> if this property is <B>not</B> to be added
119: * to the table.
120: */
121: protected Object[] generateLine(String propTitle, Object bean,
122: Method getter) throws InvocationTargetException,
123: IllegalAccessException {
124: final Object[] line = new Object[2];
125: line[0] = propTitle;
126: line[1] = executeGetter(bean, getter);
127: return line;
128: }
129:
130: protected Object executeGetter(Object bean, Method getter)
131: throws InvocationTargetException, IllegalAccessException {
132: return getter.invoke(bean, (Object[]) null);
133: }
134:
135: public final int getColumnCount() {
136: return 2;
137: }
138:
139: public DataSetDefinition getDataSetDefinition() {
140: return _dataSetDefinition;
141: }
142:
143: public synchronized boolean next(IMessageHandler msgHandler)
144: throws DataSetException {
145: // TODO: This should be handled with an Iterator!!!
146: if (++_iCurrent < _data.size()) {
147: _currentRow = _data.get(_iCurrent);
148: return true;
149: }
150: return false;
151: }
152:
153: public synchronized Object get(int columnIndex) {
154: return _currentRow[columnIndex];
155: }
156:
157: private ColumnDisplayDefinition[] createColumnDefinitions() {
158: ColumnDisplayDefinition[] columnDefs = new ColumnDisplayDefinition[2];
159: columnDefs[0] = new ColumnDisplayDefinition(50, _nameColumnName);
160: columnDefs[1] = new ColumnDisplayDefinition(50,
161: _valueColumnName);
162: return columnDefs;
163: }
164:
165: private void commonCtor() {
166: _iCurrent = -1;
167: _data = new ArrayList<Object[]>();
168:
169: ColumnDisplayDefinition[] colDefs = createColumnDefinitions();
170: _dataSetDefinition = new DataSetDefinition(colDefs);
171: }
172: }
|