001: /*
002: * Swing Explorer. Tool for developers exploring Java/Swing-based application internals.
003: * Copyright (C) 2008, Maxim Zakharenkov
004: *
005: * This program is free software; you can redistribute it and/or modify
006: * it under the terms of the GNU General Public License as published by
007: * the Free Software Foundation; either version 2 of the License, or
008: * (at your option) any later version.
009: *
010: * This program is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU General Public License for more details.
014: *
015: * You should have received a copy of the GNU General Public License along
016: * with this program; if not, write to the Free Software Foundation, Inc.,
017: * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
018: *
019: * $Header: /cvs/swingexplorer/src/org/swingexplorer/properties/MdlProperties.java,v 1.1 2008/02/13 21:52:11 maxz1 Exp $
020: */
021: package org.swingexplorer.properties;
022:
023: import java.lang.reflect.Method;
024: import java.util.HashMap;
025: import java.util.Map;
026:
027: import javax.swing.table.AbstractTableModel;
028:
029: /**
030: *
031: * @author Maxim Zakharenkov
032: */
033: public class MdlProperties extends AbstractTableModel {
034:
035: Object bean;
036: String[][] properties = new String[0][0];
037: String[] colNames = new String[] { "name", "value" };
038:
039: public Map describe(Object bean) {
040: if (bean == null) {
041: return new HashMap();
042: }
043:
044: Method[] methods = bean.getClass().getMethods();
045: HashMap<String, String> map = new HashMap<String, String>();
046:
047: for (Method m : methods) {
048: if (m.getParameterTypes().length > 0) {
049: continue;
050: }
051:
052: // construct property name
053: String propName = null;
054: if (m.getName().startsWith("get")) {
055: propName = m.getName().substring(3);
056: } else if (m.getName().startsWith("is")) {
057: propName = m.getName().substring(2);
058: }
059:
060: // obtain property value
061: if (propName != null) {
062: Object result;
063: try {
064: result = m.invoke(bean, new Object[0]);
065: String strRes = result == null ? "" : result
066: .toString();
067: map.put(propName, strRes);
068: } catch (Exception e) {
069: }
070: }
071: }
072: return map;
073: }
074:
075: public void setBean(Object _bean) {
076: bean = _bean;
077:
078: try {
079: Map props = describe(bean);
080: properties = new String[props.size()][2];
081: int i = 0;
082: for (Object key : props.keySet()) {
083: properties[i][0] = (String) key;
084: properties[i][1] = (String) props.get(key);
085: i++;
086: }
087: } catch (Exception e) {
088: properties = new String[0][0];
089: e.printStackTrace();
090: return;
091: } finally {
092: fireTableDataChanged();
093: }
094: }
095:
096: @Override
097: public String getColumnName(int column) {
098: return colNames[column];
099: }
100:
101: public Object getBean() {
102: return bean;
103: }
104:
105: public int getColumnCount() {
106: return 2;
107: }
108:
109: public int getRowCount() {
110: return properties.length;
111: }
112:
113: public Object getValueAt(int rowIndex, int columnIndex) {
114: return properties[rowIndex][columnIndex];
115: }
116: }
|