1
2 package hep.wired.util;
3
4 import java.awt.*;
5 import java.awt.event.*;
6 import javax.swing.*;
7 import javax.swing.event.*;
8 import javax.swing.table.*;
9 import java.util.*;
10
11 import org.freehep.swing.table.TableSorter;
12 import org.freehep.swing.table.TableColumnPacker;
13 import org.freehep.util.ScientificFormat;
14
15 /***
16 * Improved table supporting, variable width columns, multi-type cells per column, Color as a celltype,
17 * sorting on columns, tooltips on cells, etc.
18 *
19 * @author Mark Donszelmann
20 * @version $Id: ScientificTable.java 1934 2005-06-21 18:01:41Z duns $
21 */
22 public class ScientificTable extends JTable {
23
24 private TableColumnPacker packer = new TableColumnPacker();
25 private ScientificFormat format = new ScientificFormat();
26
27 public ScientificTable() {
28 super();
29 init();
30 }
31
32 public ScientificTable(int rows, int columns) {
33 super(rows, columns);
34 init();
35 }
36
37 public ScientificTable(Object[][] rowData, Object[] columnNames) {
38 super(rowData, columnNames);
39 init();
40 }
41
42 public ScientificTable(TableModel dm) {
43 super(dm);
44 init();
45 }
46
47 public ScientificTable(TableModel dm, TableColumnModel cm) {
48 super(dm, cm);
49 init();
50 }
51
52 public ScientificTable(TableModel dm, TableColumnModel cm, ListSelectionModel sm) {
53 super(dm, cm, sm);
54 init();
55 }
56
57 public ScientificTable(Vector rowData, Vector columnNames) {
58 super(rowData, columnNames);
59 init();
60 }
61
62 private void init() {
63
64 setDefaultRenderer(Color.class, new DefaultTableCellRenderer() {
65 protected void setValue(Object value) {
66 setText(null);
67 setBackground((Color)value);
68 }
69 });
70
71
72 setDefaultRenderer(Double.class, new DefaultTableCellRenderer() {
73 protected void setValue(Object value) {
74 setText(format.format(((Double)value).doubleValue()));
75 setHorizontalAlignment(RIGHT);
76 }
77 });
78
79
80 new TableSorter(this);
81
82
83 packer.setHeaderMargin(15);
84 }
85
86
87 public TableCellRenderer getCellRenderer(int row, int column) {
88 Object value = getValueAt(row, column);
89 return (value != null) ? getDefaultRenderer(value.getClass()) : super.getCellRenderer(row, column);
90 }
91
92
93 public Component prepareRenderer(TableCellRenderer renderer,
94 int rowIndex, int vColIndex) {
95 Component c = super.prepareRenderer(renderer, rowIndex, vColIndex);
96 if (c instanceof JComponent) {
97 ((JComponent)c).setToolTipText(getValueAt(rowIndex, vColIndex).toString());
98 }
99 return c;
100 }
101
102 public void tableChanged(TableModelEvent event) {
103 super.tableChanged(event);
104 if (packer != null) packer.packColumns(this);
105 }
106
107 public void doLayout() {
108 super.doLayout();
109 }
110 }