001: /*
002: * hgcommons 7
003: * Hammurapi Group Common Library
004: * Copyright (C) 2003 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.swing;
024:
025: import java.lang.reflect.Field;
026: import java.lang.reflect.InvocationTargetException;
027: import java.lang.reflect.Method;
028:
029: import javax.swing.table.DefaultTableModel;
030: import javax.swing.table.TableModel;
031: import javax.swing.tree.DefaultMutableTreeNode;
032: import javax.swing.tree.MutableTreeNode;
033:
034: /**
035: * @author Pavel Vlasov
036: *
037: * @version $Revision: 1.1 $
038: */
039: public class BeanVisualizer {
040:
041: public Visualizable toVisualizable(final Object obj) {
042: return new Visualizable() {
043:
044: public MutableTreeNode toTree(final String title) {
045: DefaultMutableTreeNode ret = new DefaultMutableTreeNode(
046: obj) {
047: public String toString() {
048: return title + " [" + obj.getClass().getName()
049: + "] " + obj;
050: }
051: };
052:
053: return ret;
054: }
055:
056: public TableModel toTable() {
057: int rowCount = 2;
058: DefaultTableModel tm = new DefaultTableModel(rowCount,
059: 4);
060: tm.setColumnIdentifiers(new String[] { "Property",
061: "Declared type", "Runtime type", "Value" });
062:
063: Class beanClass = obj.getClass();
064:
065: tm.setValueAt("this", 0, 0);
066: tm.setValueAt(beanClass.getName(), 0, 2);
067: tm.setValueAt(obj, 0, 3);
068:
069: tm.setValueAt("Hash code", 1, 0);
070: tm.setValueAt("int", 1, 1);
071: tm.setValueAt("int", 1, 2);
072: tm.setValueAt(Integer.toString(obj.hashCode(),
073: Character.MAX_RADIX), 1, 3);
074:
075: Method[] methods = beanClass.getMethods();
076: for (int i = 0; i < methods.length; i++) {
077: // getXXX() methods. Object.getClass() is not included.
078: if (!(methods[i].getDeclaringClass()
079: .equals(Object.class))
080: && methods[i].getName().startsWith("get")
081: && methods[i].getParameterTypes().length == 0) {
082: try {
083: Object value = methods[i].invoke(obj, null);
084: tm.setRowCount(++rowCount);
085: int idx = rowCount - 1;
086: tm.setValueAt(methods[i].getName()
087: .substring(3), idx, 0);
088: tm.setValueAt(methods[i].getReturnType()
089: .getName(), idx, 1);
090: if (value == null) {
091: tm.setValueAt("(null)", idx, 2);
092: tm.setValueAt("(null)", idx, 3);
093: } else {
094: tm.setValueAt(value.getClass()
095: .getName(), idx, 2);
096: tm.setValueAt(value, idx, 3);
097: }
098: } catch (IllegalArgumentException e) {
099: e.printStackTrace();
100: } catch (IllegalAccessException e) {
101: e.printStackTrace();
102: } catch (InvocationTargetException e) {
103: e.printStackTrace();
104: }
105: }
106: }
107:
108: Field[] fields = beanClass.getFields();
109: for (int i = 0; i < fields.length; i++) {
110: try {
111: Object value = fields[i].get(obj);
112: tm.setRowCount(++rowCount);
113: int idx = rowCount - 1;
114: tm.setValueAt(fields[i].getName().substring(3),
115: idx, 0);
116: tm.setValueAt(fields[i].getType().getName(),
117: idx, 1);
118: if (value == null) {
119: tm.setValueAt("(null)", idx, 2);
120: tm.setValueAt("(null)", idx, 3);
121: } else {
122: tm.setValueAt(value.getClass().getName(),
123: idx, 2);
124: tm.setValueAt(value, idx, 3);
125: }
126: } catch (IllegalArgumentException e) {
127: e.printStackTrace();
128: } catch (IllegalAccessException e) {
129: e.printStackTrace();
130: }
131: }
132:
133: return tm;
134: }
135:
136: };
137:
138: }
139: }
|