001: package tijmp.ui;
002:
003: import java.awt.GridBagLayout;
004: import java.awt.GridBagConstraints;
005: import java.awt.Insets;
006: import java.awt.event.ActionEvent;
007: import java.awt.event.ActionListener;
008: import java.lang.reflect.Field;
009: import java.lang.reflect.Modifier;
010: import java.util.ArrayList;
011: import java.util.List;
012: import javax.swing.JComboBox;
013: import javax.swing.JLabel;
014: import javax.swing.JList;
015: import javax.swing.JPanel;
016: import javax.swing.JScrollPane;
017:
018: /** A class that displays a class fields, the list of super classes.
019: */
020: public class ClassInspector extends JPanel {
021:
022: private JList interfaces;
023: private JList staticFields;
024: private JList instanceFields;
025:
026: public ClassInspector(Class<?> clz) {
027: JComboBox cb = getClassCombo(clz);
028: interfaces = getInterfaces(clz);
029: staticFields = getFields(clz, new StaticFieldFilter());
030: instanceFields = getFields(clz, new InstanceFieldFilter());
031:
032: GridBagLayout gb = new GridBagLayout();
033: GridBagConstraints gc = new GridBagConstraints();
034: setLayout(gb);
035:
036: gc.insets = new Insets(2, 2, 2, 2);
037: gc.gridx = 1;
038: gc.gridy = 1;
039: gc.fill = GridBagConstraints.HORIZONTAL;
040: gc.anchor = GridBagConstraints.NORTHWEST;
041: add(new JLabel("Class"), gc);
042: gc.gridy++;
043: add(new JLabel("Interfaces"), gc);
044: gc.gridy++;
045: add(new JLabel("Static fields"), gc);
046: gc.gridy++;
047: add(new JLabel("Instance fields"), gc);
048:
049: gc.gridx = 2;
050: gc.gridy = 1;
051: gc.weightx = 1;
052: gc.fill = GridBagConstraints.HORIZONTAL;
053: add(cb, gc);
054: gc.gridy++;
055: gc.weighty = 1;
056: gc.fill = GridBagConstraints.BOTH;
057: add(new JScrollPane(interfaces), gc);
058: gc.gridy++;
059: add(new JScrollPane(staticFields), gc);
060: gc.gridy++;
061: gc.weighty = 3;
062: add(new JScrollPane(instanceFields), gc);
063: }
064:
065: private static interface FieldFilter {
066: boolean accept(Field f);
067: }
068:
069: private static class InstanceFieldFilter implements FieldFilter {
070: public boolean accept(Field f) {
071: return !Modifier.isStatic(f.getModifiers());
072: }
073: }
074:
075: private static class StaticFieldFilter implements FieldFilter {
076: public boolean accept(Field f) {
077: return Modifier.isStatic(f.getModifiers());
078: }
079: }
080:
081: private JList getFields(Class<?> clz, FieldFilter ff) {
082: Field[] fields = clz.getDeclaredFields();
083: if (fields.length == 0)
084: return new JList();
085: List<Field> ls = new ArrayList<Field>(fields.length);
086: for (Field f : fields) {
087: if (ff.accept(f))
088: ls.add(f);
089: }
090: fields = new Field[ls.size()];
091: ls.toArray(fields);
092: JList jl = new JList(fields);
093: jl.setCellRenderer(new FieldRenderer());
094: return jl;
095: }
096:
097: private JList getInterfaces(Class<?> clz) {
098: Class<?>[] classes = clz.getInterfaces();
099: if (classes.length == 0)
100: return new JList();
101: JList ls = new JList(classes);
102: ls.setCellRenderer(new ListClassRenderer());
103: return ls;
104: }
105:
106: private JComboBox getClassCombo(Class<?> sc) {
107: List<Class<?>> classes = new ArrayList<Class<?>>();
108: while (sc != null) {
109: classes.add(0, sc);
110: sc = sc.getSuperclass();
111: }
112: JComboBox cb = new JComboBox(classes.toArray());
113: cb.setSelectedIndex(classes.size() - 1);
114: cb.setRenderer(new ListClassRenderer());
115: cb.addActionListener(new SetClass());
116: return cb;
117: }
118:
119: private class SetClass implements ActionListener {
120: public void actionPerformed(ActionEvent e) {
121: JComboBox cb = (JComboBox) e.getSource();
122: Class clz = (Class) cb.getSelectedItem();
123: JList ni = getInterfaces(clz);
124: interfaces.setModel(ni.getModel());
125: JList nsf = getFields(clz, new StaticFieldFilter());
126: staticFields.setModel(nsf.getModel());
127: JList nif = getFields(clz, new InstanceFieldFilter());
128: instanceFields.setModel(nif.getModel());
129: }
130: }
131: }
|