001/*
002 * $Id: TreeRolloverProducer.java 4190 2012-06-27 19:01:06Z kschaefe $
003 *
004 * Copyright 2007 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 */
022package org.jdesktop.swingx.rollover;
023
024import java.awt.Point;
025import java.awt.Rectangle;
026import java.awt.event.MouseEvent;
027
028import javax.swing.JComponent;
029import javax.swing.JTree;
030
031/**
032 * Tree-specific implementation of RolloverProducer.
033 * <p>
034 * This implementation assumes a "hit" for rollover if the mouse is anywhere in
035 * the total width of the tree. Additionally, a pressed to the right (but
036 * outside of the label bounds) is re-dispatched as a pressed just inside the
037 * label bounds. This is a first go for #166-swingx.
038 * <p>
039 * 
040 * PENDING JW: bidi-compliance of pressed?
041 * 
042 * @author Jeanette Winzenburg
043 */
044public class TreeRolloverProducer extends RolloverProducer {
045
046    @Override
047    public void mousePressed(MouseEvent e) {
048        JTree tree = (JTree) e.getComponent();
049        Point mousePoint = e.getPoint();
050        int labelRow = tree.getRowForLocation(mousePoint.x, mousePoint.y);
051        // default selection
052        if (labelRow >= 0)
053            return;
054        int row = tree.getClosestRowForLocation(mousePoint.x, mousePoint.y);
055        Rectangle bounds = tree.getRowBounds(row);
056        if (bounds == null) {
057            row = -1;
058        } else {
059            if ((bounds.y + bounds.height < mousePoint.y)
060                    || bounds.x > mousePoint.x) {
061                row = -1;
062            }
063        }
064        // no hit
065        if (row < 0)
066            return;
067        tree.dispatchEvent(new MouseEvent(tree, e.getID(), e.getWhen(), e
068                .getModifiers(), bounds.x + bounds.width - 2, mousePoint.y, e
069                .getClickCount(), e.isPopupTrigger(), e.getButton()));
070    }
071
072    @Override
073    protected void updateRolloverPoint(JComponent component, Point mousePoint) {
074        JTree tree = (JTree) component;
075        int row = tree.getClosestRowForLocation(mousePoint.x, mousePoint.y);
076        Rectangle bounds = tree.getRowBounds(row);
077        if (bounds == null) {
078            row = -1;
079        } else {
080            if ((bounds.y + bounds.height < mousePoint.y)
081                    || bounds.x > mousePoint.x) {
082                row = -1;
083            }
084        }
085        int col = row < 0 ? -1 : 0;
086        rollover.x = col;
087        rollover.y = row;
088    }
089
090}