001: package abbot.tester;
002:
003: import java.awt.*;
004:
005: import javax.swing.table.JTableHeader;
006:
007: import abbot.i18n.Strings;
008: import abbot.util.ExtendedComparator;
009:
010: /** Provides encapsulation of the location of a col on a JTableHeader (a coordinate,
011: * item index or value).
012: */
013:
014: public class JTableHeaderLocation extends ComponentLocation {
015: private String value = null;
016: private int col = -1;
017:
018: public JTableHeaderLocation() {
019: }
020:
021: public JTableHeaderLocation(String value) {
022: this .value = value;
023: }
024:
025: public JTableHeaderLocation(int col) {
026: if (col < 0) {
027: String msg = Strings.get(
028: "tester.JTableHeader.invalid_index",
029: new Object[] { new Integer(col) });
030: throw new LocationUnavailableException(msg);
031: }
032: this .col = col;
033: }
034:
035: public JTableHeaderLocation(Point where) {
036: super (where);
037: }
038:
039: protected String badFormat(String encoded) {
040: return Strings.get("location.tableheader.bad_format",
041: new Object[] { encoded });
042: }
043:
044: /** Convert the given index into a coordinate. */
045: protected Point indexToPoint(JTableHeader header, int index) {
046: if (index < 0
047: || index >= header.getColumnModel().getColumnCount()) {
048: String msg = Strings.get(
049: "tester.JTableHeader.invalid_index",
050: new Object[] { new Integer(index) });
051: throw new LocationUnavailableException(msg);
052: }
053: Rectangle rect = header.getHeaderRect(index);
054: return new Point(rect.x + rect.width / 2, rect.y + rect.height
055: / 2);
056: }
057:
058: /** Find the first String match in the columns and return the index. */
059: private int valueToIndex(JTableHeader header, String value) {
060: int size = header.getColumnModel().getColumnCount();
061: for (int i = 0; i < size; i++) {
062: String str = header.getTable().getModel().getColumnName(i);
063: if (ExtendedComparator.stringsMatch(value, str)) {
064: return i;
065: }
066: }
067: return -1;
068: }
069:
070: public int getIndex(JTableHeader header) {
071: if (value != null)
072: return valueToIndex(header, value);
073: if (col != -1) {
074: return col;
075: }
076: return header.columnAtPoint(super .getPoint(header));
077: }
078:
079: /** Return a concrete point for the abstract location. */
080: public Point getPoint(Component c) {
081: JTableHeader header = (JTableHeader) c;
082: if (value != null || col != -1)
083: return indexToPoint(header, getIndex(header));
084: return super .getPoint(header);
085: }
086:
087: public Rectangle getBounds(Component c) {
088: JTableHeader header = (JTableHeader) c;
089: int index = getIndex(header);
090: if (index == -1) {
091: String msg = Strings.get(
092: "tester.JTableHeader.invalid_index",
093: new Object[] { new Integer(index) });
094: throw new LocationUnavailableException(msg);
095: }
096: return header.getHeaderRect(index);
097: }
098:
099: public boolean equals(Object o) {
100: if (o instanceof JTableHeaderLocation) {
101: JTableHeaderLocation loc = (JTableHeaderLocation) o;
102: if (value != null)
103: return value.equals(loc.value);
104: if (col != -1)
105: return col == loc.col;
106: }
107: return super .equals(o);
108: }
109:
110: public String toString() {
111: if (value != null)
112: return encodeValue(value);
113: if (col != -1)
114: return encodeIndex(col);
115: return super .toString();
116: }
117:
118: public ComponentLocation parse(String encoded) {
119: encoded = encoded.trim();
120: if (isValue(encoded)) {
121: value = parseValue(encoded);
122: return this;
123: } else if (isIndex(encoded)) {
124: col = parseIndex(encoded);
125: return this;
126: }
127: return super.parse(encoded);
128: }
129: }
|