KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > user > client > ui > impl > FocusImplOld


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.impl;
17
18 import com.google.gwt.core.client.JavaScriptObject;
19 import com.google.gwt.user.client.Element;
20
21 /**
22  * Crazy implementation of {@link com.google.gwt.user.client.ui.impl.FocusImpl}
23  * that uses a hidden anchor to serve as a 'proxy' for focus.
24  */

25 public class FocusImplOld extends FocusImpl {
26
27   /*
28    * Use isolated method calls to create all of the handlers to avoid creating
29    * memory leaks via handler-closures-element.
30    */

31   JavaScriptObject blurHandler = createBlurHandler();
32   JavaScriptObject focusHandler = createFocusHandler();
33   JavaScriptObject mouseHandler = createMouseHandler();
34
35   public native void blur(Element elem) /*-{
36     elem.firstChild.blur();
37   }-*/
;
38
39   public native Element createFocusable() /*-{
40     // Use the infamous 'hidden input' trick to make a div effectively
41     // focusable.
42     var div = $doc.createElement('div');
43     var input = this.@com.google.gwt.user.client.ui.impl.FocusImplOld::createHiddenInput()();
44
45     // Add a mousedown listener to the div to focuses the input (to mimic the
46     // behavior of focusable elements on other browsers), and focus listeners
47     // on the input to propagate focus events back to the div.
48
49     // Note that we're using isolated lambda methods as the event listeners
50     // to avoid creating a memory leaks. (Lambdas here would create cycles
51     // involving the div and input). This also allows us to share a single
52     // set of handlers among every focusable item.
53
54     input.addEventListener(
55       'blur',
56       this.@com.google.gwt.user.client.ui.impl.FocusImplOld::blurHandler,
57       false);
58
59     input.addEventListener(
60       'focus',
61       this.@com.google.gwt.user.client.ui.impl.FocusImplOld::focusHandler,
62       false);
63
64     div.addEventListener(
65       'mousedown',
66       this.@com.google.gwt.user.client.ui.impl.FocusImplOld::mouseHandler,
67       false);
68
69     div.appendChild(input);
70     return div;
71   }-*/
;
72
73   public native void focus(Element elem) /*-{
74     elem.firstChild.focus();
75   }-*/
;
76
77   public native int getTabIndex(Element elem) /*-{
78     return elem.firstChild.tabIndex;
79   }-*/
;
80
81   public native void setAccessKey(Element elem, char key) /*-{
82     elem.firstChild.accessKey = key;
83   }-*/
;
84
85   public native void setTabIndex(Element elem, int index) /*-{
86     elem.firstChild.tabIndex = index;
87   }-*/
;
88
89   protected native JavaScriptObject createBlurHandler() /*-{
90     return function(evt) {
91       // This function is called directly as an event handler, so 'this' is
92       // set up by the browser to be the input on which the event is fired.
93       if (this.parentNode.onblur) {
94         this.parentNode.onblur(evt);
95       }
96     };
97   }-*/
;
98
99   protected native JavaScriptObject createFocusHandler() /*-{
100     return function(evt) {
101       // This function is called directly as an event handler, so 'this' is
102       // set up by the browser to be the input on which the event is fired.
103       if (this.parentNode.onfocus) {
104         this.parentNode.onfocus(evt);
105       }
106     };
107   }-*/
;
108
109   protected native Element createHiddenInput() /*-{
110     var input = $doc.createElement('input');
111     input.type = 'text';
112     input.style.width = input.style.height = 0;
113     input.style.zIndex = -1;
114     input.style.position = 'absolute';
115     return input;
116   }-*/
;
117
118   protected native JavaScriptObject createMouseHandler() /*-{
119     return function() {
120       // This function is called directly as an event handler, so 'this' is
121       // set up by the browser to be the div on which the event is fired.
122       this.firstChild.focus();
123     };
124   }-*/
;
125 }
126
Popular Tags