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,
040: MouseMotionListener {
041:
042: //----------------- mouseListener
043:
044: public static final String CLICKED_KEY = "swingx.clicked";
045: public static final String ROLLOVER_KEY = "swingx.rollover";
046:
047: // public static final String PRESSED_KEY = "swingx.pressed";
048:
049: public void mouseClicked(MouseEvent e) {
050: }
051:
052: public void mousePressed(MouseEvent e) {
053:
054: }
055:
056: public void mouseReleased(MouseEvent e) {
057: updateRollover(e, CLICKED_KEY);
058:
059: }
060:
061: public void mouseEntered(MouseEvent e) {
062: updateRollover(e, ROLLOVER_KEY);
063: }
064:
065: public void mouseExited(MouseEvent e) {
066: if (e.getSource() instanceof JComponent) {
067: ((JComponent) e.getSource()).putClientProperty(
068: ROLLOVER_KEY, null);
069: ((JComponent) e.getSource()).putClientProperty(CLICKED_KEY,
070: null);
071: // ((JComponent) e.getSource()).putClientProperty(PRESSED_KEY, null);
072: }
073:
074: }
075:
076: //---------------- MouseMotionListener
077: public void mouseDragged(MouseEvent e) {
078: // TODO Auto-generated method stub
079:
080: }
081:
082: public void mouseMoved(MouseEvent e) {
083: updateRollover(e, ROLLOVER_KEY);
084: }
085:
086: protected void updateRollover(MouseEvent e, String property) {
087: updateRolloverPoint((JComponent) e.getComponent(), e.getPoint());
088: updateClientProperty((JComponent) e.getSource(), property);
089: }
090:
091: protected Point rollover = new Point(-1, -1);
092:
093: protected void updateClientProperty(JComponent component,
094: String property) {
095: Point p = (Point) component.getClientProperty(property);
096: if (p == null || (rollover.x != p.x) || (rollover.y != p.y)) {
097: component.putClientProperty(property, new Point(rollover));
098: }
099: }
100:
101: /**
102: * Subclasses must override to map the given mouse coordinates into
103: * appropriate client coordinates. The result must be stored in the
104: * rollover field.
105: *
106: * Here: does nothing.
107: *
108: * @param component
109: * @param mousePoint
110: */
111: protected void updateRolloverPoint(JComponent component,
112: Point mousePoint) {
113:
114: }
115:
116: }
|