001: package jimm.datavision.field;
002:
003: import jimm.util.XMLWriter;
004: import java.util.Observable;
005:
006: /**
007: * A <i>border edge</i> represents one of the four edges of a {@link Border}.
008: * It has its own line style, thickness, and number of lines. <p> Note: line
009: * thickness is currently ignored.
010: *
011: * @author Jim Menard, <a href="mailto:jimm@io.com">jimm@io.com</a>
012: */
013: public class BorderEdge extends Observable implements Cloneable {
014:
015: /** Draw simple lines. This is the default style. */
016: public static final int STYLE_LINE = 0;
017: /** Draw dashed lines. */
018: public static final int STYLE_DASH = 1;
019: /** Draw dotted lines. */
020: public static final int STYLE_DOT = 2;
021:
022: public static final int DEFAULT_STYLE = STYLE_LINE;
023: public static final int DEFAULT_NUMBER = 1;
024: public static final double DEFAULT_THICKNESS = 1.0;
025:
026: protected int style; // none, line, dash, dot
027: protected double thickness; // in points
028: protected int number;
029:
030: /**
031: * Returns the integer style constant associated with the specified string.
032: * The string must be <code>null</code>, "none", "line", "dash", or "dot"
033: * and comes from the XML files that describe reports. If the string is
034: * <code>null</code>, then we return the default value of STYLE_LINE;
035: *
036: * @param styleStr one of <code>null</code>, "none", "line", "dash", or "dot"
037: * @return one of the <code>STYLE_*</code> constants; <code>STYLE_LINE</code>
038: * is returned if styleStr is <code>null</code>
039: */
040: public static int styleFromString(String styleStr) {
041: if (styleStr == null || styleStr.length() == 0) // Default
042: return DEFAULT_STYLE;
043:
044: styleStr = styleStr.toLowerCase();
045: int style = DEFAULT_STYLE;
046: if (styleStr.equals("line"))
047: style = STYLE_LINE;
048: else if (styleStr.equals("dash"))
049: style = STYLE_DASH;
050: else if (styleStr.equals("dot"))
051: style = STYLE_DOT;
052:
053: return style;
054: }
055:
056: /**
057: * Creates a new edge with <code>DEFAULT_NUMBER</code> lines of
058: * <code>DEFAULT_STYLE</code> and <code>DEFAULT_THICKNESS</code>.
059: */
060: public BorderEdge() {
061: this (DEFAULT_STYLE, DEFAULT_THICKNESS, DEFAULT_NUMBER);
062: }
063:
064: /**
065: * Creates a new edge with <code>DEFAULT_NUMBER</code> lines of
066: * the specified style and <code>DEFAULT_THICKNESS</code>.
067: *
068: * @param style one of the <code>STYLE_</code> constants
069: */
070: public BorderEdge(int style) {
071: this (style, DEFAULT_THICKNESS, DEFAULT_NUMBER);
072: }
073:
074: /**
075: * Creates a new edge with <code>DEFAULT_NUMBER</code> lines of
076: * the specified style and thickness.
077: *
078: * @param style one of the <code>STYLE_</code> constants
079: * @param thickness line thickness
080: */
081: public BorderEdge(int style, double thickness) {
082: this (style, thickness, DEFAULT_NUMBER);
083: }
084:
085: /**
086: * Creates a new edge with <var>number</var> lines of the specified style
087: * and thickness.
088: *
089: * @param style one of the <code>STYLE_</code> constants
090: * @param thickness line thickness
091: * @param number the number of liens to draw
092: */
093: public BorderEdge(int style, double thickness, int number) {
094: this .style = style;
095: this .thickness = thickness;
096: this .number = number;
097: }
098:
099: /**
100: * Returns a clone of this border. All edges are cloned as well.
101: */
102: public Object clone() {
103: BorderEdge be = new BorderEdge(style, thickness, number);
104: return be;
105: }
106:
107: public boolean equals(Object obj) {
108: if (obj == null || !(obj instanceof BorderEdge))
109: return false;
110: if (obj == this )
111: return true;
112:
113: BorderEdge be = (BorderEdge) obj;
114: return number == be.number && thickness == be.thickness
115: && style == be.style;
116: }
117:
118: public int hashCode() {
119: return style + number * 10 + (int) (thickness * 1000);
120: }
121:
122: /**
123: * Returns the edge's style.
124: *
125: * @return one of the <code>STYLE_</code> constants
126: */
127: public int getStyle() {
128: return style;
129: }
130:
131: /**
132: * Sets the edge's style.
133: *
134: * @param newStyle one of the <code>STYLE_</code> constants
135: */
136: public void setStyle(int newStyle) {
137: if (style != newStyle) {
138: style = newStyle;
139: setChanged();
140: notifyObservers();
141: }
142: }
143:
144: /**
145: * Returns the edge's thickness.
146: *
147: * @return the thickness
148: */
149: public double getThickness() {
150: return thickness;
151: }
152:
153: /**
154: * Sets the edge's thickness.
155: *
156: * @param newThickness line thickness
157: */
158: public void setThickness(double newThickness) {
159: if (thickness != newThickness) {
160: thickness = newThickness;
161: setChanged();
162: notifyObservers();
163: }
164: }
165:
166: /**
167: * Returns the number of lines to draw along this border edge.
168: *
169: * @return the number of lines to draw along this border edge
170: */
171: public int getNumber() {
172: return number;
173: }
174:
175: /**
176: * Sets the number of lines to draw along this border edge.
177: *
178: * @param newNumber the number of lines to draw
179: */
180: public void setNumber(int newNumber) {
181: if (number != newNumber) {
182: number = newNumber;
183: setChanged();
184: notifyObservers();
185: }
186: }
187:
188: /**
189: * Writes this edge as an XML tag.
190: *
191: * @param out a writer that knows how to write XML
192: * @param location from the border; the string "top", "bottom", "left", or
193: * "right"
194: */
195: public void writeXML(XMLWriter out, String location) {
196: String styleStr = null;
197: if (style != DEFAULT_STYLE) {
198: switch (style) {
199: case STYLE_LINE:
200: styleStr = "line";
201: break;
202: case STYLE_DASH:
203: styleStr = "dash";
204: break;
205: case STYLE_DOT:
206: styleStr = "dot";
207: break;
208: }
209: }
210:
211: out.startElement("edge");
212: out.attr("location", location);
213: if (number != DEFAULT_NUMBER)
214: out.attr("number", number);
215: if (thickness != DEFAULT_THICKNESS)
216: out.attr("thickness", thickness);
217: if (styleStr != null)
218: out.attr("style", styleStr);
219: out.endElement();
220: }
221:
222: public String toString() {
223: return "BorderEdge[style=" + style + ", thickness=" + thickness
224: + ", number=" + number + "]";
225: }
226:
227: }
|