Changeset 3095 in josm for trunk/src/org/openstreetmap/josm/gui/MultiSplitLayout.java
- Timestamp:
- 08.03.2010 09:24:03 (2 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/MultiSplitLayout.java
r3083 r3095 42 42 import java.util.ListIterator; 43 43 import java.util.Map; 44 44 45 import javax.swing.UIManager; 45 46 … … 141 142 */ 142 143 public void setModel(Node model) { 143 if ((model == null) || (model instanceof Divider)) {144 if ((model == null) || (model instanceof Divider)) 144 145 throw new IllegalArgumentException("invalid model"); 145 }146 146 Node oldModel = model; 147 147 this.model = model; … … 168 168 */ 169 169 public void setDividerSize(int dividerSize) { 170 if (dividerSize < 0) {170 if (dividerSize < 0) 171 171 throw new IllegalArgumentException("invalid dividerSize"); 172 }173 172 int oldDividerSize = this.dividerSize; 174 173 this.dividerSize = dividerSize; … … 212 211 */ 213 212 public void addLayoutComponent(String name, Component child) { 214 if (name == null) {213 if (name == null) 215 214 throw new IllegalArgumentException("name not specified"); 216 }217 215 childMap.put(name, child); 218 216 } … … 253 251 254 252 private Dimension preferredNodeSize(Node root) { 255 if (root instanceof Leaf) {253 if (root instanceof Leaf) 256 254 return preferredComponentSize(root); 257 }258 255 else if (root instanceof Divider) { 259 256 int dividerSize = getDividerSize(); … … 363 360 Rectangle splitBounds = split.getBounds(); 364 361 ListIterator<Node> splitChildren = split.getChildren().listIterator(); 365 Node lastWeightedChild = split.lastWeightedChild();366 362 367 363 if (split.isRowLayout()) { … … 385 381 (totalWeightedWidth - minWeightedWidth) > extraWidth; 386 382 387 while(splitChildren.hasNext()) {388 Node splitChild = splitChildren.next();389 Rectangle splitChildBounds = splitChild.getBounds();390 double minSplitChildWidth = minimumNodeSize(splitChild).getWidth();391 double splitChildWeight = (onlyShrinkWeightedComponents)383 while(splitChildren.hasNext()) { 384 Node splitChild = splitChildren.next(); 385 Rectangle splitChildBounds = splitChild.getBounds(); 386 double minSplitChildWidth = minimumNodeSize(splitChild).getWidth(); 387 double splitChildWeight = (onlyShrinkWeightedComponents) 392 388 ? splitChild.getWeight() 393 : (splitChildBounds.getWidth() / (double)totalWidth);394 395 if (!splitChildren.hasNext()) {396 double newWidth = Math.max(minSplitChildWidth, bounds.getMaxX() - x);397 Rectangle newSplitChildBounds = boundsWithXandWidth(bounds, x, newWidth);398 layout2(splitChild, newSplitChildBounds);399 }400 else if ((availableWidth > 0.0) && (splitChildWeight > 0.0)) {401 double allocatedWidth = Math.rint(splitChildWeight * extraWidth);402 double oldWidth = splitChildBounds.getWidth();403 double newWidth = Math.max(minSplitChildWidth, oldWidth - allocatedWidth);404 Rectangle newSplitChildBounds = boundsWithXandWidth(bounds, x, newWidth);405 layout2(splitChild, newSplitChildBounds);406 availableWidth -= (oldWidth - splitChild.getBounds().getWidth());407 }408 else {409 double existingWidth = splitChildBounds.getWidth();410 Rectangle newSplitChildBounds = boundsWithXandWidth(bounds, x, existingWidth);411 layout2(splitChild, newSplitChildBounds);412 }413 x = splitChild.getBounds().getMaxX();414 }389 : (splitChildBounds.getWidth() / totalWidth); 390 391 if (!splitChildren.hasNext()) { 392 double newWidth = Math.max(minSplitChildWidth, bounds.getMaxX() - x); 393 Rectangle newSplitChildBounds = boundsWithXandWidth(bounds, x, newWidth); 394 layout2(splitChild, newSplitChildBounds); 395 } 396 else if ((availableWidth > 0.0) && (splitChildWeight > 0.0)) { 397 double allocatedWidth = Math.rint(splitChildWeight * extraWidth); 398 double oldWidth = splitChildBounds.getWidth(); 399 double newWidth = Math.max(minSplitChildWidth, oldWidth - allocatedWidth); 400 Rectangle newSplitChildBounds = boundsWithXandWidth(bounds, x, newWidth); 401 layout2(splitChild, newSplitChildBounds); 402 availableWidth -= (oldWidth - splitChild.getBounds().getWidth()); 403 } 404 else { 405 double existingWidth = splitChildBounds.getWidth(); 406 Rectangle newSplitChildBounds = boundsWithXandWidth(bounds, x, existingWidth); 407 layout2(splitChild, newSplitChildBounds); 408 } 409 x = splitChild.getBounds().getMaxX(); 410 } 415 411 } 416 412 … … 435 431 (totalWeightedHeight - minWeightedHeight) > extraHeight; 436 432 437 while(splitChildren.hasNext()) {438 Node splitChild = splitChildren.next();439 Rectangle splitChildBounds = splitChild.getBounds();440 double minSplitChildHeight = minimumNodeSize(splitChild).getHeight();441 double splitChildWeight = (onlyShrinkWeightedComponents)433 while(splitChildren.hasNext()) { 434 Node splitChild = splitChildren.next(); 435 Rectangle splitChildBounds = splitChild.getBounds(); 436 double minSplitChildHeight = minimumNodeSize(splitChild).getHeight(); 437 double splitChildWeight = (onlyShrinkWeightedComponents) 442 438 ? splitChild.getWeight() 443 : (splitChildBounds.getHeight() / (double)totalHeight);444 445 if (!splitChildren.hasNext()) {446 double oldHeight = splitChildBounds.getHeight();447 double newHeight = Math.max(minSplitChildHeight, bounds.getMaxY() - y);448 Rectangle newSplitChildBounds = boundsWithYandHeight(bounds, y, newHeight);449 layout2(splitChild, newSplitChildBounds);450 availableHeight -= (oldHeight - splitChild.getBounds().getHeight());451 }452 else if ((availableHeight > 0.0) && (splitChildWeight > 0.0)) {453 double allocatedHeight = Math.rint(splitChildWeight * extraHeight);454 double oldHeight = splitChildBounds.getHeight();455 double newHeight = Math.max(minSplitChildHeight, oldHeight - allocatedHeight);456 Rectangle newSplitChildBounds = boundsWithYandHeight(bounds, y, newHeight);457 layout2(splitChild, newSplitChildBounds);458 availableHeight -= (oldHeight - splitChild.getBounds().getHeight());459 }460 else {461 double existingHeight = splitChildBounds.getHeight();462 Rectangle newSplitChildBounds = boundsWithYandHeight(bounds, y, existingHeight);463 layout2(splitChild, newSplitChildBounds);464 }465 y = splitChild.getBounds().getMaxY();466 }439 : (splitChildBounds.getHeight() / totalHeight); 440 441 if (!splitChildren.hasNext()) { 442 double oldHeight = splitChildBounds.getHeight(); 443 double newHeight = Math.max(minSplitChildHeight, bounds.getMaxY() - y); 444 Rectangle newSplitChildBounds = boundsWithYandHeight(bounds, y, newHeight); 445 layout2(splitChild, newSplitChildBounds); 446 availableHeight -= (oldHeight - splitChild.getBounds().getHeight()); 447 } 448 else if ((availableHeight > 0.0) && (splitChildWeight > 0.0)) { 449 double allocatedHeight = Math.rint(splitChildWeight * extraHeight); 450 double oldHeight = splitChildBounds.getHeight(); 451 double newHeight = Math.max(minSplitChildHeight, oldHeight - allocatedHeight); 452 Rectangle newSplitChildBounds = boundsWithYandHeight(bounds, y, newHeight); 453 layout2(splitChild, newSplitChildBounds); 454 availableHeight -= (oldHeight - splitChild.getBounds().getHeight()); 455 } 456 else { 457 double existingHeight = splitChildBounds.getHeight(); 458 Rectangle newSplitChildBounds = boundsWithYandHeight(bounds, y, existingHeight); 459 layout2(splitChild, newSplitChildBounds); 460 } 461 y = splitChild.getBounds().getMaxY(); 462 } 467 463 } 468 464 … … 508 504 else if ((availableWidth > 0.0) && (splitChildWeight > 0.0)) { 509 505 double allocatedWidth = (splitChild.equals(lastWeightedChild)) 510 ? availableWidth511 : Math.rint(splitChildWeight * extraWidth);506 ? availableWidth 507 : Math.rint(splitChildWeight * extraWidth); 512 508 double newWidth = splitChildBounds.getWidth() + allocatedWidth; 513 509 Rectangle newSplitChildBounds = boundsWithXandWidth(bounds, x, newWidth); … … 550 546 else if ((availableHeight > 0.0) && (splitChildWeight > 0.0)) { 551 547 double allocatedHeight = (splitChild.equals(lastWeightedChild)) 552 ? availableHeight553 : Math.rint(splitChildWeight * extraHeight);548 ? availableHeight 549 : Math.rint(splitChildWeight * extraHeight); 554 550 double newHeight = splitChildBounds.getHeight() + allocatedHeight; 555 551 Rectangle newSplitChildBounds = boundsWithYandHeight(bounds, y, newHeight); … … 570 566 * as needed. 571 567 */ 572 private void layout2(Node root, Rectangle bounds) {568 private void layout2(Node root, Rectangle bounds) { 573 569 if (root instanceof Leaf) { 574 570 Component child = childForNode(root); … … 584 580 Split split = (Split)root; 585 581 boolean grow = split.isRowLayout() 586 ? (split.getBounds().width <= bounds.width)587 : (split.getBounds().height <= bounds.height);582 ? (split.getBounds().width <= bounds.width) 583 : (split.getBounds().height <= bounds.height); 588 584 if (grow) { 589 585 layoutGrow(split, bounds); … … 635 631 (splitChildren.hasNext()) ? (Divider)(splitChildren.next()) : null; 636 632 637 double childWidth = 0.0; 638 if (getFloatingDividers()) { 639 childWidth = preferredNodeSize(splitChild).getWidth(); 640 } 641 else { 642 if (dividerChild != null) { 643 childWidth = dividerChild.getBounds().getX() - x; 633 double childWidth = 0.0; 634 if (getFloatingDividers()) { 635 childWidth = preferredNodeSize(splitChild).getWidth(); 644 636 } 645 637 else { 646 childWidth = split.getBounds().getMaxX() - x; 638 if (dividerChild != null) { 639 childWidth = dividerChild.getBounds().getX() - x; 640 } 641 else { 642 childWidth = split.getBounds().getMaxX() - x; 643 } 647 644 } 648 } 649 childBounds = boundsWithXandWidth(bounds, x, childWidth); 650 layout1(splitChild, childBounds); 651 652 if (getFloatingDividers() && (dividerChild != null)) { 653 double dividerX = childBounds.getMaxX(); 654 Rectangle dividerBounds = boundsWithXandWidth(bounds, dividerX, dividerSize); 655 dividerChild.setBounds(dividerBounds); 656 } 657 if (dividerChild != null) { 658 x = dividerChild.getBounds().getMaxX(); 659 } 645 childBounds = boundsWithXandWidth(bounds, x, childWidth); 646 layout1(splitChild, childBounds); 647 648 if (getFloatingDividers() && (dividerChild != null)) { 649 double dividerX = childBounds.getMaxX(); 650 Rectangle dividerBounds = boundsWithXandWidth(bounds, dividerX, dividerSize); 651 dividerChild.setBounds(dividerBounds); 652 } 653 if (dividerChild != null) { 654 x = dividerChild.getBounds().getMaxX(); 655 } 660 656 } 661 657 } … … 673 669 (splitChildren.hasNext()) ? (Divider)(splitChildren.next()) : null; 674 670 675 double childHeight = 0.0; 676 if (getFloatingDividers()) { 677 childHeight = preferredNodeSize(splitChild).getHeight(); 678 } 679 else { 680 if (dividerChild != null) { 681 childHeight = dividerChild.getBounds().getY() - y; 671 double childHeight = 0.0; 672 if (getFloatingDividers()) { 673 childHeight = preferredNodeSize(splitChild).getHeight(); 682 674 } 683 675 else { 684 childHeight = split.getBounds().getMaxY() - y; 676 if (dividerChild != null) { 677 childHeight = dividerChild.getBounds().getY() - y; 678 } 679 else { 680 childHeight = split.getBounds().getMaxY() - y; 681 } 685 682 } 686 } 687 childBounds = boundsWithYandHeight(bounds, y, childHeight); 688 layout1(splitChild, childBounds); 689 690 if (getFloatingDividers() && (dividerChild != null)) { 691 double dividerY = childBounds.getMaxY(); 692 Rectangle dividerBounds = boundsWithYandHeight(bounds, dividerY, dividerSize); 693 dividerChild.setBounds(dividerBounds); 694 } 695 if (dividerChild != null) { 696 y = dividerChild.getBounds().getMaxY(); 697 } 683 childBounds = boundsWithYandHeight(bounds, y, childHeight); 684 layout1(splitChild, childBounds); 685 686 if (getFloatingDividers() && (dividerChild != null)) { 687 double dividerY = childBounds.getMaxY(); 688 Rectangle dividerBounds = boundsWithYandHeight(bounds, dividerY, dividerSize); 689 dividerChild.setBounds(dividerBounds); 690 } 691 if (dividerChild != null) { 692 y = dividerChild.getBounds().getMaxY(); 693 } 698 694 } 699 695 } … … 781 777 Split split = (Split)root; 782 778 for(Node child : split.getChildren()) { 783 if (child.getBounds().contains(x, y)) {779 if (child.getBounds().contains(x, y)) 784 780 return dividerAt(child, x, y); 785 }786 781 } 787 782 } … … 804 799 Rectangle r1 = node.getBounds(); 805 800 return 806 (r1.x <= (r2.x + r2.width)) && ((r1.x + r1.width) >= r2.x) &&807 (r1.y <= (r2.y + r2.height)) && ((r1.y + r1.height) >= r2.y);801 (r1.x <= (r2.x + r2.width)) && ((r1.x + r1.width) >= r2.x) && 802 (r1.y <= (r2.y + r2.height)) && ((r1.y + r1.height) >= r2.y); 808 803 } 809 804 … … 822 817 } 823 818 return dividers; 824 } 825 else { 819 } else 826 820 return Collections.emptyList(); 827 }828 821 } 829 822 … … 837 830 */ 838 831 public List<Divider> dividersThatOverlap(Rectangle r) { 839 if (r == null) {832 if (r == null) 840 833 throw new IllegalArgumentException("null Rectangle"); 841 }842 834 return dividersThatOverlap(getModel(), r); 843 835 } … … 890 882 */ 891 883 public void setBounds(Rectangle bounds) { 892 if (bounds == null) {884 if (bounds == null) 893 885 throw new IllegalArgumentException("null bounds"); 894 }895 886 this.bounds = new Rectangle(bounds); 896 887 } … … 921 912 */ 922 913 public void setWeight(double weight) { 923 if ((weight < 0.0)|| (weight > 1.0)) {914 if ((weight < 0.0)|| (weight > 1.0)) 924 915 throw new IllegalArgumentException("invalid weight"); 925 }926 916 this.weight = weight; 927 917 } … … 929 919 private Node siblingAtOffset(int offset) { 930 920 Split parent = getParent(); 931 if (parent == null) { return null; } 921 if (parent == null) 922 return null; 932 923 List<Node> siblings = parent.getChildren(); 933 924 int index = siblings.indexOf(this); 934 if (index == -1) { return null; } 925 if (index == -1) 926 return null; 935 927 index += offset; 936 928 return ((index > -1) && (index < siblings.size())) ? siblings.get(index) : null; … … 1020 1012 */ 1021 1013 public void setChildren(List<Node> children) { 1022 if (children == null) {1014 if (children == null) 1023 1015 throw new IllegalArgumentException("children must be a non-null List"); 1024 }1025 1016 for(Node child : this.children) { 1026 1017 child.setParent(null); … … 1051 1042 } 1052 1043 1044 @Override 1053 1045 public String toString() { 1054 1046 int nChildren = getChildren().size(); … … 1081 1073 */ 1082 1074 public Leaf(String name) { 1083 if (name == null) {1075 if (name == null) 1084 1076 throw new IllegalArgumentException("name is null"); 1085 }1086 1077 this.name = name; 1087 1078 } … … 1102 1093 */ 1103 1094 public void setName(String name) { 1104 if (name == null) {1095 if (name == null) 1105 1096 throw new IllegalArgumentException("name is null"); 1106 }1107 1097 this.name = name; 1108 1098 } 1109 1099 1100 @Override 1110 1101 public String toString() { 1111 1102 StringBuffer sb = new StringBuffer("MultiSplitLayout.Leaf"); … … 1142 1133 * @throws UnsupportedOperationException 1143 1134 */ 1135 @Override 1144 1136 public void setWeight(double weight) { 1145 1137 throw new UnsupportedOperationException(); 1146 1138 } 1147 1139 1140 @Override 1148 1141 public String toString() { 1149 1142 return "MultiSplitLayout.Divider " + getBounds().toString();
Note: See TracChangeset
for help on using the changeset viewer.
