1 21 22 package com.izforge.izpack.gui; 23 24 import java.awt.*; 25 import java.util.Vector ; 26 27 94 public class TwoColumnLayout implements LayoutManager2 95 { 96 97 public static final int LEFT = 0; 98 99 public static final int RIGHT = 1; 100 101 public static final int CENTER = 2; 102 103 104 private Vector [] components = { new Vector (), new Vector ()}; 105 106 109 private TwoColumnConstraints title = null; 110 111 112 private int margin = 0; 113 114 117 private int topBuffer = 0; 118 119 120 private int indent = 0; 121 122 123 private int gap = 5; 124 125 private int alignment = LEFT; 126 127 private int leftRule; 128 129 private int rightRule; 130 131 private int centerRule; 132 133 private int titleHeight; 134 135 148 public TwoColumnLayout(int margin, int gap, int indent, int topBuffer, int alignment) 149 { 150 this.indent = indent; 151 this.gap = gap; 152 153 if ((margin >= 0) && (margin <= 50)) 154 { 155 this.margin = margin; 156 } 157 if ((topBuffer >= 0) && (topBuffer <= 100)) 158 { 159 this.topBuffer = topBuffer; 160 } 161 if ((alignment == LEFT) || (alignment == CENTER) || (alignment == RIGHT)) 162 { 163 this.alignment = alignment; 164 } 165 } 166 167 174 public void addLayoutComponent(Component comp, Object constraints) 175 { 176 if (constraints == null) { return; } 177 178 TwoColumnConstraints component = null; 179 try 180 { 181 component = (TwoColumnConstraints) constraints; 182 component = (TwoColumnConstraints) component.clone(); 183 } 184 catch (Throwable exception) 185 { 186 return; 187 } 188 189 component.component = comp; 190 191 if (component.position == TwoColumnConstraints.NORTH) 197 { 198 title = component; 199 if (title.stretch) 200 { 201 title.align = LEFT; 202 } 203 } 204 205 else if (component.position == TwoColumnConstraints.BOTH) 230 { 231 while (components[RIGHT].size() > components[LEFT].size()) 233 { 234 components[LEFT].add(null); 235 } 236 237 while (components[LEFT].size() > components[RIGHT].size()) 238 { 239 components[RIGHT].add(null); 240 } 241 242 components[LEFT].add(component); 243 components[RIGHT].add(null); 244 } 245 246 else if (component.position == TwoColumnConstraints.WEST) 250 { 251 components[LEFT].add(component); 252 } 253 254 else if (component.position == TwoColumnConstraints.WESTONLY) 259 { 260 components[LEFT].add(component); 261 262 while (components[RIGHT].size() < components[LEFT].size()) 264 { 265 components[RIGHT].add(null); 266 } 267 268 } 269 270 else if (component.position == TwoColumnConstraints.EAST) 274 { 275 components[RIGHT].add(component); 276 } 277 278 else if (component.position == TwoColumnConstraints.EASTONLY) 283 { 284 components[RIGHT].add(component); 285 286 while (components[LEFT].size() < components[RIGHT].size()) 288 { 289 components[LEFT].add(null); 290 } 291 292 } 293 294 } 300 301 306 public void layoutContainer(Container parent) 307 { 308 positionRules(parent); 309 positionTitle(parent); 310 positionComponents(parent); 311 } 312 313 323 private void positionRules(Container parent) 324 { 325 int margin = margin(parent); 326 327 if (alignment == LEFT) 328 { 329 leftRule = margin; 330 centerRule = leftRule + minimumColumnWidth(LEFT, parent) + gap; 331 rightRule = parent.getWidth() - margin; 332 } 333 334 else if (alignment == CENTER) 335 { 336 centerRule = (int) (parent.getMinimumSize().getWidth() / 2); 337 leftRule = centerRule - minimumColumnWidth(LEFT, parent) - gap; 338 rightRule = parent.getWidth() - margin; 339 } 340 341 else if (alignment == RIGHT) 342 { 343 rightRule = parent.getWidth() - margin; 344 centerRule = rightRule - minimumColumnWidth(RIGHT, parent); 345 leftRule = centerRule - minimumColumnWidth(LEFT, parent) - gap; 346 } 347 } 348 349 355 private void positionTitle(Container parent) 356 { 357 if (title != null) 358 { 359 Component component = title.component; 360 int width = (int) component.getMinimumSize().getWidth(); 361 titleHeight = (int) component.getMinimumSize().getHeight(); 362 363 if (component != null) 364 { 365 if (title.stretch) 366 { 367 width = rightRule - leftRule; 368 component.setBounds(leftRule, 0, width, titleHeight); 369 } 370 371 else if (title.align == TwoColumnConstraints.LEFT) 372 { 373 component.setBounds(leftRule, 0, width, titleHeight); 374 } 375 376 else if (title.align == TwoColumnConstraints.CENTER) 377 { 378 int left = centerRule - (width / 2); 379 component.setBounds(left, 0, width, titleHeight); 380 } 381 382 else if (title.align == TwoColumnConstraints.RIGHT) 383 { 384 int left = rightRule - width; 385 component.setBounds(left, 0, width, titleHeight); 386 } 387 } 388 } 389 } 390 391 396 private void positionComponents(Container parent) 397 { 398 int usedHeight = titleHeight + minimumClusterHeight(); 399 int topBuffer = topBuffer(usedHeight, parent); 400 int leftHeight = 0; 401 int rightHeight = 0; 402 403 if (topBuffer < 0) 404 { 405 topBuffer = 0; 406 } 407 408 int y = titleHeight + topBuffer; 409 410 for (int i = 0; i < rows(); i++) 411 { 412 leftHeight = height(i, LEFT); 413 rightHeight = height(i, RIGHT); 414 415 if (leftHeight > rightHeight) 416 { 417 int offset = (leftHeight - rightHeight) / 2; 418 419 positionComponent(y, i, LEFT, parent); 420 positionComponent((y + offset), i, RIGHT, parent); 421 422 y += leftHeight; 423 } 424 else if (leftHeight < rightHeight) 425 { 426 int offset = (rightHeight - leftHeight) / 2; 427 428 positionComponent((y + offset), i, LEFT, parent); 429 positionComponent(y, i, RIGHT, parent); 430 431 y += rightHeight; 432 } 433 else 434 { 435 positionComponent(y, i, LEFT, parent); 436 positionComponent(y, i, RIGHT, parent); 437 438 y += leftHeight; 439 } 440 } 441 } 442 443 453 private void positionComponent(int y, int row, int column, Container parent) 454 { 455 TwoColumnConstraints constraints = null; 456 457 try 458 { 459 constraints = (TwoColumnConstraints) (components[column].elementAt(row)); 460 } 461 catch (Throwable exception) 462 { 463 return; 464 } 465 466 int x = 0; 467 468 if (constraints != null) 469 { 470 Component component = constraints.component; 471 int width = (int) component.getPreferredSize().getWidth(); 472 int height = (int) component.getPreferredSize().getHeight(); 473 474 if (column == LEFT) 479 { 480 x = leftRule; 481 } 482 else 483 { 484 x = centerRule; 485 } 486 487 if (component != null) 488 { 489 if ((constraints.stretch) && (constraints.position == TwoColumnConstraints.BOTH)) 494 { 495 width = rightRule - leftRule; 496 x = leftRule; 497 } 498 else if ((constraints.stretch) && (column == LEFT)) 499 { 500 width = centerRule - leftRule; 501 } 502 else if ((constraints.stretch) && (column == RIGHT)) 503 { 504 width = rightRule - centerRule; 505 } 506 507 else if (constraints.position == TwoColumnConstraints.BOTH) 514 { 515 if (width > (rightRule - leftRule)) 516 { 517 width = rightRule - leftRule; 518 } 519 x = leftRule; 520 } 521 522 if (constraints.indent) 526 { 527 width -= indent; 528 x += indent; 529 } 530 531 component.setBounds(x, y, width, height); 532 } 533 } 534 } 535 536 544 private int minimumColumnWidth(int column, Container parent) 545 { 546 Component component = null; 547 TwoColumnConstraints constraints = null; 548 int width = 0; 549 int temp = 0; 550 551 for (int i = 0; i < components[column].size(); i++) 552 { 553 constraints = (TwoColumnConstraints) components[column].elementAt(i); 554 555 if ((constraints != null) && (constraints.position != TwoColumnConstraints.BOTH)) 556 { 557 component = constraints.component; 558 temp = (int) component.getMinimumSize().getWidth(); 559 560 if (constraints.indent) 561 { 562 temp += indent; 563 } 564 565 if (temp > width) 566 { 567 width = temp; 568 } 569 } 570 } 571 572 return (width); 573 } 574 575 583 private int minimumBothColumnsWidth(Container parent) 584 { 585 Component component = null; 586 TwoColumnConstraints constraints = null; 587 int width = 0; 588 int temp = 0; 589 590 if (title != null) 591 { 592 component = title.component; 593 width = (int) component.getMinimumSize().getWidth(); 594 } 595 596 for (int i = 0; i < components[LEFT].size(); i++) 597 { 598 constraints = (TwoColumnConstraints) components[LEFT].elementAt(i); 599 600 if ((constraints != null) && (constraints.position == TwoColumnConstraints.BOTH)) 601 { 602 component = constraints.component; 603 temp = (int) component.getMinimumSize().getWidth(); 604 605 if (constraints.indent) 606 { 607 temp += indent; 608 } 609 610 if (temp > width) 611 { 612 width = temp; 613 } 614 } 615 } 616 617 return (width); 618 } 619 620 private int minimumClusterHeight() 621 { 622 int height = 0; 623 624 for (int i = 0; i < rows(); i++) 625 { 626 height += rowHeight(i); 627 } 628 629 return (height); 630 } 631 632 635 private int rows() 636 { 637 int rows = 0; 638 int leftRows = components[LEFT].size(); 639 int rightRows = components[RIGHT].size(); 640 641 if (leftRows > rightRows) 642 { 643 rows = leftRows; 644 } 645 else 646 { 647 rows = rightRows; 648 } 649 650 return (rows); 651 } 652 653 659 private int rowHeight(int row) 660 { 661 int height = 0; 662 int height1 = height(row, LEFT); 663 int height2 = height(row, RIGHT); 664 665 if (height1 > height2) 669 { 670 height = height1; 671 } 672 else 673 { 674 height = height2; 675 } 676 677 return (height); 678 } 679 680 688 private int height(int row, int column) 689 { 690 int height = 0; 691 int width = 0; 692 Component component; 693 TwoColumnConstraints constraints; 694 695 try 696 { 697 constraints = (TwoColumnConstraints) components[column].elementAt(row); 698 if (constraints != null) 699 { 700 component = constraints.component; 701 width = (int) component.getMinimumSize().getWidth(); 702 height = (int) component.getMinimumSize().getHeight(); 703 704 if (constraints.position == TwoColumnConstraints.WEST) 705 { 706 if (width > (centerRule - leftRule)) 707 { 708 component.setBounds(0, 0, (centerRule - leftRule), height); 709 } 710 } 711 else if (constraints.position == TwoColumnConstraints.EAST) 712 { 713 if (width > (rightRule - centerRule)) 714 { 715 component.setBounds(0, 0, (rightRule - centerRule), height); 716 } 717 } 718 else if (constraints.position == TwoColumnConstraints.BOTH) 719 { 720 if (width > (rightRule - leftRule)) 721 { 722 component.setBounds(0, 0, (rightRule - leftRule), height); 723 } 724 } 725 726 height = (int) component.getMinimumSize().getHeight(); 727 } 728 } 729 catch (Throwable exception) 736 {} 737 738 return (height); 739 } 740 741 746 private int margin(Container parent) 747 { 748 int amount = (int) (((parent.getSize().getWidth()) * margin) / 100); 749 750 return (amount); 751 } 752 753 760 private int topBuffer(int usedHeight, Container parent) 761 { 762 int amount = ((int) parent.getSize().getHeight()) - usedHeight; 763 amount = (int) (amount * topBuffer) / 100; 764 765 return (amount); 766 } 767 768 769 774 775 781 787 public Dimension preferredLayoutSize(Container parent) 788 { 789 return (minimumLayoutSize(parent)); 790 } 791 792 798 public Dimension minimumLayoutSize(Container parent) 799 { 800 positionTitle(parent); 801 802 int width = minimumBothColumnsWidth(parent); 803 int height = minimumClusterHeight() + titleHeight; 804 805 return (new Dimension(width, height)); 806 } 807 808 814 public Dimension maximumLayoutSize(Container parent) 815 { 816 return (minimumLayoutSize(parent)); 817 } 818 819 827 public float getLayoutAlignmentX(Container parent) 828 { 829 return (0); 830 } 831 832 840 public float getLayoutAlignmentY(Container parent) 841 { 842 return (0); 843 } 844 845 851 public void invalidateLayout(Container parent) 852 { 853 leftRule = 0; 854 rightRule = 0; 855 centerRule = 0; 856 titleHeight = 0; 857 } 858 859 866 public void addLayoutComponent(String name, Component comp) 867 { 868 } 869 870 877 public void removeLayoutComponent(Component comp) 878 { 879 Vector left = components[LEFT]; 880 Vector right = components[RIGHT]; 881 882 for (int i = 0; i < left.size(); i++) 883 { 884 TwoColumnConstraints constraints = (TwoColumnConstraints) left.get(i); 885 if (constraints == null) 886 { 887 continue; 888 } 889 Component ctemp = constraints.component; 890 if (ctemp != null && ctemp.equals(comp)) 891 { 892 if (constraints.position == TwoColumnConstraints.BOTH || constraints.position == TwoColumnConstraints.WESTONLY) 893 { 894 right.remove(i); 895 } 896 break; 897 } 898 } 899 900 for (int j = 0; j < right.size(); j++) 901 { 902 TwoColumnConstraints constraints = (TwoColumnConstraints) right.get(j); 903 if (constraints == null) 904 { 905 continue; 906 } 907 Component ctemp = constraints.component; 908 if (ctemp != null && ctemp.equals(comp)) 909 { 910 if (constraints.position == TwoColumnConstraints.BOTH || constraints.position == TwoColumnConstraints.EASTONLY) 911 { 912 left.remove(j); 913 } 914 break; 915 } 916 } 917 } 918 919 938 public void showRules(Graphics2D graphics, Color color) 939 { 940 int height = graphics.getClipBounds().height; 941 942 Stroke currentStroke = graphics.getStroke(); 943 Color currentColor = graphics.getColor(); 944 945 Stroke stroke = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 1.5f, 946 new float[] { 10, 5}, 5); 947 graphics.setColor(color); 948 949 graphics.drawLine(leftRule, 0, leftRule, height); 950 graphics.drawLine(centerRule, titleHeight, centerRule, height); 951 graphics.drawLine(rightRule, 0, rightRule, height); 952 graphics.drawLine(leftRule, titleHeight, rightRule, titleHeight); 953 954 graphics.setStroke(stroke); 955 graphics.drawLine((leftRule + indent), titleHeight, (leftRule + indent), height); 956 graphics.drawLine((centerRule + indent), titleHeight, (centerRule + indent), height); 957 958 graphics.setStroke(currentStroke); 959 graphics.setColor(currentColor); 960 } 961 } 962 963 | Popular Tags |