1 /* 2 * Copyright (c) 2007, 2017, 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 */ 23 package org.jemmy.input; 24 25 import org.jemmy.action.Action; 26 import org.jemmy.control.Wrap; 27 import org.jemmy.interfaces.Keyboard.KeyboardButton; 28 import org.jemmy.interfaces.Keyboard.KeyboardModifier; 29 import org.jemmy.interfaces.Keyboard; 30 import org.jemmy.env.Environment; 31 import org.jemmy.env.Timeout; 32 import org.jemmy.interfaces.Focusable; 33 import org.jemmy.interfaces.Modifier; 34 35 /** 36 * KeyDriver 37 * 38 * @author Alexandre Iline(alexandre.iline@sun.com) 39 */ 40 public class KeyboardImpl implements Keyboard { 41 42 CharBindingMap<KeyboardButton, KeyboardModifier> map; 43 Environment env; 44 Wrap<?> target; 45 RobotDriver robotDriver; 46 private boolean detached; 47 /** 48 * Constructs a KeyRobotDriver object. 49 * @param target 50 */ 51 public KeyboardImpl(Wrap<?> target) { 52 //TODO: super(target.getEnvironment().getTimeout(RobotDriver.ROBOT_DELAY_TIMEOUT_NAME)); 53 robotDriver = new RobotDriver(target.getEnvironment()); 54 this.env = target.getEnvironment(); 55 this.map = target.getEnvironment().getBindingMap(); 56 this.target = target; 57 } 58 59 static { 60 //TODO: Environment.getEnvironment().setTimeout(new Timeout(RobotDriver.ROBOT_DELAY_TIMEOUT_NAME, 10)); 61 Environment.getEnvironment().setTimeout(new Timeout(PUSH.getName(), 100)); 62 Environment.getEnvironment().setBindingMap(new DefaultCharBindingMap()); 63 } 64 65 private void runAction(Action action) { 66 if(detached) { 67 target.getEnvironment().getExecutor().executeDetached(target.getEnvironment(), false, action); 68 } else { 69 target.getEnvironment().getExecutor().execute(target.getEnvironment(), false, action); 70 } 71 } 72 73 /** 74 * 75 * @return Environment 76 */ 77 public Environment getEnvironment() { 78 return env; 79 } 80 81 /** 82 * 83 * @param kbdButton 84 * @param modifiers 85 * @param pushTime 86 */ 87 public void pushKey(final KeyboardButton kbdButton, final Modifier modifiers[], final Timeout pushTime) { 88 runAction(new Action() { 89 public void run(Object... parameters) { 90 if(target.is(Focusable.class)) target.as(Focusable.class).focuser().focus(); 91 pressKey(kbdButton, modifiers); 92 pushTime.sleep(); 93 releaseKey(kbdButton, modifiers); 94 } 95 @Override 96 public String toString() { 97 return "push " + kbdButton + " key with " + modifiers + " modifiers"; 98 } 99 }); 100 } 101 102 /** 103 * 104 * @param keyChar 105 * @param pushTime 106 */ 107 @Override 108 public void typeChar(char keyChar, Timeout pushTime) { 109 pushKey(pushTime, map.getCharKey(keyChar), map.getCharModifiers(keyChar)); 110 } 111 112 /** 113 * Press the keyboard key specified by kbdButton preceding with 114 * pressing of modifier buttons specified by modifiers 115 * @param kbdButton one of InputEvent.VK_* constants 116 * @param modifiers combination of InputEvent.*_DOWN_MASK constants 117 * @see java.awt.event.InputEvent 118 */ 119 @Override 120 public void pressKey(final KeyboardButton kbdButton, final Modifier... modifiers) { 121 runAction(new Action() { 122 public void run(Object... parameters) { 123 robotDriver.pressKey(kbdButton, modifiers); 124 } 125 @Override 126 public String toString() { 127 return "press " + kbdButton + " key with " + modifiers + " modifiers"; 128 } 129 }); 130 } 131 132 /** 133 * Release the keyboard key specified by kbdButton and then release 134 * all the modifier keys specified by modifiers 135 * @param kbdButton one of InputEvent.VK_* constants 136 * @param modifiers combination of InputEvent.*_DOWN_MASK constants 137 * @see java.awt.event.InputEvent 138 */ 139 @Override 140 public void releaseKey(final KeyboardButton kbdButton, final Modifier... modifiers) { 141 runAction(new Action() { 142 public void run(Object... parameters) { 143 robotDriver.releaseKey(kbdButton, modifiers); 144 } 145 @Override 146 public String toString() { 147 return "press " + kbdButton + " key with " + modifiers + " modifiers"; 148 } 149 }); 150 } 151 152 /** 153 * 154 * @param kbdButton 155 */ 156 @Override 157 public void pressKey(KeyboardButton kbdButton) { 158 pressKey(kbdButton, new Modifier[]{}); 159 } 160 161 /** 162 * 163 * @param kbdButton 164 */ 165 @Override 166 public void releaseKey(KeyboardButton kbdButton) { 167 releaseKey(kbdButton, new Modifier[]{}); 168 } 169 170 /** 171 * 172 * @param kbdButton 173 * @param modifiers 174 */ 175 @Override 176 public void pushKey(KeyboardButton kbdButton, Modifier... modifiers) { 177 pushKey(kbdButton, modifiers, getEnvironment().getTimeout(PUSH.getName())); 178 } 179 180 /** 181 * 182 * @param kbdButton 183 */ 184 @Override 185 public void pushKey(KeyboardButton kbdButton) { 186 pushKey(kbdButton, new Modifier[]{}); 187 } 188 189 /** 190 * 191 * @param keyChar 192 */ 193 @Override 194 public void typeChar(char keyChar) { 195 typeChar(keyChar, getEnvironment().getTimeout(PUSH.getName())); 196 } 197 198 @Override 199 public Keyboard detached() { 200 detached = true; 201 return this; 202 } 203 204 @Override 205 public void pushKey(Timeout pushTime, KeyboardButton key, Modifier... modifiers) { 206 pushKey(key, modifiers, pushTime); 207 } 208 }