KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2007 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 package com.google.gwt.user.client.ui;
17
18 import com.google.gwt.user.client.DOM;
19 import com.google.gwt.user.client.Event;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.Iterator JavaDoc;
23
24 /**
25  * A helper class for implementers of the SourcesKeyboardEvents interface. This
26  * subclass of {@linkArrayList} assumes that all objects added to it will be of
27  * type {@link com.google.gwt.user.client.ui.KeyboardListener}.
28  */

29 public class KeyboardListenerCollection extends ArrayList JavaDoc {
30
31   /**
32    * Gets the keyboard modifiers associated with a DOMEvent.
33    *
34    * @param event the event.
35    * @return the modifiers as defined in {@link KeyboardListener}.
36    */

37   public static int getKeyboardModifiers(Event event) {
38     return (DOM.eventGetShiftKey(event) ? KeyboardListener.MODIFIER_SHIFT : 0)
39       | (DOM.eventGetMetaKey(event) ? KeyboardListener.MODIFIER_META : 0)
40       | (DOM.eventGetCtrlKey(event) ? KeyboardListener.MODIFIER_CTRL : 0)
41       | (DOM.eventGetAltKey(event) ? KeyboardListener.MODIFIER_ALT : 0);
42   }
43
44   /**
45    * Automatically fires the appropriate keyboard event to all listeners. If the
46    * given event is not a keyboard event, no action will be performed.
47    *
48    * @param sender the widget sending the event.
49    * @param event the Event received by the widget.
50    */

51   public void fireKeyboardEvent(Widget sender, Event event) {
52     int modifiers = getKeyboardModifiers(event);
53
54     switch (DOM.eventGetType(event)) {
55       case Event.ONKEYDOWN:
56         fireKeyDown(sender, (char)DOM.eventGetKeyCode(event), modifiers);
57         break;
58
59       case Event.ONKEYUP:
60         fireKeyUp(sender, (char)DOM.eventGetKeyCode(event), modifiers);
61         break;
62
63       case Event.ONKEYPRESS:
64         fireKeyPress(sender, (char)DOM.eventGetKeyCode(event), modifiers);
65         break;
66     }
67   }
68
69   /**
70    * Fires a keyDown event to all listeners.
71    *
72    * @param sender the widget sending the event.
73    * @param keyCode the keyCode to send with the event.
74    * @param modifiers the modifier keys pressed at when the event occurred. This
75    * value is a combination of the bits defined by
76    * {@link KeyboardListener#MODIFIER_SHIFT},
77    * {@link KeyboardListener#MODIFIER_CTRL}, and
78    * {@link KeyboardListener#MODIFIER_ALT}.
79    */

80   public void fireKeyDown(Widget sender, char keyCode, int modifiers) {
81     for (Iterator JavaDoc it = iterator(); it.hasNext();) {
82       KeyboardListener listener = (KeyboardListener) it.next();
83       listener.onKeyDown(sender, keyCode, modifiers);
84     }
85   }
86
87   /**
88    * Fires a keyDown event to all listeners.
89    *
90    * @param sender the widget sending the event.
91    * @param key the key to send with the event.
92    * @param modifiers the modifier keys pressed at when the event occurred. This
93    * value is a combination of the bits defined by
94    * {@link KeyboardListener#MODIFIER_SHIFT},
95    * {@link KeyboardListener#MODIFIER_CTRL}, and
96    * {@link KeyboardListener#MODIFIER_ALT}.
97    */

98   public void fireKeyPress(Widget sender, char key, int modifiers) {
99     for (Iterator JavaDoc it = iterator(); it.hasNext();) {
100       KeyboardListener listener = (KeyboardListener) it.next();
101       listener.onKeyPress(sender, key, modifiers);
102     }
103   }
104
105   /**
106    * Fires a keyDown event to all listeners.
107    *
108    * @param sender the widget sending the event.
109    * @param keyCode the keyCode to send with the event.
110    * @param modifiers the modifier keys pressed at when the event occurred. This
111    * value is a combination of the bits defined by
112    * {@link KeyboardListener#MODIFIER_SHIFT},
113    * {@link KeyboardListener#MODIFIER_CTRL}, and
114    * {@link KeyboardListener#MODIFIER_ALT}.
115    */

116   public void fireKeyUp(Widget sender, char keyCode, int modifiers) {
117     for (Iterator JavaDoc it = iterator(); it.hasNext();) {
118       KeyboardListener listener = (KeyboardListener) it.next();
119       listener.onKeyUp(sender, keyCode, modifiers);
120     }
121   }
122 }
123
Popular Tags