KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > ui > SortableTableHeaderListener


1 /* ========================================================================
2  * JCommon : a free general purpose class library for the Java(tm) platform
3  * ========================================================================
4  *
5  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jcommon/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA.
23  *
24  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * --------------------------------
28  * SortableTableHeaderListener.java
29  * --------------------------------
30  * (C) Copyright 2000-2004, by Nabuo Tamemasa and Contributors.
31  *
32  * Original Author: Nabuo Tamemasa;
33  * Contributor(s): David Gilbert (for Object Refinery Limited);
34  *
35  * $Id: SortableTableHeaderListener.java,v 1.4 2005/11/16 15:58:41 taqua Exp $
36  *
37  * Changes (from 26-Oct-2001)
38  * --------------------------
39  * 26-Oct-2001 : Changed package to com.jrefinery.ui.*;
40  * 14-Oct-2002 : Fixed errors reported by Checkstyle (DG);
41  *
42  */

43
44 package org.jfree.ui;
45
46 import java.awt.event.MouseEvent JavaDoc;
47 import java.awt.event.MouseListener JavaDoc;
48 import java.awt.event.MouseMotionListener JavaDoc;
49
50 import javax.swing.table.JTableHeader JavaDoc;
51
52 /**
53  * Captures mouse clicks on a table header, with the intention of triggering a sort. Adapted from
54  * code by Nabuo Tamemasa posted on http://www.codeguru.com.
55  *
56  * @author Nabuo Tamemasa
57  */

58 public class SortableTableHeaderListener implements MouseListener JavaDoc, MouseMotionListener JavaDoc {
59
60     /** A reference to the table model. */
61     private SortableTableModel model;
62
63     /** The header renderer. */
64     private SortButtonRenderer renderer;
65
66     /** The index of the column that is sorted - used to determine the state of the renderer. */
67     private int sortColumnIndex;
68
69     /**
70      * Standard constructor.
71      *
72      * @param model the model.
73      * @param renderer the renderer.
74      */

75     public SortableTableHeaderListener(final SortableTableModel model,
76                                        final SortButtonRenderer renderer) {
77         this.model = model;
78         this.renderer = renderer;
79     }
80
81     /**
82      * Sets the table model for the listener.
83      *
84      * @param model the model.
85      */

86     public void setTableModel(final SortableTableModel model) {
87         this.model = model;
88     }
89
90     /**
91      * Handle a mouse press event - if the user is NOT resizing a column and NOT dragging a column
92      * then give visual feedback that the column header has been pressed.
93      *
94      * @param e the mouse event.
95      */

96     public void mousePressed(final MouseEvent JavaDoc e) {
97
98         final JTableHeader JavaDoc header = (JTableHeader JavaDoc) e.getComponent();
99
100         if (header.getResizingColumn() == null) { // resizing takes precedence over sorting
101
if (header.getDraggedDistance() < 1) { // dragging also takes precedence over sorting
102
final int columnIndex = header.columnAtPoint(e.getPoint());
103                 final int modelColumnIndex
104                     = header.getTable().convertColumnIndexToModel(columnIndex);
105                 if (this.model.isSortable(modelColumnIndex)) {
106                     this.sortColumnIndex = header.getTable().convertColumnIndexToModel(columnIndex);
107                     this.renderer.setPressedColumn(this.sortColumnIndex);
108                     header.repaint();
109                     if (header.getTable().isEditing()) {
110                         header.getTable().getCellEditor().stopCellEditing();
111                     }
112                 }
113                 else {
114                     this.sortColumnIndex = -1;
115                 }
116             }
117         }
118
119     }
120
121     /**
122      * If the user is dragging or resizing, then we clear the sort column.
123      *
124      * @param e the mouse event.
125      */

126     public void mouseDragged(final MouseEvent JavaDoc e) {
127
128         final JTableHeader JavaDoc header = (JTableHeader JavaDoc) e.getComponent();
129
130         if ((header.getDraggedDistance() > 0) || (header.getResizingColumn() != null)) {
131             this.renderer.setPressedColumn(-1);
132             this.sortColumnIndex = -1;
133         }
134     }
135
136     /**
137      * This event is ignored (not required).
138      *
139      * @param e the mouse event.
140      */

141     public void mouseEntered(final MouseEvent JavaDoc e) {
142         // not required
143
}
144
145     /**
146      * This event is ignored (not required).
147      *
148      * @param e the mouse event.
149      */

150     public void mouseClicked(final MouseEvent JavaDoc e) {
151         // not required
152
}
153
154     /**
155      * This event is ignored (not required).
156      *
157      * @param e the mouse event.
158      */

159     public void mouseMoved(final MouseEvent JavaDoc e) {
160         // not required
161
}
162
163     /**
164      * This event is ignored (not required).
165      *
166      * @param e the mouse event.
167      */

168     public void mouseExited(final MouseEvent JavaDoc e) {
169         // not required
170
}
171
172     /**
173      * When the user releases the mouse button, we attempt to sort the table.
174      *
175      * @param e the mouse event.
176      */

177     public void mouseReleased(final MouseEvent JavaDoc e) {
178
179         final JTableHeader JavaDoc header = (JTableHeader JavaDoc) e.getComponent();
180
181         if (header.getResizingColumn() == null) { // resizing takes precedence over sorting
182
if (this.sortColumnIndex != -1) {
183                 final SortableTableModel model = (SortableTableModel) header.getTable().getModel();
184                 final boolean ascending = !model.isAscending();
185                 model.setAscending(ascending);
186                 model.sortByColumn(this.sortColumnIndex, ascending);
187
188                 this.renderer.setPressedColumn(-1); // clear
189
header.repaint();
190             }
191         }
192     }
193
194 }
195
Popular Tags