KickJava   Java API By Example, From Geeks To Geeks.

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


1 package jimm.datavision.field;
2 import jimm.datavision.Section;
3 import jimm.datavision.Line;
4 import jimm.datavision.Point;
5 import jimm.datavision.Writeable;
6 import jimm.datavision.layout.LineDrawer;
7 import jimm.util.XMLWriter;
8 import java.awt.Color JavaDoc;
9 import java.util.Observer JavaDoc;
10 import java.util.Observable JavaDoc;
11
12 /**
13  * A border is a visual decoration around a report field. Each of its four
14  * edges (top, left, bottom, and right) is a {@link BorderEdge} and may be
15  * <code>null</code>.
16  *
17  * @author Jim Menard, <a HREF="mailto:jimm@io.com">jimm@io.com</a>
18  */

19 public class Border
20     extends Observable JavaDoc
21     implements Writeable, Cloneable JavaDoc, Observer JavaDoc
22 {
23
24 protected static final int BORDER_LINE_SPACE_MULT = 3;
25 protected static final Color JavaDoc DEFAULT_COLOR = Color.black;
26
27 protected Field field;
28 protected BorderEdge top, left, bottom, right; // May be null
29
protected Color JavaDoc color;
30
31 /**
32  * Constructs a new border for the specified field.
33  *
34  * @param field a report field
35  */

36 public Border(Field field) {
37     this.field = field;
38 }
39
40 /**
41  * Returns a clone of this border. All edges are cloned as well.
42  */

43 public Object JavaDoc clone() {
44     Border b = new Border(field);
45     BorderEdge e;
46
47     if ((e = getTop()) == null || e.getNumber() == 0)
48     b.setTop(null);
49     else
50     b.setTop((BorderEdge)e.clone());
51
52     if ((e = getBottom()) == null || e.getNumber() == 0)
53     b.setBottom(null);
54     else
55     b.setBottom((BorderEdge)e.clone());
56
57     if ((e = getLeft()) == null || e.getNumber() == 0)
58     b.setLeft(null);
59     else
60     b.setLeft((BorderEdge)e.clone());
61
62     if ((e = getRight()) == null || e.getNumber() == 0)
63     b.setRight(null);
64     else
65     b.setRight((BorderEdge)e.clone());
66
67     b.setColor(getColor());
68
69     return b;
70 }
71
72 /**
73  * For testing only. Checks color and edges but not field.
74  *
75  * @see jimm.datavision.test.ReportTest#testCloning
76  */

77 public boolean equals(Object JavaDoc obj) {
78     if (obj == null || !(obj instanceof Border)) return false;
79     if (obj == this) return true;
80
81     Border b = (Border)obj;
82     if (color == null && b.color != null) return false;
83     if (color != null && !color.equals(b.color)) return false;
84
85     BorderEdge e, e2;
86     int n, n2;
87     e = top;
88     e2 = b.top;
89     n = e == null ? 0 : e.number;
90     n2 = e2 == null ? 0 : e2.number;
91     if (n != n2) return false;
92
93     e = left;
94     e2 = b.left;
95     n = e == null ? 0 : e.number;
96     n2 = e2 == null ? 0 : e2.number;
97     if (n != n2) return false;
98
99     e = bottom;
100     e2 = b.bottom;
101     n = e == null ? 0 : e.number;
102     n2 = e2 == null ? 0 : e2.number;
103     if (n != n2) return false;
104
105     e = right;
106     e2 = b.right;
107     n = e == null ? 0 : e.number;
108     n2 = e2 == null ? 0 : e2.number;
109     if (n != n2) return false;
110
111     return true;
112 }
113
114 public int hashCode() {
115     int hc = 0;
116     if (color != null) hc += color.hashCode();
117     if (top != null) hc += top.hashCode();
118     if (left != null) hc += left.hashCode();
119     if (bottom != null) hc += bottom.hashCode();
120     if (right != null) hc += right.hashCode();
121     return hc;
122 }
123
124 /**
125  * Used only when cloning a field, this sets our field. Otherwise, don't
126  * call this.
127  *
128  * @param f the new field
129  */

130 public void setField(Field f) { field = f; }
131
132 protected void finalize() throws Throwable JavaDoc {
133     if (top != null) top.deleteObserver(this);
134     if (left != null) left.deleteObserver(this);
135     if (bottom != null) bottom.deleteObserver(this);
136     if (right != null) right.deleteObserver(this);
137 }
138
139 public void update(Observable JavaDoc o, Object JavaDoc arg) {
140     setChanged();
141     notifyObservers(arg);
142 }
143
144 /**
145  * Returns the border's top edge. May return <code>null</code>.
146  *
147  * @return the top edge
148  */

149 public BorderEdge getTop() { return top; }
150
151 /**
152  * Sets the top edge. <i>newTop</i> may be <code>null</code>.
153  *
154  * @param newTop the new edge
155  */

156 public void setTop(BorderEdge newTop) {
157     if (top != newTop) {
158     if (top != null) top.deleteObserver(this);
159     top = newTop;
160     if (top != null) top.addObserver(this);
161     setChanged();
162     notifyObservers();
163     }
164 }
165
166 /**
167  * Returns the border's left edge. May return <code>null</code>.
168  *
169  * @return the left edge
170  */

171 public BorderEdge getLeft() { return left; }
172
173 /**
174  * Sets the left edge. <i>newLeft</i> may be <code>null</code>.
175  *
176  * @param newLeft the new edge
177  */

178 public void setLeft(BorderEdge newLeft) {
179     if (left != newLeft) {
180     if (left != null) left.deleteObserver(this);
181     left = newLeft;
182     if (left != null) left.addObserver(this);
183     setChanged();
184     notifyObservers();
185     }
186 }
187
188 /**
189  * Returns the border's bottom edge. May return <code>null</code>.
190  *
191  * @return the bottom edge
192  */

193 public BorderEdge getBottom() { return bottom; }
194
195 /**
196  * Sets the bottom edge. <i>newBottom</i> may be <code>null</code>.
197  *
198  * @param newBottom the new edge
199  */

200 public void setBottom(BorderEdge newBottom) {
201     if (bottom != newBottom) {
202     if (bottom != null) bottom.deleteObserver(this);
203     bottom = newBottom;
204     if (bottom != null) bottom.addObserver(this);
205     setChanged();
206     notifyObservers();
207     }
208 }
209
210 /**
211  * Returns the border's right edge. May return <code>null</code>.
212  *
213  * @return the right edge
214  */

215 public BorderEdge getRight() { return right; }
216
217 /**
218  * Sets the right edge. <i>newRight</i> may be <code>null</code>.
219  *
220  * @param newRight the new edge
221  */

222 public void setRight(BorderEdge newRight) {
223     if (right != newRight) {
224     if (right != null) right.deleteObserver(this);
225     right = newRight;
226     if (right != null) right.addObserver(this);
227     setChanged();
228     notifyObservers();
229     }
230 }
231
232 /**
233  * Retrieves the border color.
234  *
235  * @return the border color
236  */

237 public Color JavaDoc getColor() { return color; }
238
239 /**
240  * Sets the border color.
241  *
242  * @param c the new color
243  */

244 public void setColor(Color JavaDoc c) {
245     if (color != c) {
246     color = c;
247     setChanged();
248     notifyObservers();
249     }
250 }
251
252 /**
253  * For each edge, hand the lines that make up that edge to the specified
254  * line drawer's <code>drawLine</code> method.
255  *
256  * @param ld a line drawer
257  */

258 public void eachLine(LineDrawer ld, Object JavaDoc arg) {
259     Rectangle rect = field.getBounds();
260     Section section = field.getSection();
261     BorderEdge edge;
262     Line line;
263
264     edge = top;
265     if (edge != null && edge.getThickness() > 0 && edge.getNumber() > 0) {
266     line = new Line(null, section, edge.getThickness(),
267             null, true, new Point(rect.x, rect.y),
268             new Point(rect.x + rect.width, rect.y));
269     for (int i = 0; i < edge.getNumber(); ++i) {
270         ld.drawLine(line, arg);
271         line.getPoint(0).y += BORDER_LINE_SPACE_MULT;
272         line.getPoint(1).y += BORDER_LINE_SPACE_MULT;
273     }
274     }
275
276     edge = bottom;
277     if (edge != null && edge.getThickness() > 0 && edge.getNumber() > 0) {
278     line = new Line(null, section, edge.getThickness(),
279             null, true, new Point(rect.x, rect.y + rect.height),
280             new Point(rect.x + rect.width, rect.y + rect.height));
281     for (int i = 0; i < edge.getNumber(); ++i) {
282         ld.drawLine(line, arg);
283         line.getPoint(0).y -= BORDER_LINE_SPACE_MULT;
284         line.getPoint(1).y -= BORDER_LINE_SPACE_MULT;
285     }
286     }
287
288     edge = left;
289     if (edge != null && edge.getThickness() > 0 && edge.getNumber() > 0) {
290     line = new Line(null, section, edge.getThickness(),
291             null, true, new Point(rect.x, rect.y),
292             new Point(rect.x, rect.y + rect.height));
293     for (int i = 0; i < edge.getNumber(); ++i) {
294         ld.drawLine(line, arg);
295         line.getPoint(0).x += BORDER_LINE_SPACE_MULT;
296         line.getPoint(1).x += BORDER_LINE_SPACE_MULT;
297     }
298     }
299
300     edge = right;
301     if (edge != null && edge.getThickness() > 0 && edge.getNumber() > 0) {
302     line = new Line(null, section, edge.getThickness(),
303             null, true, new Point(rect.x + rect.width, rect.y),
304             new Point(rect.x + rect.width, rect.y + rect.height));
305     for (int i = 0; i < edge.getNumber(); ++i) {
306         ld.drawLine(line, arg);
307         line.getPoint(0).x -= BORDER_LINE_SPACE_MULT;
308         line.getPoint(1).x -= BORDER_LINE_SPACE_MULT;
309     }
310     }
311 }
312         
313 /**
314  * Returns <code>true</code> if this border's edges are all
315  * <code>null</code> or have zero count or width.
316  *
317  * @return <code>true</code> if this border's edges are all
318  * <code>null</code> or have zero count or width
319  */

320 public boolean isEmpty() {
321     return isEmptyEdge(top) && isEmptyEdge(bottom) && isEmptyEdge(left)
322     && isEmptyEdge(right);
323 }
324
325 /**
326  * Returns <code>true</code> if <var>edge</var> is <code>null</code> or has
327  * zero count or width.
328  *
329  * @return <code>true</code> if <var>edge</var> is <code>null</code> or has
330  * zero count or width
331  */

332 protected boolean isEmptyEdge(BorderEdge edge) {
333     return edge == null || edge.getNumber() == 0 || edge.getThickness() == 0;
334 }
335
336 /**
337  * Writes this border as an XML tag. Asks each edge to write itself as well.
338  * If {@link #isEmpty} returns <code>true</code>, returns without writing
339  * anything.
340  *
341  * @param out a writer that knows how to write XML
342  */

343 public void writeXML(XMLWriter out) {
344     if (isEmpty())
345     return;
346
347     out.startElement("border");
348     if (color != null && !color.equals(DEFAULT_COLOR))
349     out.attr("color", color);
350
351     if (top != null && top.getNumber() != 0) top.writeXML(out, "top");
352     if (bottom != null && bottom.getNumber() != 0)
353     bottom.writeXML(out, "bottom");
354     if (left != null && left.getNumber() != 0) left.writeXML(out, "left");
355     if (right != null && right.getNumber() != 0) right.writeXML(out, "right");
356
357     out.endElement();
358 }
359
360 }
361
362
Popular Tags