|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| AbstractKeyMapping.java | 100% | 100% | 100% | 100% |
|
||||||||||||||
| 1 |
package junit.extensions.jfcunit.keyboard;
|
|
| 2 |
|
|
| 3 |
import java.util.HashMap;
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
/**
|
|
| 7 |
* Abstract key mapping is used to translate key codes
|
|
| 8 |
* or characters into key strokes. Any characters or
|
|
| 9 |
* key codes which do not map are dropped.<br>
|
|
| 10 |
* <br>
|
|
| 11 |
* To create a new mapping:<br>
|
|
| 12 |
* 1) extend this class<br>
|
|
| 13 |
* 2) Define the Object[][]: See {@link DefaultKeyMapping} for example.<br>
|
|
| 14 |
* 3) Create a default constructor that passes the code array
|
|
| 15 |
* to this class.<br>
|
|
| 16 |
* 4) Install the class into the {@link junit.extensions.jfcunit.eventdata.AbstractKeyEventData} by calling
|
|
| 17 |
* <code>AbstractKeyEventData.setKeyMapping(yourMapping)</code><br>
|
|
| 18 |
* <br>
|
|
| 19 |
* Note: Not all key codes are valid on all systems with the {@link java.awt.Robot}.
|
|
| 20 |
* Invalid keycodes or modifiers for the OS will throw exceptions.
|
|
| 21 |
* Extensions of this class can be used to allow users to map their
|
|
| 22 |
* keyboard mappings.
|
|
| 23 |
*
|
|
| 24 |
* @author <a href="mailto:kwilson227@sourceforge.net">Kevin Wilson</a>
|
|
| 25 |
*/
|
|
| 26 |
public abstract class AbstractKeyMapping implements KeyMapping { |
|
| 27 |
/**
|
|
| 28 |
* The hash map is used to quickly access
|
|
| 29 |
* the key strokes for characters.
|
|
| 30 |
*/
|
|
| 31 |
private final HashMap m_mapChar = new HashMap(); |
|
| 32 |
|
|
| 33 |
/**
|
|
| 34 |
* The hash map is used to quickly access
|
|
| 35 |
* the key strokes for codes.
|
|
| 36 |
*/
|
|
| 37 |
private final HashMap m_mapCode = new HashMap(); |
|
| 38 |
|
|
| 39 |
/**
|
|
| 40 |
* Constructor that loads the two distinct mappings of the charMap
|
|
| 41 |
* into a HashMap for quick access.
|
|
| 42 |
*
|
|
| 43 |
* @param charMap mapping to be loaded.
|
|
| 44 |
*/
|
|
| 45 | 7 |
public AbstractKeyMapping(final Object[][] charMap) {
|
| 46 | 7 |
for (int i = 0; i < charMap.length; i++) { |
| 47 | 1764 |
Object c = charMap[i][0]; |
| 48 | 1764 |
JFCKeyStroke[] codes = new JFCKeyStroke[charMap[i].length - 1];
|
| 49 |
|
|
| 50 | 1764 |
for (int j = 1; j < charMap[i].length; j++) { |
| 51 | 1764 |
codes[j - 1] = (JFCKeyStroke) charMap[i][j]; |
| 52 |
} |
|
| 53 |
|
|
| 54 | 1764 |
if (c instanceof Character) { |
| 55 | 868 |
m_mapChar.put(c, codes); |
| 56 |
} |
|
| 57 |
|
|
| 58 | 1764 |
if (c instanceof Integer) { |
| 59 | 896 |
m_mapCode.put(c, codes); |
| 60 |
} |
|
| 61 |
} |
|
| 62 |
} |
|
| 63 |
|
|
| 64 |
/**
|
|
| 65 |
* Get the key codes required to construct
|
|
| 66 |
* the character.
|
|
| 67 |
*
|
|
| 68 |
* @param c Character to be constructed.
|
|
| 69 |
* @return JFCKeyStroke[] containing the key strokes
|
|
| 70 |
* used to create the character.
|
|
| 71 |
*/
|
|
| 72 | 8514 |
public JFCKeyStroke[] getKeyStrokes(final char c) { |
| 73 | 8514 |
return getKeyStrokes(
|
| 74 |
new Character(c),
|
|
| 75 |
m_mapChar); |
|
| 76 |
} |
|
| 77 |
|
|
| 78 |
/**
|
|
| 79 |
* Get the key strokes required to construct
|
|
| 80 |
* the keyCode.
|
|
| 81 |
*
|
|
| 82 |
* @param keyCode Key code to be constructed.
|
|
| 83 |
* @return {@link JFCKeyStroke}[] containing the key strokes
|
|
| 84 |
* used to enter the key code.
|
|
| 85 |
*/
|
|
| 86 | 356 |
public JFCKeyStroke[] getKeyStrokes(final int keyCode) { |
| 87 | 356 |
return getKeyStrokes(
|
| 88 |
new Integer(keyCode),
|
|
| 89 |
m_mapCode); |
|
| 90 |
} |
|
| 91 |
|
|
| 92 |
/**
|
|
| 93 |
* Get the key strokes form the mapping specified.
|
|
| 94 |
*
|
|
| 95 |
* @param key Key object to be found.
|
|
| 96 |
* @param map Map to look for the key in.
|
|
| 97 |
* @return {@link JFCKeyStroke}[] array of key strokes copied from
|
|
| 98 |
* the mapping.
|
|
| 99 |
*/
|
|
| 100 | 8870 |
private JFCKeyStroke[] getKeyStrokes(final Object key, final HashMap map) {
|
| 101 | 8870 |
JFCKeyStroke[] codes = (JFCKeyStroke[]) map.get(key); |
| 102 |
|
|
| 103 | 8870 |
if (codes == null) { |
| 104 | 190 |
return new JFCKeyStroke[0]; |
| 105 |
} |
|
| 106 |
|
|
| 107 | 8680 |
JFCKeyStroke[] retCodes = new JFCKeyStroke[codes.length];
|
| 108 |
|
|
| 109 | 8680 |
for (int i = 0; i < codes.length; i++) { |
| 110 | 8680 |
retCodes[i] = new JFCKeyStroke(codes[i]);
|
| 111 |
} |
|
| 112 |
|
|
| 113 | 8680 |
return retCodes;
|
| 114 |
} |
|
| 115 |
} |
|
| 116 |
|
|
||||||||||