001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s): Alexandre Iline.
025: *
026: * The Original Software is the Jemmy library.
027: * The Initial Developer of the Original Software is Alexandre Iline.
028: * All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: *
041: *
042: *
043: * $Id$ $Revision$ $Date$
044: *
045: */
046:
047: package org.netbeans.jemmy;
048:
049: import java.awt.event.InputEvent;
050: import java.awt.event.KeyEvent;
051:
052: import java.lang.reflect.Field;
053: import java.lang.reflect.InvocationTargetException;
054: import java.lang.reflect.Modifier;
055:
056: import java.util.Enumeration;
057: import java.util.Hashtable;
058:
059: /**
060: *
061: * Default implementation of CharBindingMap interface.
062: * Provides a mapping for the following symbols:<BR>
063: * @see org.netbeans.jemmy.CharBindingMap
064: *
065: * @author Alexandre Iline (alexandre.iline@sun.com)
066: */
067:
068: public class DefaultCharBindingMap implements CharBindingMap {
069:
070: private Hashtable chars;
071: private ClassReference cl;
072:
073: /**
074: * Constructor.
075: */
076: public DefaultCharBindingMap() {
077: try {
078: cl = new ClassReference("java.awt.event.KeyEvent");
079: } catch (ClassNotFoundException e) {
080: e.printStackTrace();
081: }
082: initMap();
083: }
084:
085: /**
086: * Returns the code of the primary key used to type a symbol.
087: * @param c Symbol code.
088: * @return a key code.
089: * @see CharBindingMap#getCharKey(char)
090: * @see java.awt.event.InputEvent
091: */
092: public int getCharKey(char c) {
093: return (getKeyAndModifiers(c)[0]);
094: }
095:
096: /**
097: * Returns the modifiers that should be pressed to type a symbol.
098: * @param c Symbol code.
099: * @return a combination of InputEvent MASK fields.
100: * @see CharBindingMap#getCharModifiers(char)
101: * @see java.awt.event.InputEvent
102: */
103: public int getCharModifiers(char c) {
104: return (getKeyAndModifiers(c)[1]);
105: }
106:
107: /**
108: * Returns key + modifiers pair.
109: * @param c Symbol code.
110: * @return an array of two elements: key code and modifiers mask -
111: * a combination of InputEvent MASK fields.
112: */
113: public int[] getKeyAndModifiers(char c) {
114: CharKey key = (CharKey) chars.get(new Character(c));
115: if (key != null) {
116: return (new int[] { key.key, key.modifiers });
117: } else {
118: return (new int[] { KeyEvent.VK_UNDEFINED, 0 });
119: }
120: }
121:
122: /**
123: * Returns an array of all supported chars.
124: * @return an array of chars representing the supported chars values.
125: */
126: public char[] getSupportedChars() {
127: char[] charArray = new char[chars.size()];
128: Enumeration keys = chars.keys();
129: int index = 0;
130: while (keys.hasMoreElements()) {
131: charArray[index] = ((Character) keys.nextElement())
132: .charValue();
133: }
134: return (charArray);
135: }
136:
137: /**
138: * Removes a char from supported.
139: * @param c Symbol code.
140: */
141: public void removeChar(char c) {
142: chars.remove(new Character(c));
143: }
144:
145: /**
146: * Adds a char to supported.
147: * @param c Symbol code.
148: * @param key key code.
149: * @param modifiers a combination of InputEvent MASK fields.
150: */
151: public void addChar(char c, int key, int modifiers) {
152: chars.put(new Character(c), new CharKey(key, modifiers));
153: }
154:
155: private void initMap() {
156: chars = new Hashtable();
157: //first add latters and digits represented by KeyEvent.VK_. fields
158: Field[] fields = KeyEvent.class.getFields();
159: for (int i = 0; i < fields.length; i++) {
160: String name = fields[i].getName();
161: if ((fields[i].getModifiers() & Modifier.PUBLIC) != 0
162: && (fields[i].getModifiers() & Modifier.STATIC) != 0
163: && fields[i].getType() == Integer.TYPE
164: && name.startsWith("VK_") && name.length() == 4) {
165: String latter = name.substring(3, 4);
166: try {
167: int key = fields[i].getInt(null);
168: addChar(latter.toLowerCase().charAt(0), key, 0);
169: if (!latter.toUpperCase().equals(
170: latter.toLowerCase())) {
171: addChar(latter.toUpperCase().charAt(0), key,
172: InputEvent.SHIFT_MASK);
173: }
174: } catch (IllegalAccessException e) {
175: //never could happen!
176: }
177: }
178: }
179: //add special simbols
180: addChar('\t', KeyEvent.VK_TAB, 0);
181: addChar(' ', KeyEvent.VK_SPACE, 0);
182: addChar('!', KeyEvent.VK_1, InputEvent.SHIFT_MASK);
183: addChar('"', KeyEvent.VK_QUOTE, InputEvent.SHIFT_MASK);
184: addChar('#', KeyEvent.VK_3, InputEvent.SHIFT_MASK);
185: addChar('$', KeyEvent.VK_4, InputEvent.SHIFT_MASK);
186: addChar('%', KeyEvent.VK_5, InputEvent.SHIFT_MASK);
187: addChar('&', KeyEvent.VK_7, InputEvent.SHIFT_MASK);
188: addChar('\'', KeyEvent.VK_QUOTE, 0);
189: addChar('(', KeyEvent.VK_9, InputEvent.SHIFT_MASK);
190: addChar(')', KeyEvent.VK_0, InputEvent.SHIFT_MASK);
191: addChar('*', KeyEvent.VK_8, InputEvent.SHIFT_MASK);
192: addChar('+', KeyEvent.VK_EQUALS, InputEvent.SHIFT_MASK);
193: addChar(',', KeyEvent.VK_COMMA, 0);
194: addChar('-', KeyEvent.VK_MINUS, 0);
195: addChar('.', KeyEvent.VK_PERIOD, 0);
196: addChar('/', KeyEvent.VK_SLASH, 0);
197: addChar(':', KeyEvent.VK_SEMICOLON, InputEvent.SHIFT_MASK);
198: addChar(';', KeyEvent.VK_SEMICOLON, 0);
199: addChar('<', KeyEvent.VK_COMMA, InputEvent.SHIFT_MASK);
200: addChar('=', KeyEvent.VK_EQUALS, 0);
201: addChar('>', KeyEvent.VK_PERIOD, InputEvent.SHIFT_MASK);
202: addChar('?', KeyEvent.VK_SLASH, InputEvent.SHIFT_MASK);
203: addChar('@', KeyEvent.VK_2, InputEvent.SHIFT_MASK);
204: addChar('[', KeyEvent.VK_OPEN_BRACKET, 0);
205: addChar('\\', KeyEvent.VK_BACK_SLASH, 0);
206: addChar(']', KeyEvent.VK_CLOSE_BRACKET, 0);
207: addChar('^', KeyEvent.VK_6, InputEvent.SHIFT_MASK);
208: addChar('_', KeyEvent.VK_MINUS, InputEvent.SHIFT_MASK);
209: addChar('`', KeyEvent.VK_BACK_QUOTE, 0);
210: addChar('{', KeyEvent.VK_OPEN_BRACKET, InputEvent.SHIFT_MASK);
211: addChar('|', KeyEvent.VK_BACK_SLASH, InputEvent.SHIFT_MASK);
212: addChar('}', KeyEvent.VK_CLOSE_BRACKET, InputEvent.SHIFT_MASK);
213: addChar('~', KeyEvent.VK_BACK_QUOTE, InputEvent.SHIFT_MASK);
214: addChar('\n', KeyEvent.VK_ENTER, 0);
215: }
216:
217: private static class CharKey {
218: public int key;
219: public int modifiers;
220:
221: public CharKey(int key, int modifiers) {
222: this.key = key;
223: this.modifiers = modifiers;
224: }
225: }
226:
227: }
|