1 package spoon.support.gui; 2 3 import java.awt.BorderLayout ; 4 import java.lang.reflect.Field ; 5 import java.lang.reflect.Modifier ; 6 import java.util.ArrayList ; 7 import java.util.List ; 8 9 import javax.swing.JFrame ; 10 import javax.swing.JPanel ; 11 import javax.swing.JScrollPane ; 12 import javax.swing.JTable ; 13 import javax.swing.table.AbstractTableModel ; 14 15 public class SpoonObjectFieldsTable extends JFrame { 16 public class SpoonObjectTableModel extends AbstractTableModel { 17 18 private static final long serialVersionUID = 1L; 19 20 List <Field > field; 21 22 Object o; 23 24 public SpoonObjectTableModel(Object o) { 25 super(); 26 27 this.o = o; 28 field = new ArrayList <Field >(); 29 30 scanFields(o.getClass()); 31 } 32 33 public int getColumnCount() { 34 return columnsName.length; 35 } 36 37 @Override 38 public String getColumnName(int column) { 39 return columnsName[column]; 40 } 41 42 public int getRowCount() { 43 return field.size(); 44 } 45 46 public Object getValueAt(int rowIndex, int columnIndex) { 47 Field 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 val = m.get(o); 56 if (val != null) 57 return val.getClass().getCanonicalName(); 58 } catch (IllegalArgumentException e) { 59 e.printStackTrace(); 60 } catch (IllegalAccessException e) { 61 e.printStackTrace(); 62 } 63 break; 64 case (3): 65 try { 66 return m.get(o); 67 } catch (IllegalArgumentException e) { 68 e.printStackTrace(); 69 } catch (IllegalAccessException e) { 70 e.printStackTrace(); 71 } 72 } 73 return null; 74 } 75 76 public void scanFields(Class <?> c) { 77 for (Field 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 [] columnsName = new String [] { "Name", 88 "FieldType", "currentType", "Value" }; 89 90 private static final long serialVersionUID = 1L; 91 92 private JPanel jContentPane = null; 93 94 private JScrollPane jScrollPane = null; 95 96 private JTable jTable = null; 97 98 private Object o; 99 100 103 public SpoonObjectFieldsTable(Object o) { 104 super(); 105 this.o = o; 106 initialize(); 107 } 108 109 114 private JPanel getJContentPane() { 115 if (jContentPane == null) { 116 jContentPane = new JPanel (); 117 jContentPane.setLayout(new BorderLayout ()); 118 jContentPane.add(getJScrollPane(), java.awt.BorderLayout.CENTER); 119 } 120 return jContentPane; 121 } 122 123 128 private JScrollPane getJScrollPane() { 129 if (jScrollPane == null) { 130 jScrollPane = new JScrollPane (); 131 jScrollPane.setViewportView(getJTable()); 132 } 133 return jScrollPane; 134 } 135 136 141 private JTable getJTable() { 142 if (jTable == null) { 143 jTable = new JTable (new SpoonObjectTableModel(o)); 144 } 145 return jTable; 146 } 147 148 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 |