001: /* ====================================================================
002: * The QueryForm License, Version 1.1
003: *
004: * Copyright (c) 1998 - 2003 David F. Glasser. All rights
005: * reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The end-user documentation included with the redistribution,
020: * if any, must include the following acknowledgment:
021: * "This product includes software developed by
022: * David F. Glasser."
023: * Alternately, this acknowledgment may appear in the software itself,
024: * if and wherever such third-party acknowledgments normally appear.
025: *
026: * 4. The names "QueryForm" and "David F. Glasser" must
027: * not be used to endorse or promote products derived from this
028: * software without prior written permission. For written
029: * permission, please contact dglasser@pobox.com.
030: *
031: * 5. Products derived from this software may not be called "QueryForm",
032: * nor may "QueryForm" appear in their name, without prior written
033: * permission of David F. Glasser.
034: *
035: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL DAVID F. GLASSER, THE APACHE SOFTWARE
039: * FOUNDATION OR ITS CONTRIBUTORS, OR ANY AUTHORS OR DISTRIBUTORS
040: * OF THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: * ====================================================================
049: *
050: * This product includes software developed by the
051: * Apache Software Foundation (http://www.apache.org/).
052: *
053: * ====================================================================
054: *
055: * $Source: /cvsroot/qform/qform/src/org/glasser/swing/table/ReflectionColumnManager.java,v $
056: * $Revision: 1.2 $
057: * $Author: dglasser $
058: * $Date: 2005/05/23 02:02:00 $
059: *
060: * --------------------------------------------------------------------
061: */
062: package org.glasser.swing.table;
063:
064: import javax.swing.table.*;
065: import java.lang.reflect.*;
066: import org.glasser.util.*;
067:
068: public class ReflectionColumnManager extends AbstractColumnManager {
069:
070: Class objectClass = null;
071:
072: Method[] getters = null;
073:
074: Method[] setters = null;
075:
076: public ReflectionColumnManager(String[] columnNames,
077: Class objectClass, String[] getterNames,
078: String[] setterNames) throws NoSuchMethodException {
079: super .setColumnNames(columnNames);
080: getters = new Method[getterNames.length];
081: Class[] columnClasses = new Class[getterNames.length];
082:
083: for (int j = 0; j < getters.length; j++) {
084: getters[j] = objectClass.getMethod(getterNames[j],
085: (Class[]) null);
086: columnClasses[j] = getters[j].getReturnType();
087: }
088:
089: Class[] paramTypes = new Class[1];
090:
091: for (int j = 0; setterNames != null && j < setterNames.length; j++) {
092: String setterName = setterNames[j];
093: if (setterName == null)
094: continue;
095: paramTypes[0] = columnClasses[j];
096: setters[j] = objectClass.getMethod(setterName, paramTypes);
097: }
098:
099: // now, if any of the columnClasses are for primitive types
100: // (int, long, etc.) then we'll convert them to their corresponding
101: // wrapper class. This must be done after the setters have been
102: // created.
103: for (int j = 0; j < columnClasses.length; j++) {
104: Class wrapperClass = Util.getWrapperClass(columnClasses[j]);
105: if (wrapperClass != null)
106: columnClasses[j] = wrapperClass;
107: }
108:
109: // now send the columnClasses array to the AbstractColumnManager superclass.
110: super .setColumnClasses(columnClasses);
111:
112: }
113:
114: public Object getValueAt(int rowIndex, int columnIndex,
115: Object rowObject) {
116:
117: Method getter = getters[columnIndex];
118: try {
119: if (rowObject == null)
120: return null;
121: return getter.invoke(rowObject, (Object[]) null);
122: } catch (Exception ex) {
123: ex.printStackTrace();
124: throw new RuntimeException("Caught "
125: + ex.getClass().getName() + ": Object class is "
126: + objectClass.getName() + ", method is "
127: + getter.getName());
128: }
129: }
130:
131: public void setValueAt(Object newCellValue, int rowIndex,
132: int columnIndex, Object rowObject) {
133:
134: Method setter = setters[columnIndex];
135: try {
136: setter.invoke(rowObject, new Object[] { newCellValue });
137: } catch (Exception ex) {
138: ex.printStackTrace();
139: throw new RuntimeException("Caught "
140: + ex.getClass().getName() + ": Object class is "
141: + objectClass.getName() + ", setter method is "
142: + setter.getName());
143: }
144: }
145:
146: public boolean isCellEditable(int rowIndex, int columnIndex,
147: Object rowObject) {
148: return rowObject != null && setters != null
149: && setters[columnIndex] != null;
150: }
151:
152: }
|