001    /*
002     * $Id: RolloverProducer.java,v 1.10 2005/10/24 13:20:42 kleopatra Exp $
003     *
004     * Copyright 2004 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;
022    
023    import java.awt.Point;
024    import java.awt.event.MouseEvent;
025    import java.awt.event.MouseListener;
026    import java.awt.event.MouseMotionListener;
027    
028    import javax.swing.JComponent;
029    
030    /**
031     * Mouse/Motion/Listener which stores mouse location as 
032     * client property in the target JComponent.
033     * 
034     * Note: assumes that the component it is listening to is 
035     * of type JComponent!
036     * 
037     * @author Jeanette Winzenburg
038     */
039    public class RolloverProducer implements MouseListener, MouseMotionListener {
040    
041        //----------------- mouseListener
042            
043            public static final String CLICKED_KEY = "swingx.clicked";
044            public static final String ROLLOVER_KEY = "swingx.rollover";
045    //        public static final String PRESSED_KEY = "swingx.pressed";
046            
047            
048            public void mouseClicked(MouseEvent e) {
049            }
050    
051            public void mousePressed(MouseEvent e) {
052                
053            }
054    
055            public void mouseReleased(MouseEvent e) {
056                updateRollover(e, CLICKED_KEY);
057                
058            }
059    
060            public void mouseEntered(MouseEvent e) {
061                updateRollover(e, ROLLOVER_KEY);
062            }
063    
064    
065            public void mouseExited(MouseEvent e) {
066                if (e.getSource() instanceof JComponent) {
067                    ((JComponent) e.getSource()).putClientProperty(ROLLOVER_KEY, null);
068                    ((JComponent) e.getSource()).putClientProperty(CLICKED_KEY, null);
069    //                ((JComponent) e.getSource()).putClientProperty(PRESSED_KEY, null);
070                }
071                
072            }
073    
074    //---------------- MouseMotionListener
075            public void mouseDragged(MouseEvent e) {
076                // TODO Auto-generated method stub
077                
078            }
079    
080            public void mouseMoved(MouseEvent e) {
081                updateRollover(e, ROLLOVER_KEY);
082            }
083    
084            protected void updateRollover(MouseEvent e, String property) {
085                updateRolloverPoint((JComponent) e.getComponent(), e.getPoint());
086                updateClientProperty((JComponent) e.getSource(), property);
087            }
088    
089            protected Point rollover = new Point(-1, -1);
090            
091            protected void updateClientProperty(JComponent component, String property) {
092                Point p = (Point) component.getClientProperty(property);
093                if (p == null || (rollover.x != p.x) || (rollover.y != p.y)) {
094                    component.putClientProperty(property, new Point(rollover));
095                }
096            }
097    
098            /**
099             * Subclasses must override to map the given mouse coordinates into
100             * appropriate client coordinates. The result must be stored in the 
101             * rollover field. 
102             * 
103             * Here: does nothing.
104             * 
105             * @param component
106             * @param mousePoint
107             */
108            protected void updateRolloverPoint(JComponent component, Point mousePoint) {
109                
110            }
111            
112            
113        }