001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.user.client.ui;
017:
018: import com.google.gwt.user.client.DOM;
019: import com.google.gwt.user.client.Element;
020: import com.google.gwt.user.client.Event;
021: import com.google.gwt.user.client.ui.impl.FocusImpl;
022:
023: /**
024: * Abstract base class for most widgets that can receive keyboard focus.
025: */
026: public abstract class FocusWidget extends Widget implements
027: SourcesClickEvents, SourcesFocusEvents, HasFocus {
028:
029: private static final FocusImpl impl = FocusImpl
030: .getFocusImplForWidget();
031:
032: /**
033: * Gets the FocusImpl instance.
034: *
035: * @return impl
036: */
037: protected static FocusImpl getFocusImpl() {
038: return impl;
039: }
040:
041: private ClickListenerCollection clickListeners;
042: private FocusListenerCollection focusListeners;
043: private KeyboardListenerCollection keyboardListeners;
044:
045: /**
046: * Creates a new focus widget with no element. {@link #setElement(Element)}
047: * must be called before any other methods.
048: */
049: protected FocusWidget() {
050: }
051:
052: /**
053: * Creates a new focus widget that wraps the specified browser element.
054: *
055: * @param elem the element to be wrapped
056: */
057: protected FocusWidget(Element elem) {
058: setElement(elem);
059: }
060:
061: public void addClickListener(ClickListener listener) {
062: if (clickListeners == null) {
063: clickListeners = new ClickListenerCollection();
064: }
065: clickListeners.add(listener);
066: }
067:
068: public void addFocusListener(FocusListener listener) {
069: if (focusListeners == null) {
070: focusListeners = new FocusListenerCollection();
071: }
072: focusListeners.add(listener);
073: }
074:
075: public void addKeyboardListener(KeyboardListener listener) {
076: if (keyboardListeners == null) {
077: keyboardListeners = new KeyboardListenerCollection();
078: }
079: keyboardListeners.add(listener);
080: }
081:
082: public int getTabIndex() {
083: return impl.getTabIndex(getElement());
084: }
085:
086: /**
087: * Gets whether this widget is enabled.
088: *
089: * @return <code>true</code> if the widget is enabled
090: */
091: public boolean isEnabled() {
092: return !DOM.getElementPropertyBoolean(getElement(), "disabled");
093: }
094:
095: @Override
096: public void onBrowserEvent(Event event) {
097: switch (DOM.eventGetType(event)) {
098: case Event.ONCLICK:
099: if (clickListeners != null) {
100: clickListeners.fireClick(this );
101: }
102: break;
103:
104: case Event.ONBLUR:
105: case Event.ONFOCUS:
106: if (focusListeners != null) {
107: focusListeners.fireFocusEvent(this , event);
108: }
109: break;
110:
111: case Event.ONKEYDOWN:
112: case Event.ONKEYUP:
113: case Event.ONKEYPRESS:
114: if (keyboardListeners != null) {
115: keyboardListeners.fireKeyboardEvent(this , event);
116: }
117: break;
118: }
119: }
120:
121: public void removeClickListener(ClickListener listener) {
122: if (clickListeners != null) {
123: clickListeners.remove(listener);
124: }
125: }
126:
127: public void removeFocusListener(FocusListener listener) {
128: if (focusListeners != null) {
129: focusListeners.remove(listener);
130: }
131: }
132:
133: public void removeKeyboardListener(KeyboardListener listener) {
134: if (keyboardListeners != null) {
135: keyboardListeners.remove(listener);
136: }
137: }
138:
139: public void setAccessKey(char key) {
140: DOM.setElementProperty(getElement(), "accessKey", "" + key);
141: }
142:
143: /**
144: * Sets whether this widget is enabled.
145: *
146: * @param enabled <code>true</code> to enable the widget, <code>false</code>
147: * to disable it
148: */
149: public void setEnabled(boolean enabled) {
150: DOM.setElementPropertyBoolean(getElement(), "disabled",
151: !enabled);
152: }
153:
154: public void setFocus(boolean focused) {
155: if (focused) {
156: impl.focus(getElement());
157: } else {
158: impl.blur(getElement());
159: }
160: }
161:
162: public void setTabIndex(int index) {
163: impl.setTabIndex(getElement(), index);
164: }
165:
166: @Override
167: protected void setElement(Element elem) {
168: super .setElement(elem);
169: sinkEvents(Event.ONCLICK | Event.FOCUSEVENTS | Event.KEYEVENTS);
170: }
171:
172: /**
173: * Fire all current {@link ClickListener}.
174: */
175: void fireClickListeners() {
176: /*
177: * Implementation note: PushButton needs to fire click listeners manually.
178: * Exposing this method so it can do so.
179: */
180: if (clickListeners != null) {
181: clickListeners.fireClick(this);
182: }
183: }
184: }
|