KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > spoon > support > gui > SpoonObjectFieldsTable


1 package spoon.support.gui;
2
3 import java.awt.BorderLayout JavaDoc;
4 import java.lang.reflect.Field JavaDoc;
5 import java.lang.reflect.Modifier JavaDoc;
6 import java.util.ArrayList JavaDoc;
7 import java.util.List JavaDoc;
8
9 import javax.swing.JFrame JavaDoc;
10 import javax.swing.JPanel JavaDoc;
11 import javax.swing.JScrollPane JavaDoc;
12 import javax.swing.JTable JavaDoc;
13 import javax.swing.table.AbstractTableModel JavaDoc;
14
15 public class SpoonObjectFieldsTable extends JFrame JavaDoc {
16     public class SpoonObjectTableModel extends AbstractTableModel JavaDoc {
17
18         private static final long serialVersionUID = 1L;
19
20         List JavaDoc<Field JavaDoc> field;
21
22         Object JavaDoc o;
23
24         public SpoonObjectTableModel(Object JavaDoc o) {
25             super();
26
27             this.o = o;
28             field = new ArrayList JavaDoc<Field JavaDoc>();
29
30             scanFields(o.getClass());
31         }
32
33         public int getColumnCount() {
34             return columnsName.length;
35         }
36
37         @Override JavaDoc
38         public String JavaDoc getColumnName(int column) {
39             return columnsName[column];
40         }
41
42         public int getRowCount() {
43             return field.size();
44         }
45
46         public Object JavaDoc getValueAt(int rowIndex, int columnIndex) {
47             Field JavaDoc m = field.get(rowIndex);
48             switch (columnIndex) {
49             case (0):
50                 return m.getName();
51             case (1):
52                 return m.getType().getCanonicalName();
53             case (2):
54                 try {
55                     Object JavaDoc val = m.get(o);
56                     if (val != null)
57                         return val.getClass().getCanonicalName();
58                 } catch (IllegalArgumentException JavaDoc e) {
59                     e.printStackTrace();
60                 } catch (IllegalAccessException JavaDoc e) {
61                     e.printStackTrace();
62                 }
63                 break;
64             case (3):
65                 try {
66                     return m.get(o);
67                 } catch (IllegalArgumentException JavaDoc e) {
68                     e.printStackTrace();
69                 } catch (IllegalAccessException JavaDoc e) {
70                     e.printStackTrace();
71                 }
72             }
73             return null;
74         }
75
76         public void scanFields(Class JavaDoc<?> c) {
77             for (Field JavaDoc f : c.getDeclaredFields()) {
78                 f.setAccessible(true);
79                 if (!Modifier.isStatic(f.getModifiers()))
80                     field.add(f);
81             }
82             if (c.getSuperclass() != null)
83                 scanFields(c.getSuperclass());
84         }
85     }
86
87     public static final String JavaDoc[] columnsName = new String JavaDoc[] { "Name",
88             "FieldType", "currentType", "Value" };
89
90     private static final long serialVersionUID = 1L;
91
92     private JPanel JavaDoc jContentPane = null;
93
94     private JScrollPane JavaDoc jScrollPane = null;
95
96     private JTable JavaDoc jTable = null;
97
98     private Object JavaDoc o;
99
100     /**
101      * This is the default constructor
102      */

103     public SpoonObjectFieldsTable(Object JavaDoc o) {
104         super();
105         this.o = o;
106         initialize();
107     }
108
109     /**
110      * This method initializes jContentPane
111      *
112      * @return javax.swing.JPanel
113      */

114     private JPanel JavaDoc getJContentPane() {
115         if (jContentPane == null) {
116             jContentPane = new JPanel JavaDoc();
117             jContentPane.setLayout(new BorderLayout JavaDoc());
118             jContentPane.add(getJScrollPane(), java.awt.BorderLayout.CENTER);
119         }
120         return jContentPane;
121     }
122
123     /**
124      * This method initializes jScrollPane
125      *
126      * @return javax.swing.JScrollPane
127      */

128     private JScrollPane JavaDoc getJScrollPane() {
129         if (jScrollPane == null) {
130             jScrollPane = new JScrollPane JavaDoc();
131             jScrollPane.setViewportView(getJTable());
132         }
133         return jScrollPane;
134     }
135
136     /**
137      * This method initializes jTable
138      *
139      * @return javax.swing.JTable
140      */

141     private JTable JavaDoc getJTable() {
142         if (jTable == null) {
143             jTable = new JTable JavaDoc(new SpoonObjectTableModel(o));
144         }
145         return jTable;
146     }
147
148     /**
149      * This method initializes this
150      *
151      * @return void
152      */

153     private void initialize() {
154         this.setSize(320, 240);
155         this.setLocation((getGraphicsConfiguration().getDevice()
156                 .getDisplayMode().getWidth() - getWidth()) / 2,
157                 (getGraphicsConfiguration().getDevice().getDisplayMode()
158                         .getHeight() - getHeight()) / 2);
159         this.setContentPane(getJContentPane());
160         this.setTitle(o.getClass().getSimpleName());
161         this.setVisible(true);
162         this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
163     }
164
165 }
166
Popular Tags