001: /*
002: * Spoon - http://spoon.gforge.inria.fr/
003: * Copyright (C) 2006 INRIA Futurs <renaud.pawlak@inria.fr>
004: *
005: * This software is governed by the CeCILL-C License under French law and
006: * abiding by the rules of distribution of free software. You can use, modify
007: * and/or redistribute the software under the terms of the CeCILL-C license as
008: * circulated by CEA, CNRS and INRIA at http://www.cecill.info.
009: *
010: * This program is distributed in the hope that it will be useful, but WITHOUT
011: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
012: * FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C License for more details.
013: *
014: * The fact that you are presently reading this means that you have had
015: * knowledge of the CeCILL-C license and that you accept its terms.
016: */
017:
018: package spoon.support.gui;
019:
020: import java.awt.BorderLayout;
021: import java.lang.reflect.Field;
022: import java.lang.reflect.Modifier;
023: import java.util.ArrayList;
024: import java.util.List;
025:
026: import javax.swing.JFrame;
027: import javax.swing.JPanel;
028: import javax.swing.JScrollPane;
029: import javax.swing.JTable;
030: import javax.swing.table.AbstractTableModel;
031:
032: public class SpoonObjectFieldsTable extends JFrame {
033: public class SpoonObjectTableModel extends AbstractTableModel {
034:
035: private static final long serialVersionUID = 1L;
036:
037: List<Field> field;
038:
039: Object o;
040:
041: public SpoonObjectTableModel(Object o) {
042: super ();
043:
044: this .o = o;
045: field = new ArrayList<Field>();
046:
047: scanFields(o.getClass());
048: }
049:
050: public int getColumnCount() {
051: return columnsName.length;
052: }
053:
054: @Override
055: public String getColumnName(int column) {
056: return columnsName[column];
057: }
058:
059: public int getRowCount() {
060: return field.size();
061: }
062:
063: public Object getValueAt(int rowIndex, int columnIndex) {
064: Field m = field.get(rowIndex);
065: switch (columnIndex) {
066: case (0):
067: return m.getName();
068: case (1):
069: return m.getType().getCanonicalName();
070: case (2):
071: try {
072: Object val = m.get(o);
073: if (val != null)
074: return val.getClass().getCanonicalName();
075: } catch (IllegalArgumentException e) {
076: e.printStackTrace();
077: } catch (IllegalAccessException e) {
078: e.printStackTrace();
079: }
080: break;
081: case (3):
082: try {
083: return m.get(o);
084: } catch (IllegalArgumentException e) {
085: e.printStackTrace();
086: } catch (IllegalAccessException e) {
087: e.printStackTrace();
088: }
089: }
090: return null;
091: }
092:
093: public void scanFields(Class<?> c) {
094: for (Field f : c.getDeclaredFields()) {
095: f.setAccessible(true);
096: if (!Modifier.isStatic(f.getModifiers()))
097: field.add(f);
098: }
099: if (c.getSuperclass() != null)
100: scanFields(c.getSuperclass());
101: }
102: }
103:
104: public static final String[] columnsName = new String[] { "Name",
105: "FieldType", "currentType", "Value" };
106:
107: private static final long serialVersionUID = 1L;
108:
109: private JPanel jContentPane = null;
110:
111: private JScrollPane jScrollPane = null;
112:
113: private JTable jTable = null;
114:
115: private Object o;
116:
117: /**
118: * This is the default constructor
119: */
120: public SpoonObjectFieldsTable(Object o) {
121: super ();
122: this .o = o;
123: initialize();
124: }
125:
126: /**
127: * This method initializes jContentPane
128: *
129: * @return javax.swing.JPanel
130: */
131: private JPanel getJContentPane() {
132: if (jContentPane == null) {
133: jContentPane = new JPanel();
134: jContentPane.setLayout(new BorderLayout());
135: jContentPane.add(getJScrollPane(),
136: java.awt.BorderLayout.CENTER);
137: }
138: return jContentPane;
139: }
140:
141: /**
142: * This method initializes jScrollPane
143: *
144: * @return javax.swing.JScrollPane
145: */
146: private JScrollPane getJScrollPane() {
147: if (jScrollPane == null) {
148: jScrollPane = new JScrollPane();
149: jScrollPane.setViewportView(getJTable());
150: }
151: return jScrollPane;
152: }
153:
154: /**
155: * This method initializes jTable
156: *
157: * @return javax.swing.JTable
158: */
159: private JTable getJTable() {
160: if (jTable == null) {
161: jTable = new JTable(new SpoonObjectTableModel(o));
162: }
163: return jTable;
164: }
165:
166: /**
167: * This method initializes this
168: *
169: * @return void
170: */
171: private void initialize() {
172: this .setSize(320, 240);
173: this
174: .setLocation(
175: (getGraphicsConfiguration().getDevice()
176: .getDisplayMode().getWidth() - getWidth()) / 2,
177: (getGraphicsConfiguration().getDevice()
178: .getDisplayMode().getHeight() - getHeight()) / 2);
179: this .setContentPane(getJContentPane());
180: this .setTitle(o.getClass().getSimpleName());
181: this .setVisible(true);
182: this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
183: }
184:
185: }
|