001 /* 002 * $Id: ListRolloverController.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.JList; 028 import 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 */ 037 public class ListRolloverController<T extends JList> extends 038 RolloverController<T> { 039 040 private Cursor oldCursor; 041 042 // --------------------------------- JList rollover 043 044 @Override 045 protected void rollover(Point oldLocation, Point newLocation) { 046 // PENDING JW - track down the -1 in location.y 047 if (oldLocation != null) { 048 Rectangle r = component.getCellBounds(oldLocation.y, oldLocation.y); 049 // LOG.info("old index/cellbounds: " + index + "/" + r); 050 if (r != null) { 051 component.repaint(r); 052 } 053 } 054 if (newLocation != null) { 055 Rectangle r = component.getCellBounds(newLocation.y, newLocation.y); 056 // LOG.info("new index/cellbounds: " + index + "/" + r); 057 if (r != null) { 058 component.repaint(r); 059 } 060 } 061 setRolloverCursor(newLocation); 062 } 063 064 /** 065 * something weird: cursor in JList behaves different from JTable? 066 * Hmm .. no: using the table code snippets seems to fix #503-swingx 067 * @param location 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 // if (hasRollover(location)) { 083 // oldCursor = component.getCursor(); 084 // component.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); 085 // } else { 086 // component.setCursor(oldCursor); 087 // oldCursor = null; 088 // } 089 090 } 091 092 @Override 093 protected RolloverRenderer getRolloverRenderer(Point location, 094 boolean prepare) { 095 ListCellRenderer renderer = component.getCellRenderer(); 096 RolloverRenderer rollover = renderer instanceof RolloverRenderer 097 ? (RolloverRenderer) renderer : null; 098 if ((rollover != null) && !rollover.isEnabled()) { 099 rollover = null; 100 } 101 if ((rollover != null) && prepare) { 102 Object element = component.getModel().getElementAt(location.y); 103 renderer.getListCellRendererComponent(component, element, 104 location.y, false, true); 105 } 106 return rollover; 107 } 108 109 @Override 110 protected Point getFocusedCell() { 111 int leadRow = component.getLeadSelectionIndex(); 112 if (leadRow < 0) 113 return null; 114 return new Point(0, leadRow); 115 } 116 117 }