01: package abbot.tester;
02:
03: import java.awt.*;
04: import java.awt.event.*;
05: import abbot.util.*;
06: import abbot.i18n.Strings;
07:
08: /** Provides actions for <code>java.awt.List</code>. */
09: // TODO: double-click (actionPerformed)
10: // TODO: multi-select
11: public class ListTester extends ComponentTester {
12:
13: private int LIST_DELAY = Properties.getProperty(
14: "abbot.tester.list_delay", 30000, 0, 60000);
15:
16: private class Listener implements AWTEventListener {
17: public volatile boolean selected;
18: private int targetIndex = -1;
19:
20: public Listener(int index, boolean state) {
21: targetIndex = index;
22: selected = !state;
23: }
24:
25: public void eventDispatched(AWTEvent e) {
26: if (e.getID() == ItemEvent.ITEM_STATE_CHANGED
27: && e.getSource() instanceof List) {
28: if (((List) e.getSource()).getSelectedIndex() == targetIndex) {
29: selected = ((ItemEvent) e).getStateChange() == ItemEvent.SELECTED;
30: }
31: }
32: }
33: }
34:
35: /** @deprecated Use actionSelectRow instead. */
36: public void actionSelectIndex(Component c, int index) {
37: actionSelectRow(c, new ListLocation(index));
38: }
39:
40: /** Select the row corresponding to the given ListLocation. */
41: public void actionSelectRow(Component c, ListLocation location) {
42: List list = (List) c;
43: try {
44: int index = location.getIndex(list);
45: if (index < 0 || index >= list.getItemCount()) {
46: String msg = Strings.get("tester.JList.invalid_index",
47: new Object[] { new Integer(index) });
48: throw new ActionFailedException(msg);
49: }
50: if (list.getSelectedIndex() != index) {
51: setSelected(list, index, true);
52: }
53: } catch (LocationUnavailableException e) {
54: actionClick(c, location);
55: }
56: }
57:
58: protected void setSelected(List list, int index, boolean selected) {
59: Listener listener = new Listener(index, selected);
60: new WeakAWTEventListener(listener, ItemEvent.ITEM_EVENT_MASK);
61: list.select(index);
62: ItemEvent ie = new ItemEvent(list,
63: ItemEvent.ITEM_STATE_CHANGED, list.getSelectedItem(),
64: selected ? ItemEvent.SELECTED : ItemEvent.DESELECTED);
65: postEvent(list, ie);
66: long now = System.currentTimeMillis();
67: while (listener.selected != selected) {
68: if (System.currentTimeMillis() - now > LIST_DELAY)
69: throw new ActionFailedException("List didn't fire for "
70: + "index " + index + " selection");
71: sleep();
72: }
73: waitForIdle();
74: }
75:
76: /** Parse the String representation of a ListLocation into the actual
77: ListLocation object.
78: */
79: public ComponentLocation parseLocation(String encoded) {
80: return new ListLocation().parse(encoded);
81: }
82:
83: /** Return the value, row, or coordinate location. */
84: public ComponentLocation getLocation(Component c, Point p) {
85: throw new LocationUnavailableException(
86: "Locations on java.awt.List must be entered manually");
87: }
88: }
|