1/* 2 * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23package org.netbeans.jemmy.drivers.input; 24 25import java.awt.Component; 26import java.awt.event.KeyEvent; 27 28import org.netbeans.jemmy.Timeout; 29import org.netbeans.jemmy.drivers.KeyDriver; 30import org.netbeans.jemmy.operators.ComponentOperator; 31 32/** 33 * KeyDriver using event dispatching. 34 * 35 * @author Alexandre Iline(alexandre.iline@oracle.com) 36 */ 37public class KeyEventDriver extends EventDriver implements KeyDriver { 38 39 /** 40 * Constructs a KeyEventDriver object. 41 * 42 * @param supported an array of supported class names 43 */ 44 public KeyEventDriver(String[] supported) { 45 super(supported); 46 } 47 48 /** 49 * Constructs an KeyEventDriver object suporting ComponentOperator. 50 */ 51 public KeyEventDriver() { 52 super(); 53 } 54 55 @Override 56 public void pressKey(ComponentOperator oper, int keyCode, int modifiers) { 57 pressKey(findNativeParent(oper.getSource()), keyCode, modifiers); 58 } 59 60 @Override 61 public void typedKey(ComponentOperator oper, int keyCode, char keyChar, int modifiers) { 62 typedKey(findNativeParent(oper.getSource()), keyChar, modifiers); 63 } 64 65 @Override 66 public void releaseKey(ComponentOperator oper, int keyCode, int modifiers) { 67 releaseKey(findNativeParent(oper.getSource()), keyCode, modifiers); 68 } 69 70 @Override 71 public void pushKey(ComponentOperator oper, int keyCode, int modifiers, Timeout pushTime) { 72 Component nativeContainer = findNativeParent(oper.getSource()); 73 pressKey(nativeContainer, keyCode, modifiers); 74 pushTime.sleep(); 75 releaseKey(nativeContainer, keyCode, modifiers); 76 } 77 78 @Override 79 public void typeKey(ComponentOperator oper, int keyCode, char keyChar, int modifiers, Timeout pushTime) { 80 Component nativeContainer = findNativeParent(oper.getSource()); 81 pressKey(nativeContainer, keyCode, modifiers); 82 pushTime.sleep(); 83 typedKey(nativeContainer, keyChar, modifiers); 84 releaseKey(nativeContainer, keyCode, modifiers); 85 } 86 87 @Deprecated 88 private void pressKey(Component nativeContainer, int keyCode, int modifiers) { 89 dispatchEvent(nativeContainer, 90 new KeyEvent(nativeContainer, 91 KeyEvent.KEY_PRESSED, 92 System.currentTimeMillis(), 93 modifiers, keyCode)); 94 } 95 96 private void typedKey(Component nativeContainer, char keyChar, int modifiers) { 97 dispatchEvent(nativeContainer, 98 new KeyEvent(nativeContainer, 99 KeyEvent.KEY_TYPED, 100 System.currentTimeMillis(), 101 modifiers, KeyEvent.VK_UNDEFINED, keyChar)); 102 } 103 104 @Deprecated 105 private void releaseKey(Component nativeContainer, int keyCode, int modifiers) { 106 dispatchEvent(nativeContainer, 107 new KeyEvent(nativeContainer, 108 KeyEvent.KEY_RELEASED, 109 System.currentTimeMillis(), 110 modifiers, keyCode)); 111 } 112 113 private Component findNativeParent(Component source) { 114 Component nativeOne = source; 115 while (nativeOne != null) { 116 if (!nativeOne.isLightweight()) { 117 return nativeOne; 118 } 119 nativeOne = nativeOne.getParent(); 120 } 121 return source; 122 } 123} 124