|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| JFCKeyStroke.java | 33.3% | 62.3% | 92.9% | 63.3% |
|
||||||||||||||
| 1 |
package junit.extensions.jfcunit.keyboard;
|
|
| 2 |
|
|
| 3 |
import java.awt.event.KeyEvent;
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
/**
|
|
| 7 |
* The JFCKeyStroke class holds all of the data pertaining to
|
|
| 8 |
* a keystroke to be pressed/released by the TestHelper.
|
|
| 9 |
*
|
|
| 10 |
* @author Kevin Wilson
|
|
| 11 |
*/
|
|
| 12 |
public class JFCKeyStroke { |
|
| 13 |
/**
|
|
| 14 |
* isTyped true if the key stroke is to emit a typed event.
|
|
| 15 |
*/
|
|
| 16 |
private boolean m_isTyped; |
|
| 17 |
|
|
| 18 |
/**
|
|
| 19 |
* Key Character.
|
|
| 20 |
*/
|
|
| 21 |
private char m_keyChar; |
|
| 22 |
|
|
| 23 |
/**
|
|
| 24 |
* Key Code.
|
|
| 25 |
*/
|
|
| 26 |
private int m_keyCode; |
|
| 27 |
|
|
| 28 |
/**
|
|
| 29 |
* Modifiers to be pressed for this event.
|
|
| 30 |
*/
|
|
| 31 |
private int m_modifiers; |
|
| 32 |
|
|
| 33 |
/**
|
|
| 34 |
* Constructor.
|
|
| 35 |
*
|
|
| 36 |
* @param c Character.
|
|
| 37 |
* @param code Key code.
|
|
| 38 |
* @param modifiers Modifiers to be pressed when sending the keystroke.
|
|
| 39 |
* @param typed true if a typed event should be emitted.
|
|
| 40 |
*/
|
|
| 41 | 1775 |
public JFCKeyStroke(final char c, final int code, final int modifiers, |
| 42 |
final boolean typed) {
|
|
| 43 | 1775 |
setKeyChar(c); |
| 44 | 1775 |
setKeyCode(code); |
| 45 | 1775 |
setModifiers(modifiers); |
| 46 | 1775 |
setTyped(typed); |
| 47 |
} |
|
| 48 |
|
|
| 49 | 0 |
public JFCKeyStroke(String keyDesc) {
|
| 50 | 0 |
int modifiers = 0;
|
| 51 | 0 |
boolean typed = true; |
| 52 | 0 |
String key = new String(keyDesc);
|
| 53 | 0 |
if (key.indexOf("ctrl") != -1) { |
| 54 | 0 |
modifiers += KeyEvent.CTRL_MASK; |
| 55 |
} |
|
| 56 | 0 |
if (key.indexOf("altGr") != -1) { |
| 57 | 0 |
modifiers += KeyEvent.ALT_MASK; |
| 58 | 0 |
key = key.replaceFirst("altGr", " "); |
| 59 | 0 |
typed = false;
|
| 60 |
} |
|
| 61 | 0 |
if (keyDesc.indexOf("alt") != -1) { |
| 62 | 0 |
modifiers += KeyEvent.ALT_MASK; |
| 63 | 0 |
typed = false;
|
| 64 |
} |
|
| 65 | 0 |
if (key.indexOf("shift") != -1) { |
| 66 | 0 |
modifiers += KeyEvent.SHIFT_MASK; |
| 67 |
} |
|
| 68 | 0 |
setModifiers(modifiers); |
| 69 | 0 |
setTyped(typed); |
| 70 |
|
|
| 71 | 0 |
key = key.trim(); |
| 72 | 0 |
char c = key.charAt(key.length() - 1);
|
| 73 | 0 |
setKeyChar(c); |
| 74 | 0 |
setKeyCode(c); |
| 75 |
} |
|
| 76 |
|
|
| 77 |
/**
|
|
| 78 |
* Copy Constructor.
|
|
| 79 |
*
|
|
| 80 |
* @param stroke Keystroke to be copied.
|
|
| 81 |
*/
|
|
| 82 | 8681 |
public JFCKeyStroke(final JFCKeyStroke stroke) {
|
| 83 | 8681 |
setKeyChar(stroke.getKeyChar()); |
| 84 | 8681 |
setKeyCode(stroke.getKeyCode()); |
| 85 | 8681 |
setModifiers(stroke.getModifiers()); |
| 86 | 8681 |
setTyped(stroke.isTyped()); |
| 87 |
} |
|
| 88 |
|
|
| 89 |
/**
|
|
| 90 |
* Get the key character.
|
|
| 91 |
*
|
|
| 92 |
* @return char key character.
|
|
| 93 |
*/
|
|
| 94 | 9645 |
public final char getKeyChar() { |
| 95 | 9645 |
return m_keyChar;
|
| 96 |
} |
|
| 97 |
|
|
| 98 |
/**
|
|
| 99 |
* Get the key code.
|
|
| 100 |
*
|
|
| 101 |
* @return int key code
|
|
| 102 |
*/
|
|
| 103 | 10691 |
public final int getKeyCode() { |
| 104 | 10691 |
return m_keyCode;
|
| 105 |
} |
|
| 106 |
|
|
| 107 |
/**
|
|
| 108 |
* Set the modifiers.
|
|
| 109 |
*
|
|
| 110 |
* @param modifiers Modifiers to be pressed for this keystroke.
|
|
| 111 |
*/
|
|
| 112 | 18218 |
public void setModifiers(final int modifiers) { |
| 113 | 18218 |
this.m_modifiers = modifiers;
|
| 114 |
} |
|
| 115 |
|
|
| 116 |
/**
|
|
| 117 |
* Get the modifiers.
|
|
| 118 |
*
|
|
| 119 |
* @return int modifiers.
|
|
| 120 |
*/
|
|
| 121 | 18891 |
public final int getModifiers() { |
| 122 | 18891 |
return m_modifiers;
|
| 123 |
} |
|
| 124 |
|
|
| 125 |
/**
|
|
| 126 |
* Is the keystroke to emit a typed event.
|
|
| 127 |
*
|
|
| 128 |
* @return true if the typed event is to be emitted.
|
|
| 129 |
*/
|
|
| 130 | 9016 |
public final boolean isTyped() { |
| 131 | 9016 |
return m_isTyped;
|
| 132 |
} |
|
| 133 |
|
|
| 134 |
/**
|
|
| 135 |
* Test for equality.
|
|
| 136 |
*
|
|
| 137 |
* @param o Object to be tested for equality.
|
|
| 138 |
* @return true if the object is equal to this object.
|
|
| 139 |
*/
|
|
| 140 | 9 |
public boolean equals(final Object o) { |
| 141 | 9 |
if (o instanceof JFCKeyStroke) { |
| 142 | 7 |
JFCKeyStroke stroke = (JFCKeyStroke) o; |
| 143 |
|
|
| 144 | 7 |
return ((getKeyChar() == stroke.getKeyChar())
|
| 145 |
&& (getKeyCode() == stroke.getKeyCode()) |
|
| 146 |
&& (getModifiers() == stroke.getModifiers()) |
|
| 147 |
&& (m_isTyped == stroke.isTyped())); |
|
| 148 |
} |
|
| 149 |
|
|
| 150 | 2 |
return false; |
| 151 |
} |
|
| 152 |
|
|
| 153 |
/**
|
|
| 154 |
* Get hashCode.
|
|
| 155 |
* @return int hashCode.
|
|
| 156 |
*/
|
|
| 157 | 3 |
public int hashCode() { |
| 158 | 3 |
return super.hashCode() + getKeyChar() + getKeyCode() + getModifiers(); |
| 159 |
} |
|
| 160 |
|
|
| 161 |
/**
|
|
| 162 |
* Create a string representation of the keystroke.
|
|
| 163 |
*
|
|
| 164 |
* @return String representation of the keystroke.
|
|
| 165 |
*/
|
|
| 166 | 2 |
public String toString() {
|
| 167 | 2 |
StringBuffer buf = new StringBuffer(100);
|
| 168 |
|
|
| 169 | 2 |
if ((getKeyChar() < 26) && (getModifiers() == 2)) {
|
| 170 | 1 |
buf.append("<control ");
|
| 171 |
|
|
| 172 | 1 |
int x = getKeyChar();
|
| 173 | 1 |
x += ('A' - 1);
|
| 174 | 1 |
buf.append((char) x);
|
| 175 | 1 |
buf.append(">");
|
| 176 |
} else {
|
|
| 177 | 1 |
buf.append("Char:" + getKeyChar());
|
| 178 | 1 |
buf.append(" Code:" + getKeyCode());
|
| 179 | 1 |
buf.append(" Mods:" + getModifiers());
|
| 180 | 1 |
buf.append(" IsTyped:" + isTyped());
|
| 181 |
} |
|
| 182 |
|
|
| 183 | 2 |
return buf.toString();
|
| 184 |
} |
|
| 185 |
|
|
| 186 |
/**
|
|
| 187 |
* Set the key character.
|
|
| 188 |
*
|
|
| 189 |
* @param c Character.
|
|
| 190 |
*/
|
|
| 191 | 10456 |
private void setKeyChar(final char c) { |
| 192 | 10456 |
m_keyChar = c; |
| 193 |
} |
|
| 194 |
|
|
| 195 |
/**
|
|
| 196 |
* Set the key code.
|
|
| 197 |
*
|
|
| 198 |
* @param code Key code
|
|
| 199 |
*/
|
|
| 200 | 10456 |
private void setKeyCode(final int code) { |
| 201 | 10456 |
m_keyCode = code; |
| 202 |
} |
|
| 203 |
|
|
| 204 |
/**
|
|
| 205 |
* Set the typed state.
|
|
| 206 |
*
|
|
| 207 |
* @param typed true if the keystroke is to emit a typed event.
|
|
| 208 |
*/
|
|
| 209 | 10456 |
private void setTyped(final boolean typed) { |
| 210 | 10456 |
m_isTyped = typed; |
| 211 |
} |
|
| 212 |
} |
|
| 213 |
|
|
||||||||||