001: package abbot.tester;
002:
003: import java.awt.*;
004:
005: import javax.swing.*;
006:
007: import abbot.Log;
008: import abbot.i18n.Strings;
009: import abbot.script.ArgumentParser;
010: import abbot.util.Condition;
011:
012: /** Provide actions and assertions for a {@link JList} component.
013: The {@link JList} substructure is a "row", and {@link JListLocation}
014: provides different identifiers for a row.
015: <ul>
016: <li>Select an item by index
017: <li>Select an item by value (its string representation)
018: </ul>
019: Note that {@link JList} uses "index" and "value" in its API. For
020: convenience, the <code>JListTester</code> API also provides "row" and
021: "item" as synonyms for "index".
022:
023: @see JListLocation
024: */
025: // TODO multi-select
026: public class JListTester extends JComponentTester {
027:
028: /** Convert the value in the list at the given index into a reasonable
029: string representation, or null if one can not be obtained.
030: */
031: public static String valueToString(JList list, int index) {
032: Object value = list.getModel().getElementAt(index);
033: Component cr = list.getCellRenderer()
034: .getListCellRendererComponent(list, value, index,
035: false, false);
036: String string = convertRendererToString(cr);
037: return string;
038: }
039:
040: /** JList doesn't provide direct access to its contents, so make up for
041: * that oversight.
042: */
043: public Object getElementAt(JList list, int index) {
044: return list.getModel().getElementAt(index);
045: }
046:
047: /** Return the size of the given list. */
048: public int getSize(JList list) {
049: return list.getModel().getSize();
050: }
051:
052: /** Return an array of strings that represents the list's contents. */
053: public String[] getContents(JList list) {
054: ListModel model = list.getModel();
055: String[] values = new String[model.getSize()];
056: for (int i = 0; i < values.length; i++) {
057: values[i] = model.getElementAt(i).toString();
058: }
059: return values;
060: }
061:
062: /** Select the given index.
063: Equivalent to actionSelectRow(c, new JListLocation(index), delay).
064: */
065: public void actionSelectIndex(Component c, int index, long delay) {
066: actionSelectRow(c, new JListLocation(index), delay);
067: }
068:
069: /** Select the given index.
070: Equivalent to actionSelectRow(c, new JListLocation(index)).
071: */
072: public void actionSelectIndex(Component c, int index) {
073: actionSelectRow(c, new JListLocation(index));
074: }
075:
076: /** Select the first item in the list matching the given String
077: representation of the item.<p>
078: Equivalent to actionSelectRow(c, new JListLocation(item), delay).
079: */
080: public void actionSelectItem(Component c, String item, long delay) {
081: actionSelectRow(c, new JListLocation(item), delay);
082: }
083:
084: /** Select the first item in the list matching the given String
085: representation of the item.<p>
086: Equivalent to actionSelectRow(c, new JListLocation(item)).
087: */
088: public void actionSelectItem(Component c, String item) {
089: actionSelectRow(c, new JListLocation(item));
090: }
091:
092: /** Select the first value in the list matching the given String
093: representation of the value.<p>
094: Equivalent to actionSelectRow(c, new JListLocation(value)).
095: */
096: public void actionSelectValue(Component c, String value) {
097: actionSelectRow(c, new JListLocation(value));
098: }
099:
100: /** Select the given row. Does nothing if the index is already
101: * selected.
102: */
103: public void actionSelectRow(Component c,
104: final JListLocation location) {
105: actionSelectRow(c, location, componentDelay);
106: }
107:
108: /** Select the given row. Does nothing if the index is already
109: * selected.
110: */
111: public void actionSelectRow(Component c,
112: final JListLocation location, long delay) {
113: final JList list = (JList) c;
114:
115: // Wait for the selected location to become avaliable
116: //
117:
118: wait(new Condition() {
119: public boolean test() {
120: int index = location.getIndex(list);
121: return index >= 0 && index < list.getModel().getSize();
122: }
123:
124: public String toString() {
125: return Strings.get("tester.Component.show_wait",
126: new Object[] { location.toString() });
127: }
128: }, delay);
129:
130: // Select the location
131: //
132:
133: int index = location.getIndex(list);
134: if (index < 0 || index >= list.getModel().getSize()) {
135: String msg = Strings.get("tester.JList.invalid_index",
136: new Object[] { new Integer(index) });
137: throw new ActionFailedException(msg);
138: }
139: if (list.getSelectedIndex() != index) {
140: Log.debug("Click on index=" + index);
141: super .actionClick(c, location);
142: }
143: }
144:
145: /** Parse the String representation of a JListLocation into the actual
146: JListLocation object.
147: */
148: public ComponentLocation parseLocation(String encoded) {
149: return new JListLocation().parse(encoded);
150: }
151:
152: /** Return the value, row, or coordinate location. */
153: public ComponentLocation getLocation(Component c, Point p) {
154: JList list = (JList) c;
155: int index = list.locationToIndex(p);
156: String value = valueToString(list, index);
157: if (value != null) {
158: return new JListLocation(value);
159: } else if (index != -1) {
160: return new JListLocation(index);
161: }
162: return new JListLocation(p);
163: }
164: }
|