1 22 23 24 package net.sourceforge.chart2d; 25 26 27 import java.awt.*; 28 29 30 46 final class HorizontalTextListArea extends TextListArea { 47 48 49 private boolean labelsExistence = true; 50 private TextArea[] labels; 51 private String [] labelStrings; 52 53 private boolean bulletsExistence = true; 54 private Rectangle[] bullets; 55 private Dimension bulletsSizeModel; 56 private int bulletsAlignment; 57 private int bulletsRelation; 58 private Color[] bulletColors; 59 private boolean bulletsOutline; 60 private Color bulletsOutlineColor; 61 62 private boolean betweenLabelsGapExistence; 63 private int betweenLabelsGapThicknessModel; 64 private boolean betweenBulletsGapExistence; 65 private int betweenBulletsGapThicknessModel; 66 private boolean betweenBulletsAndLabelsGapExistence; 67 private int betweenBulletsAndLabelsGapThicknessModel; 68 private boolean customizeSpaceMinWidth; 69 private int customSpaceMinWidth; 70 71 private boolean resetHorizontalTextListAreaModel; 72 private boolean allowSelfSize; 73 private boolean needsUpdate; 74 75 76 79 HorizontalTextListArea() { 80 81 labels = new TextArea[0]; 82 bullets = new Rectangle[0]; 83 bulletColors = new Color[0]; 84 85 setLabels (new String [0]); 86 setBulletColors (new Color[0]); 87 setBulletsSizeModel (new Dimension (9, 9)); 88 setBulletsOutline (true); 89 setBulletsOutlineColor (Color.black); 90 setBetweenLabelsGapExistence (true); 91 setBetweenLabelsGapThicknessModel (3); 92 setBetweenBulletsGapExistence (true); 93 setBetweenBulletsGapThicknessModel (3); 94 setBetweenBulletsAndLabelsGapExistence (true); 95 setBetweenBulletsAndLabelsGapThicknessModel (3); 96 setBulletsAlignment (CENTERED); 97 setBulletsRelation (TOP); 98 99 setAllowSelfSize (true); 100 resetHorizontalTextListAreaModel (true); 101 needsUpdate = true; 102 customizeSpaceMinWidth = false; 103 } 104 105 106 113 final void setAllowSelfSize (boolean allow) { 114 115 needsUpdate = true; 116 allowSelfSize = allow; 117 } 118 119 120 125 final void setLabels (String [] labels) { 126 127 needsUpdate = true; 128 labelStrings = labels; 129 } 130 131 132 139 final void setCustomSpaceMinWidth (boolean customize, int width) { 140 141 needsUpdate = true; 142 customizeSpaceMinWidth = customize; 143 customSpaceMinWidth = width; 144 } 145 146 147 154 final void setBulletsSizeModel (Dimension model) { 155 156 needsUpdate = true; 157 bulletsSizeModel = model; 158 } 159 160 161 169 final void setBulletsAlignment (int alignment) { 170 171 needsUpdate = true; 172 bulletsAlignment = alignment; 173 } 174 175 176 182 final void setBulletsRelation (int relation) { 183 184 needsUpdate = true; 185 bulletsRelation = relation; 186 } 187 188 189 201 final void setBulletColors (Color[] colors) { 202 203 needsUpdate = true; 204 bulletColors = colors; 205 } 206 207 208 214 final void setBulletsOutline (boolean outline) { 215 216 needsUpdate = true; 217 bulletsOutline = outline; 218 } 219 220 221 225 final void setBulletsOutlineColor (Color color) { 226 227 bulletsOutlineColor = color; 228 } 229 230 231 237 final void setBetweenLabelsGapExistence (boolean existence) { 238 239 needsUpdate = true; 240 betweenLabelsGapExistence = existence; 241 } 242 243 244 249 final void setBetweenLabelsGapThicknessModel (int model) { 250 251 needsUpdate = true; 252 betweenLabelsGapThicknessModel = model; 253 } 254 255 256 262 final void setBetweenBulletsGapExistence (boolean existence) { 263 264 needsUpdate = true; 265 betweenBulletsGapExistence = existence; 266 } 267 268 269 275 final void setBetweenBulletsGapThicknessModel (int model) { 276 277 needsUpdate = true; 278 betweenBulletsGapThicknessModel = model; 279 } 280 281 282 289 final void setBetweenBulletsAndLabelsGapExistence (boolean existence) { 290 291 needsUpdate = true; 292 betweenBulletsAndLabelsGapExistence = existence; 293 } 294 295 296 302 final void setBetweenBulletsAndLabelsGapThicknessModel (int model) { 303 304 needsUpdate = true; 305 betweenBulletsAndLabelsGapThicknessModel = model; 306 } 307 308 309 313 final String [] getLabelStrings() { 314 315 return labelStrings; 316 } 317 318 319 326 final TextArea[] getLabels (Graphics2D g2D) { 327 328 updateHorizontalTextListArea (g2D); 329 return labels; 330 } 331 332 333 337 final int getBetweenBulletsGapThicknessModel() { 338 339 return betweenBulletsGapThicknessModel; 340 } 341 342 343 347 final int getBetweenLabelsGapThicknessModel() { 348 349 return betweenLabelsGapThicknessModel; 350 } 351 352 353 358 final int getBetweenBulletsAndLabelsGapThicknessModel() { 359 360 return betweenBulletsAndLabelsGapThicknessModel; 361 } 362 363 364 371 final Rectangle[] getBullets (Graphics2D g2D) { 372 373 updateHorizontalTextListArea (g2D); 374 return bullets; 375 } 376 377 378 382 final Dimension getBulletsSizeModel() { 383 384 return bulletsSizeModel; 385 } 386 387 388 396 final int getBulletsAlignment() { 397 398 return bulletsAlignment; 399 } 400 401 402 408 final boolean getBulletsOutline() { 409 410 return bulletsOutline; 411 } 412 413 414 418 final Color getBulletsOutlineColor() { 419 420 return bulletsOutlineColor; 421 } 422 423 424 428 public int getNumBullets() { 429 int numBullets = bulletsExistence && labelsExistence ? 430 (bulletsAlignment == CENTERED ? 431 labelStrings.length : labelStrings.length - 1) : 0; 432 return (numBullets > 0 ? numBullets : 0); 433 } 434 435 436 440 final boolean getHorizontalTextListAreaNeedsUpdate() { 441 442 return (needsUpdate || getFontAreaNeedsUpdate()); 443 } 444 445 446 451 final void updateHorizontalTextListArea (Graphics2D g2D) { 452 453 if (getHorizontalTextListAreaNeedsUpdate()) { 454 updateFontArea (); 455 update (g2D); 456 } 457 needsUpdate = false; 458 } 459 460 461 473 final void resetHorizontalTextListAreaModel (boolean reset) { 474 475 needsUpdate = true; 476 resetFontAreaModel (reset); 477 } 478 479 480 484 final void paintComponent (Graphics2D g2D) { 485 486 updateHorizontalTextListArea (g2D); 487 super.paintComponent (g2D); 488 489 Color oldColor = g2D.getColor(); 490 491 int num = labelsExistence ? labels.length : 0; 492 for (int i = 0; i < num; ++i) { 493 labels[i].paintComponent (g2D); 494 } 495 496 num = bulletsExistence ? bullets.length : 0; 497 for (int i = 0; i < num; ++i) { 498 499 g2D.setColor (bulletColors[i]); 500 g2D.fill (bullets[i]); 501 502 if (bulletsOutline) { 503 g2D.setColor (bulletsOutlineColor); 504 g2D.draw (bullets[i]); 505 } 506 } 507 508 g2D.setColor (oldColor); 509 } 510 511 512 private void update (Graphics2D g2D) { 513 514 int numLabels = labelsExistence ? labelStrings.length : 0; 515 labels = new TextArea[numLabels]; 516 int numBullets = getNumBullets(); 517 bullets = new Rectangle[numBullets]; 518 519 int numBulletGaps = bulletsAlignment == CENTERED ? 520 (numBullets - 1) : (numBullets + 1); 521 int requiredNumBulletsForGaps = bulletsAlignment == CENTERED ? 2 : 1; 522 int requiredNumBulletsForSpacing = bulletsAlignment == CENTERED ? 523 numBullets : (numBullets + 1); 524 525 int betweenBulletsAndLabelsGapThickness = 0; 526 int availableHeight = getSpaceSize (MAX).height; 527 if (betweenBulletsAndLabelsGapExistence && labelsExistence) { 528 betweenBulletsAndLabelsGapThickness = applyRatio ( 529 betweenBulletsAndLabelsGapThicknessModel, getRatio (HEIGHT)); 530 betweenBulletsAndLabelsGapThickness = 531 betweenBulletsAndLabelsGapThickness <= availableHeight ? 532 betweenBulletsAndLabelsGapThickness : availableHeight; 533 availableHeight -= betweenBulletsAndLabelsGapThickness; 534 } 535 536 int availableWidthForLabels = getSpaceSize (MAX).width; 537 int availableWidthForBullets = getSpaceSize (MAX).width; 538 int betweenLabelsGapThickness = 0; 539 if (betweenLabelsGapExistence && numLabels > 1) { 540 betweenLabelsGapThickness = 541 applyRatio (betweenLabelsGapThicknessModel, getRatio (WIDTH)); 542 betweenLabelsGapThickness = betweenLabelsGapThickness <= 543 (availableWidthForLabels / (numLabels - 1)) ? 544 betweenLabelsGapThickness : 545 (availableWidthForLabels / (numLabels - 1)); 546 availableWidthForLabels -= 547 ((numLabels - 1) * betweenLabelsGapThickness); 548 } 549 550 int betweenBulletsGapThickness = 0; 551 if (betweenBulletsGapExistence && numBullets >= requiredNumBulletsForGaps) { 552 betweenBulletsGapThickness = 553 applyRatio (betweenBulletsGapThicknessModel, getRatio (WIDTH)); 554 betweenBulletsGapThickness = betweenBulletsGapThickness <= 555 (availableWidthForBullets / numBulletGaps) ? 556 betweenBulletsGapThickness : 557 (availableWidthForBullets / numBulletGaps); 558 availableWidthForBullets -= 559 (numBulletGaps * betweenBulletsGapThickness); 560 } 561 562 int bulletsWidth = 0; 563 int bulletsHeight = 0; 564 if (bulletsExistence) { 565 if (numBullets > 0) { 566 bulletsWidth = applyRatio (bulletsSizeModel.width, getRatio (WIDTH)); 567 bulletsWidth = bulletsWidth <= 568 (availableWidthForBullets / requiredNumBulletsForSpacing) ? 569 bulletsWidth : 570 (availableWidthForBullets / requiredNumBulletsForSpacing); 571 bulletsHeight = applyRatio (bulletsSizeModel.height, getRatio (HEIGHT)); 572 bulletsHeight = 573 bulletsHeight <= availableHeight ? bulletsHeight : availableHeight; 574 } 575 if (bulletsWidth <= 0 || bulletsHeight <= 0) { 576 bulletsWidth = 0; 577 bulletsHeight = 0; 578 } 579 else { 580 availableHeight -= bulletsHeight; 581 } 582 } 583 584 int labelsWidth = 0; 585 int labelsHeight = 0; 586 if (labelsExistence && numLabels > 0) { 587 labelsWidth = availableWidthForLabels / numLabels; 588 labelsHeight = availableHeight; 589 } 590 591 for (int i = 0; i < numLabels; ++i) { 592 labels[i] = new TextArea(); 593 labels[i].setCustomRatio (WIDTH, true, getRatio (WIDTH)); 594 labels[i].setCustomRatio (HEIGHT, true, getRatio (HEIGHT)); 595 labels[i].setAutoSizes (true, false); 596 labels[i].setSize (MAX, new Dimension (labelsWidth, labelsHeight)); 597 labels[i].setBorderExistence (false); 598 labels[i].setGapExistence (false); 599 labels[i].setBackgroundExistence (false); 600 labels[i].setText (labelStrings[i]); 601 labels[i].setFontName(getFontName()); 602 labels[i].setFontStyle(getFontStyle()); 603 labels[i].setFontPointModel(getFontPointModel()); 604 labels[i].setFontColor(getFontColor()); 605 } 606 607 int greatestLabelWidth = 0; 608 int smallestLabelWidth = Integer.MAX_VALUE; 609 int greatestLabelHeight = 0; 610 for (int i = 0; i < numLabels; ++i) { 611 labels[i].updateTextArea (g2D); 612 int thisLabelWidth = labels[i].getSize (MIN).width; 613 greatestLabelWidth = thisLabelWidth > greatestLabelWidth ? 614 thisLabelWidth : greatestLabelWidth; 615 smallestLabelWidth = thisLabelWidth < smallestLabelWidth ? 616 thisLabelWidth : smallestLabelWidth; 617 int thisLabelHeight = labels[i].getSize (MIN).height; 618 greatestLabelHeight = thisLabelHeight > greatestLabelHeight ? 619 thisLabelHeight : greatestLabelHeight; 620 } 621 622 if (smallestLabelWidth > 0) { 623 labelsWidth = greatestLabelWidth; 624 labelsHeight = greatestLabelHeight; 625 } 626 else { 627 labelsWidth = 0; 628 labelsHeight = 0; 629 betweenLabelsGapThickness = 0; 630 for (int i = 0; i < numLabels; ++i) { 631 labels[i].setSize (MAX, new Dimension()); 632 } 633 634 betweenBulletsAndLabelsGapThickness = 0; 635 availableHeight = getSpaceSize (MAX).height; 636 bulletsWidth = 0; 637 bulletsHeight = 0; 638 if (bulletsExistence && numBullets > 0) { 639 bulletsWidth = applyRatio (bulletsSizeModel.width, getRatio (WIDTH)); 640 bulletsWidth = bulletsWidth <= 641 (availableWidthForBullets / requiredNumBulletsForSpacing) ? 642 bulletsWidth : 643 (availableWidthForBullets / requiredNumBulletsForSpacing); 644 bulletsHeight = applyRatio (bulletsSizeModel.height, getRatio (HEIGHT)); 645 bulletsHeight = 646 bulletsHeight <= availableHeight ? bulletsHeight : availableHeight; 647 } 648 if (bulletsWidth <= 0 || bulletsHeight <= 0) { 649 bulletsWidth = 0; 650 bulletsHeight = 0; 651 betweenBulletsGapThickness = 0; 652 } 653 } 654 655 if (!getAutoSize (MIN) && allowSelfSize) { 656 int possibleMinSpaceWidthLabels = 657 numLabels * labelsWidth + (numLabels - 1) * betweenLabelsGapThickness; 658 int possibleMinSpaceWidthBullets = 659 requiredNumBulletsForSpacing * bulletsWidth + 660 numBulletGaps * betweenBulletsGapThickness; 661 int minSpaceWidth = 662 possibleMinSpaceWidthLabels > possibleMinSpaceWidthBullets ? 663 possibleMinSpaceWidthLabels : possibleMinSpaceWidthBullets; 664 minSpaceWidth = 665 customizeSpaceMinWidth && customSpaceMinWidth > minSpaceWidth ? 666 customSpaceMinWidth : minSpaceWidth; 667 int minSpaceHeight = 668 bulletsHeight + betweenBulletsAndLabelsGapThickness + labelsHeight; 669 setSpaceSize (MIN, 670 new Dimension (minSpaceWidth, minSpaceHeight)); 671 } 672 673 int numComps = numLabels; 674 float compWidth = getSpaceSize (MIN).width / (float)numComps; 675 int x = getSpaceSizeLocation (MIN).x; 676 int y = getSpaceSizeLocation (MIN).y; 677 int labelsX = 0, labelsY = 0, bulletsX = 0, bulletsY = 0; 678 for (int i = 0; i < numComps; ++i) { 679 680 labels[i].setAutoJustifys (false, false); 681 if (bulletsRelation == TOP) { 682 labelsY = y + bulletsHeight + betweenBulletsAndLabelsGapThickness; 683 bulletsY = y; 684 } 685 else { 686 labelsY = y + labelsHeight - labels[i].getSpaceSize(MIN).height; 687 bulletsY = y + labelsHeight + betweenBulletsAndLabelsGapThickness; 688 } 689 labelsX = (int)(x + i * compWidth + 690 (compWidth - labels[i].getSpaceSize (MIN).width) / 2f); 691 labels[i].setSpaceSizeLocation (MIN, new Point (labelsX, labelsY)); 692 if (i < numBullets) { 693 if (bulletsAlignment == CENTERED) { 694 bulletsX = 695 (int)(x + i * compWidth + (compWidth - bulletsWidth) / 2f); 696 } 697 else { 698 bulletsX = 699 (int)(x + (i + 1) * compWidth - bulletsWidth / 2f); 700 } 701 bullets[i] = new Rectangle(); 702 bullets[i].setLocation (bulletsX, bulletsY); 703 bullets[i].setSize (bulletsWidth, bulletsHeight); 704 } 705 } 706 } 707 } | Popular Tags |