|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
AbstractKeyEventData.java | 53.6% | 57.1% | 71.4% | 58.2% |
|
1 |
package junit.extensions.jfcunit.eventdata;
|
|
2 |
|
|
3 |
import junit.extensions.jfcunit.TestHelper;
|
|
4 |
import junit.extensions.jfcunit.keyboard.JFCKeyStroke;
|
|
5 |
import junit.extensions.jfcunit.keyboard.KeyMapping;
|
|
6 |
|
|
7 |
import java.awt.AWTEvent;
|
|
8 |
import java.awt.Component;
|
|
9 |
import java.awt.Toolkit;
|
|
10 |
import java.awt.event.KeyEvent;
|
|
11 |
|
|
12 |
import java.util.Arrays;
|
|
13 |
import java.util.Vector;
|
|
14 |
|
|
15 |
|
|
16 |
/**
|
|
17 |
* Abstract data container class that holds most of the data necessary for jfcUnit to fire key events.
|
|
18 |
*
|
|
19 |
* @author <a href="mailto:vraravam@thoughtworks.com">Vijay Aravamudhan : ThoughtWorks Inc.</a>
|
|
20 |
*/
|
|
21 |
public abstract class AbstractKeyEventData extends AbstractEventData { |
|
22 |
/**
|
|
23 |
* Key Strokes to be simulated.
|
|
24 |
*/
|
|
25 |
private final Vector m_keyStrokes = new Vector(); |
|
26 |
|
|
27 |
/**
|
|
28 |
* Get the key strokes for this event.
|
|
29 |
*
|
|
30 |
* @return JFCKeyStroke[] to be simulated by this event.
|
|
31 |
*/
|
|
32 | 82 |
public final JFCKeyStroke[] getKeyStrokes() {
|
33 | 82 |
return (JFCKeyStroke[]) m_keyStrokes.toArray(new JFCKeyStroke[0]); |
34 |
} |
|
35 |
|
|
36 |
/**
|
|
37 |
* Set the modifiers on the event.
|
|
38 |
*
|
|
39 |
* @param modifiers modifiers to be set.
|
|
40 |
*/
|
|
41 | 404 |
public final void setModifiers(final int modifiers) { |
42 | 404 |
super.setModifiers(modifiers);
|
43 | 404 |
setupKeyStrokes(); |
44 |
} |
|
45 |
|
|
46 |
/**
|
|
47 |
* Get the default modifiers.
|
|
48 |
* @return Default key modiers.
|
|
49 |
*/
|
|
50 | 0 |
public int getDefaultModifiers() { |
51 | 0 |
return EventDataConstants.DEFAULT_KEY_MODIFIERS;
|
52 |
} |
|
53 |
|
|
54 |
/**
|
|
55 |
* Set the attribute value.
|
|
56 |
*
|
|
57 |
* @param km The new KeyMapping
|
|
58 |
*/
|
|
59 | 0 |
public static void setKeyMapping(final KeyMapping km) { |
60 | 0 |
TestHelper.setKeyMapping(km); |
61 |
} |
|
62 |
|
|
63 |
/**
|
|
64 |
* Get the attribute value.
|
|
65 |
*
|
|
66 |
* @return KeyMapping The KeyMapping that is being currently used.
|
|
67 |
*/
|
|
68 | 1041 |
public static KeyMapping getKeyMapping() { |
69 | 1041 |
return TestHelper.getKeyMapping();
|
70 |
} |
|
71 |
|
|
72 |
/**
|
|
73 |
* Get the current modifier text.
|
|
74 |
* @return String containing the text form of the modifiers.
|
|
75 |
*/
|
|
76 | 0 |
public final String getModifierText() {
|
77 | 0 |
StringBuffer buf = new StringBuffer();
|
78 | 0 |
int modifiers = getModifiers();
|
79 |
|
|
80 | 0 |
if ((modifiers & KeyEvent.ALT_MASK) != 0) {
|
81 | 0 |
buf.append(Toolkit.getProperty("AWT.alt", "Alt")); |
82 | 0 |
buf.append("+");
|
83 |
} |
|
84 |
|
|
85 | 0 |
if ((modifiers & KeyEvent.META_MASK) != 0) {
|
86 | 0 |
buf.append(Toolkit.getProperty("AWT.meta", "Meta")); |
87 | 0 |
buf.append("+");
|
88 |
} |
|
89 |
|
|
90 | 0 |
if ((modifiers & KeyEvent.CTRL_MASK) != 0) {
|
91 | 0 |
buf.append(Toolkit.getProperty("AWT.control", "Ctrl")); |
92 | 0 |
buf.append("+");
|
93 |
} |
|
94 |
|
|
95 | 0 |
if ((modifiers & KeyEvent.SHIFT_MASK) != 0) {
|
96 | 0 |
buf.append(Toolkit.getProperty("AWT.shift", "Shift")); |
97 | 0 |
buf.append("+");
|
98 |
} |
|
99 |
|
|
100 | 0 |
if ((modifiers & KeyEvent.ALT_GRAPH_MASK) != 0) {
|
101 | 0 |
buf.append(Toolkit.getProperty("AWT.altGraph", "Alt Graph")); |
102 | 0 |
buf.append("+");
|
103 |
} |
|
104 |
|
|
105 | 0 |
if (buf.length() > 0) {
|
106 | 0 |
buf.setLength(buf.length() - 1); // remove trailing '+'
|
107 |
} |
|
108 |
|
|
109 | 0 |
return buf.toString();
|
110 |
} |
|
111 |
|
|
112 |
/**
|
|
113 |
* Check if this event can consume the {@link java.awt.AWTEvent}.
|
|
114 |
*
|
|
115 |
* @param ae {@link java.awt.AWTEvent} to be consumed.
|
|
116 |
* @return boolean true if the event can be consumed.
|
|
117 |
*/
|
|
118 | 1023 |
public boolean canConsume(final AWTEvent ae) { |
119 | 1023 |
if (!isValid()) {
|
120 | 194 |
return true; |
121 |
} |
|
122 |
|
|
123 | 829 |
return (ae instanceof KeyEvent) |
124 |
&& getRoot((Component) ae.getSource()).equals(getRoot()); |
|
125 |
} |
|
126 |
|
|
127 |
/**
|
|
128 |
* Get the attribute value.
|
|
129 |
*
|
|
130 |
* @param ae {@link java.awt.AWTEvent} to be processed.
|
|
131 |
* @return boolean The value of the attribute
|
|
132 |
*/
|
|
133 | 972 |
public boolean consume(final AWTEvent ae) { |
134 | 972 |
if (!(ae instanceof KeyEvent)) { |
135 | 0 |
return false; |
136 |
} |
|
137 |
|
|
138 | 972 |
KeyEvent ke = (KeyEvent) ae; |
139 | 972 |
int id = ke.getID();
|
140 |
|
|
141 | 972 |
if ((id == KeyEvent.KEY_TYPED)
|
142 |
|| (id == KeyEvent.KEY_RELEASED) |
|
143 |
|| (isMetaChar(ke.getKeyCode())) |
|
144 |
|| ae.getSource().equals(getRoot())) { |
|
145 |
// Ignore the event.
|
|
146 | 671 |
return true; |
147 |
} |
|
148 |
|
|
149 | 301 |
return false; |
150 |
} |
|
151 |
|
|
152 |
/**
|
|
153 |
* Compare to event datas and deterime if they are equal.
|
|
154 |
*
|
|
155 |
* @param o Object to be compared.
|
|
156 |
* @return true if the events are the same.
|
|
157 |
*/
|
|
158 | 15 |
public boolean equals(final Object o) { |
159 | 15 |
if (!super.equals(o)) { |
160 | 11 |
return false; |
161 |
} |
|
162 |
|
|
163 |
// isValid
|
|
164 | 4 |
AbstractKeyEventData data = (AbstractKeyEventData) o; |
165 | 4 |
JFCKeyStroke[] dataStrokes = data.getKeyStrokes(); |
166 | 4 |
JFCKeyStroke[] thisStrokes = getKeyStrokes(); |
167 |
|
|
168 | 4 |
return Arrays.equals(dataStrokes, thisStrokes);
|
169 |
} |
|
170 |
|
|
171 |
/**
|
|
172 |
* Return the hashCode of the object.
|
|
173 |
* @return hashCode of the super class.
|
|
174 |
*/
|
|
175 | 0 |
public int hashCode() { |
176 | 0 |
return super.hashCode(); |
177 |
} |
|
178 |
|
|
179 |
/**
|
|
180 |
* Prepare the text component.
|
|
181 |
* @return boolean true if the component is ready.
|
|
182 |
*/
|
|
183 | 82 |
public boolean prepareComponent() { |
184 | 82 |
if (super.prepareComponent()) { |
185 | 74 |
if (!getComponent().hasFocus()) {
|
186 | 30 |
getComponent().requestFocus(); |
187 | 30 |
getTestCase().flushAWT(); |
188 |
} |
|
189 |
|
|
190 | 74 |
return true; |
191 |
} else {
|
|
192 | 8 |
return false; |
193 |
} |
|
194 |
} |
|
195 |
|
|
196 |
/**
|
|
197 |
* Each implementing class needs to provide a
|
|
198 |
* method to translate the string into the
|
|
199 |
* keystorkes required to produce the string.
|
|
200 |
*/
|
|
201 |
protected abstract void setupKeyStrokes(); |
|
202 |
|
|
203 |
/**
|
|
204 |
* Add key strokes to this event.
|
|
205 |
*
|
|
206 |
* @param strokes Key strokes to be added.
|
|
207 |
*/
|
|
208 | 7607 |
protected final void addKeyStrokes(final JFCKeyStroke[] strokes) { |
209 | 7607 |
for (int i = 0; i < strokes.length; i++) { |
210 | 7540 |
m_keyStrokes.add(strokes[i]); |
211 |
} |
|
212 |
} |
|
213 |
|
|
214 |
/**
|
|
215 |
* Apply the modifiers specified to all of the key strokes.
|
|
216 |
*
|
|
217 |
* @param modifiers Modifiers to be applied.
|
|
218 |
*/
|
|
219 | 808 |
protected final void applyModifier(final int modifiers) { |
220 | 808 |
for (int i = 0; i < m_keyStrokes.size(); i++) { |
221 | 7540 |
JFCKeyStroke s = (JFCKeyStroke) m_keyStrokes.get(i); |
222 | 7540 |
int mods = s.getModifiers() | modifiers;
|
223 | 7540 |
s.setModifiers(mods); |
224 |
} |
|
225 |
} |
|
226 |
|
|
227 |
/**
|
|
228 |
* Clear key Strokes.
|
|
229 |
*/
|
|
230 | 808 |
protected final void clearKeyStrokes() { |
231 | 808 |
m_keyStrokes.clear(); |
232 |
} |
|
233 |
} |
|
234 |
|
|