Java Swing Mouse convertMouseEvent(Component source, MouseEvent sourceEvent, Component destination)

Here you can find the source of convertMouseEvent(Component source, MouseEvent sourceEvent, Component destination)

Description

Convert a mouse event which refrers to one component to another.

License

Open Source License

Parameter

Parameter Description
source The component the mouse event currently refers to
sourceEvent The mouse event to convert
destination The component the new mouse event should refer to

Return

A new mouse event expressed in terms of the destination component's coordinate space, and with the destination component as its source

Declaration

public static MouseEvent convertMouseEvent(Component source, MouseEvent sourceEvent, Component destination) 

Method Source Code


//package com.java2s;
/* AWTUtilities.java -- Common utility methods for AWT and Swing.
   Copyright (C) 2005, 2007  Free Software Foundation, Inc.
    //  w  w w .  j  a v  a 2s .com
This file is part of GNU Classpath.
    
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
    
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
    
Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
    
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */

import java.applet.Applet;
import java.awt.Component;

import java.awt.Point;

import java.awt.Window;

import java.awt.event.MouseEvent;

public class Main {
    /**
     * Convert a mouse event which refrers to one component to another.  This
     * includes changing the mouse event's coordinate space, as well as the
     * source property of the event. If <code>source</code> is
     * <code>null</code>, it is taken to refer to <code>destination</code>'s
     * root component. If <code>destination</code> is <code>null</code>, the
     * new event will remain expressed in <code>source</code>'s coordinate
     * system.
     *
     * @param source The component the mouse event currently refers to
     * @param sourceEvent The mouse event to convert
     * @param destination The component the new mouse event should refer to
     *
     * @return A new mouse event expressed in terms of the destination
     * component's coordinate space, and with the destination component as
     * its source
     *
     * @see #convertPoint
     */
    public static MouseEvent convertMouseEvent(Component source, MouseEvent sourceEvent, Component destination) {
        Point newpt = convertPoint(source, sourceEvent.getX(), sourceEvent.getY(), destination);

        return new MouseEvent(destination, sourceEvent.getID(), sourceEvent.getWhen(), sourceEvent.getModifiers(),
                newpt.x, newpt.y, sourceEvent.getClickCount(), sourceEvent.isPopupTrigger(),
                sourceEvent.getButton());
    }

    /**
     * Converts a point <code>(x,y)</code> from the coordinate space of one
     * component to another. This is equivalent to converting the point from
     * <code>source</code> space to screen space, then back from screen space
     * to <code>destination</code> space. If exactly one of the two
     * Components is <code>null</code>, it is taken to refer to the root
     * ancestor of the other component. If both are <code>null</code>, no
     * transformation is done.
     *
     * @param source The component which the point is expressed in terms of
     * @param x Horizontal coordinate of point to transform
     * @param y Vertical coordinate of point to transform
     * @param destination The component which the return value will be
     * expressed in terms of
     *
     * @return The point <code>(x,y)</code> converted from the coordinate
     *         space of the
     * source component to the coordinate space of the destination component
     *
     * @see #convertPointToScreen
     * @see #convertPointFromScreen
     * @see #convertRectangle
     * @see #getRoot
     */
    public static Point convertPoint(Component source, int x, int y, Component destination) {
        Point pt = new Point(x, y);

        if (source == null && destination == null)
            return pt;

        if (source == null)
            source = getRoot(destination);

        if (destination == null)
            destination = getRoot(source);

        if (source.isShowing() && destination.isShowing()) {
            convertPointToScreen(pt, source);
            convertPointFromScreen(pt, destination);
        }

        return pt;
    }

    /**
     * Returns the "root" of the component tree containint <code>comp</code>
     * The root is defined as either the <em>least</em> ancestor of
     * <code>comp</code> which is a {@link Window}, or the <em>greatest</em>
     * ancestor of <code>comp</code> which is a {@link Applet} if no {@link
     * Window} ancestors are found.
     *
     * @param comp The component to search for a root
     *
     * @return The root of the component's tree, or <code>null</code>
     */
    public static Component getRoot(Component comp) {
        Applet app = null;
        Window win = null;

        while (comp != null) {
            if (win == null && comp instanceof Window)
                win = (Window) comp;
            else if (comp instanceof Applet)
                app = (Applet) comp;
            comp = comp.getParent();
        }

        if (win != null)
            return win;
        else
            return app;
    }

    /**
     * Converts a point from a component's local coordinate space to "screen"
     * coordinates (such as the coordinate space mouse events are delivered
     * in). This operation is equivalent to translating the point by the
     * location of the component (which is the origin of its coordinate
     * space).
     *
     * @param p The point to convert
     * @param c The component which the point is expressed in terms of
     *
     * @see convertPointFromScreen
     */
    public static void convertPointToScreen(Point p, Component c) {
        Point c0 = c.getLocationOnScreen();
        p.translate(c0.x, c0.y);
    }

    /**
     * Converts a point from "screen" coordinates (such as the coordinate
     * space mouse events are delivered in) to a component's local coordinate
     * space. This operation is equivalent to translating the point by the
     * negation of the component's location (which is the origin of its
     * coordinate space).
     *
     * @param p The point to convert
     * @param c The component which the point should be expressed in terms of
     */
    public static void convertPointFromScreen(Point p, Component c) {
        Point c0 = c.getLocationOnScreen();
        p.translate(-c0.x, -c0.y);
    }
}

Related

  1. addMouseListenerToHierarchy(JComponent c, MouseListener listener)
  2. animateMouse(Robot robot, JComponent jc, boolean moveMouse, boolean clickOnArrival)
  3. clickedInSelection(MouseEvent e)
  4. convert(MouseEvent event, Component newSource)
  5. convertMouseEvent(Component source, MouseEvent sourceEvent, Component destination)
  6. convertMouseEvent(java.awt.Component source, java.awt.event.MouseEvent sourceEvent, java.awt.Component destination)
  7. convertPoint(MouseEvent evt, Component target)
  8. createButton(JComponent parent, String description, MouseListener listener)
  9. decorateComponentWithMouseOverCursorChange(final JComponent toDecorate, final int mouseEntered, final int mouseExited)