Source Code Cross Referenced for KeyStrokeCatcher.java in  » Testing » jacareto » jacareto » catching » 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 » Testing » jacareto » jacareto.catching 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Jacareto Copyright (c) 2002-2005
003:         * Applied Computer Science Research Group, Darmstadt University of
004:         * Technology, Institute of Mathematics & Computer Science,
005:         * Ludwigsburg University of Education, and Computer Based
006:         * Learning Research Group, Aachen University. All rights reserved.
007:         *
008:         * Jacareto is free software; you can redistribute it and/or
009:         * modify it under the terms of the GNU General Public
010:         * License as published by the Free Software Foundation; either
011:         * version 2 of the License, or (at your option) any later version.
012:         *
013:         * Jacareto is distributed in the hope that it will be useful,
014:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
015:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
016:         * General Public License for more details.
017:         *
018:         * You should have received a copy of the GNU General Public
019:         * License along with Jacareto; if not, write to the Free
020:         * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021:         *
022:         */
023:
024:        package jacareto.catching;
025:
026:        import jacareto.eventmask.KeyEventMask;
027:        import jacareto.system.Environment;
028:        import jacareto.toolkit.EnhancedHashtable;
029:        import jacareto.toolkit.PriorityList;
030:
031:        import java.awt.event.KeyEvent;
032:
033:        import java.util.Enumeration;
034:        import java.util.EventObject;
035:        import java.util.Hashtable;
036:        import java.util.Iterator;
037:
038:        import javax.swing.KeyStroke;
039:
040:        /**
041:         * <p>
042:         * This class catches keyboard strokes.
043:         * </p>
044:         * 
045:         * <p>
046:         * A key stroke must be registered before instances of this class will pay attention to it. Every
047:         * registered stroke has a name. Whenever a key event which matches one of the registered key
048:         * strokes has been dispatched, the key stroke observers which have been registered before will be
049:         * notified of that event.
050:         * </p>
051:         * 
052:         * <p>
053:         * This instance defines its event mask automatically; you do not have to do this.
054:         * </p>
055:         *
056:         * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
057:         * @version 1.01
058:         */
059:        public class KeyStrokeCatcher extends Catcher {
060:            /** The hashtable which maps the key strings to the key stroke names. */
061:            private Hashtable keyStringToName;
062:
063:            /** The hashtable which maps the key stroke names to the key strings. */
064:            private Hashtable nameToKeyString;
065:
066:            /** List of all registered instances which are waiting for a stroke. */
067:            private PriorityList observers;
068:
069:            /** The last handled key event. */
070:            private KeyEvent lastEvent;
071:
072:            /**
073:             * Creates a new key Stroke catcher
074:             *
075:             * @param env the environment
076:             */
077:            public KeyStrokeCatcher(Environment env) {
078:                super (env);
079:
080:                keyStringToName = new Hashtable();
081:                nameToKeyString = new Hashtable();
082:                observers = new PriorityList();
083:                lastEvent = null;
084:
085:                setAWTEventMask(new KeyEventMask(env, KeyEventMask.ALL_INCLUDED));
086:            }
087:
088:            /**
089:             * Notifies all instances registered as key stroke observers.
090:             *
091:             * @param event the event
092:             *
093:             * @return whether any other listener should get the event or not.  Other listeners will get
094:             *         the event if it was not a key stroke.
095:             */
096:            public boolean handleEvent(EventObject event) {
097:                KeyEvent keyEvent = (KeyEvent) event;
098:                KeyStroke keyStroke = KeyStroke.getKeyStrokeForEvent(keyEvent);
099:
100:                if (contains(keyStroke)) {
101:                    if ((keyEvent.getID() == KeyEvent.KEY_PRESSED)
102:                            && !(keyEvent == lastEvent)) {
103:                        String name = getKeyStrokeName(keyStroke);
104:                        Iterator i = observers.iterator();
105:
106:                        while (i.hasNext()) {
107:                            ((KeyStrokeObserver) i.next())
108:                                    .handleKeyStroke(name);
109:                        }
110:
111:                        getLogger()
112:                                .debug(
113:                                        getLanguage()
114:                                                .getString(
115:                                                        "Catching.KeyStrokeCatcher.Msg.GotStroke")
116:                                                + "!");
117:                        lastEvent = keyEvent;
118:                    }
119:
120:                    keyEvent.consume();
121:
122:                    return false;
123:                }
124:
125:                return true;
126:            }
127:
128:            /**
129:             * Registers an instance which is waiting for strokes with the specified priority. A greater
130:             * value of priority symbolizes a higher priority.
131:             *
132:             * @param observer the observer to add
133:             * @param priority the priority
134:             */
135:            public void addKeyStrokeObserver(KeyStrokeObserver observer,
136:                    int priority) {
137:                if (priority >= 0) {
138:                    observers.add(observer, priority);
139:                } else {
140:                    observers.add(observer);
141:                }
142:            }
143:
144:            /**
145:             * Registers an instance which is waiting for strokes with the lowest priority.
146:             *
147:             * @param observer the observer to add
148:             */
149:            public void addKeyStrokeObserver(KeyStrokeObserver observer) {
150:                addKeyStrokeObserver(observer, -1);
151:            }
152:
153:            /**
154:             * Registers a key stroke (specified as a string) with a specified name. The string will be
155:             * parsed via the <code>getKeyCode(String)</code> method of
156:             * <code>javax.swing.KeyStroke</code>.
157:             *
158:             * @param keyString the key string
159:             * @param keyStrokeName the name of the key stroke
160:             */
161:            public void addKeyStroke(String keyString, String keyStrokeName) {
162:                addKeyStroke(KeyStroke.getKeyStroke(keyString), keyStrokeName);
163:            }
164:
165:            /**
166:             * Registers a key stroke with a specified name.
167:             *
168:             * @param keyStroke the key stroke
169:             * @param keyStrokeName the name of the key stroke
170:             */
171:            public void addKeyStroke(KeyStroke keyStroke, String keyStrokeName) {
172:                keyStringToName.put(keyStrokeToHashtableKey(keyStroke),
173:                        keyStrokeName);
174:
175:                String keyString = keyStroke.toString();
176:                keyString = keyString.substring(8, keyString.length() - 2);
177:                nameToKeyString.put(keyStrokeName, keyString);
178:            }
179:
180:            /**
181:             * Returns the name of a key stroke as a string. The string will be parsed via the
182:             * <code>getKeyCode(String)</code> method of <code>javax.swing.KeyStroke</code>.
183:             *
184:             * @param keyString the key string of the stroke
185:             *
186:             * @return DOCUMENT ME!
187:             */
188:            public String getKeyStrokeName(String keyString) {
189:                return getKeyStrokeName(KeyStroke.getKeyStroke(keyString));
190:            }
191:
192:            /**
193:             * Returns the name of a key stroke specified.
194:             *
195:             * @param keyStroke the key stroke
196:             *
197:             * @return DOCUMENT ME!
198:             */
199:            public String getKeyStrokeName(KeyStroke keyStroke) {
200:                return (String) keyStringToName
201:                        .get(keyStrokeToHashtableKey(keyStroke));
202:            }
203:
204:            /**
205:             * Returns a string representing the key stroke belonging to the specified  key stroke name.
206:             *
207:             * @param keyStrokeName DOCUMENT ME!
208:             *
209:             * @return DOCUMENT ME!
210:             */
211:            public String getKeyString(String keyStrokeName) {
212:                return (String) nameToKeyString.get(keyStrokeName);
213:            }
214:
215:            /**
216:             * Returns whether the given key stroke specified as a string is a registered stroke. The
217:             * string will be parsed via the <code>getKeyCode(String)</code> method of
218:             * <code>javax.swing.KeyStroke</code>.
219:             *
220:             * @param keyString the key string of the stroke
221:             *
222:             * @return DOCUMENT ME!
223:             */
224:            public boolean contains(String keyString) {
225:                return contains(KeyStroke.getKeyStroke(keyString));
226:            }
227:
228:            /**
229:             * Returns whether the given key stroke specified is a registered stroke.
230:             *
231:             * @param keyStroke the key stroke
232:             *
233:             * @return DOCUMENT ME!
234:             */
235:            public boolean contains(KeyStroke keyStroke) {
236:                return keyStringToName
237:                        .containsKey(keyStrokeToHashtableKey(keyStroke));
238:            }
239:
240:            /**
241:             * Adds the key strokes specified in the customization instance of the env object. The
242:             * specified setting must be a map.
243:             *
244:             * @param setting DOCUMENT ME!
245:             */
246:            public void applyCustomization(String setting) {
247:                EnhancedHashtable strokeMap = getCustomization().getMap(
248:                        setting, new EnhancedHashtable());
249:                Enumeration enumeration = strokeMap.keys();
250:
251:                while (enumeration.hasMoreElements()) {
252:                    String keyString = (String) enumeration.nextElement();
253:                    addKeyStroke(keyString, (String) strokeMap.get(keyString));
254:                }
255:            }
256:
257:            /**
258:             * Converts the keystroke to the hashtable key.
259:             *
260:             * @param keyStroke the key stroke
261:             *
262:             * @return the key for the hashtable <code>keyStringToName</code>
263:             */
264:            private String keyStrokeToHashtableKey(KeyStroke keyStroke) {
265:                return keyStroke.getKeyCode() + ";" + keyStroke.getModifiers();
266:            }
267:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.