KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > user > client > ui > DelegatingKeyboardListenerCollection


1 /*
2  * Copyright 2006 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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     fireKeyPress(owner, keyCode, modifiers);
70   }
71 }
72
Popular Tags