001: package jimm.datavision;
002:
003: import jimm.util.XMLWriter;
004: import java.awt.Color;
005:
006: /**
007: * A line is a visual report element. Lines are used in field {@link
008: * jimm.datavision.field.Border}s and independently.
009: * <p>
010: * Note that currently, line thickness is ignored in the Java GUI (but not
011: * in layout engines such as the LaTeXLE).
012: *
013: * @author Jim Menard, <a href="mailto:jimm@io.com">jimm@io.com</a>
014: */
015: public class Line extends Element {
016:
017: protected static final Color DEFAULT_COLOR = Color.black;
018:
019: protected double thickness;
020: protected Point[] points;
021: protected Color color;
022:
023: /**
024: * Constructor.
025: *
026: * @param report the report containing this line
027: * @param section the section containing this line
028: * @param thickness the line thickness
029: * @param color may be <code>null</code>
030: * @param visible show/hide flag
031: */
032: public Line(Report report, Section section, double thickness,
033: Color color, boolean visible) {
034: this (report, section, thickness, color, visible, null, null);
035: }
036:
037: /**
038: * Constructor.
039: *
040: * @param report the report containing this line
041: * @param section the section containing this line
042: * @param thickness the line thickness
043: * @param color may be <code>null</code>
044: * @param visible show/hide flag
045: * @param p0 one end point of the line
046: * @param p1 the other end point of the line
047: */
048: public Line(Report report, Section section, double thickness,
049: Color color, boolean visible, Point p0, Point p1) {
050: super (report, section, visible);
051: this .thickness = thickness;
052: points = new Point[2];
053: points[0] = p0;
054: points[1] = p1;
055: this .color = color == null ? DEFAULT_COLOR : color;
056: }
057:
058: /**
059: * Adds an end point to the line. Used when constructing a line from XML,
060: * where we don't see the point until after creating this line.
061: *
062: * @param x the x coordinate
063: * @param y the y coordinate
064: */
065: public void addEndPoint(double x, double y) {
066: Point p = new Point(x, y);
067: if (points[0] == null)
068: points[0] = p;
069: else
070: points[1] = p;
071: p.addObserver(this );
072: }
073:
074: /**
075: * Returns the line thickness.
076: *
077: * @return the line thickness
078: */
079: public double getThickness() {
080: return thickness;
081: }
082:
083: /**
084: * Sets the line thickness.
085: *
086: * @param newThickness the new line thickness
087: */
088: public void setThickness(double newThickness) {
089: if (thickness != newThickness) {
090: thickness = newThickness;
091: setChanged();
092: notifyObservers();
093: }
094: }
095:
096: /**
097: * Returns one of the two end points of the line.
098: *
099: * @param index either 0 or 1
100: * @return a point
101: */
102: public Point getPoint(int index) {
103: return points[index];
104: }
105:
106: /**
107: * Sets one of the two end points.
108: *
109: * @param newPoint a point
110: * @param index either 0 or 1
111: */
112: public void setPoint(Point newPoint, int index) {
113: if (points[index] != newPoint) {
114: if (points[index] != null)
115: points[index].deleteObserver(this );
116: points[index] = newPoint;
117: if (points[index] != null)
118: points[index].addObserver(this );
119: setChanged();
120: notifyObservers();
121: }
122: }
123:
124: /**
125: * Returns the length of the line.
126: *
127: * @return the distance between the two end points
128: */
129: public double length() {
130: return points[0].distanceTo(points[1]);
131: }
132:
133: /**
134: * Returns a string representation of this line.
135: *
136: * @return a string representing this line
137: */
138: public String toString() {
139: return "(" + points[0] + ", " + points[1] + ")";
140: }
141:
142: /**
143: * Returns the line's color. The return value will never be
144: * <code>null</code>.
145: *
146: * @return the line's color
147: */
148: public Color getColor() {
149: return color == null ? DEFAULT_COLOR : color;
150: }
151:
152: /**
153: * Sets the line's color. If <var>c</var> is <code>null</code>, then the
154: * color is set to <code>DEFAULT_COLOR</code>.
155: *
156: * @param c new line color; if <code>null</code>, color is set to
157: * <code>DEFAULT_COLOR</code>
158: */
159: public void setColor(Color c) {
160: color = c == null ? DEFAULT_COLOR : c;
161: }
162:
163: /**
164: * Writes this line as an XML tag.
165: *
166: * @param out a writer that knows how to write XML
167: */
168: public void writeXML(XMLWriter out) {
169: out.startElement("line");
170: out.attr("thickness", thickness);
171: if (color != null && !color.equals(DEFAULT_COLOR))
172: out.attr("color", color);
173: if (!visible)
174: out.attr("visible", visible);
175: points[0].writeXML(out);
176: points[1].writeXML(out);
177: out.endElement();
178: }
179:
180: }
|