29 import org.jemmy.interfaces.Keyboard.KeyboardButton;
30 import org.jemmy.interfaces.Keyboard.KeyboardModifier;
31 import org.jemmy.interfaces.Keyboard;
32 import org.jemmy.env.Environment;
33 import org.jemmy.env.Timeout;
34 import org.jemmy.interfaces.Focusable;
35 import org.jemmy.interfaces.Modifier;
36
37 /**
38 * KeyDriver
39 *
40 * @author Alexandre Iline(alexandre.iline@sun.com)
41 */
42 public class KeyboardImpl implements Keyboard {
43
44 CharBindingMap<KeyboardButton, KeyboardModifier> map;
45 Environment env;
46 Wrap<?> target;
47 RobotDriver robotDriver;
48 private boolean detached;
49 /**
50 * Constructs a KeyRobotDriver object.
51 * @param target
52 */
53 public KeyboardImpl(Wrap<?> target) {
54 //TODO: super(target.getEnvironment().getTimeout(RobotDriver.ROBOT_DELAY_TIMEOUT_NAME));
55 robotDriver = new RobotDriver(target.getEnvironment());
56 this.env = target.getEnvironment();
57 this.map = target.getEnvironment().getBindingMap();
58 this.target = target;
59 }
60
61 static {
62 //TODO: Environment.getEnvironment().setTimeout(new Timeout(RobotDriver.ROBOT_DELAY_TIMEOUT_NAME, 10));
63 Environment.getEnvironment().setTimeout(new Timeout(PUSH.getName(), 100));
64 Environment.getEnvironment().setBindingMap(new DefaultCharBindingMap());
65 }
66
67 private void runAction(Action action) {
68 if(detached) {
69 target.getEnvironment().getExecutor().executeDetached(target.getEnvironment(), false, action);
70 } else {
71 target.getEnvironment().getExecutor().execute(target.getEnvironment(), false, action);
72 }
73 }
74
75 /**
76 *
77 * @return Environment
78 */
79 public Environment getEnvironment() {
80 return env;
81 }
82
83 /**
84 *
85 * @param kbdButton
86 * @param modifiers
87 * @param pushTime
88 */
89 public void pushKey(final KeyboardButton kbdButton, final Modifier modifiers[], final Timeout pushTime) {
90 runAction(new Action() {
91 public void run(Object... parameters) {
92 if(target.is(Focusable.class)) target.as(Focusable.class).focuser().focus();
93 pressKey(kbdButton, modifiers);
94 pushTime.sleep();
95 releaseKey(kbdButton, modifiers);
96 }
97 @Override
98 public String toString() {
99 return "push " + kbdButton + " key with " + modifiers + " modifiers";
100 }
101 });
102 }
103
104 /**
105 *
106 * @param keyChar
107 * @param pushTime
108 */
109 @Override
110 public void typeChar(char keyChar, Timeout pushTime) {
111 pushKey(pushTime, map.getCharKey(keyChar), map.getCharModifiers(keyChar));
112 }
113
114 /**
115 * Press the keyboard key specified by kbdButton preceding with
116 * pressing of modifier buttons specified by modifiers
117 * @param kbdButton one of InputEvent.VK_* constants
118 * @param modifiers combination of InputEvent.*_DOWN_MASK constants
119 * @see java.awt.event.InputEvent
120 */
121 @Override
122 public void pressKey(final KeyboardButton kbdButton, final Modifier... modifiers) {
123 runAction(new Action() {
124 public void run(Object... parameters) {
125 robotDriver.pressKey(kbdButton, modifiers);
126 }
127 @Override
128 public String toString() {
134 /**
135 * Release the keyboard key specified by kbdButton and then release
136 * all the modifier keys specified by modifiers
137 * @param kbdButton one of InputEvent.VK_* constants
138 * @param modifiers combination of InputEvent.*_DOWN_MASK constants
139 * @see java.awt.event.InputEvent
140 */
141 @Override
142 public void releaseKey(final KeyboardButton kbdButton, final Modifier... modifiers) {
143 runAction(new Action() {
144 public void run(Object... parameters) {
145 robotDriver.releaseKey(kbdButton, modifiers);
146 }
147 @Override
148 public String toString() {
149 return "press " + kbdButton + " key with " + modifiers + " modifiers";
150 }
151 });
152 }
153
154 /**
155 *
156 * @param kbdButton
157 */
158 @Override
159 public void pressKey(KeyboardButton kbdButton) {
160 pressKey(kbdButton, new Modifier[]{});
161 }
162
163 /**
164 *
165 * @param kbdButton
166 */
167 @Override
168 public void releaseKey(KeyboardButton kbdButton) {
169 releaseKey(kbdButton, new Modifier[]{});
170 }
171
172 /**
173 *
174 * @param kbdButton
175 * @param modifiers
176 */
177 @Override
178 public void pushKey(KeyboardButton kbdButton, Modifier... modifiers) {
179 pushKey(kbdButton, modifiers, getEnvironment().getTimeout(PUSH.getName()));
180 }
181
182 /**
183 *
184 * @param kbdButton
185 */
186 @Override
187 public void pushKey(KeyboardButton kbdButton) {
188 pushKey(kbdButton, new Modifier[]{});
189 }
190
191 /**
192 *
193 * @param keyChar
194 */
195 @Override
196 public void typeChar(char keyChar) {
197 typeChar(keyChar, getEnvironment().getTimeout(PUSH.getName()));
198 }
199
200 @Override
201 public Keyboard detached() {
202 detached = true;
203 return this;
204 }
205
206 @Override
207 public void pushKey(Timeout pushTime, KeyboardButton key, Modifier... modifiers) {
208 pushKey(key, modifiers, pushTime);
209 }
210 }
|
29 import org.jemmy.interfaces.Keyboard.KeyboardButton;
30 import org.jemmy.interfaces.Keyboard.KeyboardModifier;
31 import org.jemmy.interfaces.Keyboard;
32 import org.jemmy.env.Environment;
33 import org.jemmy.env.Timeout;
34 import org.jemmy.interfaces.Focusable;
35 import org.jemmy.interfaces.Modifier;
36
37 /**
38 * KeyDriver
39 *
40 * @author Alexandre Iline(alexandre.iline@sun.com)
41 */
42 public class KeyboardImpl implements Keyboard {
43
44 CharBindingMap<KeyboardButton, KeyboardModifier> map;
45 Environment env;
46 Wrap<?> target;
47 RobotDriver robotDriver;
48 private boolean detached;
49
50 public KeyboardImpl(Wrap<?> target) {
51 //TODO: super(target.getEnvironment().getTimeout(RobotDriver.ROBOT_DELAY_TIMEOUT_NAME));
52 robotDriver = new RobotDriver(target.getEnvironment());
53 this.env = target.getEnvironment();
54 this.map = target.getEnvironment().getBindingMap();
55 this.target = target;
56 }
57
58 static {
59 //TODO: Environment.getEnvironment().setTimeout(new Timeout(RobotDriver.ROBOT_DELAY_TIMEOUT_NAME, 10));
60 Environment.getEnvironment().setTimeout(new Timeout(PUSH.getName(), 100));
61 Environment.getEnvironment().setBindingMap(new DefaultCharBindingMap());
62 }
63
64 private void runAction(Action action) {
65 if(detached) {
66 target.getEnvironment().getExecutor().executeDetached(target.getEnvironment(), false, action);
67 } else {
68 target.getEnvironment().getExecutor().execute(target.getEnvironment(), false, action);
69 }
70 }
71
72 public Environment getEnvironment() {
73 return env;
74 }
75
76 public void pushKey(final KeyboardButton kbdButton, final Modifier modifiers[], final Timeout pushTime) {
77 runAction(new Action() {
78 public void run(Object... parameters) {
79 if(target.is(Focusable.class)) target.as(Focusable.class).focuser().focus();
80 pressKey(kbdButton, modifiers);
81 pushTime.sleep();
82 releaseKey(kbdButton, modifiers);
83 }
84 @Override
85 public String toString() {
86 return "push " + kbdButton + " key with " + modifiers + " modifiers";
87 }
88 });
89 }
90
91 @Override
92 public void typeChar(char keyChar, Timeout pushTime) {
93 pushKey(pushTime, map.getCharKey(keyChar), map.getCharModifiers(keyChar));
94 }
95
96 /**
97 * Press the keyboard key specified by kbdButton preceding with
98 * pressing of modifier buttons specified by modifiers
99 * @param kbdButton one of InputEvent.VK_* constants
100 * @param modifiers combination of InputEvent.*_DOWN_MASK constants
101 * @see java.awt.event.InputEvent
102 */
103 @Override
104 public void pressKey(final KeyboardButton kbdButton, final Modifier... modifiers) {
105 runAction(new Action() {
106 public void run(Object... parameters) {
107 robotDriver.pressKey(kbdButton, modifiers);
108 }
109 @Override
110 public String toString() {
116 /**
117 * Release the keyboard key specified by kbdButton and then release
118 * all the modifier keys specified by modifiers
119 * @param kbdButton one of InputEvent.VK_* constants
120 * @param modifiers combination of InputEvent.*_DOWN_MASK constants
121 * @see java.awt.event.InputEvent
122 */
123 @Override
124 public void releaseKey(final KeyboardButton kbdButton, final Modifier... modifiers) {
125 runAction(new Action() {
126 public void run(Object... parameters) {
127 robotDriver.releaseKey(kbdButton, modifiers);
128 }
129 @Override
130 public String toString() {
131 return "press " + kbdButton + " key with " + modifiers + " modifiers";
132 }
133 });
134 }
135
136 @Override
137 public void pressKey(KeyboardButton kbdButton) {
138 pressKey(kbdButton, new Modifier[]{});
139 }
140
141
142 @Override
143 public void releaseKey(KeyboardButton kbdButton) {
144 releaseKey(kbdButton, new Modifier[]{});
145 }
146
147
148 @Override
149 public void pushKey(KeyboardButton kbdButton, Modifier... modifiers) {
150 pushKey(kbdButton, modifiers, getEnvironment().getTimeout(PUSH.getName()));
151 }
152
153 @Override
154 public void pushKey(KeyboardButton kbdButton) {
155 pushKey(kbdButton, new Modifier[]{});
156 }
157
158 @Override
159 public void typeChar(char keyChar) {
160 typeChar(keyChar, getEnvironment().getTimeout(PUSH.getName()));
161 }
162
163 @Override
164 public Keyboard detached() {
165 detached = true;
166 return this;
167 }
168
169 @Override
170 public void pushKey(Timeout pushTime, KeyboardButton key, Modifier... modifiers) {
171 pushKey(key, modifiers, pushTime);
172 }
173 }
|