1 22 23 24 package net.sourceforge.chart2d; 25 26 27 import java.awt.*; 28 29 30 45 class VerticalTextListArea extends TextListArea { 46 47 48 private boolean labelsExistence = true; 49 private TextArea[] labels; 50 private String [] labelStrings; 51 52 private boolean bulletsExistence = true; 53 private Rectangle[] bullets; 54 private Dimension bulletsSizeModel; 55 private int bulletsAlignment; 56 private int bulletsRelation; 57 private Color[] bulletColors; 58 private boolean bulletsOutline; 59 private Color bulletsOutlineColor; 60 61 private boolean betweenLabelsGapExistence; 62 private int betweenLabelsGapThicknessModel; 63 private boolean betweenBulletsGapExistence; 64 private int betweenBulletsGapThicknessModel; 65 private boolean betweenBulletsAndLabelsGapExistence; 66 private int betweenBulletsAndLabelsGapThicknessModel; 67 private boolean customizeSpaceMinHeight; 68 private int customSpaceMinHeight; 69 70 private boolean allowSelfSize; 71 private boolean needsUpdate; 72 73 74 76 VerticalTextListArea() { 77 78 labelStrings = new String [0]; 79 labels = new TextArea[0]; 80 bullets = new Rectangle[0]; 81 bulletColors = new Color[0]; 82 83 setBulletsSizeModel (new Dimension (9, 9)); 84 setBulletsOutline (true); 85 setBulletsOutlineColor (Color.black); 86 setBetweenLabelsGapExistence (true); 87 setBetweenLabelsGapThicknessModel (3); 88 setBetweenBulletsGapExistence (true); 89 setBetweenBulletsGapThicknessModel (3); 90 setBetweenBulletsAndLabelsGapExistence (true); 91 setBetweenBulletsAndLabelsGapThicknessModel (3); 92 setBulletsAlignment (CENTERED); 93 setBulletsRelation (LEFT); 94 setAllowSelfSize (true); 95 resetVerticalTextListAreaModel (true); 96 needsUpdate = true; 97 customizeSpaceMinHeight = false; 98 } 99 100 101 108 final void setAllowSelfSize (boolean allow) { 109 110 needsUpdate = true; 111 allowSelfSize = allow; 112 } 113 114 115 120 final void setLabels (String [] labels) { 121 122 needsUpdate = true; 123 labelStrings = labels; 124 } 125 126 127 134 final void setCustomSpaceMinHeight (boolean customize, int height) { 135 136 needsUpdate = true; 137 customizeSpaceMinHeight = customize; 138 customSpaceMinHeight = height; 139 } 140 141 142 149 final void setBulletsSizeModel (Dimension model) { 150 151 needsUpdate = true; 152 bulletsSizeModel = model; 153 } 154 155 156 164 final void setBulletsAlignment (int alignment) { 165 166 needsUpdate = true; 167 bulletsAlignment = alignment; 168 } 169 170 171 178 final void setBulletsRelation (int relation) { 179 180 needsUpdate = true; 181 bulletsRelation = relation; 182 } 183 184 185 197 final void setBulletColors (Color[] colors) { 198 199 needsUpdate = true; 200 bulletColors = colors; 201 } 202 203 204 210 final void setBulletsOutline (boolean outline) { 211 212 needsUpdate = true; 213 bulletsOutline = outline; 214 } 215 216 217 221 final void setBulletsOutlineColor (Color color) { 222 223 bulletsOutlineColor = color; 224 } 225 226 227 233 final void setBetweenLabelsGapExistence (boolean existence) { 234 235 needsUpdate = true; 236 betweenLabelsGapExistence = existence; 237 } 238 239 240 245 final void setBetweenLabelsGapThicknessModel (int model) { 246 247 needsUpdate = true; 248 betweenLabelsGapThicknessModel = model; 249 } 250 251 252 258 final void setBetweenBulletsGapExistence (boolean existence) { 259 260 needsUpdate = true; 261 betweenBulletsGapExistence = existence; 262 } 263 264 265 271 final void setBetweenBulletsGapThicknessModel (int model) { 272 273 needsUpdate = true; 274 betweenBulletsGapThicknessModel = model; 275 } 276 277 278 285 final void setBetweenBulletsAndLabelsGapExistence (boolean existence) { 286 287 needsUpdate = true; 288 betweenBulletsAndLabelsGapExistence = existence; 289 } 290 291 292 298 final void setBetweenBulletsAndLabelsGapThicknessModel (int model) { 299 300 needsUpdate = true; 301 betweenBulletsAndLabelsGapThicknessModel = model; 302 } 303 304 305 309 final String [] getLabelStrings() { 310 311 return labelStrings; 312 } 313 314 315 319 final int getBetweenBulletsGapThicknessModel() { 320 321 return betweenBulletsGapThicknessModel; 322 } 323 324 325 329 final int getBetweenLabelsGapThicknessModel() { 330 331 return betweenLabelsGapThicknessModel; 332 } 333 334 335 340 final int getBetweenBulletsAndLabelsGapThicknessModel() { 341 342 return betweenBulletsAndLabelsGapThicknessModel; 343 } 344 345 346 353 final Rectangle[] getBullets (Graphics2D g2D) { 354 355 updateVerticalTextListArea (g2D); 356 return bullets; 357 } 358 359 360 364 final int getNumBullets() { 365 int numBullets = bulletsExistence && labelsExistence ? 366 (bulletsAlignment == CENTERED ? 367 labelStrings.length : labelStrings.length - 1) : 0; 368 return (numBullets > 0 ? numBullets : 0); 369 } 370 371 372 376 final Dimension getBulletsSizeModel() { 377 378 return bulletsSizeModel; 379 } 380 381 382 394 final Color[] getBulletColors() { 395 return bulletColors; 396 } 397 398 399 407 final int getBulletsAlignment() { 408 409 return bulletsAlignment; 410 } 411 412 413 419 final boolean getBulletsOutline() { 420 421 return bulletsOutline; 422 } 423 424 425 429 final Color getBulletsOutlineColor() { 430 431 return bulletsOutlineColor; 432 } 433 434 435 442 final TextArea[] getLabels (Graphics2D g2D) { 443 444 updateVerticalTextListArea (g2D); 445 return labels; 446 } 447 448 449 461 final void resetVerticalTextListAreaModel (boolean reset) { 462 463 needsUpdate = true; 464 resetFontAreaModel (reset); 465 } 466 467 468 472 final boolean getVerticalTextListAreaNeedsUpdate() { 473 474 return (needsUpdate || getFontAreaNeedsUpdate()); 475 } 476 477 478 483 final void updateVerticalTextListArea (Graphics2D g2D) { 484 485 if (getVerticalTextListAreaNeedsUpdate()) { 486 updateFontArea (); 487 update (g2D); 488 } 489 needsUpdate = false; 490 } 491 492 493 497 void paintComponent (Graphics2D g2D) { 498 499 updateVerticalTextListArea (g2D); 500 super.paintComponent (g2D); 501 502 Color oldColor = g2D.getColor(); 503 504 int num = labelsExistence ? labels.length : 0; 505 for (int i = 0; i < num; ++i) { 506 labels[i].paintComponent (g2D); 507 } 508 509 num = bulletsExistence ? bullets.length : 0; 510 for (int i = 0; i < num; ++i) { 511 512 g2D.setColor (bulletColors[i]); 513 g2D.fill (bullets[i]); 514 515 if (bulletsOutline) { 516 g2D.setColor (bulletsOutlineColor); 517 g2D.draw (bullets[i]); 518 } 519 } 520 521 g2D.setColor (oldColor); 522 } 523 524 525 private void update (Graphics2D g2D) { 526 527 int numLabels = labelsExistence ? labelStrings.length : 0; 528 labels = new TextArea[numLabels]; 529 int numBullets = getNumBullets(); 530 bullets = new Rectangle[numBullets]; 531 532 int numBulletGaps = bulletsAlignment == CENTERED ? 533 (numBullets - 1) : (numBullets + 1); 534 int requiredNumBulletsForGaps = bulletsAlignment == CENTERED ? 2 : 1; 535 int requiredNumBulletsForSpacing = bulletsAlignment == CENTERED ? 536 numBullets : (numBullets + 1); 537 538 int betweenBulletsAndLabelsGapThickness = 0; 539 int availableWidth = getSpaceSize (MAX).width; 540 if (betweenBulletsAndLabelsGapExistence && labelsExistence) { 541 betweenBulletsAndLabelsGapThickness = applyRatio ( 542 betweenBulletsAndLabelsGapThicknessModel, getRatio (WIDTH)); 543 betweenBulletsAndLabelsGapThickness = 544 betweenBulletsAndLabelsGapThickness <= availableWidth ? 545 betweenBulletsAndLabelsGapThickness : availableWidth; 546 availableWidth -= betweenBulletsAndLabelsGapThickness; 547 } 548 549 int availableHeightForLabels = getSpaceSize (MAX).height; 550 int availableHeightForBullets = getSpaceSize (MAX).height; 551 int betweenLabelsGapThickness = 0; 552 int betweenBulletsGapThickness = 0; 553 if (betweenLabelsGapExistence && numLabels > 1) { 554 betweenLabelsGapThickness = 555 applyRatio (betweenLabelsGapThicknessModel, getRatio (HEIGHT)); 556 betweenLabelsGapThickness = betweenLabelsGapThickness <= 557 (availableHeightForLabels / (numLabels - 1)) ? 558 betweenLabelsGapThickness : 559 (availableHeightForLabels / (numLabels - 1)); 560 availableHeightForLabels -= 561 ((numLabels - 1) * betweenLabelsGapThickness); 562 } 563 if (betweenBulletsGapExistence && numBullets >= requiredNumBulletsForGaps) { 564 betweenBulletsGapThickness = 565 applyRatio (betweenBulletsGapThicknessModel, getRatio (HEIGHT)); 566 betweenBulletsGapThickness = betweenBulletsGapThickness <= 567 (availableHeightForBullets / numBulletGaps) ? 568 betweenBulletsGapThickness : 569 (availableHeightForBullets / numBulletGaps); 570 availableHeightForBullets -= 571 (numBulletGaps * betweenBulletsGapThickness); 572 } 573 574 int bulletsWidth = 0; 575 int bulletsHeight = 0; 576 if (bulletsExistence) { 577 if (numBullets > 0) { 578 bulletsWidth = applyRatio (bulletsSizeModel.width, getRatio (WIDTH)); 579 bulletsWidth = 580 bulletsWidth <= availableWidth ? bulletsWidth : availableWidth; 581 bulletsHeight = applyRatio (bulletsSizeModel.height, getRatio (HEIGHT)); 582 bulletsHeight = bulletsHeight <= 583 (availableHeightForBullets / requiredNumBulletsForSpacing) ? 584 bulletsHeight : 585 (availableHeightForBullets / requiredNumBulletsForSpacing); 586 } 587 if (bulletsWidth <= 0 || bulletsHeight <= 0) { 588 bulletsWidth = 0; 589 bulletsHeight = 0; 590 } 591 else { 592 availableWidth -= bulletsWidth; 593 } 594 } 595 596 int labelsWidth = 0; 597 int labelsHeight = 0; 598 if (labelsExistence && numLabels > 0) { 599 labelsWidth = availableWidth; 600 labelsHeight = availableHeightForLabels / numLabels; 601 } 602 603 for (int i = 0; i < numLabels; ++i) { 604 labels[i] = new TextArea(); 605 labels[i].setCustomRatio (WIDTH, true, getRatio (WIDTH)); 606 labels[i].setCustomRatio (HEIGHT, true, getRatio (HEIGHT)); 607 labels[i].setAutoSizes (true, false); 608 labels[i].setSize (MAX, new Dimension (labelsWidth, labelsHeight)); 609 labels[i].setBorderExistence (false); 610 labels[i].setGapExistence (false); 611 labels[i].setBackgroundExistence (false); 612 labels[i].setText (labelStrings[i]); 613 labels[i].setFontName(getFontName()); 614 labels[i].setFontStyle(getFontStyle()); 615 labels[i].setFontPointModel(getFontPointModel()); 616 labels[i].setFontColor(getFontColor()); 617 } 618 619 int greatestLabelWidth = 0; 620 int smallestLabelWidth = Integer.MAX_VALUE; 621 int greatestLabelHeight = 0; 622 for (int i = 0; i < numLabels; ++i) { 623 labels[i].updateTextArea (g2D); 624 int thisLabelWidth = labels[i].getSize (MIN).width; 625 greatestLabelWidth = thisLabelWidth > greatestLabelWidth ? 626 thisLabelWidth : greatestLabelWidth; 627 smallestLabelWidth = thisLabelWidth < smallestLabelWidth ? 628 thisLabelWidth : smallestLabelWidth; 629 int thisLabelHeight = labels[i].getSize (MIN).height; 630 greatestLabelHeight = thisLabelHeight > greatestLabelHeight ? 631 thisLabelHeight : greatestLabelHeight; 632 } 633 634 if (smallestLabelWidth > 0) { 635 labelsWidth = greatestLabelWidth; 636 labelsHeight = greatestLabelHeight; 637 } 638 else { 639 labelsWidth = 0; 640 labelsHeight = 0; 641 betweenLabelsGapThickness = 0; 642 for (int i = 0; i < numLabels; ++i) { 643 labels[i].setSize (MAX, new Dimension()); 644 } 645 646 betweenBulletsAndLabelsGapThickness = 0; 647 availableWidth = getSpaceSize (MAX).width; 648 bulletsWidth = 0; 649 bulletsHeight = 0; 650 if (bulletsExistence && numBullets > 0) { 651 bulletsWidth = applyRatio (bulletsSizeModel.width, getRatio (WIDTH)); 652 bulletsWidth = 653 bulletsWidth <= availableWidth ? bulletsWidth : availableWidth; 654 bulletsHeight = applyRatio (bulletsSizeModel.height, getRatio (HEIGHT)); 655 bulletsHeight = bulletsHeight <= 656 (availableHeightForBullets / requiredNumBulletsForSpacing) ? 657 bulletsHeight : 658 (availableHeightForBullets / requiredNumBulletsForSpacing); 659 } 660 if (bulletsWidth <= 0 || bulletsHeight <= 0) { 661 bulletsWidth = 0; 662 bulletsHeight = 0; 663 betweenBulletsGapThickness = 0; 664 } 665 } 666 667 if (!getAutoSize (MIN) && allowSelfSize) { 668 int minSpaceWidth = 669 bulletsWidth + betweenBulletsAndLabelsGapThickness + labelsWidth; 670 int possibleMinSpaceHeightLabels = 671 numLabels * labelsHeight + (numLabels - 1) * betweenLabelsGapThickness; 672 int possibleMinSpaceHeightBullets = 673 requiredNumBulletsForSpacing * bulletsHeight + 674 numBulletGaps * betweenBulletsGapThickness; 675 int minSpaceHeight = 676 possibleMinSpaceHeightLabels > possibleMinSpaceHeightBullets ? 677 possibleMinSpaceHeightLabels : possibleMinSpaceHeightBullets; 678 minSpaceHeight = 679 customizeSpaceMinHeight && customSpaceMinHeight > minSpaceHeight ? 680 customSpaceMinHeight : minSpaceHeight; 681 setSpaceSize (MIN, 682 new Dimension (minSpaceWidth, minSpaceHeight)); 683 } 684 685 int numComps = numLabels; 686 float compHeight = getSpaceSize (MIN).height / (float)numComps; 687 int x = getSpaceSizeLocation (MIN).x; 688 int y = getSpaceSizeLocation (MIN).y; 689 int labelsX = 0, labelsY = 0, bulletsX = 0, bulletsY = 0; 690 for (int i = 0; i < numComps; ++i) { 691 692 labels[i].setAutoJustifys (false, false); 693 if (bulletsRelation == LEFT) { 694 labelsX = x + bulletsWidth + betweenBulletsAndLabelsGapThickness; 695 bulletsX = x; 696 } 697 else { 698 labelsX = x + labelsWidth - labels[i].getSpaceSize(MIN).width; 699 bulletsX = x + labelsWidth + betweenBulletsAndLabelsGapThickness; 700 } 701 labelsY = (int)(y + i * compHeight + 702 (compHeight - labels[i].getSpaceSize (MIN).height) / 2f); 703 labels[i].setSpaceSizeLocation (MIN, new Point (labelsX, labelsY)); 704 if (i < numBullets) { 705 if (bulletsAlignment == CENTERED) { 706 bulletsY = 707 (int)(y + i * compHeight + (compHeight - bulletsHeight) / 2f); 708 } 709 else { 710 bulletsY = 711 (int)(y + (i + 1) * compHeight - bulletsHeight / 2f); 712 } 713 bullets[i] = new Rectangle(); 714 bullets[i].setLocation (bulletsX, bulletsY); 715 bullets[i].setSize (bulletsWidth, bulletsHeight); 716 } 717 } 718 } 719 } | Popular Tags |