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