001    /*
002     * $Id: TableRolloverController.java 3100 2008-10-14 22:33:10Z rah003 $
003     *
004     * Copyright 2008 Sun Microsystems, Inc., 4150 Network Circle,
005     * Santa Clara, California 95054, U.S.A. All rights reserved.
006     *
007     * This library is free software; you can redistribute it and/or
008     * modify it under the terms of the GNU Lesser General Public
009     * License as published by the Free Software Foundation; either
010     * version 2.1 of the License, or (at your option) any later version.
011     *
012     * This library is distributed in the hope that it will be useful,
013     * but WITHOUT ANY WARRANTY; without even the implied warranty of
014     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
015     * Lesser General Public License for more details.
016     *
017     * You should have received a copy of the GNU Lesser General Public
018     * License along with this library; if not, write to the Free Software
019     * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
020     */
021    package org.jdesktop.swingx.rollover;
022    
023    import java.awt.Cursor;
024    import java.awt.Point;
025    import java.awt.Rectangle;
026    
027    import javax.swing.JTable;
028    import javax.swing.table.TableCellRenderer;
029    
030    
031    /**
032         * listens to rollover properties. 
033         * Repaints effected component regions.
034         * Updates link cursor.
035         * 
036         * @author Jeanette Winzenburg
037         */
038        public class TableRolloverController<T extends JTable>  extends RolloverController<T> {
039    
040            private Cursor oldCursor;
041    
042    //    --------------------------- JTable rollover
043            
044            @Override
045            protected void rollover(Point oldLocation, Point newLocation) {
046                if (oldLocation != null) {
047                    Rectangle r = component.getCellRect(oldLocation.y, oldLocation.x, false);
048                    r.x = 0;
049                    r.width = component.getWidth();
050                    component.repaint(r);
051                }
052                if (newLocation != null) {
053                    Rectangle r = component.getCellRect(newLocation.y, newLocation.x, false);
054                    r.x = 0;
055                    r.width = component.getWidth();
056                    component.repaint(r);
057                }
058                setRolloverCursor(newLocation);
059            }
060    
061            /**
062             * overridden to return false if cell editable.
063             */
064            @Override
065            protected boolean isClickable(Point location) {
066                return super.isClickable(location) && !component.isCellEditable(location.y, location.x);
067            }
068    
069            @Override
070            protected RolloverRenderer getRolloverRenderer(Point location, boolean prepare) {
071                TableCellRenderer renderer = component.getCellRenderer(location.y, location.x);
072                RolloverRenderer rollover = renderer instanceof RolloverRenderer ?
073                        (RolloverRenderer) renderer : null;
074                if ((rollover != null) && !rollover.isEnabled()) {
075                    rollover = null;
076                }
077                if ((rollover != null) && prepare) {
078                    component.prepareRenderer(renderer, location.y, location.x);
079                }
080                return rollover;
081            }
082    
083    
084            private void setRolloverCursor(Point location) {
085                if (hasRollover(location)) {
086                    if (oldCursor == null) {
087                        oldCursor = component.getCursor();
088                        component.setCursor(Cursor
089                                .getPredefinedCursor(Cursor.HAND_CURSOR));
090                    }
091                } else {
092                    if (oldCursor != null) {
093                        component.setCursor(oldCursor);
094                        oldCursor = null;
095                    }
096                }
097    
098            }
099            
100    
101            @Override
102            protected Point getFocusedCell() {
103                int leadRow = component.getSelectionModel()
104                        .getLeadSelectionIndex();
105                int leadColumn = component.getColumnModel().getSelectionModel()
106                        .getLeadSelectionIndex();
107                return new Point(leadColumn, leadRow);
108            }
109    
110        }