01: /*
02: * SortHeaderRenderer.java
03: *
04: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
05: *
06: * Copyright 2002-2008, Thomas Kellerer
07: * No part of this code maybe reused without the permission of the author
08: *
09: * To contact the author please send an email to: support@sql-workbench.net
10: *
11: */
12: package workbench.gui.components;
13:
14: import java.awt.Component;
15:
16: import javax.swing.JTable;
17: import javax.swing.UIManager;
18: import javax.swing.border.Border;
19: import javax.swing.border.CompoundBorder;
20: import javax.swing.border.EmptyBorder;
21: import javax.swing.table.DefaultTableCellRenderer;
22:
23: public class SortHeaderRenderer extends DefaultTableCellRenderer {
24: private static Border DEFAULT_HEADER_BORDER = new CompoundBorder(
25: UIManager.getBorder("TableHeader.cellBorder"),
26: new EmptyBorder(0, 1, 0, 1));
27:
28: public SortHeaderRenderer() {
29: setHorizontalTextPosition(LEFT);
30: setHorizontalAlignment(LEFT);
31: setBorder(DEFAULT_HEADER_BORDER);
32: }
33:
34: public Component getTableCellRendererComponent(JTable table,
35: Object value, boolean isSelected, boolean hasFocus,
36: int row, int col) {
37: boolean sorted = false;
38: boolean ascending = false;
39: boolean primary = false;
40:
41: String type = null;
42:
43: if (table instanceof WbTable) {
44: WbTable sortTable = (WbTable) table;
45:
46: sorted = sortTable.isViewColumnSorted(col);
47: if (sorted) {
48: ascending = sortTable.isViewColumnSortAscending(col);
49: primary = sortTable.isPrimarySortColumn(col);
50: }
51: DataStoreTableModel model = sortTable
52: .getDataStoreTableModel();
53: if (model != null) {
54: type = model.getDbmsType(col);
55: }
56: }
57:
58: if (sorted) {
59: if (primary) {
60: this .setIcon(ascending ? SortArrowIcon.ARROW_DOWN
61: : SortArrowIcon.ARROW_UP);
62: } else {
63: this .setIcon(ascending ? SortArrowIcon.SMALL_ARROW_DOWN
64: : SortArrowIcon.SMALL_ARROW_UP);
65: }
66: } else {
67: this .setIcon(null);
68: }
69: String text = (value == null) ? "" : value.toString();
70: setText(text);
71:
72: if (type == null) {
73: setToolTipText(text);
74: } else {
75: StringBuilder tip = new StringBuilder(text.length() + 20);
76: tip.append("<html> ");
77: tip.append(text);
78: tip.append(" <br> <code>");
79: tip.append(type);
80: tip.append("</code> </html>");
81: setToolTipText(tip.toString());
82: }
83: return this;
84: }
85: }
|