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 ; 9 import java.util.Observer ; 10 import java.util.Observable ; 11 12 19 public class Border 20 extends Observable 21 implements Writeable, Cloneable , Observer 22 { 23 24 protected static final int BORDER_LINE_SPACE_MULT = 3; 25 protected static final Color DEFAULT_COLOR = Color.black; 26 27 protected Field field; 28 protected BorderEdge top, left, bottom, right; protected Color color; 30 31 36 public Border(Field field) { 37 this.field = field; 38 } 39 40 43 public Object 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 77 public boolean equals(Object 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 130 public void setField(Field f) { field = f; } 131 132 protected void finalize() throws Throwable { 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 o, Object arg) { 140 setChanged(); 141 notifyObservers(arg); 142 } 143 144 149 public BorderEdge getTop() { return top; } 150 151 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 171 public BorderEdge getLeft() { return left; } 172 173 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 193 public BorderEdge getBottom() { return bottom; } 194 195 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 215 public BorderEdge getRight() { return right; } 216 217 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 237 public Color getColor() { return color; } 238 239 244 public void setColor(Color c) { 245 if (color != c) { 246 color = c; 247 setChanged(); 248 notifyObservers(); 249 } 250 } 251 252 258 public void eachLine(LineDrawer ld, Object 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 320 public boolean isEmpty() { 321 return isEmptyEdge(top) && isEmptyEdge(bottom) && isEmptyEdge(left) 322 && isEmptyEdge(right); 323 } 324 325 332 protected boolean isEmptyEdge(BorderEdge edge) { 333 return edge == null || edge.getNumber() == 0 || edge.getThickness() == 0; 334 } 335 336 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 |