01: /*
02: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
03: * Distributed under the terms of either:
04: * - the common development and distribution license (CDDL), v1.0; or
05: * - the GNU Lesser General Public License, v2.1 or later
06: * $Id: MouseMotionEventTranslator.java 3634 2007-01-08 21:42:24Z gbevin $
07: */
08: package com.uwyn.rife.swing;
09:
10: import java.awt.Component;
11: import java.awt.Point;
12: import java.awt.event.MouseEvent;
13: import java.awt.event.MouseMotionListener;
14:
15: public class MouseMotionEventTranslator implements MouseMotionListener {
16: private Component mTargetComponent = null;
17:
18: public MouseMotionEventTranslator(Component targetComponent) {
19: mTargetComponent = targetComponent;
20: }
21:
22: public void mouseDragged(MouseEvent e) {
23: if (mTargetComponent instanceof MouseMotionListener) {
24: MouseMotionListener target_listener = (MouseMotionListener) mTargetComponent;
25: target_listener.mouseDragged(translateMouseEvent(e));
26: }
27: }
28:
29: public void mouseMoved(MouseEvent e) {
30: if (mTargetComponent instanceof MouseMotionListener) {
31: MouseMotionListener target_listener = (MouseMotionListener) mTargetComponent;
32: target_listener.mouseMoved(translateMouseEvent(e));
33: }
34: }
35:
36: private MouseEvent translateMouseEvent(MouseEvent e) {
37: MouseEvent new_mouse_event = null;
38: Point target_location = mTargetComponent.getLocationOnScreen();
39: Component source_component = (Component) e.getSource();
40:
41: int new_x = (int) (source_component.getX()
42: - target_location.getX() + e.getX());
43: int new_y = (int) (source_component.getY()
44: - target_location.getY() + e.getY());
45:
46: new_mouse_event = new MouseEvent(e.getComponent(), e.getID(), e
47: .getWhen(), e.getModifiers(), new_x, new_y, e
48: .getClickCount(), e.isPopupTrigger());
49:
50: return new_mouse_event;
51: }
52: }
|