001    /*
002     * $Id: TreeRolloverController.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.JTree;
028    import javax.swing.tree.TreeCellRenderer;
029    import javax.swing.tree.TreePath;
030    
031    
032    /**
033         * listens to rollover properties. 
034         * Repaints effected component regions.
035         * Updates link cursor.
036         * 
037         * @author Jeanette Winzenburg
038         */
039        public class TreeRolloverController<T extends JTree>  extends RolloverController<T> {
040        
041            private Cursor oldCursor;
042            
043    //    -------------------------------------JTree rollover
044            
045            @Override
046            protected void rollover(Point oldLocation, Point newLocation) {
047                // JW: conditional repaint not working?
048    //            component.repaint();
049                if (oldLocation != null) {
050                    Rectangle r = component.getRowBounds(oldLocation.y);
051                    if (r != null) {
052                        r.x = 0;
053                        r.width = component.getWidth();
054                        component.repaint(r);
055                    }
056                }
057                if (newLocation != null) {
058                    Rectangle r = component.getRowBounds(newLocation.y);
059                    if (r != null) {
060                        r.x = 0;
061                        r.width = component.getWidth();
062                        component.repaint(r);
063                    }
064                }
065                setRolloverCursor(newLocation);
066            }
067    
068    
069            private void setRolloverCursor(Point location) {
070                if (hasRollover(location)) {
071                    if (oldCursor == null) {
072                        oldCursor = component.getCursor();
073                        component.setCursor(Cursor
074                                .getPredefinedCursor(Cursor.HAND_CURSOR));
075                    }
076                } else {
077                    if (oldCursor != null) {
078                        component.setCursor(oldCursor);
079                        oldCursor = null;
080                    }
081                }
082    
083            }
084    
085    
086            @Override
087            protected RolloverRenderer getRolloverRenderer(Point location, boolean prepare) {
088                TreeCellRenderer renderer = component.getCellRenderer();
089                RolloverRenderer rollover = renderer instanceof RolloverRenderer 
090                    ? (RolloverRenderer) renderer : null;
091                if ((rollover != null) && !rollover.isEnabled()) {
092                    rollover = null;
093                }
094                if ((rollover != null) && prepare) {
095                    TreePath path = component.getPathForRow(location.y);
096                    Object element = path != null ? path.getLastPathComponent() : null;
097                    renderer.getTreeCellRendererComponent(component, element, false, 
098                            false, false, 
099                            location.y, false);
100                }
101                return rollover;
102            }
103    
104    
105            @Override
106            protected Point getFocusedCell() {
107                // TODO Auto-generated method stub
108                return null;
109            }
110    
111        }