1 /***
2 * BindingMouseHandler.java
3 *
4 * This file is part of the creme library.
5 * The creme library intends to ease the development effort of large
6 * applications by providing easy to use support classes.
7 *
8 * Copyright (C) 2004 Denis Bregeon
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
24 * contact information: dbregeon@sourceforge.net
25 */
26 package org.jcreme.swing;
27
28 import java.awt.Component;
29 import java.awt.event.MouseEvent;
30 import java.awt.event.MouseListener;
31 import java.awt.event.MouseMotionListener;
32
33 /***
34 * This class describes a mouseListener that transmits mouse events from a given
35 * component to another, first setting the source of the event to the new
36 * component.
37 *
38 * @author $Author: dbregeon $
39 * @version $Version$
40 */
41 class BindingMouseHandler implements MouseListener, MouseMotionListener {
42
43 /***
44 * The Component to which events are redispatched.
45 */
46 private final Component boundComponent;
47
48 /***
49 * Enables to avoid infinite loops.
50 */
51 private boolean loopController = false;
52
53 /***
54 * Creates a new BindingMouseHandler.
55 *
56 * @param bound
57 * the Component that will have the received events dispatched.
58 */
59 public BindingMouseHandler(Component bound) {
60 this.boundComponent = bound;
61 }
62
63 /***
64 * @see MouseListener#mousePressed(java.awt.event.MouseEvent)
65 */
66 public void mousePressed(MouseEvent e) {
67 dispatchEvent(e);
68 }
69
70 /***
71 * @see MouseListener#mouseReleased(java.awt.event.MouseEvent)
72 */
73 public void mouseReleased(MouseEvent e) {
74 dispatchEvent(e);
75 }
76
77 /***
78 * @see MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
79 */
80 public void mouseDragged(MouseEvent e) {
81 dispatchEvent(e);
82 }
83
84 /***
85 * @see MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
86 */
87 public void mouseMoved(MouseEvent e) {
88 dispatchEvent(e);
89 }
90
91 /***
92 * @see MouseListener#mouseClicked(java.awt.event.MouseEvent)
93 */
94 public void mouseClicked(MouseEvent e) {
95 dispatchEvent(e);
96 }
97
98 /***
99 * @see MouseListener#mouseEntered(java.awt.event.MouseEvent)
100 */
101 public void mouseEntered(MouseEvent e) {
102 dispatchEvent(e);
103 }
104
105 /***
106 * @see MouseListener#mouseExited(java.awt.event.MouseEvent)
107 */
108 public void mouseExited(MouseEvent e) {
109 dispatchEvent(e);
110 }
111
112 /***
113 * This method dispatches the given event to the boundComponent.
114 *
115 * @param e
116 * the event to dispach.
117 */
118 protected void dispatchEvent(MouseEvent e) {
119 if (!this.loopController) {
120 this.loopController = true;
121 this.boundComponent.dispatchEvent(changeMouseEventSource(e,
122 this.boundComponent));
123 this.loopController = false;
124 }
125 }
126
127 /***
128 * This method creates a new MouseEvent that mimics the parameter e except
129 * for the source which is replaced by the parameter source.
130 *
131 * @param e
132 * the mouse event to mimic.
133 * @param source
134 * the source for the new MouseEvent.
135 * @return a new MouseEvent with source as a source and values taken from e.
136 */
137 protected MouseEvent changeMouseEventSource(MouseEvent e, Component source) {
138 return new MouseEvent(source, e.getID(), e.getWhen(), e.getModifiers(),
139 e.getX(), e.getY(), e.getClickCount(), e.isPopupTrigger());
140 }
141 }