001    /*
002     * Created on 21/7/2004
003     * 
004     * Copyright (C) 2004 Denis Krukovsky. All rights reserved.
005     * ====================================================================
006     * The Software License (based on Apache Software License, Version 1.1)
007     *
008     * Redistribution and use in source and binary forms, with or without
009     * modification, are permitted provided that the following conditions
010     * are met:
011     *
012     * 1. Redistributions of source code must retain the above copyright
013     *    notice, this list of conditions and the following disclaimer.
014     *
015     * 2. Redistributions in binary form must reproduce the above copyright
016     *    notice, this list of conditions and the following disclaimer in
017     *    the documentation and/or other materials provided with the
018     *    distribution.
019     *
020     * 3. The end-user documentation included with the redistribution,
021     *    if any, must include the following acknowledgment:
022     *       "This product includes software developed by
023     *        Denis Krukovsky (dkrukovsky at yahoo.com)."
024     *    Alternately, this acknowledgment may appear in the software itself,
025     *    if and wherever such third-party acknowledgments normally appear.
026     *
027     * 4. The names "dot useful" and "Denis Krukovsky" must not be used to
028     *    endorse or promote products derived from this software without
029     *    prior written permission. For written permission, please
030     *    contact dkrukovsky at yahoo.com.
031     *
032     * 5. Products derived from this software may not be called "useful",
033     *    nor may "useful" appear in their name, without prior written
034     *    permission of Denis Krukovsky.
035     *
036     * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037     * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038     * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039     * DISCLAIMED.  IN NO EVENT SHALL JIVE SOFTWARE OR
040     * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041     * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042     * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043     * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044     * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045     * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046     * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047     * SUCH DAMAGE.
048     * ====================================================================
049     */
050    
051    package org.dotuseful.ui.tree;
052    
053    import java.awt.event.MouseAdapter;
054    import java.awt.event.MouseEvent;
055    import java.awt.event.MouseListener;
056    
057    import javax.swing.JTree;
058    import javax.swing.tree.TreePath;
059    
060    /**
061     * A mouse handler for MouseAdaptedTree which transmits mouse events to
062     * corresponding tree nodes. Tree nodes must implement MouseListener interface.
063     * <br>
064     * Unfortunately you can't transmit mouseEntered and mouseExited events in easy
065     * way because these events are fired for JTree component at all.
066     * 
067     * @author Denis Krukovsky
068     */
069    public class MouseAdaptedTreeMouseHandler extends MouseAdapter {
070        /*
071         * (non-Javadoc)
072         * 
073         * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
074         */
075        public void mouseClicked(MouseEvent e) {
076            MouseListener node = getNode(e);
077            if (node != null) {
078                node.mouseClicked(e);
079            }
080        }
081    
082        public void mousePressed(MouseEvent e) {
083            MouseListener node = getNode(e);
084            if (node != null) {
085                node.mousePressed(e);
086            }
087        }
088    
089        public void mouseReleased(MouseEvent e) {
090            MouseListener node = getNode(e);
091            if (node != null) {
092                node.mouseReleased(e);
093            }
094        }
095    
096        /**
097         * Returns a tree node which is mouse event on.
098         * 
099         * @param e
100         *            a mouse event to calculate the node to
101         * @return a tree node which is mouse event on, or null if there is no node.
102         */
103        protected MouseListener getNode(MouseEvent e) {
104            JTree tree = getTree(e);
105            int x = e.getX();
106            int y = e.getY();
107            TreePath path = tree.getPathForLocation(x, y);
108            if (path != null) {
109                return (MouseListener) (path.getLastPathComponent());
110            } else {
111                return null;
112            }
113        }
114    
115        protected JTree getTree(MouseEvent e) {
116            return (JTree) (e.getSource());
117        }
118    }