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 mrkam 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 todo document 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 public int convert(Modifier... modifiers) { 145 int result = 0; 146 for (Modifier modifier : modifiers) { 147 try { 148 result |= modifier2int.get(modifier); 149 } catch (Exception e) { 150 throw new JemmyException("Unable to recognize modifier", e, modifier); 151 } 152 } 153 return result; 154 } 155 156 public int convert(MouseButton button) { 157 try { 158 return button2int.get(button); 159 } catch (Exception e) { 160 throw new JemmyException("Unable to recognize mouse button", e, button); 161 } 162 } 163 164 public KeyboardButton convertKeyboardButton(int key) { 165 KeyboardButton res = int2key.get(key); 166 if (res == null) { 167 throw new JemmyException("Unable to recognize key", key); 168 } 169 return res; 170 } 171 172 public Modifier[] convertModifiers(int modifiers) { 173 List<Modifier> result = new ArrayList<Modifier>(); 174 for (int key : int2modifier.keySet()) { 175 if ((key & modifiers) != 0) { 176 Modifier res = int2modifier.get(key); 177 if (res == null) { 178 throw new JemmyException("Unable to recognize modifiers", modifiers); 179 } 180 result.add(res); 181 } 182 } 183 return result.toArray(new Modifier[result.size()]); 184 } 185 186 public MouseButton convertMouseButton(int button) { 187 MouseButton res = int2button.get(button); 188 if (res == null) { 189 throw new JemmyException("Unable to recognize mouse button", button); 190 } 191 return res; 192 } 193 }