01: /*
02: * Copyright 2001-2006 C:1 Financial Services GmbH
03: *
04: * This software is free software; you can redistribute it and/or
05: * modify it under the terms of the GNU Lesser General Public
06: * License Version 2.1, as published by the Free Software Foundation.
07: *
08: * This software is distributed in the hope that it will be useful,
09: * but WITHOUT ANY WARRANTY; without even the implied warranty of
10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11: * Lesser General Public License for more details.
12: *
13: * You should have received a copy of the GNU Lesser General Public
14: * License along with this library; if not, write to the Free Software
15: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
16: */
17:
18: package de.finix.contelligent.client.util;
19:
20: import java.awt.event.MouseListener;
21:
22: import javax.swing.JTable;
23: import javax.swing.event.TableModelEvent;
24:
25: /**
26: *
27: * Table that can be sorted by clicking on headers.
28: */
29: public class SortableTable extends JTable implements MouseListener {
30:
31: private boolean[] sortDirectionDescending;
32:
33: private TableModelSorter sorter;
34:
35: public SortableTable(TableModelSorter sorter) {
36: super (sorter);
37: this .sorter = sorter;
38:
39: getTableHeader().addMouseListener(this );
40: initSortDirections(false);
41: }
42:
43: public void tableChanged(TableModelEvent e) {
44: initSortDirections(false);
45: super .tableChanged(e);
46: }
47:
48: public void mouseClicked(java.awt.event.MouseEvent e) {
49: if (e.getClickCount() == 1) {
50: javax.swing.table.TableColumnModel columnModel = getColumnModel();
51: int viewColumn = columnModel.getColumnIndexAtX(e.getX());
52: int column = convertColumnIndexToModel(viewColumn);
53: if (column != -1
54: && sorter.getModel().isColumnSortable(column)) {
55: sorter.sort(column, getSortDirectionForColumn(column));
56: toggleSortDirectionForColumn(column);
57: }
58: }
59: }
60:
61: public void mouseEntered(java.awt.event.MouseEvent e) {
62: }
63:
64: public void mouseExited(java.awt.event.MouseEvent e) {
65: }
66:
67: public void mousePressed(java.awt.event.MouseEvent e) {
68: }
69:
70: public void mouseReleased(java.awt.event.MouseEvent e) {
71: }
72:
73: private void initSortDirections(boolean value) {
74: int columns = getModel().getColumnCount();
75: sortDirectionDescending = new boolean[columns];
76: for (int i = 0; i < columns; i++) {
77: sortDirectionDescending[i] = value;
78: }
79: }
80:
81: public boolean getSortDirectionForColumn(int column) {
82: return sortDirectionDescending[column];
83: }
84:
85: protected void toggleSortDirectionForColumn(int column) {
86: sortDirectionDescending[column] = !sortDirectionDescending[column];
87: }
88: }
|