1 /***
2 * SortableColumnTableHeaderRenderer.java
3 *
4 * This file is part of the creme library.
5 * The creme library intends to ease the development effort of large
6 * applications by providing easy to use support classes.
7 *
8 * Copyright (C) 2002 Denis Bregeon
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
24 * contact information: dbregeon@sourceforge.net
25 */
26 package org.jcreme.swing.table;
27
28 import java.awt.Component;
29
30 import javax.swing.ImageIcon;
31 import javax.swing.JLabel;
32 import javax.swing.JTable;
33 import javax.swing.SwingConstants;
34 import javax.swing.UIManager;
35 import javax.swing.table.JTableHeader;
36 import javax.swing.table.TableCellRenderer;
37 import javax.swing.table.TableColumn;
38
39 import org.jcreme.enumerations.SortOrder;
40
41 /***
42 * This class enables to display an icon in the table header for a column,
43 * depending on the order of the column.
44 *
45 * @author $Author: dbregeon $
46 * @version $Revision: 1.1 $
47 */
48 public class SortableColumnTableHeaderRenderer extends JLabel implements
49 TableCellRenderer, TableColumnOrderListener {
50 /***
51 * The column that for which this object displays the header.
52 */
53 private SortableTableColumn column = null;
54
55 /***
56 * The ascending icon.
57 */
58 private ImageIcon ascIcon = null;
59
60 /***
61 * The descending icon.
62 */
63 private ImageIcon descIcon = null;
64
65 /***
66 * Creates new SortableColumnTableHeaderRenderer
67 *
68 * @param column
69 * the column that is presented by this renderer.
70 */
71 public SortableColumnTableHeaderRenderer(SortableTableColumn column) {
72 this(column, null, null);
73 }
74
75 /***
76 * Creates new SortableColumnTableHeaderRenderer
77 *
78 * @param column
79 * the column that is presented by this renderer.
80 * @param ascendingIcon
81 * the icon that is displayed in the renderer when the column is
82 * sorted in the ascending order.
83 * @param descendingIcon
84 * the icon that is displayed in the renderer when the column is
85 * sorted in the descending order.
86 */
87 public SortableColumnTableHeaderRenderer(SortableTableColumn column,
88 ImageIcon ascendingIcon, ImageIcon descendingIcon) {
89 this.column = column;
90 this.column.addTableColumnOrderListener(this);
91 this.ascIcon = ascendingIcon;
92 this.descIcon = descendingIcon;
93
94 this.setHorizontalTextPosition(SwingConstants.LEADING);
95
96 this.setHorizontalAlignment(SwingConstants.CENTER);
97 }
98
99 /***
100 * This method gives access to the actual renderer of the column header.
101 *
102 * @param table
103 * @param value
104 * @param isSelected
105 * @param hasFocus
106 * @param row
107 * @param col
108 *
109 * @return the component to use to display the column header.
110 */
111 public Component getTableCellRendererComponent(JTable table, Object value,
112 boolean isSelected, boolean hasFocus, int row, int col) {
113 if (table != null) {
114 JTableHeader header = table.getTableHeader();
115 if (header != null) {
116 setForeground(header.getForeground());
117 setBackground(header.getBackground());
118 setFont(header.getFont());
119 }
120 TableColumn tc = table.getColumnModel().getColumn(col);
121 if (tc instanceof SortableTableColumn) {
122 SortableTableColumn c = (SortableTableColumn) tc;
123 if (c.getOrder() == SortOrder.ASCENDING) {
124 setIcon(this.ascIcon);
125 } else if (c.getOrder() == SortOrder.DESCENDING) {
126 setIcon(this.descIcon);
127 } else {
128 setIcon(null);
129 }
130 }
131 }
132 setText((value == null) ? "" : value.toString());
133 setBorder(UIManager.getBorder("TableHeader.cellBorder"));
134 return this;
135 }
136
137 /***
138 * This is the method that is called when the order of the column is
139 * changed.
140 *
141 * @param e
142 * the method to invalidate the renderer whenever the order on
143 * the column changes.
144 */
145 public void tableColumnOrderChanged(TableColumnOrderEvent e) {
146 this.invalidate();
147 }
148
149 /***
150 * Sets the icon to be displayed in the column header when the column is
151 * sorted in the ascending order.
152 *
153 * @param icon
154 * the icon to be used when this column is sorted by ascending
155 * values.
156 */
157 public void setAscIcon(ImageIcon icon) {
158 this.ascIcon = icon;
159 }
160
161 /***
162 * Gives access to the icon to be displayed in the column header when the
163 * column is sorted in the ascending order.
164 *
165 * @return the icon currently used when the column when it is sorted by
166 * ascending values.
167 */
168 public ImageIcon getAscIcon() {
169 return this.ascIcon;
170 }
171
172 /***
173 * Sets the icon to be displayed in the column header when the column is
174 * sorted in the descending order.
175 *
176 * @param icon
177 * the icon to be used when this column is sorted by descending
178 * values.
179 */
180 public void setDescIcon(ImageIcon icon) {
181 this.descIcon = icon;
182 }
183
184 /***
185 * Gives access to the icon to be displayed in the column header when the
186 * column is sorted in the descending order.
187 *
188 * @return the icon currently used when the column when it is sorted by
189 * descending values.
190 */
191 public ImageIcon getDescIcon() {
192 return this.descIcon;
193 }
194 }