KickJava   Java API By Example, From Geeks To Geeks.

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


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.core.client.JavaScriptObject;
19 import com.google.gwt.user.client.Element;
20 import com.google.gwt.user.client.Event;
21
22 /**
23  * Internet Explorer 6 implementation of
24  * {@link com.google.gwt.user.client.impl.DOMImpl}.
25  */

26 class DOMImplIE6 extends DOMImpl {
27
28   private static native int getBodyClientLeft() /*-{
29     // Standard mode uses $doc.documentElement.clientLeft
30     // Quirks mode uses $doc.body.clientLeft
31     return $doc.documentElement.clientLeft || $doc.body.clientLeft;
32   }-*/
;
33
34   private static native int getBodyClientTop() /*-{
35     // Standards mode uses $doc.documentElement.clientTop
36     // Quirks mode uses $doc.body.clientTop
37     return $doc.documentElement.clientTop || $doc.body.clientTop;
38   }-*/
;
39
40   /**
41    * A native map of image source URL strings to Image objects. All Image
42    * objects with values in this map are waiting on an asynchronous load to
43    * complete and have event handlers hooked. The moment the image finishes
44    * loading, it will be removed from this map.
45    */

46   JavaScriptObject srcImgMap;
47
48   public native boolean compare(Element elem1, Element elem2) /*-{
49     if (!elem1 && !elem2)
50       return true;
51     else if (!elem1 || !elem2)
52       return false;
53     return (elem1.uniqueID == elem2.uniqueID);
54   }-*/
;
55
56   public native Element createInputRadioElement(String JavaDoc group) /*-{
57     return $doc.createElement("<INPUT type='RADIO' name='" + group + "'>");
58   }-*/
;
59
60   /**
61    * Supports creating a select control with the multiple attribute to work
62    * around a bug in IE6 where changing the multiple attribute in a
63    * setAttribute call can cause subsequent setSelected calls to misbehave.
64    * Although this bug is fixed in IE7, this DOMImpl specialization is used
65    * for both IE6 and IE7, but it should be harmless.
66    */

67   public native Element createSelectElement(boolean multiple) /*-{
68     var html = multiple ? "<SELECT MULTIPLE>" : "<SELECT>";
69     return $doc.createElement(html);
70   }-*/
;
71   
72   public native int eventGetClientX(Event evt) /*-{
73     return evt.clientX -
74         @com.google.gwt.user.client.impl.DOMImplIE6::getBodyClientLeft()();
75   }-*/
;
76
77   public native int eventGetClientY(Event evt) /*-{
78     return evt.clientY -
79         @com.google.gwt.user.client.impl.DOMImplIE6::getBodyClientTop()();
80   }-*/
;
81
82   public native Element eventGetFromElement(Event evt) /*-{
83     return evt.fromElement ? evt.fromElement : null;
84   }-*/
;
85
86   public native int eventGetMouseWheelVelocityY(Event evt) /*-{
87     return Math.round(-evt.wheelDelta / 40);
88   }-*/
;
89
90   public native Element eventGetTarget(Event evt) /*-{
91     return evt.srcElement || null;
92   }-*/
;
93
94   public native Element eventGetToElement(Event evt) /*-{
95     return evt.toElement || null;
96   }-*/
;
97
98   public native void eventPreventDefault(Event evt) /*-{
99     evt.returnValue = false;
100   }-*/
;
101
102   public native String JavaDoc eventToString(Event evt) /*-{
103     if (evt.toString) return evt.toString();
104       return "[object Event]";
105   }-*/
;
106
107   public native int getAbsoluteLeft(Element elem) /*-{
108     // Standard mode || Quirks mode.
109     var scrollLeft = $doc.documentElement.scrollLeft || $doc.body.scrollLeft;
110     return (elem.getBoundingClientRect().left + scrollLeft)
111         - @com.google.gwt.user.client.impl.DOMImplIE6::getBodyClientLeft()();
112   }-*/
;
113
114   public native int getAbsoluteTop(Element elem) /*-{
115     // Standard mode || Quirks mode.
116     var scrollTop = $doc.documentElement.scrollTop || $doc.body.scrollTop;
117     return (elem.getBoundingClientRect().top + scrollTop)
118         - @com.google.gwt.user.client.impl.DOMImplIE6::getBodyClientTop()();
119    }-*/
;
120
121   public native Element getChild(Element elem, int index) /*-{
122     var child = elem.children[index];
123     return child || null;
124   }-*/
;
125
126   public native int getChildCount(Element elem) /*-{
127     return elem.children.length;
128   }-*/
;
129
130   public native int getChildIndex(Element parent, Element child) /*-{
131     var count = parent.children.length;
132     for (var i = 0; i < count; ++i) {
133       if (child.uniqueID == parent.children[i].uniqueID)
134         return i;
135     }
136     return -1;
137   }-*/
;
138
139   public native Element getFirstChild(Element elem) /*-{
140     var child = elem.firstChild;
141     return child || null;
142   }-*/
;
143
144   /*
145    * The src may not be set yet because of funky logic in setImgSrc(). See
146    * setImgSrc().
147    */

148   public native String JavaDoc getImgSrc(Element img) /*-{
149     return img.__targetSrc || img.src;
150   }-*/
;
151
152   public native String JavaDoc getInnerText(Element elem) /*-{
153     var ret = elem.innerText;
154     return (ret == null) ? null : ret;
155   }-*/
;
156
157   public native Element getNextSibling(Element elem) /*-{
158     var sib = elem.nextSibling;
159     return sib || null;
160   }-*/
;
161
162   public native Element getParent(Element elem) /*-{
163     var parent = elem.parentElement;
164     return parent || null;
165   }-*/
;
166
167   public native String JavaDoc iframeGetSrc(Element elem) /*-{
168     return elem.src;
169   }-*/
;
170
171   public native void init() /*-{
172     // Fix IE background image refresh bug, present through IE6
173     // see http://www.mister-pixel.com/#Content__state=is_that_simple
174     // this only works with IE6 SP1+
175     try {
176       $doc.execCommand("BackgroundImageCache", false, true);
177     } catch (e) {
178       // ignore error on other browsers
179     }
180   
181     // Initialize the URL -> Image map.
182     this.@com.google.gwt.user.client.impl.DOMImplIE6::srcImgMap = {};
183
184     // Set up event dispatchers.
185     $wnd.__dispatchEvent = function() {
186       if ($wnd.event.returnValue == null) {
187         $wnd.event.returnValue = true;
188         if (!@com.google.gwt.user.client.DOM::previewEvent(Lcom/google/gwt/user/client/Event;)($wnd.event))
189           return;
190       }
191
192       if (this.__listener)
193         @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;)($wnd.event, this, this.__listener);
194     };
195
196     $wnd.__dispatchDblClickEvent = function() {
197       var newEvent = $doc.createEventObject();
198       this.fireEvent('onclick', newEvent);
199       if (this.__eventBits & 2)
200         $wnd.__dispatchEvent.call(this);
201     };
202
203     $doc.body.onclick =
204     $doc.body.onmousedown =
205     $doc.body.onmouseup =
206     $doc.body.onmousemove =
207     $doc.body.onmousewheel =
208     $doc.body.onkeydown =
209     $doc.body.onkeypress =
210     $doc.body.onkeyup =
211     $doc.body.onfocus =
212     $doc.body.onblur =
213     $doc.body.ondblclick = $wnd.__dispatchEvent;
214   }-*/
;
215
216   public native void insertChild(Element parent, Element child, int index) /*-{
217     if (index >= parent.children.length)
218       parent.appendChild(child);
219     else
220       parent.insertBefore(child, parent.children[index]);
221   }-*/
;
222
223   public native void insertListItem(Element select, String JavaDoc text, String JavaDoc value,
224       int index) /*-{
225     // When we try to pass the populated option into this method, IE
226     // chokes, so we create the option here instead.
227     var newOption = document.createElement("Option");
228     if (index == -1) {
229       select.add(newOption);
230     } else{
231       select.add(newOption, index);
232     }
233     newOption.text = text; // assumed not null
234     newOption.value = value; // assumed not null
235   }-*/
;
236
237   public native boolean isOrHasChild(Element parent, Element child) /*-{
238     while (child) {
239       if (parent.uniqueID == child.uniqueID)
240         return true;
241       child = child.parentElement;
242     }
243     return false;
244   }-*/
;
245
246   public native void releaseCapture(Element elem) /*-{
247     elem.releaseCapture();
248   }-*/
;
249
250   public native void setCapture(Element elem) /*-{
251     elem.setCapture();
252   }-*/
;
253
254   /*
255    * Works around an IE problem where multiple images trying to load at the same
256    * time will generate a request per image. We fix this by only allowing the
257    * first image of a given URL to set its source immediately, but simultaneous
258    * requests for the same URL don't actually get their source set until the
259    * original load is complete.
260    *
261    * See comment on srcImgMap for the invariants that hold for the synthetic
262    * Image objects which are values in the map.
263    */

264   public native void setImgSrc(Element img, String JavaDoc src) /*-{
265     // Grab the URL -> Image map.
266     var map = this.@com.google.gwt.user.client.impl.DOMImplIE6::srcImgMap;
267     
268     // See if there's already an image for this URL in the map (e.g. loading).
269     var queuedImg = map[src];
270     if (queuedImg) {
271       // just add myself to its array, it will set my source later
272       queuedImg.__kids.push(img);
273       // record the desired src so it can be retreived synchronously
274       img.__targetSrc = src;
275       return;
276     }
277
278     // No outstanding requests; load the image.
279     img.src = src;
280
281     // If the image was in cache, the load may have just happened synchronously.
282     if (img.complete) {
283       // We're done
284       return;
285     }
286
287     // Image is loading asynchronously; put in map for chaining.
288     var kids = img.__kids = [];
289     map[src] = img;
290
291     // Store all the old handlers
292     var _onload = img.onload, _onerror = img.onerror, _onabort = img.onabort;
293
294     // Same cleanup code matter what state we end up in.
295     img.onload = function() {
296       finish("onload");
297     }
298     img.onerror = function() {
299       finish("onerror");
300     }
301     img.onabort = function() {
302       finish("onabort");
303     }
304
305     function finish(whichEvent) {
306       // Clean up: restore event handlers and remove from map.
307       img.onload = _onload; img.onerror = _onerror; img.onabort = _onabort;
308       delete map[src];
309       
310       // Set the source for all kids in a timer to ensure caching has happened.
311       window.setTimeout(function() {
312         for (var i = 0; i < kids.length; ++i) {
313           kids[i].src = img.src;
314           // clear the target src now that it's resolved
315           kids[i].__targetSrc = null;
316         }
317       }, 0);
318       
319       // call the original handler, if any
320       if (img[whichEvent]) {
321         img[whichEvent]();
322       }
323     }
324   }-*/
;
325
326   public native void setInnerText(Element elem, String JavaDoc text) /*-{
327     if (!text)
328       text = '';
329     elem.innerText = text;
330   }-*/
;
331
332   public native void sinkEvents(Element elem, int bits) /*-{
333     elem.__eventBits = bits;
334
335     elem.onclick = (bits & 0x00001) ? $wnd.__dispatchEvent : null;
336     // Add an ondblclick handler if onclick is desired to ensure that
337     // a user's double-click will result in two onclick events
338     elem.ondblclick = (bits & (0x00001 | 0x00002)) ? $wnd.__dispatchDblClickEvent : null;
339     elem.onmousedown = (bits & 0x00004) ? $wnd.__dispatchEvent : null;
340     elem.onmouseup = (bits & 0x00008) ? $wnd.__dispatchEvent : null;
341     elem.onmouseover = (bits & 0x00010) ? $wnd.__dispatchEvent : null;
342     elem.onmouseout = (bits & 0x00020) ? $wnd.__dispatchEvent : null;
343     elem.onmousemove = (bits & 0x00040) ? $wnd.__dispatchEvent : null;
344     elem.onkeydown = (bits & 0x00080) ? $wnd.__dispatchEvent : null;
345     elem.onkeypress = (bits & 0x00100) ? $wnd.__dispatchEvent : null;
346     elem.onkeyup = (bits & 0x00200) ? $wnd.__dispatchEvent : null;
347     elem.onchange = (bits & 0x00400) ? $wnd.__dispatchEvent : null;
348     elem.onfocus = (bits & 0x00800) ? $wnd.__dispatchEvent : null;
349     elem.onblur = (bits & 0x01000) ? $wnd.__dispatchEvent : null;
350     elem.onlosecapture = (bits & 0x02000) ? $wnd.__dispatchEvent : null;
351     elem.onscroll = (bits & 0x04000) ? $wnd.__dispatchEvent : null;
352     elem.onload = (bits & 0x08000) ? $wnd.__dispatchEvent : null;
353     elem.onerror = (bits & 0x10000) ? $wnd.__dispatchEvent : null;
354     elem.onmousewheel = (bits & 0x20000) ? $wnd.__dispatchEvent : null;
355   }-*/
;
356 }
357
Popular Tags