001: package abbot.tester.extensions;
002:
003: import java.awt.*;
004: import java.awt.geom.*;
005:
006: import org.jgraph.JGraph;
007:
008: import abbot.Log;
009: import abbot.tester.*;
010: import abbot.i18n.Strings;
011: import abbot.util.ExtendedComparator;
012:
013: /** Provides encapsulation of the location of vertices and edges in a
014: {@link JGraph} (a coordinate or item index).
015: This class provides an example of a {@link ComponentLocation} extension.
016: Note that all bounds information must be converted to screen coordinates
017: with {@link JGraph#toScreen(Point2D)} or
018: {@link JGraph#toScreen(Rectangle2D)} before use in actions.
019: */
020: // TODO: encode cell "values" (view.toString?)
021: public class JGraphLocation extends ComponentLocation {
022: private int index = -1;
023:
024: public JGraphLocation() {
025: }
026:
027: public JGraphLocation(int index) {
028: if (index < 0) {
029: String msg = Strings.get("tester.JGraph.invalid_index",
030: new Object[] { new Integer(index) });
031: throw new LocationUnavailableException(msg);
032: }
033: this .index = index;
034: }
035:
036: public JGraphLocation(Point where) {
037: super (where);
038: }
039:
040: protected String badFormat(String encoded) {
041: return Strings.get("location.graph.bad_format",
042: new Object[] { encoded });
043: }
044:
045: private void checkIndex(JGraph graph, int index) {
046: if (index < 0 || index >= graph.getRoots().length) {
047: String msg = Strings.get("tester.JGraph.invalid_index",
048: new Object[] { new Integer(index) });
049: throw new LocationUnavailableException(msg);
050: }
051: }
052:
053: /** Return the bounds of the cell in screen coordinates. */
054: protected Rectangle getCellBounds(JGraph graph, Object o) {
055: Rectangle2D rect = graph.getCellBounds(o);
056: if (rect != null) {
057: return toScreen(graph, (Rectangle2D) rect.clone());
058: }
059: String msg = Strings.get("tester.JGraph.cell_not_found",
060: new Object[] { o });
061: throw new LocationUnavailableException(msg);
062: }
063:
064: /** Convert the given object index into a screen coordinate. */
065: protected Point indexToPoint(JGraph graph, int index) {
066: checkIndex(graph, index);
067: Rectangle rect = getCellBounds(graph, graph.getRoots()[index]);
068:
069: // FIXME this is the center of the bounding box; should use the center
070: // of the CellView/Renderer instead
071: return new Point(rect.x + rect.width / 2, rect.y + rect.height
072: / 2);
073: }
074:
075: /** Return a concrete point for the abstract location. */
076: public Point getPoint(Component c) {
077: if (index != -1)
078: return indexToPoint((JGraph) c, index);
079: return super .getPoint(c);
080: }
081:
082: /** Return the bounds of this location. If the location refers to a cell,
083: returns the cell's bounding box.
084: */
085: public Rectangle getBounds(Component c) {
086: JGraph graph = (JGraph) c;
087: if (index != -1) {
088: checkIndex(graph, index);
089: return getCellBounds(graph, graph.getRoots()[index]);
090: }
091: return super .getBounds(c);
092: }
093:
094: public boolean equals(Object o) {
095: if (o instanceof JGraphLocation) {
096: JGraphLocation loc = (JGraphLocation) o;
097: if (index != -1)
098: return index == loc.index;
099: }
100: return super .equals(o);
101: }
102:
103: public String toString() {
104: if (index != -1)
105: return encodeIndex(index);
106: return super .toString();
107: }
108:
109: public ComponentLocation parse(String encoded) {
110: encoded = encoded.trim();
111: if (isIndex(encoded)) {
112: index = parseIndex(encoded);
113: return this ;
114: }
115: return super .parse(encoded);
116: }
117:
118: protected Point toScreen(JGraph graph, int x, int y) {
119: Point2D p = graph.toScreen(new Point(x, y));
120: return new Point((int) p.getX(), (int) p.getY());
121: }
122:
123: protected Rectangle toScreen(JGraph graph, Rectangle2D rect) {
124: Rectangle2D r = graph.toScreen(rect);
125: return new Rectangle((int) r.getX(), (int) r.getY(), (int) r
126: .getWidth(), (int) r.getHeight());
127: }
128: }
|