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.4 2005/11/16 15:58:41 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    
044    package org.jfree.ui;
045    
046    import java.awt.event.MouseEvent;
047    import java.awt.event.MouseListener;
048    import java.awt.event.MouseMotionListener;
049    
050    import javax.swing.table.JTableHeader;
051    
052    /**
053     * Captures mouse clicks on a table header, with the intention of triggering a sort.  Adapted from
054     * code by Nabuo Tamemasa posted on http://www.codeguru.com.
055     *
056     * @author Nabuo Tamemasa
057     */
058    public class SortableTableHeaderListener implements MouseListener, MouseMotionListener {
059    
060        /** A reference to the table model. */
061        private SortableTableModel model;
062    
063        /** The header renderer. */
064        private SortButtonRenderer renderer;
065    
066        /** The index of the column that is sorted - used to determine the state of the renderer. */
067        private int sortColumnIndex;
068    
069        /**
070         * Standard constructor.
071         *
072         * @param model  the model.
073         * @param renderer  the renderer.
074         */
075        public SortableTableHeaderListener(final SortableTableModel model, 
076                                           final SortButtonRenderer renderer) {
077            this.model = model;
078            this.renderer = renderer;
079        }
080    
081        /**
082         * Sets the table model for the listener.
083         *
084         * @param model  the model.
085         */
086        public void setTableModel(final SortableTableModel model) {
087            this.model = model;
088        }
089    
090        /**
091         * Handle a mouse press event - if the user is NOT resizing a column and NOT dragging a column
092         * then give visual feedback that the column header has been pressed.
093         *
094         * @param e  the mouse event.
095         */
096        public void mousePressed(final MouseEvent e) {
097    
098            final JTableHeader header = (JTableHeader) e.getComponent();
099    
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 e) {
127    
128            final JTableHeader header = (JTableHeader) 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 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 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 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 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 e) {
178    
179            final JTableHeader header = (JTableHeader) 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    }