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