001: package abbot.tester;
002:
003: import java.awt.*;
004: import java.util.StringTokenizer;
005:
006: import javax.swing.JTable;
007:
008: import abbot.i18n.Strings;
009: import abbot.util.ExtendedComparator;
010:
011: /** Provides encapsulation of a location on a JTable (notably a row).
012: Use the JTableLocation#JTableLocation(Point) ctor to indicate a specific
013: coordinate.
014: */
015:
016: public class JTableLocation extends ComponentLocation {
017: public static class Cell {
018: public int row;
019: public int col;
020:
021: public Cell(int row, int col) {
022: this .row = row;
023: this .col = col;
024: }
025:
026: public boolean equals(Object o) {
027: return (o instanceof Cell) && ((Cell) o).row == row
028: && ((Cell) o).col == col;
029: }
030:
031: public String toString() {
032: return "[" + row + "," + col + "]";
033: }
034: }
035:
036: protected String value = null;
037: protected Cell cell = null;
038:
039: public JTableLocation() {
040: }
041:
042: public JTableLocation(String value) {
043: this .value = value;
044: }
045:
046: public JTableLocation(int row, int col) {
047: if (row < 0 || col < 0) {
048: String msg = Strings
049: .get("tester.JTable.invalid_cell", new Object[] {
050: new Integer(row), new Integer(col) });
051: throw new LocationUnavailableException(msg);
052: }
053: cell = new Cell(row, col);
054: }
055:
056: public JTableLocation(Point p) {
057: super (p);
058: }
059:
060: protected String badFormat(String encoded) {
061: return Strings.get("location.table.bad_format",
062: new Object[] { encoded });
063: }
064:
065: /** Convert the given row, col into a coordinate pair. */
066: protected Point cellToPoint(JTable table, int row, int col) {
067: if (row < 0 || row >= table.getRowCount() || col < 0
068: || col >= table.getColumnCount()) {
069: String msg = Strings
070: .get("tester.JTable.invalid_cell", new Object[] {
071: new Integer(row), new Integer(col) });
072: throw new LocationUnavailableException(msg);
073: }
074: Rectangle rect = getCellBounds(table, row, col);
075: return new Point(rect.x + rect.width / 2, rect.y + rect.height
076: / 2);
077: }
078:
079: protected Rectangle getCellBounds(JTable table, int row, int col) {
080: return table.getCellRect(row, col, false);
081: }
082:
083: /** Return the row, col of the first object matching the given String. */
084: private Cell valueToCell(JTable table, String value) {
085: for (int row = 0; row < table.getRowCount(); row++) {
086: for (int col = 0; col < table.getColumnCount(); col++) {
087: String str = JTableTester
088: .valueToString(table, row, col);
089: if (ExtendedComparator.stringsMatch(value, str)) {
090: return new JTableLocation.Cell(row, col);
091: }
092: }
093: }
094: String msg = Strings.get("tester.JTable.invalid_value",
095: new Object[] { value });
096: throw new LocationUnavailableException(msg);
097: }
098:
099: public Point getPoint(Component c) {
100: JTable table = (JTable) c;
101: if (value != null) {
102: Cell tmp = valueToCell(table, value);
103: return cellToPoint(table, tmp.row, tmp.col);
104: }
105: if (cell != null) {
106: return cellToPoint(table, cell.row, cell.col);
107: }
108: return super .getPoint(table);
109: }
110:
111: public Cell getCell(JTable table) {
112: if (value != null)
113: return valueToCell(table, value);
114: if (cell != null)
115: return new Cell(cell.row, cell.col);
116: Point where = super .getPoint(table);
117: return new Cell(table.rowAtPoint(where), table
118: .columnAtPoint(where));
119: }
120:
121: public Rectangle getBounds(Component c) {
122: JTable table = (JTable) c;
123: Cell cell = getCell(table);
124: if (cell == null) {
125: String msg = Strings.get("tester.JTable.invalid_cell",
126: new Object[] { new Integer(cell.row),
127: new Integer(cell.col), });
128: throw new LocationUnavailableException(msg);
129: }
130: return getCellBounds(table, cell.row, cell.col);
131: }
132:
133: public boolean equals(Object o) {
134: if (o instanceof JTableLocation) {
135: JTableLocation loc = (JTableLocation) o;
136: if (value != null)
137: return value.equals(loc.value);
138: if (cell != null)
139: return cell.equals(loc.cell);
140: }
141: return super .equals(o);
142: }
143:
144: public String toString() {
145: if (value != null)
146: return encodeValue(value);
147: if (cell != null)
148: return cell.toString();
149: return super .toString();
150: }
151:
152: public ComponentLocation parse(String encoded) {
153: encoded = encoded.trim();
154: if (isValue(encoded)) {
155: value = parseValue(encoded);
156: return this ;
157: } else if (isIndex(encoded)) {
158: String num = encoded.substring(1, encoded.length() - 1)
159: .trim();
160: StringTokenizer st = new StringTokenizer(num, ",");
161: try {
162: int r = Integer.parseInt(st.nextToken().trim());
163: int c = Integer.parseInt(st.nextToken().trim());
164: cell = new Cell(r, c);
165: return this ;
166: } catch (NumberFormatException e) {
167: throw new IllegalArgumentException(badFormat(encoded));
168: }
169: }
170: return super.parse(encoded);
171: }
172: }
|