001/*
002 * $Id: ListRolloverController.java 3707 2010-07-08 19:19:25Z kschaefe $
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 */
021package org.jdesktop.swingx.rollover;
022
023import java.awt.Cursor;
024import java.awt.Point;
025import java.awt.Rectangle;
026
027import javax.swing.JList;
028import javax.swing.ListCellRenderer;
029
030
031/**
032 * listens to rollover properties. Repaints effected component regions.
033 * Updates link cursor.
034 * 
035 * @author Jeanette Winzenburg
036 */
037public class ListRolloverController<T extends JList> extends RolloverController<T> {
038
039        private Cursor oldCursor;
040
041        // --------------------------------- JList rollover
042
043        @Override
044        protected void rollover(Point oldLocation, Point newLocation) {
045            // PENDING JW - track down the -1 in location.y
046            if (oldLocation != null) {
047                Rectangle r = component.getCellBounds(oldLocation.y, oldLocation.y);
048                // LOG.info("old index/cellbounds: " + index + "/" + r);
049                if (r != null) {
050                    component.repaint(r);
051                }
052            }
053            if (newLocation != null) {
054                Rectangle r = component.getCellBounds(newLocation.y, newLocation.y);
055                // LOG.info("new index/cellbounds: " + index + "/" + r);
056                if (r != null) {
057                    component.repaint(r);
058                }
059            }
060            setRolloverCursor(newLocation);
061        }
062
063        /**
064         * something weird: cursor in JList behaves different from JTable?
065         * Hmm .. no: using the table code snippets seems to fix #503-swingx
066         * @param location
067         */
068        private void setRolloverCursor(Point location) {
069            if (hasRollover(location)) {
070                if (oldCursor == null) {
071                    oldCursor = component.getCursor();
072                    component.setCursor(Cursor
073                            .getPredefinedCursor(Cursor.HAND_CURSOR));
074                }
075            } else {
076                if (oldCursor != null) {
077                    component.setCursor(oldCursor);
078                    oldCursor = null;
079                }
080            }
081//            if (hasRollover(location)) {
082//                oldCursor = component.getCursor();
083//                component.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
084//            } else {
085//                component.setCursor(oldCursor);
086//                oldCursor = null;
087//            }
088
089        }
090
091        @Override
092        protected RolloverRenderer getRolloverRenderer(Point location,
093                boolean prepare) {
094            ListCellRenderer renderer = component.getCellRenderer();
095            RolloverRenderer rollover = renderer instanceof RolloverRenderer 
096                ? (RolloverRenderer) renderer : null;
097            if ((rollover != null) && !rollover.isEnabled()) {
098                rollover = null;
099            }
100            if ((rollover != null) && prepare) {
101                Object element = component.getModel().getElementAt(location.y);
102                renderer.getListCellRendererComponent(component, element,
103                        location.y, false, true);
104            }
105            return rollover;
106        }
107
108        @Override
109        protected Point getFocusedCell() {
110            int leadRow = component.getLeadSelectionIndex();
111            if (leadRow < 0)
112                return null;
113            return new Point(0, leadRow);
114        }
115
116    }