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     public KeyboardImpl(Wrap<?> target) {
  51         //TODO: super(target.getEnvironment().getTimeout(RobotDriver.ROBOT_DELAY_TIMEOUT_NAME));
  52         robotDriver = new RobotDriver(target.getEnvironment());
  53         this.env = target.getEnvironment();
  54         this.map = target.getEnvironment().getBindingMap();
  55         this.target = target;
  56     }
  57 
  58     static {
  59         //TODO: Environment.getEnvironment().setTimeout(new Timeout(RobotDriver.ROBOT_DELAY_TIMEOUT_NAME, 10));
  60         Environment.getEnvironment().setTimeout(new Timeout(PUSH.getName(), 100));
  61         Environment.getEnvironment().setBindingMap(new DefaultCharBindingMap());
  62     }
  63 
  64     private void runAction(Action action) {
  65         if(detached) {
  66             target.getEnvironment().getExecutor().executeDetached(target.getEnvironment(), false, action);
  67         } else {
  68             target.getEnvironment().getExecutor().execute(target.getEnvironment(), false, action);
  69         }
  70     }
  71 
  72     public Environment getEnvironment() {
  73         return env;
  74     }
  75 
  76     public void pushKey(final KeyboardButton kbdButton, final Modifier modifiers[], final Timeout pushTime) {
  77         runAction(new Action() {
  78             public void run(Object... parameters) {
  79                 if(target.is(Focusable.class)) target.as(Focusable.class).focuser().focus();
  80                 pressKey(kbdButton, modifiers);
  81                 pushTime.sleep();
  82                 releaseKey(kbdButton, modifiers);
  83             }
  84             @Override
  85             public String toString() {
  86                 return "push " + kbdButton + " key with " + modifiers + " modifiers";
  87             }
  88         });
  89     }
  90 
  91     @Override
  92     public void typeChar(char keyChar, Timeout pushTime) {
  93         pushKey(pushTime, map.getCharKey(keyChar), map.getCharModifiers(keyChar));
  94     }
  95 
  96     /**
  97      * Press the keyboard key specified by kbdButton preceding with
  98      * pressing of modifier buttons specified by modifiers
  99      * @param kbdButton one of InputEvent.VK_* constants
 100      * @param modifiers combination of InputEvent.*_DOWN_MASK constants
 101      * @see java.awt.event.InputEvent
 102      */
 103     @Override
 104     public void pressKey(final KeyboardButton kbdButton, final Modifier... modifiers) {
 105         runAction(new Action() {
 106             public void run(Object... parameters) {
 107                 robotDriver.pressKey(kbdButton, modifiers);
 108             }
 109             @Override
 110             public String toString() {
 111                 return "press " + kbdButton + " key with " + modifiers + " modifiers";
 112             }
 113         });
 114     }
 115 
 116     /**
 117      * Release the keyboard key specified by kbdButton and then release
 118      * all the modifier keys specified by modifiers
 119      * @param kbdButton one of InputEvent.VK_* constants
 120      * @param modifiers combination of InputEvent.*_DOWN_MASK constants
 121      * @see java.awt.event.InputEvent
 122      */
 123     @Override
 124     public void releaseKey(final KeyboardButton kbdButton, final Modifier... modifiers) {
 125         runAction(new Action() {
 126             public void run(Object... parameters) {
 127                 robotDriver.releaseKey(kbdButton, modifiers);
 128             }
 129             @Override
 130             public String toString() {
 131                 return "press " + kbdButton + " key with " + modifiers + " modifiers";
 132             }
 133         });
 134     }
 135 
 136     @Override
 137     public void pressKey(KeyboardButton kbdButton) {
 138         pressKey(kbdButton, new Modifier[]{});
 139     }
 140 
 141 
 142     @Override
 143     public void releaseKey(KeyboardButton kbdButton) {
 144         releaseKey(kbdButton, new Modifier[]{});
 145     }
 146 
 147 
 148     @Override
 149     public void pushKey(KeyboardButton kbdButton, Modifier... modifiers) {
 150         pushKey(kbdButton, modifiers, getEnvironment().getTimeout(PUSH.getName()));
 151     }
 152 
 153     @Override
 154     public void pushKey(KeyboardButton kbdButton) {
 155         pushKey(kbdButton, new Modifier[]{});
 156     }
 157 
 158     @Override
 159     public void typeChar(char keyChar) {
 160         typeChar(keyChar, getEnvironment().getTimeout(PUSH.getName()));
 161     }
 162 
 163     @Override
 164     public Keyboard detached() {
 165         detached = true;
 166         return this;
 167     }
 168 
 169     @Override
 170     public void pushKey(Timeout pushTime, KeyboardButton key, Modifier... modifiers) {
 171         pushKey(key, modifiers, pushTime);
 172     }
 173 }