KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > user > client > ui > MouseListenerCollection


1 /*
2  * Copyright 2007 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.user.client.ui;
17
18 import com.google.gwt.user.client.DOM;
19 import com.google.gwt.user.client.Element;
20 import com.google.gwt.user.client.Event;
21 import com.google.gwt.user.client.Window;
22
23 import java.util.ArrayList JavaDoc;
24 import java.util.Iterator JavaDoc;
25
26 /**
27  * A helper class for implementers of the SourcesMouseEvents interface. This
28  * subclass of {@linkArrayList} assumes that all objects added to it will be of
29  * type {@link com.google.gwt.user.client.ui.MouseListener}.
30  */

31 public class MouseListenerCollection extends ArrayList JavaDoc {
32
33   /**
34    * Fires a mouse down event to all listeners.
35    *
36    * @param sender the widget sending the event
37    * @param x the x coordinate of the mouse
38    * @param y the y coordinate of the mouse
39    */

40   public void fireMouseDown(Widget sender, int x, int y) {
41     for (Iterator JavaDoc it = iterator(); it.hasNext();) {
42       MouseListener listener = (MouseListener) it.next();
43       listener.onMouseDown(sender, x, y);
44     }
45   }
46
47   /**
48    * Fires a mouse enter event to all listeners.
49    *
50    * @param sender the widget sending the event
51    */

52   public void fireMouseEnter(Widget sender) {
53     for (Iterator JavaDoc it = iterator(); it.hasNext();) {
54       MouseListener listener = (MouseListener) it.next();
55       listener.onMouseEnter(sender);
56     }
57   }
58
59   /**
60    * A helper for widgets that source mouse events.
61    *
62    * @param sender the widget sending the event
63    * @param event the {@link Event} received by the widget
64    */

65   public void fireMouseEvent(Widget sender, Event event) {
66     final Element senderElem = sender.getElement();
67     int x = DOM.eventGetClientX(event)
68         - DOM.getAbsoluteLeft(sender.getElement())
69         + DOM.getElementPropertyInt(senderElem, "scrollLeft")
70         + Window.getScrollLeft();
71     int y = DOM.eventGetClientY(event)
72         - DOM.getAbsoluteTop(sender.getElement())
73         + DOM.getElementPropertyInt(senderElem, "scrollTop")
74         + Window.getScrollTop();
75
76     switch (DOM.eventGetType(event)) {
77       case Event.ONMOUSEDOWN:
78         fireMouseDown(sender, x, y);
79         break;
80       case Event.ONMOUSEUP:
81         fireMouseUp(sender, x, y);
82         break;
83       case Event.ONMOUSEMOVE:
84         fireMouseMove(sender, x, y);
85         break;
86       case Event.ONMOUSEOVER:
87         // Only fire the mouseEnter event if it's coming from outside this
88
// widget.
89
Element from = DOM.eventGetFromElement(event);
90         if (!DOM.isOrHasChild(sender.getElement(), from)) {
91           fireMouseEnter(sender);
92         }
93         break;
94       case Event.ONMOUSEOUT:
95         // Only fire the mouseLeave event if it's actually leaving this
96
// widget.
97
Element to = DOM.eventGetToElement(event);
98         if (!DOM.isOrHasChild(sender.getElement(), to)) {
99           fireMouseLeave(sender);
100         }
101         break;
102     }
103   }
104
105   /**
106    * Fires a mouse leave event to all listeners.
107    *
108    * @param sender the widget sending the event
109    */

110   public void fireMouseLeave(Widget sender) {
111     for (Iterator JavaDoc it = iterator(); it.hasNext();) {
112       MouseListener listener = (MouseListener) it.next();
113       listener.onMouseLeave(sender);
114     }
115   }
116
117   /**
118    * Fires a mouse move event to all listeners.
119    *
120    * @param sender the widget sending the event
121    * @param x the x coordinate of the mouse
122    * @param y the y coordinate of the mouse
123    */

124   public void fireMouseMove(Widget sender, int x, int y) {
125     for (Iterator JavaDoc it = iterator(); it.hasNext();) {
126       MouseListener listener = (MouseListener) it.next();
127       listener.onMouseMove(sender, x, y);
128     }
129   }
130
131   /**
132    * Fires a mouse up event to all listeners.
133    *
134    * @param sender the widget sending the event
135    * @param x the x coordinate of the mouse
136    * @param y the y coordinate of the mouse
137    */

138   public void fireMouseUp(Widget sender, int x, int y) {
139     for (Iterator JavaDoc it = iterator(); it.hasNext();) {
140       MouseListener listener = (MouseListener) it.next();
141       listener.onMouseUp(sender, x, y);
142     }
143   }
144 }
145
Popular Tags