1 package jimm.datavision.field; 2 import jimm.datavision.Writeable; 3 import jimm.util.XMLWriter; 4 import java.awt.Color ; 5 import java.awt.Font ; 6 import java.util.Observable ; 7 8 19 public class Format extends Observable implements Writeable, Cloneable { 20 21 protected static final String DEFAULT_FONT_FAMILY_NAME = "Times New Roman"; 22 23 protected static final int DEFAULT_SIZE = 11; 24 protected static final Color DEFAULT_COLOR = Color.black; 25 26 27 public static final int ALIGN_LEFT = 0; 28 29 public static final int ALIGN_CENTER = 1; 30 31 public static final int ALIGN_RIGHT = 2; 32 33 protected Field field; 34 protected String fontFamilyName; 35 protected Double size; protected Boolean bold; 37 protected Boolean italic; 38 protected Boolean underline; 39 protected Boolean wrap; 40 protected Integer align; protected String format; protected Color color; 43 protected Font font; 45 55 public static int alignFromString(String s) { 56 if (s == null) return ALIGN_LEFT; 57 String copy = s.toLowerCase(); 58 if ("center".equals(copy)) return ALIGN_CENTER; 59 if ("right".equals(copy)) return ALIGN_RIGHT; 60 return ALIGN_LEFT; 61 } 62 63 70 public static String alignToString(int align) { 71 switch (align) { 72 case ALIGN_RIGHT: return "right"; 73 case ALIGN_CENTER: return "center"; 74 default: return "left"; 75 } 76 } 77 78 public static Format createEmptyFormat() { 79 return new Format(); 80 } 81 82 public static Format createDefaultFormat() { 83 return new DefaultFormat(); 84 } 85 86 89 Format() {} 90 91 97 void setField(Field f) { field = f; } 98 99 102 public Object clone() { 103 Format f = new Format(); 104 fillClonedField(f); 105 return f; 106 } 107 108 protected void fillClonedField(Format f) { 109 f.field = field; 110 f.setFontFamilyName(getFontFamilyName()); 111 f.setSize(getSize()); 112 f.setBold(isBold()); 113 f.setItalic(isItalic()); 114 f.setUnderline(isUnderline()); 115 f.setWrap(isWrap()); 116 f.setAlign(getAlign()); 117 f.setFormat(getFormat()); 118 f.setColor(getColor()); 119 } 120 121 public boolean equals(Object obj) { 122 if (obj == null || !(obj instanceof Format)) 123 return false; 124 if (obj == this) 125 return true; 126 127 Format f = (Format)obj; 128 return getSize() == f.getSize() 131 && isBold() == f.isBold() 132 && isItalic() == f.isItalic() 133 && isUnderline() == f.isUnderline() 134 && isWrap() == f.isWrap() 135 && getAlign() == f.getAlign() 136 && (format == f.getFormat() 137 || (format != null && format.equals(f.getFormat()))) 138 && getColor().equals(f.getColor()); 139 } 140 141 public int hashCode() { 142 return (color == null ? 0 : color.hashCode()) 143 + getFormat().hashCode() 144 + (int)getSize() 145 + (isBold() ? 101 : 100) 146 + (isItalic() ? 501 : 500) 147 + (isUnderline() ? 1001 : 1000) 148 + (isWrap() ? 5001 : 5000) 149 + (getAlign() + 10000); 150 } 151 152 155 public Format getDefaultFormat() { 156 return field.getReport().getDefaultField().getFormat(); 157 } 158 159 164 public String getFontFamilyName() { 165 return fontFamilyName == null ? getDefaultFormat().getFontFamilyName() 166 : fontFamilyName; 167 } 168 169 174 public void setFontFamilyName(String newFontFamilyName) { 175 if (newFontFamilyName != null) { 176 newFontFamilyName = newFontFamilyName.trim(); 177 if (newFontFamilyName.length() == 0) 178 newFontFamilyName = null; 179 } 180 181 if (fontFamilyName != newFontFamilyName 182 && (fontFamilyName == null 183 || !fontFamilyName.equals(newFontFamilyName))) 184 { 185 fontFamilyName = newFontFamilyName; 186 font = null; 187 setChanged(); 188 notifyObservers(); 189 } 190 } 191 192 198 public Font getFont() { 199 if (font == null) { 200 String name = getFontFamilyName(); 201 202 int style = 0; 203 if (isBold()) 204 style = Font.BOLD; 205 if (isItalic()) 206 style |= Font.ITALIC; 207 if (style == 0) 208 style = Font.PLAIN; 209 210 font = new Font (name, style, (int)getSize()); 211 } 212 return font; 213 } 214 215 216 public void clearFontCache() { font = null; } 217 218 223 public double getSize() { 224 return size == null ? getDefaultFormat().getSize() : size.doubleValue(); 225 } 226 227 232 public void setSize(double newSize) { 233 Double newSizeObj = new Double (newSize); 234 if (size != newSizeObj && (size == null || !size.equals(newSizeObj))) { 235 size = newSizeObj; 236 setChanged(); 237 notifyObservers(); 238 } 239 } 240 241 246 public boolean isBold() { 247 return bold == null ? getDefaultFormat().isBold() : bold.booleanValue(); 248 } 249 250 255 public void setBold(boolean newBold) { 256 Boolean newBoldObj = newBold ? Boolean.TRUE : Boolean.FALSE; 257 if (bold != newBoldObj && (bold == null || !bold.equals(newBoldObj))) { 258 bold = newBoldObj; 259 setChanged(); 260 notifyObservers(); 261 } 262 } 263 264 269 public boolean isItalic() { 270 return italic == null ? getDefaultFormat().isItalic() 271 : italic.booleanValue(); 272 } 273 274 279 public void setItalic(boolean newItalic) { 280 Boolean newItalicObj = newItalic ? Boolean.TRUE : Boolean.FALSE; 281 if (italic != newItalicObj 282 && (italic == null || !italic.equals(newItalicObj))) 283 { 284 italic = newItalicObj; 285 setChanged(); 286 notifyObservers(); 287 } 288 } 289 290 295 public boolean isUnderline() { 296 return underline == null ? getDefaultFormat().isUnderline() 297 : underline.booleanValue(); 298 } 299 300 305 public void setUnderline(boolean newUnderline) { 306 Boolean newUnderlineObj = newUnderline ? Boolean.TRUE : Boolean.FALSE; 307 if (underline != newUnderlineObj 308 && (underline == null || !underline.equals(newUnderlineObj))) 309 { 310 underline = newUnderlineObj; 311 setChanged(); 312 notifyObservers(); 313 } 314 } 315 316 321 public boolean isWrap() { 322 return wrap == null ? getDefaultFormat().isWrap() : wrap.booleanValue(); 323 } 324 325 330 public void setWrap(boolean newWrap) { 331 Boolean newWrapObj = newWrap ? Boolean.TRUE : Boolean.FALSE; 332 if (wrap != newWrapObj && (wrap == null || !wrap.equals(newWrapObj))) { 333 wrap = newWrapObj; 334 setChanged(); 335 notifyObservers(); 336 } 337 } 338 339 344 public int getAlign() { 345 return align == null ? getDefaultFormat().getAlign() : align.intValue(); 346 } 347 348 353 public void setAlign(int newAlign) { 354 Integer newAlignObj = new Integer (newAlign); 355 if (align != newAlignObj && (align == null || !align.equals(newAlignObj))) { 356 align = newAlignObj; 357 setChanged(); 358 notifyObservers(); 359 } 360 } 361 362 367 public String getFormat() { 368 return format == null ? getDefaultFormat().getFormat() : format; 369 } 370 371 376 public void setFormat(String newFormat) { 377 if (format != newFormat && (format == null || !format.equals(newFormat))) { 378 format = newFormat; 379 setChanged(); 380 notifyObservers(); 381 } 382 } 383 384 389 public Color getColor() { 390 return color == null ? getDefaultFormat().getColor() : color; 391 } 392 393 398 public void setColor(Color newColor) { 399 if (color != newColor) { 400 color = newColor; 401 setChanged(); 402 notifyObservers(); 403 } 404 } 405 406 412 public String toString() { 413 return "Format[size=" + size + ", bold=" + bold + ", italic=" + italic 414 + ", underline=" + underline + ", wrap=" + wrap + ", align=" 415 + (align == null ? "null" : alignToString(align.intValue())) 416 + ", format=" + format + ", color=" + color + ", font=" 417 + fontFamilyName + "]"; 418 } 419 420 426 public void writeXML(XMLWriter out) { 427 Format def = getDefaultFormat(); 428 429 if (this.equals(def)) return; 431 432 out.startElement("format"); 433 if (fontFamilyName != null 434 && !def.getFontFamilyName().equals(fontFamilyName)) 435 out.attr("font", fontFamilyName); 436 if (size != null && size.doubleValue() != def.getSize()) 437 out.attr("size", size); 438 if (bold != null && bold.booleanValue() != def.isBold()) 439 out.attr("bold", bold); 440 if (italic != null && italic.booleanValue() != def.isItalic()) 441 out.attr("italic", italic); 442 if (underline != null && underline.booleanValue() != def.isUnderline()) 443 out.attr("underline", underline); 444 if (wrap != null && wrap.booleanValue() != def.isWrap()) 445 out.attr("wrap", wrap); 446 if (align != null && align.intValue() != def.getAlign()) 447 out.attr("align", alignToString(align.intValue())); 448 if (color != null && !color.equals(def.getColor())) 449 out.attr("color", color); 450 if (format != null && !format.equals(def.getFormat()) 451 && format.length() > 0) 452 out.attr("format", format); 453 out.endElement(); 454 } 455 456 } 457 458 459 class DefaultFormat extends Format { 460 461 464 DefaultFormat() { 465 fontFamilyName = DEFAULT_FONT_FAMILY_NAME; 466 size = new Double (DEFAULT_SIZE); 467 bold = italic = underline = Boolean.FALSE; 468 wrap = Boolean.TRUE; 469 align = new Integer (ALIGN_LEFT); 470 color = DEFAULT_COLOR; 471 } 472 473 public String getFormat() { return format; } 474 475 478 public Object clone() { 479 DefaultFormat f = new DefaultFormat(); 480 fillClonedField(f); 481 return f; 482 } 483 484 489 public void writeXML(XMLWriter out) { 490 out.startElement("format"); 491 out.attr("font", fontFamilyName); 492 out.attr("size", size); 493 out.attr("bold", bold); 494 out.attr("italic", italic); 495 out.attr("underline", underline); 496 out.attr("wrap", wrap); 497 out.attr("align", alignToString(align.intValue())); 498 out.attr("color", color); 499 if (format != null && format.length() > 0) 500 out.attr("format", format); 501 out.endElement(); 502 } 503 504 } 505 | Popular Tags |