001/* ========================================================================
002 * JCommon : a free general purpose class library for the Java(tm) platform
003 * ========================================================================
004 *
005 * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
006 * 
007 * Project Info:  http://www.jfree.org/jcommon/index.html
008 *
009 * This library is free software; you can redistribute it and/or modify it 
010 * under the terms of the GNU Lesser General Public License as published by 
011 * the Free Software Foundation; either version 2.1 of the License, or 
012 * (at your option) any later version.
013 *
014 * This library is distributed in the hope that it will be useful, but 
015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
017 * License for more details.
018 *
019 * You should have received a copy of the GNU Lesser General Public
020 * License along with this library; if not, write to the Free Software
021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
022 * USA.  
023 *
024 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
025 * in the United States and other countries.]
026 * 
027 * --------------------------------
028 * SortableTableHeaderListener.java
029 * --------------------------------
030 * (C) Copyright 2000-2004, by Nabuo Tamemasa and Contributors.
031 *
032 * Original Author:  Nabuo Tamemasa;
033 * Contributor(s):   David Gilbert (for Object Refinery Limited);
034 *
035 * $Id: SortableTableHeaderListener.java,v 1.5 2007/11/02 17:50:36 taqua Exp $
036 *
037 * Changes (from 26-Oct-2001)
038 * --------------------------
039 * 26-Oct-2001 : Changed package to com.jrefinery.ui.*;
040 * 14-Oct-2002 : Fixed errors reported by Checkstyle (DG);
041 *
042 */
043
044package org.jfree.ui;
045
046import java.awt.event.MouseEvent;
047import java.awt.event.MouseListener;
048import java.awt.event.MouseMotionListener;
049import javax.swing.table.JTableHeader;
050
051/**
052 * Captures mouse clicks on a table header, with the intention of triggering a sort.  Adapted from
053 * code by Nabuo Tamemasa posted on http://www.codeguru.com.
054 *
055 * @author Nabuo Tamemasa
056 */
057public class SortableTableHeaderListener implements MouseListener, MouseMotionListener {
058
059    /** A reference to the table model. */
060    private SortableTableModel model;
061
062    /** The header renderer. */
063    private SortButtonRenderer renderer;
064
065    /** The index of the column that is sorted - used to determine the state of the renderer. */
066    private int sortColumnIndex;
067
068    /**
069     * Standard constructor.
070     *
071     * @param model  the model.
072     * @param renderer  the renderer.
073     */
074    public SortableTableHeaderListener(final SortableTableModel model, 
075                                       final SortButtonRenderer renderer) {
076        this.model = model;
077        this.renderer = renderer;
078    }
079
080    /**
081     * Sets the table model for the listener.
082     *
083     * @param model  the model.
084     */
085    public void setTableModel(final SortableTableModel model) {
086        this.model = model;
087    }
088
089    /**
090     * Handle a mouse press event - if the user is NOT resizing a column and NOT dragging a column
091     * then give visual feedback that the column header has been pressed.
092     *
093     * @param e  the mouse event.
094     */
095    public void mousePressed(final MouseEvent e) {
096
097        final JTableHeader header = (JTableHeader) e.getComponent();
098
099        if (header.getResizingColumn() == null) {  // resizing takes precedence over sorting
100            if (header.getDraggedDistance() < 1) {   // dragging also takes precedence over sorting
101                final int columnIndex = header.columnAtPoint(e.getPoint());
102                final int modelColumnIndex 
103                    = header.getTable().convertColumnIndexToModel(columnIndex);
104                if (this.model.isSortable(modelColumnIndex)) {
105                    this.sortColumnIndex = header.getTable().convertColumnIndexToModel(columnIndex);
106                    this.renderer.setPressedColumn(this.sortColumnIndex);
107                    header.repaint();
108                    if (header.getTable().isEditing()) {
109                        header.getTable().getCellEditor().stopCellEditing();
110                    }
111                }
112                else {
113                    this.sortColumnIndex = -1;
114                }
115            }
116        }
117
118    }
119
120    /**
121     * If the user is dragging or resizing, then we clear the sort column.
122     *
123     * @param e  the mouse event.
124     */
125    public void mouseDragged(final MouseEvent e) {
126
127        final JTableHeader header = (JTableHeader) e.getComponent();
128
129        if ((header.getDraggedDistance() > 0) || (header.getResizingColumn() != null)) {
130            this.renderer.setPressedColumn(-1);
131            this.sortColumnIndex = -1;
132        }
133    }
134
135    /**
136     * This event is ignored (not required).
137     *
138     * @param e  the mouse event.
139     */
140    public void mouseEntered(final MouseEvent e) {
141        // not required
142    }
143
144    /**
145     * This event is ignored (not required).
146     *
147     * @param e  the mouse event.
148     */
149    public void mouseClicked(final MouseEvent e) {
150        // not required
151    }
152
153    /**
154     * This event is ignored (not required).
155     *
156     * @param e  the mouse event.
157     */
158    public void mouseMoved(final MouseEvent e) {
159        // not required
160    }
161
162    /**
163     * This event is ignored (not required).
164     *
165     * @param e  the mouse event.
166     */
167    public void mouseExited(final MouseEvent e) {
168        // not required
169    }
170
171    /**
172     * When the user releases the mouse button, we attempt to sort the table.
173     *
174     * @param e  the mouse event.
175     */
176    public void mouseReleased(final MouseEvent e) {
177
178        final JTableHeader header = (JTableHeader) e.getComponent();
179
180        if (header.getResizingColumn() == null) {  // resizing takes precedence over sorting
181            if (this.sortColumnIndex != -1) {
182                final SortableTableModel model = (SortableTableModel) header.getTable().getModel();
183                final boolean ascending = !model.isAscending();
184                model.setAscending(ascending);
185                model.sortByColumn(this.sortColumnIndex, ascending);
186
187                this.renderer.setPressedColumn(-1);       // clear
188                header.repaint();
189            }
190        }
191    }
192
193}