Source code: dexter/swingExtensions/sortedTable/SortableTableHeaderRenderer.java
1 /*
2 * SortableTableHeaderRenderer.java
3 *
4 * Created on October 27, 2002, 9:21 PM
5 */
6
7 package dexter.swingExtensions.sortedTable;
8
9 import javax.swing.table.*;
10 import javax.swing.*;
11 import java.awt.*;
12 import java.util.*;
13
14 /**
15 * <p>
16 *
17 * Title: </p> <p>
18 *
19 * Description: </p> <p>
20 *
21 * Copyright: Copyright (c) 2001</p> <p>
22 *
23 * Company: </p>
24 *
25 *@author unascribed
26 *@created 19. Jänner 2002
27 *@version 1.0
28 */
29
30 public class SortableTableHeaderRenderer extends DefaultTableCellRenderer {
31
32 private SortableTableModel sorter = null;
33
34 private boolean paintSortDirection = false;
35
36 private int columnIndex = 0;
37
38 //private String toolTipText;
39
40 /**
41 * Constructor for the BuddyTableCellRenderer object
42 */
43 protected SortableTableHeaderRenderer() {
44 super();
45 }
46
47
48 /**
49 * Constructor for the BuddyTableCellRenderer object
50 *
51 *@param sorter Description of Parameter
52 */
53 public SortableTableHeaderRenderer(SortableTableModel sorter) {
54 this();
55
56 this.sorter = sorter;
57 paintSortDirection = true;
58 }
59
60
61 /**
62 * Gets the tableCellRendererComponent attribute of the
63 * BuddyTableCellRenderer object
64 *
65 *@param table Description of Parameter
66 *@param value Description of Parameter
67 *@param isSelected Description of Parameter
68 *@param hasFocus Description of Parameter
69 *@param row Description of Parameter
70 *@param column Description of Parameter
71 *@return The tableCellRendererComponent value
72 */
73 public Component getTableCellRendererComponent(JTable table, Object value,
74 boolean isSelected, boolean hasFocus, int row, int column) {
75
76 if (table != null) {
77 JTableHeader header = table.getTableHeader();
78 if (header != null) {
79 setForeground(header.getForeground());
80 setBackground(header.getBackground());
81 setFont(header.getFont());
82 }
83 }
84
85 setText((value == null) ? "" : " " + value.toString());
86 setBorder(UIManager.getBorder("TableHeader.cellBorder"));
87 setHorizontalAlignment(JLabel.LEFT);
88 //if (toolTipText != null) super.setToolTipText(toolTipText);
89 columnIndex = column;
90 return this;
91 }
92
93 /*public void setToolTipText(String toolTipText) {
94 this.toolTipText = toolTipText;
95 }*/
96
97
98 /**
99 * Gets the paintSortDirection attribute of the BuddyTableCellRenderer
100 * object
101 *
102 *@return The paintSortDirection value
103 */
104 public boolean isPaintSortDirection() {
105 return paintSortDirection;
106 }
107
108
109 /**
110 * Description of the Method
111 *
112 *@param g Description of Parameter
113 */
114 protected void paintComponent(Graphics g) {
115 //int sortDirection = comparator.getAsc(propDesc);
116
117 super.paintComponent(g);
118 int x = super.getWidth();
119 int y = super.getHeight();
120
121 if (isPaintSortDirection()) {
122 int sortDirection = 0;
123
124 if (sorter.isSorted(columnIndex)) {
125 if (sorter.isAscending(columnIndex)) {
126 sortDirection = 1;
127 } else {
128 sortDirection = 2;
129 }
130 }
131
132 if (sortDirection == 1) {
133 g.setColor(Color.white);
134 g.drawLine(x - 8, 6, x - 4, 14);
135 g.drawLine(x - 12, 14, x - 4, 14);
136 g.setColor(Color.darkGray);
137 g.drawLine(x - 8, 6, x - 12, 14);
138 }
139 if (sortDirection == 2) {
140
141 g.setColor(Color.darkGray);
142 g.drawLine(x - 8, 14, x - 12, 6);
143 g.drawLine(x - 12, 6, x - 4, 6);
144 g.setColor(Color.white);
145 g.drawLine(x - 8, 14, x - 4, 6);
146 }
147 }
148 }
149 }