001: package abbot.tester;
002:
003: import java.awt.*;
004:
005: import abbot.i18n.Strings;
006: import abbot.util.ExtendedComparator;
007:
008: /** Provides encapsulation of the location of a row on a List (a coordinate,
009: * item index or value).
010: */
011:
012: public class ListLocation extends ComponentLocation {
013: private String value = null;
014: private int row = -1;
015:
016: public ListLocation() {
017: }
018:
019: public ListLocation(String value) {
020: this .value = value;
021: }
022:
023: public ListLocation(int row) {
024: if (row < 0) {
025: String msg = Strings.get("tester.JList.invalid_index",
026: new Object[] { new Integer(row) });
027: throw new LocationUnavailableException(msg);
028: }
029: this .row = row;
030: }
031:
032: public ListLocation(Point where) {
033: super (where);
034: }
035:
036: protected String badFormat(String encoded) {
037: return Strings.get("location.list.bad_format",
038: new Object[] { encoded });
039: }
040:
041: /** Find the first String match in the list and return the index. */
042: private int valueToIndex(List list, String value) {
043: int size = list.getItemCount();
044: for (int i = 0; i < size; i++) {
045: if (ExtendedComparator.stringsMatch(value, list.getItem(i))) {
046: return i;
047: }
048: }
049: return -1;
050: }
051:
052: public int getIndex(List list) {
053: if (value != null)
054: return valueToIndex(list, value);
055: if (row != -1) {
056: return row;
057: }
058: throw new LocationUnavailableException(
059: "Can't derive an index from a Point on java.awt.List");
060: }
061:
062: /** Return a concrete point for the abstract location. */
063: public Point getPoint(Component c) {
064: List list = (List) c;
065: if (value != null || row != -1)
066: throw new LocationUnavailableException(
067: "Can't derive a Point from an index on java.awt.List");
068: return super .getPoint(list);
069: }
070:
071: public Rectangle getBounds(Component c) {
072: throw new LocationUnavailableException(
073: "Can't determine bounds on java.awt.List");
074: }
075:
076: public boolean equals(Object o) {
077: if (o instanceof ListLocation) {
078: ListLocation loc = (ListLocation) o;
079: if (value != null)
080: return value.equals(loc.value);
081: if (row != -1)
082: return row == loc.row;
083: }
084: return super .equals(o);
085: }
086:
087: public String toString() {
088: if (value != null)
089: return encodeValue(value);
090: if (row != -1)
091: return encodeIndex(row);
092: return super .toString();
093: }
094:
095: public ComponentLocation parse(String encoded) {
096: encoded = encoded.trim();
097: if (isValue(encoded)) {
098: value = parseValue(encoded);
099: return this;
100: } else if (isIndex(encoded)) {
101: row = parseIndex(encoded);
102: return this;
103: }
104: return super.parse(encoded);
105: }
106: }
|