1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.eclipse.swtbot.swt.finder.keyboard; |
12 | |
|
13 | |
import org.apache.log4j.Logger; |
14 | |
import org.eclipse.jface.bindings.keys.KeyStroke; |
15 | |
import org.eclipse.swt.widgets.Widget; |
16 | |
import org.eclipse.swtbot.swt.finder.utils.MessageFormat; |
17 | |
import org.eclipse.swtbot.swt.finder.utils.internal.Assert; |
18 | |
import org.hamcrest.SelfDescribing; |
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
public abstract class AbstractKeyboardStrategy implements KeyboardStrategy { |
27 | |
|
28 | |
protected final Logger log; |
29 | |
|
30 | 11 | protected AbstractKeyboardStrategy() { |
31 | 11 | this.log = Logger.getLogger(getClass()); |
32 | 11 | } |
33 | |
|
34 | |
public void init(Widget widget, SelfDescribing description) { |
35 | |
|
36 | 4 | } |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
protected abstract void pressKey(KeyStroke key); |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
protected abstract void releaseKey(KeyStroke key); |
51 | |
|
52 | |
public void pressKeys(KeyStroke... keys) { |
53 | 68 | assertKeys(keys); |
54 | 147 | for (KeyStroke key : keys) { |
55 | 79 | log.trace(MessageFormat.format("Pressing down key {0}", key)); |
56 | 79 | pressKey(key); |
57 | |
} |
58 | 68 | } |
59 | |
|
60 | |
public void releaseKeys(KeyStroke... keys) { |
61 | 68 | assertKeys(keys); |
62 | 147 | for (KeyStroke key : keys) { |
63 | 79 | log.trace(MessageFormat.format("Releasing key {0}", key)); |
64 | 79 | releaseKey(key); |
65 | |
} |
66 | 68 | } |
67 | |
|
68 | |
private void assertKeys(KeyStroke... keys) { |
69 | 294 | for (KeyStroke key : keys) { |
70 | 158 | assertKey(key); |
71 | |
} |
72 | 136 | } |
73 | |
|
74 | |
private void assertKey(KeyStroke key) { |
75 | 158 | boolean hasNaturalKey = key.getNaturalKey() != KeyStroke.NO_KEY; |
76 | 158 | boolean hasModifiers = key.getModifierKeys() != KeyStroke.NO_KEY; |
77 | |
|
78 | 158 | Assert.isTrue(hasNaturalKey ^ hasModifiers, "You just gave me a complex keystroke. Please split the keystroke into multiple keystrokes."); |
79 | 158 | } |
80 | |
|
81 | |
} |