KickJava   Java API By Example, From Geeks To Geeks.

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


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.DOM;
19 import com.google.gwt.user.client.Element;
20 import com.google.gwt.user.client.Event;
21 import com.google.gwt.user.client.EventListener;
22
23 /**
24  * Native implementation associated with {@link com.google.gwt.user.client.DOM}.
25  */

26 public abstract class DOMImpl {
27
28   public native void appendChild(Element parent, Element child) /*-{
29     parent.appendChild(child);
30   }-*/
;
31
32   public abstract boolean compare(Element elem1, Element elem2);
33
34   public native Element createElement(String JavaDoc tag) /*-{
35     return $doc.createElement(tag);
36   }-*/
;
37
38   public native Element createInputElement(String JavaDoc type) /*-{
39     var e = $doc.createElement("INPUT");
40     e.type = type;
41     return e;
42   }-*/
;
43
44   public abstract Element createInputRadioElement(String JavaDoc group);
45
46   public Element createSelectElement(boolean multiple) {
47     Element select = createElement("select");
48     if (multiple) {
49       setElementPropertyBoolean(select, "multiple", true);
50     }
51     return select;
52   }
53
54   public native void eventCancelBubble(Event evt, boolean cancel) /*-{
55     evt.cancelBubble = cancel;
56   }-*/
;
57
58   public native boolean eventGetAltKey(Event evt) /*-{
59     return evt.altKey;
60   }-*/
;
61
62   public native int eventGetButton(Event evt) /*-{
63     return evt.button;
64   }-*/
;
65
66   public native int eventGetClientX(Event evt) /*-{
67     return evt.clientX;
68   }-*/
;
69
70   public native int eventGetClientY(Event evt) /*-{
71     return evt.clientY;
72   }-*/
;
73
74   public native boolean eventGetCtrlKey(Event evt) /*-{
75     return evt.ctrlKey;
76   }-*/
;
77
78   public abstract Element eventGetFromElement(Event evt);
79
80   public native int eventGetKeyCode(Event evt) /*-{
81     // 'which' gives the right key value, except when it doesn't -- in which
82     // case, keyCode gives the right value on all browsers.
83     return evt.which || evt.keyCode;
84   }-*/
;
85
86   public native boolean eventGetMetaKey(Event evt) /*-{
87     return !!evt.getMetaKey;
88   }-*/
;
89
90   public abstract int eventGetMouseWheelVelocityY(Event evt);
91
92   public native boolean eventGetRepeat(Event evt) /*-{
93     return evt.repeat;
94   }-*/
;
95
96   public native int eventGetScreenX(Event evt) /*-{
97     return evt.screenX;
98   }-*/
;
99
100   public native int eventGetScreenY(Event evt) /*-{
101     return evt.screenY;
102   }-*/
;
103
104   public native boolean eventGetShiftKey(Event evt) /*-{
105     return evt.shiftKey;
106   }-*/
;
107
108   public abstract Element eventGetTarget(Event evt);
109
110   public abstract Element eventGetToElement(Event evt);
111
112   public native String JavaDoc eventGetType(Event evt) /*-{
113     return evt.type;
114   }-*/
;
115
116   public native int eventGetTypeInt(Event evt) /*-{
117     switch (evt.type) {
118       case "blur": return 0x01000;
119       case "change": return 0x00400;
120       case "click": return 0x00001;
121       case "dblclick": return 0x00002;
122       case "focus": return 0x00800;
123       case "keydown": return 0x00080;
124       case "keypress": return 0x00100;
125       case "keyup": return 0x00200;
126       case "load": return 0x08000;
127       case "losecapture": return 0x02000;
128       case "mousedown": return 0x00004;
129       case "mousemove": return 0x00040;
130       case "mouseout": return 0x00020;
131       case "mouseover": return 0x00010;
132       case "mouseup": return 0x00008;
133       case "scroll": return 0x04000;
134       case "error": return 0x10000;
135       case "mousewheel": return 0x20000;
136       case "DOMMouseScroll": return 0x20000;
137     }
138   }-*/
;
139
140   public abstract void eventPreventDefault(Event evt);
141
142   public native void eventSetKeyCode(Event evt, char key) /*-{
143     evt.keyCode = key;
144   }-*/
;
145
146   public abstract String JavaDoc eventToString(Event evt);
147
148   public native int getAbsoluteLeft(Element elem) /*-{
149     var left = 0;
150     var curr = elem;
151     // This intentionally excludes body which has a null offsetParent.
152     while (curr.offsetParent) {
153       left -= curr.scrollLeft;
154       curr = curr.parentNode;
155     }
156     while (elem) {
157       left += elem.offsetLeft;
158       elem = elem.offsetParent;
159     }
160     return left;
161   }-*/
;
162
163   public native int getAbsoluteTop(Element elem) /*-{
164     var top = 0;
165     var curr = elem;
166     // This intentionally excludes body which has a null offsetParent.
167     while (curr.offsetParent) {
168       top -= curr.scrollTop;
169       curr = curr.parentNode;
170     }
171     while (elem) {
172       top += elem.offsetTop;
173       elem = elem.offsetParent;
174     }
175     return top;
176   }-*/
;
177
178   public abstract Element getChild(Element elem, int index);
179
180   public abstract int getChildCount(Element elem);
181
182   public abstract int getChildIndex(Element parent, Element child);
183
184   public native String JavaDoc getElementAttribute(Element elem, String JavaDoc attr) /*-{
185     var ret = elem.getAttribute(attr);
186     return (ret == null) ? null : ret;
187   }-*/
;
188
189   public native Element getElementById(String JavaDoc id) /*-{
190     var elem = $doc.getElementById(id);
191     return elem || null;
192   }-*/
;
193
194   public native String JavaDoc getElementProperty(Element elem, String JavaDoc prop) /*-{
195     var ret = elem[prop];
196     return (ret == null) ? null : String(ret);
197   }-*/
;
198
199   public native boolean getElementPropertyBoolean(Element elem, String JavaDoc prop) /*-{
200     return !!elem[prop];
201   }-*/
;
202
203   public native int getElementPropertyInt(Element elem, String JavaDoc prop) /*-{
204     var i = parseInt(elem[prop]);
205     if (!i) {
206       return 0;
207     }
208     return i;
209   }-*/
;
210
211   public native int getEventsSunk(Element elem) /*-{
212     return elem.__eventBits || 0;
213   }-*/
;
214
215   public abstract Element getFirstChild(Element elem);
216
217   public native String JavaDoc getImgSrc(Element img) /*-{
218     return img.src;
219   }-*/
;
220
221   public native String JavaDoc getInnerHTML(Element elem) /*-{
222     var ret = elem.innerHTML;
223     return (ret == null) ? null : ret;
224   }-*/
;
225
226   public native String JavaDoc getInnerText(Element node) /*-{
227     // To mimic IE's 'innerText' property in the W3C DOM, we need to recursively
228     // concatenate all child text nodes (depth first).
229     var text = '', child = node.firstChild;
230     while (child) {
231       // 1 == Element node
232       if (child.nodeType == 1) {
233         text += this.@com.google.gwt.user.client.impl.DOMImpl::getInnerText(Lcom/google/gwt/user/client/Element;)(child);
234       } else if (child.nodeValue) {
235         text += child.nodeValue;
236       }
237       child = child.nextSibling;
238     }
239     return text;
240   }-*/
;
241
242   public native int getIntStyleAttribute(Element elem, String JavaDoc attr) /*-{
243     var i = parseInt(elem.style[attr]);
244     if (!i) {
245       return 0;
246     }
247     return i;
248   }-*/
;
249
250   public abstract Element getNextSibling(Element elem);
251
252   public abstract Element getParent(Element elem);
253
254   public native String JavaDoc getStyleAttribute(Element elem, String JavaDoc attr) /*-{
255     var ret = elem.style[attr];
256     return (ret == null) ? null : ret;
257   }-*/
;
258
259   public abstract void init();
260
261   public native void insertBefore(Element parent, Element child, Element before) /*-{
262     parent.insertBefore(child, before);
263   }-*/
;
264
265   public abstract void insertChild(Element parent, Element child,
266       int index);
267
268   /**
269    * @see DOM#insertListItem(Element, String, String, int)
270    */

271   public void insertListItem(Element select, String JavaDoc item, String JavaDoc value,
272       int index) {
273     Element option = DOM.createElement("OPTION");
274     DOM.setInnerText(option, item);
275     DOM.setElementProperty(option, "value", value);
276     if (index == -1) {
277       DOM.appendChild(select, option);
278     } else {
279       DOM.insertChild(select, option, index);
280     }
281   }
282
283   public abstract boolean isOrHasChild(Element parent, Element child);
284
285   public abstract void releaseCapture(Element elem);
286
287   public native void removeChild(Element parent, Element child) /*-{
288     parent.removeChild(child);
289   }-*/
;
290
291   public native void removeElementAttribute(Element elem, String JavaDoc attr) /*-{
292     elem.removeAttribute(attr);
293   }-*/
;
294
295   public native void scrollIntoView(Element elem) /*-{
296     var left = elem.offsetLeft, top = elem.offsetTop;
297     var width = elem.offsetWidth, height = elem.offsetHeight;
298
299     if (elem.parentNode != elem.offsetParent) {
300       left -= elem.parentNode.offsetLeft;
301       top -= elem.parentNode.offsetTop;
302     }
303
304     var cur = elem.parentNode;
305     while (cur && (cur.nodeType == 1)) {
306       // body tags are implicitly scrollable
307       if ((cur.style.overflow == 'auto') || (cur.style.overflow == 'scroll') ||
308           (cur.tagName == 'BODY')) {
309       
310         if (left < cur.scrollLeft) {
311           cur.scrollLeft = left;
312         }
313         if (left + width > cur.scrollLeft + cur.clientWidth) {
314           cur.scrollLeft = (left + width) - cur.clientWidth;
315         }
316         if (top < cur.scrollTop) {
317           cur.scrollTop = top;
318         }
319         if (top + height > cur.scrollTop + cur.clientHeight) {
320           cur.scrollTop = (top + height) - cur.clientHeight;
321         }
322       }
323
324       var offsetLeft = cur.offsetLeft, offsetTop = cur.offsetTop;
325       if (cur.parentNode != cur.offsetParent) {
326         offsetLeft -= cur.parentNode.offsetLeft;
327         offsetTop -= cur.parentNode.offsetTop;
328       }
329
330       left += offsetLeft - cur.scrollLeft;
331       top += offsetTop - cur.scrollTop;
332       cur = cur.parentNode;
333     }
334   }-*/
;
335
336   public abstract void setCapture(Element elem);
337
338   public native void setElementAttribute(Element elem, String JavaDoc attr, String JavaDoc value) /*-{
339     elem.setAttribute(attr, value);
340   }-*/
;
341
342   public native void setElementProperty(Element elem, String JavaDoc prop, String JavaDoc value) /*-{
343     elem[prop] = value;
344   }-*/
;
345
346   public native void setElementPropertyBoolean(Element elem, String JavaDoc prop,
347       boolean value) /*-{
348     elem[prop] = value;
349   }-*/
;
350
351   public native void setElementPropertyInt(Element elem, String JavaDoc prop, int value) /*-{
352     elem[prop] = value;
353   }-*/
;
354
355   public native void setEventListener(Element elem,
356       EventListener listener) /*-{
357     elem.__listener = listener;
358   }-*/
;
359
360   public native void setImgSrc(Element img, String JavaDoc src) /*-{
361     img.src = src;
362   }-*/
;
363
364   public native void setInnerHTML(Element elem, String JavaDoc html) /*-{
365     if (!html) {
366       html = '';
367     }
368     elem.innerHTML = html;
369   }-*/
;
370
371   public native void setInnerText(Element elem, String JavaDoc text) /*-{
372     // Remove all children first.
373     while (elem.firstChild) {
374       elem.removeChild(elem.firstChild);
375     }
376     // Add a new text node.
377     if (text != null) {
378       elem.appendChild($doc.createTextNode(text));
379     }
380   }-*/
;
381
382   public native void setIntStyleAttribute(Element elem, String JavaDoc attr, int value) /*-{
383     elem.style[attr] = value;
384   }-*/
;
385
386   public native void setOptionText(Element select, String JavaDoc text, int index) /*-{
387     // IE doesn't properly update the screen when you use
388     // setAttribute("option", text), so we instead directly assign to the
389     // 'option' property, which works correctly on all browsers.
390     var option = select.options[index];
391     option.text = text;
392   }-*/
;
393
394   public native void setStyleAttribute(Element elem, String JavaDoc attr,
395       String JavaDoc value) /*-{
396     elem.style[attr] = value;
397   }-*/
;
398
399   public abstract void sinkEvents(Element elem, int eventBits);
400
401   public native String JavaDoc toString(Element elem) /*-{
402     return elem.outerHTML;
403   }-*/
;
404 }
405
Popular Tags