Source Code Cross Referenced for DelegatingKeyboardListenerCollectionTest.java in  » Ajax » GWT » com » google » gwt » user » client » ui » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Ajax » GWT » com.google.gwt.user.client.ui 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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.junit.client.GWTTestCase;
019:
020:        /**
021:         * Tests keyboard events in the {@link DelegatingKeyboardListenerCollection}
022:         * class.
023:         */
024:        public class DelegatingKeyboardListenerCollectionTest extends
025:                GWTTestCase implements  KeyboardListener {
026:
027:            /**
028:             * A {@link Widget} that uses the
029:             * {@link DelegatingKeyboardListenerCollection} to save its list
030:             * of keyboard events.
031:             */
032:            public class DelegatingWidget extends Widget {
033:                // The delegating collection of keyboard listeners
034:                private DelegatingKeyboardListenerCollection keyboardListeners;
035:
036:                // The owner of all events
037:                private Widget eventOwner;
038:
039:                /**
040:                 * Adds a listener interface to receive keyboard events.
041:                 * 
042:                 * @param listener the listener interface to add
043:                 */
044:                public void addKeyboardListener(KeyboardListener listener) {
045:                    if (keyboardListeners == null) {
046:                        this .eventOwner = new Widget();
047:                        keyboardListeners = new DelegatingKeyboardListenerCollection(
048:                                this .eventOwner, new TextBox());
049:                    }
050:                    keyboardListeners.add(listener);
051:                }
052:
053:                /**
054:                 * Get the event owner.
055:                 * 
056:                 * @return the event owner
057:                 */
058:                public Widget getEventOwner() {
059:                    return this .eventOwner;
060:                }
061:
062:                /**
063:                 * Get the keyboardListener.
064:                 * 
065:                 * @return the keyboardListener
066:                 */
067:                public DelegatingKeyboardListenerCollection getKeyboardListeners() {
068:                    return this .keyboardListeners;
069:                }
070:
071:                /**
072:                 * Removes a previously added listener interface.
073:                 * 
074:                 * @param listener the listener interface to remove
075:                 */
076:                public void removeKeyboardListener(KeyboardListener listener) {
077:                    /*
078:                     * This method is not used in the test or in the
079:                     * DelegatingKeyboardListenerCollection constructor
080:                     */
081:                }
082:            }
083:
084:            // The owner of the events
085:            private DelegatingWidget delegatingWidget;
086:
087:            // A bit used to verify that some event handler was called
088:            private boolean eventHandled;
089:
090:            // The name of the last event fired
091:            private String lastEventName;
092:
093:            public String getModuleName() {
094:                return "com.google.gwt.user.User";
095:            }
096:
097:            /**
098:             * Handle the key down event from the ownerOfEvents.
099:             * 
100:             * @param sender the widget sending the event.
101:             * @param keyCode the key to send with the event.
102:             * @param modifiers the modifier keys pressed at when the event occurred. This
103:             *          value is a combination of the bits defined by
104:             *          {@link KeyboardListener#MODIFIER_SHIFT},
105:             *          {@link KeyboardListener#MODIFIER_CTRL}, and
106:             *          {@link KeyboardListener#MODIFIER_ALT}.
107:             */
108:            public void onKeyDown(Widget sender, char keyCode, int modifiers) {
109:                this .handleKeyEvent(sender, "onKeyDown");
110:            }
111:
112:            /**
113:             * Handle the key press event from the ownerOfEvents.
114:             * 
115:             * @param sender the widget sending the event.
116:             * @param keyCode the key to send with the event.
117:             * @param modifiers the modifier keys pressed at when the event occurred. This
118:             *          value is a combination of the bits defined by
119:             *          {@link KeyboardListener#MODIFIER_SHIFT},
120:             *          {@link KeyboardListener#MODIFIER_CTRL}, and
121:             *          {@link KeyboardListener#MODIFIER_ALT}.
122:             */
123:            public void onKeyPress(Widget sender, char keyCode, int modifiers) {
124:                this .handleKeyEvent(sender, "onKeyPress");
125:            }
126:
127:            /**
128:             * Handle the key up event from the ownerOfEvents.
129:             * 
130:             * @param sender the widget sending the event.
131:             * @param keyCode the key to send with the event.
132:             * @param modifiers the modifier keys pressed at when the event occurred. This
133:             *          value is a combination of the bits defined by
134:             *          {@link KeyboardListener#MODIFIER_SHIFT},
135:             *          {@link KeyboardListener#MODIFIER_CTRL}, and
136:             *          {@link KeyboardListener#MODIFIER_ALT}.
137:             */
138:            public void onKeyUp(Widget sender, char keyCode, int modifiers) {
139:                this .handleKeyEvent(sender, "onKeyUp");
140:            }
141:
142:            /**
143:             * Tests that the key event handlers re-fire the correct key events to
144:             * the correct owner.  The owner of the events is a {@link TextBox}, which
145:             * implements {@link SourcesKeyboardEvents} and allows us to check that
146:             * the owner is correctly re-firing the events. 
147:             */
148:            public void testKeyEvents() {
149:                // Create a keyboard event listener with a DeletagingWidgetknown owner
150:                this .delegatingWidget = new DelegatingWidget();
151:                this .delegatingWidget.addKeyboardListener(this );
152:
153:                // Fire events from through delegate, which should set the correct owner
154:                this .fireKeyEvent("onKeyDown");
155:                this .fireKeyEvent("onKeyUp");
156:                this .fireKeyEvent("onKeyPress");
157:            }
158:
159:            /**
160:             * This helper method simulates the firing of an event by the delegating widget,
161:             * with a generic Widget as the source.
162:             * 
163:             * @param eventName the name of the event to fire
164:             */
165:            private void fireKeyEvent(String eventName) {
166:                this .lastEventName = eventName; // Set the name of this event
167:                this .eventHandled = false; // Mark that we haven't handled it yet
168:
169:                // Fire the actual event through the delegate
170:                if (eventName.compareTo("onKeyDown") == 0) {
171:                    this .delegatingWidget.getKeyboardListeners().onKeyDown(
172:                            new Widget(), 'a', 0);
173:                } else if (eventName.compareTo("onKeyUp") == 0) {
174:                    this .delegatingWidget.getKeyboardListeners().onKeyUp(
175:                            new Widget(), 'a', 0);
176:                } else if (eventName.compareTo("onKeyPress") == 0) {
177:                    this .delegatingWidget.getKeyboardListeners().onKeyPress(
178:                            new Widget(), 'a', 0);
179:                } else {
180:                    fail("The event " + eventName + " is not supported");
181:                }
182:
183:                // Verify that the event was handled
184:                assertTrue(this .eventHandled);
185:            }
186:
187:            /**
188:             * Handle an event from the ownerOfEvents by verifying the the
189:             * event the the source are correct.
190:             * 
191:             * @param sender the Widget that fired the event
192:             * @param eventName the name of the event
193:             */
194:            private void handleKeyEvent(Widget sender, String eventName) {
195:                assertEquals(this .delegatingWidget.getEventOwner(), sender);
196:                assertEquals(this .lastEventName, eventName);
197:                this .eventHandled = true;
198:            }
199:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.