KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Mozilla implementation of StandardBrowser. The main difference between
23  * Mozilla and others is that element comparison must be done using isSameNode()
24  * (== comparison doesn't always give you the right answer, probably because of
25  * its JavaScript wrappers for xpcom dom nodes).
26  */

27 class DOMImplMozilla extends DOMImplStandard {
28
29   public native boolean compare(Element elem1, Element elem2) /*-{
30     if (!elem1 && !elem2) {
31       return true;
32     } else if (!elem1 || !elem2) {
33       return false;
34     }
35     return (elem1.isSameNode(elem2));
36   }-*/
;
37
38   public native int eventGetButton(Event evt) /*-{
39     // Mozilla and IE disagree on what the button codes for buttons should be.
40     // Translating to match IE standard.
41     var button = evt.button;
42     if(button == 0) {
43       return 1;
44     } else if (button == 1) {
45       return 4;
46     } else {
47       return button;
48     }
49  }-*/
;
50
51   public native int eventGetMouseWheelVelocityY(Event evt) /*-{
52     return evt.detail;
53   }-*/
;
54
55   public native int getAbsoluteLeft(Element elem) /*-{
56     // We cannot use DOMImpl here because offsetLeft/Top return erroneous
57     // values when overflow is not visible. We have to difference screenX
58     // here due to a change in getBoxObjectFor which causes inconsistencies
59     // on whether the calculations are inside or outside of the element's
60     // border.
61     return $doc.getBoxObjectFor(elem).screenX
62         - $doc.getBoxObjectFor($doc.documentElement).screenX;
63   }-*/
;
64
65   public native int getAbsoluteTop(Element elem) /*-{
66     // We cannot use DOMImpl here because offsetLeft/Top return erroneous
67     // values when overflow is not visible. We have to difference screenY
68     // here due to a change in getBoxObjectFor which causes inconsistencies
69     // on whether the calculations are inside or outside of the element's
70     // border.
71     return $doc.getBoxObjectFor(elem).screenY
72         - $doc.getBoxObjectFor($doc.documentElement).screenY;
73   }-*/
;
74   
75   public native int getChildIndex(Element parent, Element toFind) /*-{
76     var count = 0, child = parent.firstChild;
77     while (child) {
78       if (child.isSameNode(toFind)) {
79         return count;
80       }
81       if (child.nodeType == 1) {
82         ++count;
83       }
84       child = child.nextSibling;
85     }
86     return -1;
87   }-*/
;
88
89   public void init() {
90     super.init();
91     initMozilla();
92   }
93
94   public native boolean isOrHasChild(Element parent, Element child) /*-{
95     while (child) {
96       if (parent.isSameNode(child)) {
97         return true;
98       }
99
100       try {
101         child = child.parentNode;
102       } catch(e) {
103         // Give up on 'Permission denied to get property
104         // HTMLDivElement.parentNode'
105         // See https://bugzilla.mozilla.org/show_bug.cgi?id=208427
106         return false;
107       }
108
109       if (child && (child.nodeType != 1)) {
110         child = null;
111       }
112     }
113     return false;
114   }-*/
;
115
116   public native void releaseCapture(Element elem) /*-{
117     if (elem.isSameNode($wnd.__captureElem)) {
118       $wnd.__captureElem = null;
119     }
120   }-*/
;
121
122   public void sinkEvents(Element elem, int bits) {
123     super.sinkEvents(elem, bits);
124     sinkEventsMozilla(elem, bits);
125   }
126
127   public native void sinkEventsMozilla(Element elem, int bits) /*-{
128     if (bits & 0x20000) {
129       elem.addEventListener('DOMMouseScroll', $wnd.__dispatchEvent, false);
130     }
131   }-*/
;
132
133   public native String JavaDoc toString(Element elem) /*-{
134     // Basic idea is to use the innerHTML property by copying the node into a
135     // div and getting the innerHTML
136     var temp = elem.cloneNode(true);
137     var tempDiv = $doc.createElement("DIV");
138     tempDiv.appendChild(temp);
139     outer = tempDiv.innerHTML;
140     temp.innerHTML = "";
141     return outer;
142   }-*/
;
143
144   protected native void initMozilla() /*-{
145     $wnd.addEventListener(
146       'mouseout',
147       function(evt) {
148         var cap = $wnd.__captureElem;
149         if (cap && !evt.relatedTarget) {
150           // Mozilla has the interesting habit of sending a mouseout event
151           // with an 'html' element as the target when the mouse is released
152           // outside of the browser window.
153           if ('html' == evt.target.tagName.toLowerCase()) {
154             // When this occurs, we synthesize a mouseup event, which is
155             // useful for all sorts of dragging code (like in DialogBox).
156             var muEvent = $doc.createEvent('MouseEvents');
157             muEvent.initMouseEvent('mouseup', true, true, $wnd, 0,
158               evt.screenX, evt.screenY, evt.clientX, evt.clientY, evt.ctrlKey,
159               evt.altKey, evt.shiftKey, evt.metaKey, evt.button, null);
160             cap.dispatchEvent(muEvent);
161           }
162         }
163       },
164       true
165     );
166
167     $wnd.addEventListener('DOMMouseScroll', $wnd.__dispatchCapturedMouseEvent,
168       true);
169   }-*/
;
170 }
171
Popular Tags