001: package tijmp.ui;
002:
003: import java.awt.Dimension;
004: import java.awt.GridBagConstraints;
005: import java.awt.GridBagLayout;
006: import java.awt.Insets;
007: import java.util.HashMap;
008: import java.util.Map;
009: import javax.swing.JPanel;
010: import javax.swing.JScrollPane;
011: import javax.swing.JTable;
012: import javax.swing.table.AbstractTableModel;
013: import javax.swing.table.TableColumn;
014:
015: /** A panel that shows child object summary.
016: */
017: public class ChildObjectTable extends JPanel {
018:
019: public ChildObjectTable(Object[] objects) {
020: Map<Class<?>, Integer> m = new HashMap<Class<?>, Integer>();
021: for (Object o : objects) {
022: Class<?> c = o.getClass();
023: Integer i = m.get(c);
024: if (i == null) {
025: m.put(c, 1);
026: } else {
027: m.put(c, i + 1);
028: }
029: }
030: Class<?>[] cs = new Class<?>[m.size()];
031: int[] is = new int[m.size()];
032: int i = 0;
033: for (Map.Entry<Class<?>, Integer> me : m.entrySet()) {
034: cs[i] = me.getKey();
035: is[i] = me.getValue();
036: i++;
037: }
038: COTM cotm = new COTM(cs, is);
039: JTable table = new JTable(cotm);
040: JScrollPane sp = new JScrollPane(table);
041: table.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
042: table.setAutoCreateRowSorter(true);
043: TableColumn column = table.getColumnModel().getColumn(
044: COTM.COL_CLASS);
045: column.setPreferredWidth(200);
046: table
047: .setPreferredScrollableViewportSize(new Dimension(300,
048: 100));
049: table.getRowSorter().toggleSortOrder(COTM.COL_COUNT);
050: table.getRowSorter().toggleSortOrder(COTM.COL_COUNT);
051: table.setDefaultRenderer(Class.class, new ClassRenderer());
052:
053: GridBagLayout gb = new GridBagLayout();
054: GridBagConstraints c = new GridBagConstraints();
055: setLayout(gb);
056: c.insets = new Insets(2, 2, 2, 2);
057:
058: c.gridx = 0;
059: c.gridy = 0;
060: c.weightx = 1;
061: c.weighty = 1;
062: c.fill = GridBagConstraints.BOTH;
063: add(sp, c);
064: }
065: }
066:
067: class COTM extends AbstractTableModel {
068: public static final int COL_CLASS = 0;
069: public static final int COL_COUNT = COL_CLASS + 1;
070:
071: private static final String[] columnNames = { "Class", "Count" };
072:
073: private Class<?>[] cs;
074: private int[] is;
075:
076: public COTM(Class<?>[] cs, int[] is) {
077: this .cs = cs;
078: this .is = is;
079: }
080:
081: public String getColumnName(int col) {
082: return columnNames[col];
083: }
084:
085: public int getRowCount() {
086: return cs.length;
087: }
088:
089: public int getColumnCount() {
090: return columnNames.length;
091: }
092:
093: public Object getValueAt(int row, int col) {
094: switch (col) {
095: case COL_CLASS:
096: return cs[row];
097: case COL_COUNT:
098: return is[row];
099: }
100: throw new IllegalStateException("unknown column: " + col);
101: }
102:
103: public Class<?> getColumnClass(int col) {
104: switch (col) {
105: case COL_CLASS:
106: return Class.class;
107: case COL_COUNT:
108: return Integer.class;
109: }
110: throw new IllegalArgumentException("unknown column: " + col);
111: }
112:
113: public boolean isCellEditable(int row, int col) {
114: return false;
115: }
116:
117: public void setValueAt(Object value, int row, int col) {
118: throw new IllegalStateException("non editable table");
119: }
120: }
|