blob: cb39b1ea90bfb14d22bd09a96e35d10cd4523575 [file] [log] [blame]
/*
* Copyright 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.gwt.user.client.ui;
import com.google.gwt.dom.client.Element;
import com.google.gwt.event.dom.client.BlurEvent;
import com.google.gwt.event.dom.client.BlurHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.DoubleClickEvent;
import com.google.gwt.event.dom.client.DoubleClickHandler;
import com.google.gwt.event.dom.client.FocusEvent;
import com.google.gwt.event.dom.client.FocusHandler;
import com.google.gwt.event.dom.client.HasAllFocusHandlers;
import com.google.gwt.event.dom.client.HasAllKeyHandlers;
import com.google.gwt.event.dom.client.HasAllMouseHandlers;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.dom.client.HasDoubleClickHandlers;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.MouseDownHandler;
import com.google.gwt.event.dom.client.MouseMoveEvent;
import com.google.gwt.event.dom.client.MouseMoveHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.event.dom.client.MouseUpEvent;
import com.google.gwt.event.dom.client.MouseUpHandler;
import com.google.gwt.event.dom.client.MouseWheelEvent;
import com.google.gwt.event.dom.client.MouseWheelHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.impl.FocusImpl;
/**
* Abstract base class for most widgets that can receive keyboard focus.
*/
@SuppressWarnings("deprecation")
public abstract class FocusWidget extends Widget implements SourcesClickEvents,
HasClickHandlers, HasDoubleClickHandlers, HasFocus, HasEnabled,
HasAllFocusHandlers, HasAllKeyHandlers, HasAllMouseHandlers,
SourcesMouseEvents {
private static final FocusImpl impl = FocusImpl.getFocusImplForWidget();
/**
* Gets the FocusImpl instance.
*
* @return impl
*/
protected static FocusImpl getFocusImpl() {
return impl;
}
/**
* Creates a new focus widget with no element. {@link #setElement(Element)}
* must be called before any other methods.
*/
protected FocusWidget() {
}
/**
* Creates a new focus widget that wraps the specified browser element.
*
* @param elem the element to be wrapped
*/
protected FocusWidget(Element elem) {
setElement(elem);
}
public HandlerRegistration addBlurHandler(BlurHandler handler) {
return addDomHandler(handler, BlurEvent.getType());
}
public HandlerRegistration addClickHandler(ClickHandler handler) {
return addDomHandler(handler, ClickEvent.getType());
}
/**
* @deprecated Use {@link #addClickHandler} instead
*/
@Deprecated
public void addClickListener(ClickListener listener) {
ListenerWrapper.WrappedClickListener.add(this, listener);
}
public HandlerRegistration addDoubleClickHandler(DoubleClickHandler handler) {
return addDomHandler(handler, DoubleClickEvent.getType());
}
public HandlerRegistration addFocusHandler(FocusHandler handler) {
return addDomHandler(handler, FocusEvent.getType());
}
/**
* @deprecated Use {@link #addFocusHandler} instead
*/
@Deprecated
public void addFocusListener(FocusListener listener) {
ListenerWrapper.WrappedFocusListener.add(this, listener);
}
/**
* @deprecated Use {@link #addKeyDownHandler}, {@link
* #addKeyUpHandler} and {@link #addKeyPressHandler} instead
*/
@Deprecated
public void addKeyboardListener(KeyboardListener listener) {
ListenerWrapper.WrappedKeyboardListener.add(this, listener);
}
public HandlerRegistration addKeyDownHandler(KeyDownHandler handler) {
return addDomHandler(handler, KeyDownEvent.getType());
}
public HandlerRegistration addKeyPressHandler(KeyPressHandler handler) {
return addDomHandler(handler, KeyPressEvent.getType());
}
public HandlerRegistration addKeyUpHandler(KeyUpHandler handler) {
return addDomHandler(handler, KeyUpEvent.getType());
}
public HandlerRegistration addMouseDownHandler(MouseDownHandler handler) {
return addDomHandler(handler, MouseDownEvent.getType());
}
/**
* @deprecated Use {@link #addMouseOverHandler} {@link
* #addMouseMoveHandler}, {@link #addMouseDownHandler}, {@link
* #addMouseUpHandler} and {@link #addMouseOutHandler} instead
*/
@Deprecated
public void addMouseListener(MouseListener listener) {
ListenerWrapper.WrappedMouseListener.add(this, listener);
}
public HandlerRegistration addMouseMoveHandler(MouseMoveHandler handler) {
return addDomHandler(handler, MouseMoveEvent.getType());
}
public HandlerRegistration addMouseOutHandler(MouseOutHandler handler) {
return addDomHandler(handler, MouseOutEvent.getType());
}
public HandlerRegistration addMouseOverHandler(MouseOverHandler handler) {
return addDomHandler(handler, MouseOverEvent.getType());
}
public HandlerRegistration addMouseUpHandler(MouseUpHandler handler) {
return addDomHandler(handler, MouseUpEvent.getType());
}
public HandlerRegistration addMouseWheelHandler(MouseWheelHandler handler) {
return addDomHandler(handler, MouseWheelEvent.getType());
}
/**
* @deprecated Use {@link #addMouseWheelHandler} instead
*/
@Deprecated
public void addMouseWheelListener(MouseWheelListener listener) {
ListenerWrapper.WrappedMouseWheelListener.add(this, listener);
}
/**
* Gets the tab index.
*
* @return the tab index
*/
public int getTabIndex() {
return impl.getTabIndex(getElement());
}
/**
* Gets whether this widget is enabled.
*
* @return <code>true</code> if the widget is enabled
*/
public boolean isEnabled() {
return !DOM.getElementPropertyBoolean(getElement(), "disabled");
}
/**
* @deprecated Use the {@link HandlerRegistration#removeHandler} method on
* the object returned by {@link #addClickHandler} instead
*/
@Deprecated
public void removeClickListener(ClickListener listener) {
ListenerWrapper.WrappedClickListener.remove(this, listener);
}
/**
* @deprecated Use the {@link HandlerRegistration#removeHandler} method on
* the object returned by {@link #addFocusHandler} instead
*/
@Deprecated
public void removeFocusListener(FocusListener listener) {
ListenerWrapper.WrappedFocusListener.remove(this, listener);
}
/**
* @deprecated Use the {@link HandlerRegistration#removeHandler}
* method on the object returned by an add*Handler method instead
*/
@Deprecated
public void removeKeyboardListener(KeyboardListener listener) {
ListenerWrapper.WrappedKeyboardListener.remove(this, listener);
}
/**
* @deprecated Use the {@link HandlerRegistration#removeHandler}
* method on the object returned by an add*Handler method instead
*/
@Deprecated
public void removeMouseListener(MouseListener listener) {
ListenerWrapper.WrappedMouseListener.remove(this, listener);
}
/**
* @deprecated Use the {@link HandlerRegistration#removeHandler}
* method on the object returned by {@link #addMouseWheelHandler} instead
*/
@Deprecated
public void removeMouseWheelListener(MouseWheelListener listener) {
ListenerWrapper.WrappedMouseWheelListener.remove(this, listener);
}
public void setAccessKey(char key) {
DOM.setElementProperty(getElement(), "accessKey", "" + key);
}
/**
* Sets whether this widget is enabled.
*
* @param enabled <code>true</code> to enable the widget, <code>false</code>
* to disable it
*/
public void setEnabled(boolean enabled) {
DOM.setElementPropertyBoolean(getElement(), "disabled", !enabled);
}
public void setFocus(boolean focused) {
if (focused) {
impl.focus(getElement());
} else {
impl.blur(getElement());
}
}
public void setTabIndex(int index) {
impl.setTabIndex(getElement(), index);
}
@Override
protected void onAttach() {
super.onAttach();
// Accessibility: setting tab index to be 0 by default, ensuring element
// appears in tab sequence. We must ensure that the element doesn't already
// have a tabIndex set. This is not a problem for normal widgets, but when
// a widget is used to wrap an existing static element, it can already have
// a tabIndex.
int tabIndex = getTabIndex();
if (-1 == tabIndex) {
setTabIndex(0);
}
}
}