001: package abbot.tester;
002:
003: import java.awt.*;
004:
005: import javax.swing.JList;
006:
007: import abbot.i18n.Strings;
008: import abbot.util.ExtendedComparator;
009:
010: /** Provides encapsulation of the location of a row on a JList (a coordinate,
011: * item index or value).
012: */
013:
014: public class JListLocation extends ComponentLocation {
015: private Object value;
016: private int row = -1;
017:
018: /** The center of the target list. */
019: public JListLocation() {
020: }
021:
022: /** Specify location by explicit reference to an object. */
023: public JListLocation(Object value) {
024: this .value = value;
025: }
026:
027: /** Specify location by string representation of an object.
028: * @see JListTester#valueToString
029: */
030: public JListLocation(String value) {
031: this ((Object) value);
032: }
033:
034: /** Specify location by row in the list. */
035: public JListLocation(int row) {
036: if (row < 0) {
037: String msg = Strings.get("tester.JList.invalid_index",
038: new Object[] { new Integer(row) });
039: throw new LocationUnavailableException(msg);
040: }
041: this .row = row;
042: }
043:
044: /** Specify a location by component-relative coordinate. */
045: public JListLocation(Point where) {
046: super (where);
047: }
048:
049: protected String badFormat(String encoded) {
050: return Strings.get("location.list.bad_format",
051: new Object[] { encoded });
052: }
053:
054: /** Convert the given index into a coordinate. */
055: protected Point indexToPoint(JList list, int index) {
056: if (index < 0 || index >= list.getModel().getSize()) {
057: String msg = Strings.get("tester.JList.invalid_index",
058: new Object[] { new Integer(index) });
059: throw new LocationUnavailableException(msg);
060: }
061: Rectangle rect = list.getCellBounds(index, index);
062: return new Point(rect.x + rect.width / 2, rect.y + rect.height
063: / 2);
064: }
065:
066: /** Find the first String match in the list and return the index. */
067: private int valueToIndex(JList list, Object value) {
068: int size = list.getModel().getSize();
069: if (value instanceof String) {
070: for (int i = 0; i < size; i++) {
071: String str = JListTester.valueToString(list, i);
072: if (ExtendedComparator
073: .stringsMatch((String) value, str)) {
074: return i;
075: }
076: }
077: } else {
078: for (int i = 0; i < size; i++) {
079: Object el = list.getModel().getElementAt(i);
080: if (el == null && value == null || el != null
081: && el.equals(value)) {
082: return i;
083: }
084: }
085: }
086: return -1;
087: }
088:
089: public int getIndex(JList list) {
090: if (value != null)
091: return valueToIndex(list, value);
092: if (row != -1) {
093: return row;
094: }
095: return list.locationToIndex(super .getPoint(list));
096: }
097:
098: /** Return a concrete point for the abstract location. */
099: public Point getPoint(Component c) {
100: JList list = (JList) c;
101: if (value != null || row != -1) {
102: int idx = getIndex(list);
103: if (idx == -1) {
104: throw new LocationUnavailableException(
105: invalidMessage(list));
106: }
107: return indexToPoint(list, idx);
108: }
109: return super .getPoint(list);
110: }
111:
112: private String invalidMessage(JList list) {
113: if (value != null) {
114: return Strings.get("tester.JList.item_not_found",
115: new Object[] { value });
116: }
117: if (row != -1) {
118: return Strings.get("tester.JList.invalid_index",
119: new Object[] { new Integer(row) });
120: }
121: return Strings.get("tester.JList.point_not_found",
122: new Object[] { super .getPoint(list) });
123: }
124:
125: public Rectangle getBounds(Component c) {
126: JList list = (JList) c;
127: int index = getIndex(list);
128: if (index == -1) {
129: throw new LocationUnavailableException(invalidMessage(list));
130: }
131: return list.getCellBounds(index, index);
132: }
133:
134: public boolean equals(Object o) {
135: if (o instanceof JListLocation) {
136: JListLocation loc = (JListLocation) o;
137: if (value != null)
138: return value.equals(loc.value);
139: if (row != -1)
140: return row == loc.row;
141: }
142: return super .equals(o);
143: }
144:
145: public String toString() {
146: if (value != null)
147: return encodeValue(value.toString());
148: if (row != -1)
149: return encodeIndex(row);
150: return super .toString();
151: }
152:
153: public ComponentLocation parse(String encoded) {
154: encoded = encoded.trim();
155: if (isValue(encoded)) {
156: value = parseValue(encoded);
157: return this;
158: }
159: if (isIndex(encoded)) {
160: row = parseIndex(encoded);
161: return this;
162: }
163: return super.parse(encoded);
164: }
165: }
|