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.Event;
020: import com.google.gwt.user.client.ui.impl.FocusImpl;
021:
022: /**
023: * A simple panel that makes its contents focusable, and adds the ability to
024: * catch mouse and keyboard events.
025: */
026: public class FocusPanel extends SimplePanel implements HasFocus,
027: SourcesClickEvents, SourcesMouseEvents, SourcesMouseWheelEvents {
028:
029: static final FocusImpl impl = FocusImpl.getFocusImplForPanel();
030:
031: private ClickListenerCollection clickListeners;
032: private FocusListenerCollection focusListeners;
033: private KeyboardListenerCollection keyboardListeners;
034: private MouseListenerCollection mouseListeners;
035: private MouseWheelListenerCollection mouseWheelListeners;
036:
037: public FocusPanel() {
038: super (impl.createFocusable());
039: sinkEvents(Event.FOCUSEVENTS | Event.KEYEVENTS | Event.ONCLICK
040: | Event.MOUSEEVENTS | Event.ONMOUSEWHEEL);
041: }
042:
043: public FocusPanel(Widget child) {
044: this ();
045: setWidget(child);
046: }
047:
048: public void addClickListener(ClickListener listener) {
049: if (clickListeners == null) {
050: clickListeners = new ClickListenerCollection();
051: }
052: clickListeners.add(listener);
053: }
054:
055: public void addFocusListener(FocusListener listener) {
056: if (focusListeners == null) {
057: focusListeners = new FocusListenerCollection();
058: }
059: focusListeners.add(listener);
060: }
061:
062: public void addKeyboardListener(KeyboardListener listener) {
063: if (keyboardListeners == null) {
064: keyboardListeners = new KeyboardListenerCollection();
065: }
066: keyboardListeners.add(listener);
067: }
068:
069: public void addMouseListener(MouseListener listener) {
070: if (mouseListeners == null) {
071: mouseListeners = new MouseListenerCollection();
072: }
073: mouseListeners.add(listener);
074: }
075:
076: public void addMouseWheelListener(MouseWheelListener listener) {
077: if (mouseWheelListeners == null) {
078: mouseWheelListeners = new MouseWheelListenerCollection();
079: }
080: mouseWheelListeners.add(listener);
081: }
082:
083: public int getTabIndex() {
084: return impl.getTabIndex(getElement());
085: }
086:
087: @Override
088: public void onBrowserEvent(Event event) {
089: switch (DOM.eventGetType(event)) {
090: case Event.ONCLICK:
091: if (clickListeners != null) {
092: clickListeners.fireClick(this );
093: }
094: break;
095:
096: case Event.ONMOUSEDOWN:
097: case Event.ONMOUSEUP:
098: case Event.ONMOUSEMOVE:
099: case Event.ONMOUSEOVER:
100: case Event.ONMOUSEOUT:
101: if (mouseListeners != null) {
102: mouseListeners.fireMouseEvent(this , event);
103: }
104: break;
105:
106: case Event.ONMOUSEWHEEL:
107: if (mouseWheelListeners != null) {
108: mouseWheelListeners.fireMouseWheelEvent(this , event);
109: }
110: break;
111:
112: case Event.ONBLUR:
113: case Event.ONFOCUS:
114: if (focusListeners != null) {
115: focusListeners.fireFocusEvent(this , event);
116: }
117: break;
118:
119: case Event.ONKEYDOWN:
120: case Event.ONKEYUP:
121: case Event.ONKEYPRESS:
122: if (keyboardListeners != null) {
123: keyboardListeners.fireKeyboardEvent(this , event);
124: }
125: break;
126: }
127: }
128:
129: public void removeClickListener(ClickListener listener) {
130: if (clickListeners != null) {
131: clickListeners.remove(listener);
132: }
133: }
134:
135: public void removeFocusListener(FocusListener listener) {
136: if (focusListeners != null) {
137: focusListeners.remove(listener);
138: }
139: }
140:
141: public void removeKeyboardListener(KeyboardListener listener) {
142: if (keyboardListeners != null) {
143: keyboardListeners.remove(listener);
144: }
145: }
146:
147: public void removeMouseListener(MouseListener listener) {
148: if (mouseListeners != null) {
149: mouseListeners.remove(listener);
150: }
151: }
152:
153: public void removeMouseWheelListener(MouseWheelListener listener) {
154: if (mouseWheelListeners != null) {
155: mouseWheelListeners.remove(listener);
156: }
157: }
158:
159: public void setAccessKey(char key) {
160: impl.setAccessKey(getElement(), key);
161: }
162:
163: public void setFocus(boolean focused) {
164: if (focused) {
165: impl.focus(getElement());
166: } else {
167: impl.blur(getElement());
168: }
169: }
170:
171: public void setTabIndex(int index) {
172: impl.setTabIndex(getElement(), index);
173: }
174: }
|