KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jimm > datavision > field > BorderEdge


1 package jimm.datavision.field;
2 import jimm.util.XMLWriter;
3 import java.util.Observable JavaDoc;
4
5 /**
6  * A <i>border edge</i> represents one of the four edges of a {@link Border}.
7  * It has its own line style, thickness, and number of lines. <p> Note: line
8  * thickness is currently ignored.
9  *
10  * @author Jim Menard, <a HREF="mailto:jimm@io.com">jimm@io.com</a>
11  */

12 public class BorderEdge extends Observable JavaDoc implements Cloneable JavaDoc {
13
14 /** Draw simple lines. This is the default style. */
15 public static final int STYLE_LINE = 0;
16 /** Draw dashed lines. */
17 public static final int STYLE_DASH = 1;
18 /** Draw dotted lines. */
19 public static final int STYLE_DOT = 2;
20
21 public static final int DEFAULT_STYLE = STYLE_LINE;
22 public static final int DEFAULT_NUMBER = 1;
23 public static final double DEFAULT_THICKNESS = 1.0;
24
25 protected int style; // none, line, dash, dot
26
protected double thickness; // in points
27
protected int number;
28
29 /**
30  * Returns the integer style constant associated with the specified string.
31  * The string must be <code>null</code>, "none", "line", "dash", or "dot"
32  * and comes from the XML files that describe reports. If the string is
33  * <code>null</code>, then we return the default value of STYLE_LINE;
34  *
35  * @param styleStr one of <code>null</code>, "none", "line", "dash", or "dot"
36  * @return one of the <code>STYLE_*</code> constants; <code>STYLE_LINE</code>
37  * is returned if styleStr is <code>null</code>
38  */

39 public static int styleFromString(String JavaDoc styleStr) {
40     if (styleStr == null || styleStr.length() == 0) // Default
41
return DEFAULT_STYLE;
42
43     styleStr = styleStr.toLowerCase();
44     int style = DEFAULT_STYLE;
45     if (styleStr.equals("line")) style = STYLE_LINE;
46     else if (styleStr.equals("dash")) style = STYLE_DASH;
47     else if (styleStr.equals("dot")) style = STYLE_DOT;
48
49     return style;
50 }
51
52 /**
53  * Creates a new edge with <code>DEFAULT_NUMBER</code> lines of
54  * <code>DEFAULT_STYLE</code> and <code>DEFAULT_THICKNESS</code>.
55  */

56 public BorderEdge() {
57     this(DEFAULT_STYLE, DEFAULT_THICKNESS, DEFAULT_NUMBER);
58 }
59
60 /**
61  * Creates a new edge with <code>DEFAULT_NUMBER</code> lines of
62  * the specified style and <code>DEFAULT_THICKNESS</code>.
63  *
64  * @param style one of the <code>STYLE_</code> constants
65  */

66 public BorderEdge(int style) {
67     this(style, DEFAULT_THICKNESS, DEFAULT_NUMBER);
68 }
69
70 /**
71  * Creates a new edge with <code>DEFAULT_NUMBER</code> lines of
72  * the specified style and thickness.
73  *
74  * @param style one of the <code>STYLE_</code> constants
75  * @param thickness line thickness
76  */

77 public BorderEdge(int style, double thickness) {
78     this(style, thickness, DEFAULT_NUMBER);
79 }
80
81 /**
82  * Creates a new edge with <var>number</var> lines of the specified style
83  * and thickness.
84  *
85  * @param style one of the <code>STYLE_</code> constants
86  * @param thickness line thickness
87  * @param number the number of liens to draw
88  */

89 public BorderEdge(int style, double thickness, int number) {
90     this.style = style;
91     this.thickness = thickness;
92     this.number = number;
93 }
94
95 /**
96  * Returns a clone of this border. All edges are cloned as well.
97  */

98 public Object JavaDoc clone() {
99     BorderEdge be = new BorderEdge(style, thickness, number);
100     return be;
101 }
102
103 public boolean equals(Object JavaDoc obj) {
104     if (obj == null || !(obj instanceof BorderEdge)) return false;
105     if (obj == this) return true;
106
107     BorderEdge be = (BorderEdge)obj;
108     return number == be.number
109     && thickness == be.thickness
110     && style == be.style;
111 }
112
113 public int hashCode() {
114     return style + number * 10 + (int)(thickness * 1000);
115 }
116
117 /**
118  * Returns the edge's style.
119  *
120  * @return one of the <code>STYLE_</code> constants
121  */

122 public int getStyle() { return style; }
123
124 /**
125  * Sets the edge's style.
126  *
127  * @param newStyle one of the <code>STYLE_</code> constants
128  */

129 public void setStyle(int newStyle) {
130     if (style != newStyle) {
131     style = newStyle;
132     setChanged();
133     notifyObservers();
134     }
135 }
136
137 /**
138  * Returns the edge's thickness.
139  *
140  * @return the thickness
141  */

142 public double getThickness() { return thickness; }
143
144 /**
145  * Sets the edge's thickness.
146  *
147  * @param newThickness line thickness
148  */

149 public void setThickness(double newThickness) {
150     if (thickness != newThickness) {
151     thickness = newThickness;
152     setChanged();
153     notifyObservers();
154     }
155 }
156
157 /**
158  * Returns the number of lines to draw along this border edge.
159  *
160  * @return the number of lines to draw along this border edge
161  */

162 public int getNumber() { return number; }
163
164 /**
165  * Sets the number of lines to draw along this border edge.
166  *
167  * @param newNumber the number of lines to draw
168  */

169 public void setNumber(int newNumber) {
170     if (number != newNumber) {
171     number = newNumber;
172     setChanged();
173     notifyObservers();
174     }
175 }
176
177 /**
178  * Writes this edge as an XML tag.
179  *
180  * @param out a writer that knows how to write XML
181  * @param location from the border; the string "top", "bottom", "left", or
182  * "right"
183  */

184 public void writeXML(XMLWriter out, String JavaDoc location) {
185     String JavaDoc styleStr = null;
186     if (style != DEFAULT_STYLE) {
187     switch (style) {
188     case STYLE_LINE: styleStr = "line"; break;
189     case STYLE_DASH: styleStr = "dash"; break;
190     case STYLE_DOT: styleStr = "dot"; break;
191     }
192     }
193
194     out.startElement("edge");
195     out.attr("location", location);
196     if (number != DEFAULT_NUMBER)
197     out.attr("number", number);
198     if (thickness != DEFAULT_THICKNESS)
199     out.attr("thickness", thickness);
200     if (styleStr != null)
201     out.attr("style", styleStr);
202     out.endElement();
203 }
204
205 public String JavaDoc toString() {
206     return "BorderEdge[style=" + style + ", thickness=" + thickness
207     + ", number=" + number + "]";
208 }
209
210 }
211
Popular Tags