1 19 20 package jxl.biff; 21 22 import common.Assert; 23 import common.Logger; 24 25 34 public abstract class HeaderFooter 35 { 36 39 private static Logger logger = Logger.getLogger(HeaderFooter.class); 40 41 43 46 private static final String BOLD_TOGGLE = "&B"; 47 48 51 private static final String UNDERLINE_TOGGLE = "&U"; 52 53 56 private static final String ITALICS_TOGGLE = "&I"; 57 58 61 private static final String STRIKETHROUGH_TOGGLE = "&S"; 62 63 66 private static final String DOUBLE_UNDERLINE_TOGGLE = "&E"; 67 68 71 private static final String SUPERSCRIPT_TOGGLE = "&X"; 72 73 76 private static final String SUBSCRIPT_TOGGLE = "&Y"; 77 78 81 private static final String OUTLINE_TOGGLE = "&O"; 82 83 86 private static final String SHADOW_TOGGLE = "&H"; 87 88 91 private static final String LEFT_ALIGN = "&L"; 92 93 96 private static final String CENTRE = "&C"; 97 98 101 private static final String RIGHT_ALIGN = "&R"; 102 103 105 108 private static final String PAGENUM = "&P"; 109 110 113 private static final String TOTAL_PAGENUM = "&N"; 114 115 118 private static final String DATE = "&D"; 119 120 123 private static final String TIME = "&T"; 124 125 128 private static final String WORKBOOK_NAME = "&F"; 129 130 133 private static final String WORKSHEET_NAME = "&A"; 134 135 138 protected static class Contents 139 { 140 143 private StringBuffer contents; 144 145 148 protected Contents() 149 { 150 contents = new StringBuffer (); 151 } 152 153 159 protected Contents(String s) 160 { 161 contents = new StringBuffer (s); 162 } 163 164 169 protected Contents(Contents copy) 170 { 171 contents = new StringBuffer (copy.getContents()); 172 } 173 174 180 protected String getContents() 181 { 182 return contents != null ? contents.toString() : ""; 183 } 184 185 190 private void appendInternal(String txt) 191 { 192 if (contents == null) 193 { 194 contents = new StringBuffer (); 195 } 196 197 contents.append(txt); 198 } 199 200 205 private void appendInternal(char ch) 206 { 207 if (contents == null) 208 { 209 contents = new StringBuffer (); 210 } 211 212 contents.append(ch); 213 } 214 215 220 protected void append(String txt) 221 { 222 appendInternal(txt); 223 } 224 225 231 protected void toggleBold() 232 { 233 appendInternal(BOLD_TOGGLE); 234 } 235 236 242 protected void toggleUnderline() 243 { 244 appendInternal(UNDERLINE_TOGGLE); 245 } 246 247 253 protected void toggleItalics() 254 { 255 appendInternal(ITALICS_TOGGLE); 256 } 257 258 264 protected void toggleStrikethrough() 265 { 266 appendInternal(STRIKETHROUGH_TOGGLE); 267 } 268 269 275 protected void toggleDoubleUnderline() 276 { 277 appendInternal(DOUBLE_UNDERLINE_TOGGLE); 278 } 279 280 286 protected void toggleSuperScript() 287 { 288 appendInternal(SUPERSCRIPT_TOGGLE); 289 } 290 291 297 protected void toggleSubScript() 298 { 299 appendInternal(SUBSCRIPT_TOGGLE); 300 } 301 302 308 protected void toggleOutline() 309 { 310 appendInternal(OUTLINE_TOGGLE); 311 } 312 313 319 protected void toggleShadow() 320 { 321 appendInternal(SHADOW_TOGGLE); 322 } 323 324 333 protected void setFontName(String fontName) 334 { 335 appendInternal("&\""); 337 appendInternal(fontName); 338 appendInternal('\"'); 339 } 340 341 355 protected boolean setFontSize(int size) 356 { 357 if (size < 1 || size > 99) 358 { 359 return false; 360 } 361 362 String fontSize; 365 if (size < 10) 366 { 367 fontSize = "0" + size; 369 } 370 else 371 { 372 fontSize = Integer.toString(size); 373 } 374 375 appendInternal('&'); 376 appendInternal(fontSize); 377 return true; 378 } 379 380 383 protected void appendPageNumber() 384 { 385 appendInternal(PAGENUM); 386 } 387 388 391 protected void appendTotalPages() 392 { 393 appendInternal(TOTAL_PAGENUM); 394 } 395 396 399 protected void appendDate() 400 { 401 appendInternal(DATE); 402 } 403 404 407 protected void appendTime() 408 { 409 appendInternal(TIME); 410 } 411 412 415 protected void appendWorkbookName() 416 { 417 appendInternal(WORKBOOK_NAME); 418 } 419 420 423 protected void appendWorkSheetName() 424 { 425 appendInternal(WORKSHEET_NAME); 426 } 427 428 431 protected void clear() 432 { 433 contents = null; 434 } 435 436 441 protected boolean empty() 442 { 443 if (contents == null || contents.length() == 0) 444 { 445 return true; 446 } 447 else 448 { 449 return false; 450 } 451 } 452 } 453 454 457 private Contents left; 458 459 462 private Contents right; 463 464 467 private Contents centre; 468 469 472 protected HeaderFooter() 473 { 474 left = createContents(); 475 right = createContents(); 476 centre = createContents(); 477 } 478 479 484 protected HeaderFooter(HeaderFooter hf) 485 { 486 left = createContents(hf.left); 487 right = createContents(hf.right); 488 centre = createContents(hf.centre); 489 } 490 491 495 protected HeaderFooter(String s) 496 { 497 if (s == null || s.length() == 0) 498 { 499 left = createContents(); 500 right = createContents(); 501 centre = createContents(); 502 return; 503 } 504 505 int pos = 0; 506 int leftPos = s.indexOf(LEFT_ALIGN); 507 int rightPos = s.indexOf(RIGHT_ALIGN); 508 int centrePos = s.indexOf(CENTRE); 509 510 if (pos == leftPos) 512 { 513 if (centrePos != -1) 514 { 515 left = createContents(s.substring(pos + 2, centrePos)); 516 pos = centrePos; 517 } 518 else if (rightPos != -1 ) 519 { 520 left = createContents(s.substring(pos + 2, rightPos)); 521 pos = rightPos; 522 } 523 else 524 { 525 left = createContents(s.substring(pos + 2)); 526 pos = s.length(); 527 } 528 } 529 530 if (pos == centrePos || 533 (leftPos == -1 && rightPos == -1 && centrePos == -1)) 534 { 535 if (rightPos != -1) 536 { 537 centre = createContents(s.substring(pos + 2, rightPos)); 538 pos = rightPos; 539 } 540 else 541 { 542 centre = createContents(s.substring(pos + 2)); 543 pos = s.length(); 544 } 545 } 546 547 if (pos == rightPos) 549 { 550 right = createContents(s.substring(pos + 2)); 551 pos = s.length(); 552 } 553 554 if (left == null) 555 { 556 left = createContents(); 557 } 558 559 if (centre == null) 560 { 561 centre = createContents(); 562 } 563 564 if (right == null) 565 { 566 right = createContents(); 567 } 568 } 569 570 576 public String toString() 577 { 578 StringBuffer hf = new StringBuffer (); 579 if (!left.empty()) 580 { 581 hf.append(LEFT_ALIGN); 582 hf.append(left.getContents()); 583 } 584 585 if (!centre.empty()) 586 { 587 hf.append(CENTRE); 588 hf.append(centre.getContents()); 589 } 590 591 if (!right.empty()) 592 { 593 hf.append(RIGHT_ALIGN); 594 hf.append(right.getContents()); 595 } 596 597 return hf.toString(); 598 } 599 600 605 protected Contents getRightText() 606 { 607 return right; 608 } 609 610 615 protected Contents getCentreText() 616 { 617 return centre; 618 } 619 620 625 protected Contents getLeftText() 626 { 627 return left; 628 } 629 630 633 protected void clear() 634 { 635 left.clear(); 636 right.clear(); 637 centre.clear(); 638 } 639 640 643 protected abstract Contents createContents(); 644 645 648 protected abstract Contents createContents(String s); 649 650 653 protected abstract Contents createContents(Contents c); 654 } 655 | Popular Tags |