01: /*
02: * Copyright 2007 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:
17: package com.google.gwt.user.client.ui;
18:
19: /**
20: * {@link KeyboardListenerCollection} used to correctly hook up event listeners
21: * to the composite's wrapped widget.
22: *
23: * <p>
24: * For example, {@link Composite} widgets often need to listen to events
25: * generated on their wrapped widget. Upon the firing of a wrapped widget's
26: * event, the composite widget must fire its own listeners with itself as the
27: * source of the event. To use a {@link DelegatingKeyboardListenerCollection},
28: * simply use the {@link DelegatingKeyboardListenerCollection} instead of a
29: * {@link KeyboardListenerCollection}. For example, in {@link SuggestBox}, the
30: * following code is used to listen to keyboard events on the {@link SuggestBox}'s
31: * underlying widget.
32: * </p>
33: *
34: * <pre>
35: * public void addKeyboardListener(KeyboardListener listener) {
36: * if (keyboardListeners == null) {
37: * keyboardListeners = new DelegatingKeyboardListenerCollection(this, box);
38: * }
39: * keyboardListeners.add(listener);
40: * }
41: *</pre>
42: */
43: public class DelegatingKeyboardListenerCollection extends
44: KeyboardListenerCollection implements KeyboardListener {
45:
46: private final Widget owner;
47:
48: /**
49: * Constructor for {@link DelegatingKeyboardListenerCollection}.
50: *
51: * @param owner owner of listeners
52: * @param delegatedTo source of events
53: */
54: public DelegatingKeyboardListenerCollection(Widget owner,
55: SourcesKeyboardEvents delegatedTo) {
56: this .owner = owner;
57: delegatedTo.addKeyboardListener(this );
58: }
59:
60: public void onKeyDown(Widget sender, char keyCode, int modifiers) {
61: fireKeyDown(owner, keyCode, modifiers);
62: }
63:
64: public void onKeyPress(Widget sender, char keyCode, int modifiers) {
65: fireKeyPress(owner, keyCode, modifiers);
66: }
67:
68: public void onKeyUp(Widget sender, char keyCode, int modifiers) {
69: fireKeyUp(owner, keyCode, modifiers);
70: }
71: }
|