KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > user > client > impl > DOMImplStandard


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.impl;
17
18 import com.google.gwt.user.client.Element;
19 import com.google.gwt.user.client.Event;
20
21 /**
22  * Base implementation of {@link com.google.gwt.user.client.impl.DOMImpl} shared
23  * by those browsers that come a bit closer to supporting a common standard (ie,
24  * not IE).
25  */

26 abstract class DOMImplStandard extends DOMImpl {
27
28   public native boolean compare(Element elem1, Element elem2) /*-{
29     return (elem1 == elem2);
30   }-*/
;
31
32   public native Element createInputRadioElement(String JavaDoc group) /*-{
33     var elem = $doc.createElement("INPUT");
34     elem.type = 'radio';
35     elem.name = group;
36     return elem;
37   }-*/
;
38
39   public native Element eventGetFromElement(Event evt) /*-{
40     // Standard browsers use relatedTarget rather than fromElement.
41     return evt.relatedTarget ? evt.relatedTarget : null;
42   }-*/
;
43
44   public native Element eventGetTarget(Event evt) /*-{
45     return evt.target || null;
46   }-*/
;
47
48   public native Element eventGetToElement(Event evt) /*-{
49     // Standard browsers use relatedTarget rather than toElement.
50     return evt.relatedTarget || null;
51   }-*/
;
52
53   public native void eventPreventDefault(Event evt) /*-{
54     evt.preventDefault();
55   }-*/
;
56
57   public native String JavaDoc eventToString(Event evt) /*-{
58     return evt.toString();
59   }-*/
;
60
61   public native Element getChild(Element elem, int index) /*-{
62     var count = 0, child = elem.firstChild;
63     while (child) {
64       var next = child.nextSibling;
65       if (child.nodeType == 1) {
66         if (index == count)
67           return child;
68         ++count;
69       }
70       child = next;
71     }
72
73     return null;
74   }-*/
;
75
76   public native int getChildCount(Element elem) /*-{
77     var count = 0, child = elem.firstChild;
78     while (child) {
79       if (child.nodeType == 1)
80         ++count;
81       child = child.nextSibling;
82     }
83     return count;
84   }-*/
;
85
86   public native int getChildIndex(Element parent, Element toFind) /*-{
87     var count = 0, child = parent.firstChild;
88     while (child) {
89       if (child == toFind)
90         return count;
91       if (child.nodeType == 1)
92         ++count;
93       child = child.nextSibling;
94     }
95
96     return -1;
97   }-*/
;
98
99   public native Element getFirstChild(Element elem) /*-{
100     var child = elem.firstChild;
101     while (child && child.nodeType != 1)
102       child = child.nextSibling;
103     return child || null;
104   }-*/
;
105
106   public native Element getNextSibling(Element elem) /*-{
107     var sib = elem.nextSibling;
108     while (sib && sib.nodeType != 1)
109       sib = sib.nextSibling;
110     return sib || null;
111   }-*/
;
112
113   public native Element getParent(Element elem) /*-{
114     var parent = elem.parentNode;
115     if(parent == null) {
116       return null;
117     }
118     if (parent.nodeType != 1)
119       parent = null;
120     return parent || null;
121   }-*/
;
122
123   public native String JavaDoc iframeGetSrc(Element elem) /*-{
124     return elem.src;
125   }-*/
;
126
127   public native void init() /*-{
128     // Set up capture event dispatchers.
129     $wnd.__dispatchCapturedMouseEvent = function(evt) {
130       if ($wnd.__dispatchCapturedEvent(evt)) {
131         var cap = $wnd.__captureElem;
132         if (cap && cap.__listener) {
133           @com.google.gwt.user.client.DOM::dispatchEvent(Lcom/google/gwt/user/client/Event;Lcom/google/gwt/user/client/Element;Lcom/google/gwt/user/client/EventListener;)(evt, cap, cap.__listener);
134           evt.stopPropagation();
135         }
136       }
137     };
138
139     $wnd.__dispatchCapturedEvent = function(evt) {
140       if (!@com.google.gwt.user.client.DOM::previewEvent(Lcom/google/gwt/user/client/Event;)(evt)) {
141         evt.stopPropagation();
142         evt.preventDefault();
143         return false;
144       }
145
146       return true;
147     };
148
149     $wnd.addEventListener('click', $wnd.__dispatchCapturedMouseEvent, true);
150     $wnd.addEventListener('dblclick', $wnd.__dispatchCapturedMouseEvent, true);
151     $wnd.addEventListener('mousedown', $wnd.__dispatchCapturedMouseEvent, true);
152     $wnd.addEventListener('mouseup', $wnd.__dispatchCapturedMouseEvent, true);
153     $wnd.addEventListener('mousemove', $wnd.__dispatchCapturedMouseEvent, true);
154     $wnd.addEventListener('mousewheel', $wnd.__dispatchCapturedMouseEvent, true);
155     $wnd.addEventListener('keydown', $wnd.__dispatchCapturedEvent, true);
156     $wnd.addEventListener('keyup', $wnd.__dispatchCapturedEvent, true);
157     $wnd.addEventListener('keypress', $wnd.__dispatchCapturedEvent, true);
158
159     // Set up normal event dispatcher.
160     $wnd.__dispatchEvent = function(evt) {
161       if (this.__listener)
162         @com.google.gwt.user.client.DOM::dispatchEvent(Lcom/google/gwt/user/client/Event;Lcom/google/gwt/user/client/Element;Lcom/google/gwt/user/client/EventListener;)(evt, this, this.__listener);
163     };
164
165     $wnd.__captureElem = null;
166   }-*/
;
167
168   public native void insertChild(Element parent, Element toAdd, int index) /*-{
169     var count = 0, child = parent.firstChild, before = null;
170     while (child) {
171       if (child.nodeType == 1) {
172         if (count == index) {
173           before = child;
174           break;
175         }
176         ++count;
177       }
178       child = child.nextSibling;
179     }
180
181     parent.insertBefore(toAdd, before);
182   }-*/
;
183
184   public native boolean isOrHasChild(Element parent, Element child) /*-{
185     while (child) {
186       if (parent == child) {
187         return true;
188       }
189       child = child.parentNode;
190       if (child && (child.nodeType != 1)) {
191         child = null;
192       }
193     }
194     return false;
195   }-*/
;
196
197   public native void releaseCapture(Element elem) /*-{
198     if (elem == $wnd.__captureElem)
199       $wnd.__captureElem = null;
200   }-*/
;
201
202   public native void setCapture(Element elem) /*-{
203     $wnd.__captureElem = elem;
204   }-*/
;
205
206   public native void sinkEvents(Element elem, int bits) /*-{
207     elem.__eventBits = bits;
208
209     elem.onclick = (bits & 0x00001) ? $wnd.__dispatchEvent : null;
210     elem.ondblclick = (bits & 0x00002) ? $wnd.__dispatchEvent : null;
211     elem.onmousedown = (bits & 0x00004) ? $wnd.__dispatchEvent : null;
212     elem.onmouseup = (bits & 0x00008) ? $wnd.__dispatchEvent : null;
213     elem.onmouseover = (bits & 0x00010) ? $wnd.__dispatchEvent : null;
214     elem.onmouseout = (bits & 0x00020) ? $wnd.__dispatchEvent : null;
215     elem.onmousemove = (bits & 0x00040) ? $wnd.__dispatchEvent : null;
216     elem.onkeydown = (bits & 0x00080) ? $wnd.__dispatchEvent : null;
217     elem.onkeypress = (bits & 0x00100) ? $wnd.__dispatchEvent : null;
218     elem.onkeyup = (bits & 0x00200) ? $wnd.__dispatchEvent : null;
219     elem.onchange = (bits & 0x00400) ? $wnd.__dispatchEvent : null;
220     elem.onfocus = (bits & 0x00800) ? $wnd.__dispatchEvent : null;
221     elem.onblur = (bits & 0x01000) ? $wnd.__dispatchEvent : null;
222     elem.onlosecapture = (bits & 0x02000) ? $wnd.__dispatchEvent : null;
223     elem.onscroll = (bits & 0x04000) ? $wnd.__dispatchEvent : null;
224     elem.onload = (bits & 0x08000) ? $wnd.__dispatchEvent : null;
225     elem.onerror = (bits & 0x10000) ? $wnd.__dispatchEvent : null;
226     elem.onmousewheel = (bits & 0x20000) ? $wnd.__dispatchEvent : null;
227   }-*/
;
228 }
229
Popular Tags