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 
  26 import java.awt.event.InputEvent;
  27 import java.awt.event.KeyEvent;
  28 import java.util.ArrayList;
  29 import java.util.HashMap;
  30 import java.util.List;
  31 import java.util.Map;
  32 import org.jemmy.JemmyException;
  33 import org.jemmy.interfaces.Keyboard.KeyboardButton;
  34 import org.jemmy.interfaces.Keyboard.KeyboardButtons;
  35 import org.jemmy.interfaces.Keyboard.KeyboardModifiers;
  36 import org.jemmy.interfaces.Modifier;
  37 import org.jemmy.interfaces.Mouse.MouseButton;
  38 import org.jemmy.interfaces.Mouse.MouseButtons;
  39 import org.jemmy.interfaces.Mouse.MouseModifiers;
  40 
  41 
  42 /**
  43  * Converts
  44  * @author Alexander Kouznetsov <mrkam@mail.ru>
  45  */
  46 public class AWTMap {
  47 
  48     private static Map<Integer, KeyboardButton> int2key = new HashMap<Integer, KeyboardButton>();
  49     private static Map<Integer, Modifier> int2modifier = new HashMap<Integer, Modifier>();
  50     private static Map<Integer, MouseButton> int2button = new HashMap<Integer, MouseButton>();
  51     private static Map<KeyboardButton, Integer> key2int = new HashMap<KeyboardButton, Integer>();
  52     private static Map<Modifier, Integer> modifier2int = new HashMap<Modifier, Integer>();
  53     private static Map<MouseButton, Integer> button2int = new HashMap<MouseButton, Integer>();
  54 
  55     static {
  56         for (KeyboardButtons button : KeyboardButtons.values()) {
  57             String name = button.name();
  58             try {
  59                 int key = KeyEvent.VK_UNDEFINED;
  60                 if (name.length() == 2 && name.startsWith("D")) {
  61                     // digit
  62                     key = KeyEvent.class.getDeclaredField("VK_" + name.substring(1)).getInt(null);
  63                 } else {
  64                     key = KeyEvent.class.getDeclaredField("VK_" + name).getInt(null);
  65                 }
  66                 int2key.put(key, button);
  67                 key2int.put(button, key);
  68             } catch (NoSuchFieldException ex) {
  69                 throw new JemmyException("Unable to recognize key", ex, button);
  70             } catch (SecurityException ex) {
  71                 throw new JemmyException("Unable to recognize key", ex, button);
  72             } catch (IllegalArgumentException ex) {
  73                 throw new JemmyException("Unable to recognize key", ex, button);
  74             } catch (IllegalAccessException ex) {
  75                 throw new JemmyException("Unable to recognize key", ex, button);
  76             }
  77         }
  78         for (KeyboardModifiers modifier : KeyboardModifiers.values()) {
  79             String name = modifier.name();
  80             try {
  81                 int key = InputEvent.class.getDeclaredField(name).getInt(null);
  82                 int2modifier.put(key, modifier);
  83                 modifier2int.put(modifier, key);
  84             } catch (NoSuchFieldException ex) {
  85                 throw new JemmyException("Unable to recognize modifier", ex, modifier);
  86             } catch (SecurityException ex) {
  87                 throw new JemmyException("Unable to recognize modifier", ex, modifier);
  88             } catch (IllegalArgumentException ex) {
  89                 throw new JemmyException("Unable to recognize modifier", ex, modifier);
  90             } catch (IllegalAccessException ex) {
  91                 throw new JemmyException("Unable to recognize modifier", ex, modifier);
  92             }
  93         }
  94         for (MouseModifiers modifier : MouseModifiers.values()) {
  95             String name = modifier.name();
  96             try {
  97                 int key = InputEvent.class.getDeclaredField(name).getInt(null);
  98                 int2modifier.put(key, modifier);
  99                 modifier2int.put(modifier, key);
 100             } catch (NoSuchFieldException ex) {
 101                 throw new JemmyException("Unable to recognize modifier", ex, modifier);
 102             } catch (SecurityException ex) {
 103                 throw new JemmyException("Unable to recognize modifier", ex, modifier);
 104             } catch (IllegalArgumentException ex) {
 105                 throw new JemmyException("Unable to recognize modifier", ex, modifier);
 106             } catch (IllegalAccessException ex) {
 107                 throw new JemmyException("Unable to recognize modifier", ex, modifier);
 108             }
 109         }
 110         for (MouseButtons button : MouseButtons.values()) {
 111             String name = button.name();
 112             try {
 113                 int key = InputEvent.class.getDeclaredField(name + "_MASK").getInt(null);
 114                 int2button.put(key, button);
 115                 button2int.put(button, key);
 116             } catch (NoSuchFieldException ex) {
 117                 throw new JemmyException("Unable to recognize button", ex, button);
 118             } catch (SecurityException ex) {
 119                 throw new JemmyException("Unable to recognize button", ex, button);
 120             } catch (IllegalArgumentException ex) {
 121                 throw new JemmyException("Unable to recognize button", ex, button);
 122             } catch (IllegalAccessException ex) {
 123                 throw new JemmyException("Unable to recognize button", ex, button);
 124             }
 125         }
 126     }
 127 
 128     /**
 129      * TODO Provide javadoc
 130      * @param button
 131      * @return One of InputEvent.VK_* constants
 132      * @see InputEvent
 133      */
 134     public int convert(KeyboardButton button) {
 135         try {
 136             return key2int.get(button);
 137         } catch(Exception e) {
 138             throw new JemmyException("Unable to recognize key", e, button);
 139         }
 140     }
 141 
 142     /**
 143      * TODO Provide javadoc
 144      * @param modifiers
 145      * @return
 146      */
 147     public int convert(Modifier... modifiers) {
 148         int result = 0;
 149         for (Modifier modifier : modifiers) {
 150             try {
 151                 result |= modifier2int.get(modifier);
 152             } catch (Exception e) {
 153                 throw new JemmyException("Unable to recognize modifier", e, modifier);
 154             }
 155         }
 156         return result;
 157     }
 158 
 159     /**
 160      * TODO Provide javadoc
 161      * @param button
 162      * @return
 163      */
 164     public int convert(MouseButton button) {
 165         try {
 166             return button2int.get(button);
 167         } catch (Exception e) {
 168             throw new JemmyException("Unable to recognize mouse button", e, button);
 169         }
 170     }
 171 
 172     /**
 173      * TODO Provide javadoc
 174      * @param key
 175      * @return
 176      */
 177     public KeyboardButton convertKeyboardButton(int key) {
 178         KeyboardButton res = int2key.get(key);
 179         if (res == null) {
 180             throw new JemmyException("Unable to recognize key", key);
 181         }
 182         return res;
 183     }
 184 
 185     /**
 186      * TODO Provide javadoc
 187      * @param modifiers
 188      * @return
 189      */
 190     public Modifier[] convertModifiers(int modifiers) {
 191         List<Modifier> result = new ArrayList<Modifier>();
 192         for (int key : int2modifier.keySet()) {
 193             if ((key & modifiers) != 0) {
 194                 Modifier res = int2modifier.get(key);
 195                 if (res == null) {
 196                     throw new JemmyException("Unable to recognize modifiers", modifiers);
 197                 }
 198                 result.add(res);
 199             }
 200         }
 201         return result.toArray(new Modifier[result.size()]);
 202     }
 203 
 204     /**
 205      * TODO Provide javadoc
 206      * @param button
 207      * @return
 208      */
 209     public MouseButton convertMouseButton(int button) {
 210         MouseButton res = int2button.get(button);
 211         if (res == null) {
 212             throw new JemmyException("Unable to recognize mouse button", button);
 213         }
 214         return res;
 215     }
 216 }