Frames | No Frames |
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.5 2007/11/02 17:50:36 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; 47: import java.awt.event.MouseListener; 48: import java.awt.event.MouseMotionListener; 49: import javax.swing.table.JTableHeader; 50: 51: /** 52: * Captures mouse clicks on a table header, with the intention of triggering a sort. Adapted from 53: * code by Nabuo Tamemasa posted on http://www.codeguru.com. 54: * 55: * @author Nabuo Tamemasa 56: */ 57: public class SortableTableHeaderListener implements MouseListener, MouseMotionListener { 58: 59: /** A reference to the table model. */ 60: private SortableTableModel model; 61: 62: /** The header renderer. */ 63: private SortButtonRenderer renderer; 64: 65: /** The index of the column that is sorted - used to determine the state of the renderer. */ 66: private int sortColumnIndex; 67: 68: /** 69: * Standard constructor. 70: * 71: * @param model the model. 72: * @param renderer the renderer. 73: */ 74: public SortableTableHeaderListener(final SortableTableModel model, 75: final SortButtonRenderer renderer) { 76: this.model = model; 77: this.renderer = renderer; 78: } 79: 80: /** 81: * Sets the table model for the listener. 82: * 83: * @param model the model. 84: */ 85: public void setTableModel(final SortableTableModel model) { 86: this.model = model; 87: } 88: 89: /** 90: * Handle a mouse press event - if the user is NOT resizing a column and NOT dragging a column 91: * then give visual feedback that the column header has been pressed. 92: * 93: * @param e the mouse event. 94: */ 95: public void mousePressed(final MouseEvent e) { 96: 97: final JTableHeader header = (JTableHeader) e.getComponent(); 98: 99: 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: }