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. Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package org.jemmy.input;
  26 
  27 import org.jemmy.action.Action;
  28 import org.jemmy.control.Wrap;
  29 import org.jemmy.interfaces.Keyboard.KeyboardButton;
  30 import org.jemmy.interfaces.Keyboard.KeyboardModifier;
  31 import org.jemmy.interfaces.Keyboard;
  32 import org.jemmy.env.Environment;
  33 import org.jemmy.env.Timeout;
  34 import org.jemmy.interfaces.Focusable;
  35 import org.jemmy.interfaces.Modifier;
  36 
  37 /**
  38  * KeyDriver
  39  *
  40  * @author Alexandre Iline(alexandre.iline@sun.com)
  41  */
  42 public class KeyboardImpl implements Keyboard {
  43 
  44     CharBindingMap<KeyboardButton, KeyboardModifier> map;
  45     Environment env;
  46     Wrap<?> target;
  47     RobotDriver robotDriver;
  48     private boolean detached;
  49     /**
  50      * Constructs a KeyRobotDriver object.
  51      * @param target
  52      */
  53     public KeyboardImpl(Wrap<?> target) {
  54         //TODO: super(target.getEnvironment().getTimeout(RobotDriver.ROBOT_DELAY_TIMEOUT_NAME));
  55         robotDriver = new RobotDriver(target.getEnvironment());
  56         this.env = target.getEnvironment();
  57         this.map = target.getEnvironment().getBindingMap();
  58         this.target = target;
  59     }
  60 
  61     static {
  62         //TODO: Environment.getEnvironment().setTimeout(new Timeout(RobotDriver.ROBOT_DELAY_TIMEOUT_NAME, 10));
  63         Environment.getEnvironment().setTimeout(new Timeout(PUSH.getName(), 100));
  64         Environment.getEnvironment().setBindingMap(new DefaultCharBindingMap());
  65     }
  66 
  67     private void runAction(Action action) {
  68         if(detached) {
  69             target.getEnvironment().getExecutor().executeDetached(target.getEnvironment(), false, action);
  70         } else {
  71             target.getEnvironment().getExecutor().execute(target.getEnvironment(), false, action);
  72         }
  73     }
  74 
  75     /**
  76      *
  77      * @return Environment
  78      */
  79     public Environment getEnvironment() {
  80         return env;
  81     }
  82 
  83     /**
  84      *
  85      * @param kbdButton
  86      * @param modifiers
  87      * @param pushTime
  88      */
  89     public void pushKey(final KeyboardButton kbdButton, final Modifier modifiers[], final Timeout pushTime) {
  90         runAction(new Action() {
  91             public void run(Object... parameters) {
  92                 if(target.is(Focusable.class)) target.as(Focusable.class).focuser().focus();
  93                 pressKey(kbdButton, modifiers);
  94                 pushTime.sleep();
  95                 releaseKey(kbdButton, modifiers);
  96             }
  97             @Override
  98             public String toString() {
  99                 return "push " + kbdButton + " key with " + modifiers + " modifiers";
 100             }
 101         });
 102     }
 103 
 104     /**
 105      *
 106      * @param keyChar
 107      * @param pushTime
 108      */
 109     @Override
 110     public void typeChar(char keyChar, Timeout pushTime) {
 111         pushKey(pushTime, map.getCharKey(keyChar), map.getCharModifiers(keyChar));
 112     }
 113 
 114     /**
 115      * Press the keyboard key specified by kbdButton preceding with
 116      * pressing of modifier buttons specified by modifiers
 117      * @param kbdButton one of InputEvent.VK_* constants
 118      * @param modifiers combination of InputEvent.*_DOWN_MASK constants
 119      * @see java.awt.event.InputEvent
 120      */
 121     @Override
 122     public void pressKey(final KeyboardButton kbdButton, final Modifier... modifiers) {
 123         runAction(new Action() {
 124             public void run(Object... parameters) {
 125                 robotDriver.pressKey(kbdButton, modifiers);
 126             }
 127             @Override
 128             public String toString() {
 129                 return "press " + kbdButton + " key with " + modifiers + " modifiers";
 130             }
 131         });
 132     }
 133 
 134     /**
 135      * Release the keyboard key specified by kbdButton and then release
 136      * all the modifier keys specified by modifiers
 137      * @param kbdButton one of InputEvent.VK_* constants
 138      * @param modifiers combination of InputEvent.*_DOWN_MASK constants
 139      * @see java.awt.event.InputEvent
 140      */
 141     @Override
 142     public void releaseKey(final KeyboardButton kbdButton, final Modifier... modifiers) {
 143         runAction(new Action() {
 144             public void run(Object... parameters) {
 145                 robotDriver.releaseKey(kbdButton, modifiers);
 146             }
 147             @Override
 148             public String toString() {
 149                 return "press " + kbdButton + " key with " + modifiers + " modifiers";
 150             }
 151         });
 152     }
 153 
 154     /**
 155      *
 156      * @param kbdButton
 157      */
 158     @Override
 159     public void pressKey(KeyboardButton kbdButton) {
 160         pressKey(kbdButton, new Modifier[]{});
 161     }
 162 
 163     /**
 164      *
 165      * @param kbdButton
 166      */
 167     @Override
 168     public void releaseKey(KeyboardButton kbdButton) {
 169         releaseKey(kbdButton, new Modifier[]{});
 170     }
 171 
 172     /**
 173      *
 174      * @param kbdButton
 175      * @param modifiers
 176      */
 177     @Override
 178     public void pushKey(KeyboardButton kbdButton, Modifier... modifiers) {
 179         pushKey(kbdButton, modifiers, getEnvironment().getTimeout(PUSH.getName()));
 180     }
 181 
 182     /**
 183      *
 184      * @param kbdButton
 185      */
 186     @Override
 187     public void pushKey(KeyboardButton kbdButton) {
 188         pushKey(kbdButton, new Modifier[]{});
 189     }
 190 
 191     /**
 192      *
 193      * @param keyChar
 194      */
 195     @Override
 196     public void typeChar(char keyChar) {
 197         typeChar(keyChar, getEnvironment().getTimeout(PUSH.getName()));
 198     }
 199 
 200     @Override
 201     public Keyboard detached() {
 202         detached = true;
 203         return this;
 204     }
 205 
 206     @Override
 207     public void pushKey(Timeout pushTime, KeyboardButton key, Modifier... modifiers) {
 208         pushKey(key, modifiers, pushTime);
 209     }
 210 }