01: /*
02: * Copyright 2006 Google Inc.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
05: * use this file except in compliance with the License. You may obtain a copy of
06: * the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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.GWT;
19: import com.google.gwt.user.client.Element;
20:
21: /**
22: * Implementation interface for creating and manipulating focusable elements
23: * that aren't naturally focusable in all browsers, such as DIVs.
24: */
25: public class FocusImpl {
26:
27: private static FocusImpl implPanel = GWT.create(FocusImpl.class);
28:
29: /**
30: * This instance may not be a {@link FocusImplOld}, because that special case
31: * is only needed for things that aren't naturally focusable on some browsers,
32: * such as DIVs. This exact class works for truly focusable widgets on those
33: * browsers.
34: *
35: * The compiler will optimize out the conditional.
36: */
37: private static FocusImpl implWidget = (implPanel instanceof FocusImplOld) ? new FocusImpl()
38: : implPanel;
39:
40: /**
41: * Returns the focus implementation class for creating and manipulating
42: * focusable elements that aren't naturally focusable in all browsers, such as
43: * DIVs.
44: */
45: public static FocusImpl getFocusImplForPanel() {
46: return implPanel;
47: }
48:
49: /**
50: * Returns the focus implementation class for manipulating focusable elements
51: * that are naturally focusable in all browsers, such as text boxes.
52: */
53: public static FocusImpl getFocusImplForWidget() {
54: return implWidget;
55: }
56:
57: /**
58: * Not externally instantiable or extensible.
59: */
60: FocusImpl() {
61: }
62:
63: public native void blur(Element elem) /*-{
64: elem.blur();
65: }-*/;
66:
67: public native Element createFocusable() /*-{
68: var e = $doc.createElement("DIV");
69: e.tabIndex = 0;
70: return e;
71: }-*/;
72:
73: public native void focus(Element elem) /*-{
74: elem.focus();
75: }-*/;
76:
77: public native int getTabIndex(Element elem) /*-{
78: return elem.tabIndex;
79: }-*/;
80:
81: public native void setAccessKey(Element elem, char key) /*-{
82: elem.accessKey = key;
83: }-*/;
84:
85: public native void setTabIndex(Element elem, int index) /*-{
86: elem.tabIndex = index;
87: }-*/;
88: }
|