1 7 8 9 10 package javax.swing; 11 12 13 14 import javax.swing.border.Border ; 15 import javax.swing.plaf.*; 16 import javax.accessibility.*; 17 18 import java.awt.*; 19 20 import java.io.ObjectOutputStream ; 21 import java.io.ObjectInputStream ; 22 import java.io.IOException ; 23 24 25 26 79 public class JSplitPane extends JComponent implements Accessible 80 { 81 85 private static final String uiClassID = "SplitPaneUI"; 86 87 92 public final static int VERTICAL_SPLIT = 0; 93 94 100 public final static int HORIZONTAL_SPLIT = 1; 101 102 106 public final static String LEFT = "left"; 107 108 112 public final static String RIGHT = "right"; 113 114 118 public final static String TOP = "top"; 119 120 124 public final static String BOTTOM = "bottom"; 125 126 129 public final static String DIVIDER = "divider"; 130 131 134 public final static String ORIENTATION_PROPERTY = "orientation"; 135 136 139 public final static String CONTINUOUS_LAYOUT_PROPERTY = "continuousLayout"; 140 141 144 public final static String DIVIDER_SIZE_PROPERTY = "dividerSize"; 145 146 149 public final static String ONE_TOUCH_EXPANDABLE_PROPERTY = 150 "oneTouchExpandable"; 151 152 155 public final static String LAST_DIVIDER_LOCATION_PROPERTY = 156 "lastDividerLocation"; 157 158 162 public final static String DIVIDER_LOCATION_PROPERTY = "dividerLocation"; 163 164 168 public final static String RESIZE_WEIGHT_PROPERTY = "resizeWeight"; 169 170 173 protected int orientation; 174 175 179 protected boolean continuousLayout; 180 181 184 protected Component leftComponent; 185 186 189 protected Component rightComponent; 190 191 194 protected int dividerSize; 195 private boolean dividerSizeSet = false; 196 197 201 protected boolean oneTouchExpandable; 202 private boolean oneTouchExpandableSet; 203 204 207 protected int lastDividerLocation; 208 209 212 private double resizeWeight; 213 214 218 private int dividerLocation; 219 220 221 226 public JSplitPane() { 227 this(JSplitPane.HORIZONTAL_SPLIT, false, 228 new JButton (UIManager.getString("SplitPane.leftButtonText")), 229 new JButton (UIManager.getString("SplitPane.rightButtonText"))); 230 } 231 232 233 242 public JSplitPane(int newOrientation) { 243 this(newOrientation, false); 244 } 245 246 247 259 public JSplitPane(int newOrientation, 260 boolean newContinuousLayout) { 261 this(newOrientation, newContinuousLayout, null, null); 262 } 263 264 265 284 public JSplitPane(int newOrientation, 285 Component newLeftComponent, 286 Component newRightComponent){ 287 this(newOrientation, false, newLeftComponent, newRightComponent); 288 } 289 290 291 312 public JSplitPane(int newOrientation, 313 boolean newContinuousLayout, 314 Component newLeftComponent, 315 Component newRightComponent){ 316 super(); 317 318 dividerLocation = -1; 319 setLayout(null); 320 setUIProperty("opaque", Boolean.TRUE); 321 orientation = newOrientation; 322 if (orientation != HORIZONTAL_SPLIT && orientation != VERTICAL_SPLIT) 323 throw new IllegalArgumentException ("cannot create JSplitPane, " + 324 "orientation must be one of " + 325 "JSplitPane.HORIZONTAL_SPLIT " + 326 "or JSplitPane.VERTICAL_SPLIT"); 327 continuousLayout = newContinuousLayout; 328 if (newLeftComponent != null) 329 setLeftComponent(newLeftComponent); 330 if (newRightComponent != null) 331 setRightComponent(newRightComponent); 332 updateUI(); 333 334 } 335 336 337 348 public void setUI(SplitPaneUI ui) { 349 if ((SplitPaneUI)this.ui != ui) { 350 super.setUI(ui); 351 revalidate(); 352 } 353 } 354 355 356 365 public SplitPaneUI getUI() { 366 return (SplitPaneUI)ui; 367 } 368 369 370 377 public void updateUI() { 378 setUI((SplitPaneUI)UIManager.getUI(this)); 379 revalidate(); 380 } 381 382 383 393 public String getUIClassID() { 394 return uiClassID; 395 } 396 397 398 406 public void setDividerSize(int newSize) { 407 int oldSize = dividerSize; 408 409 dividerSizeSet = true; 410 if (oldSize != newSize) { 411 dividerSize = newSize; 412 firePropertyChange(DIVIDER_SIZE_PROPERTY, oldSize, newSize); 413 } 414 } 415 416 417 422 public int getDividerSize() { 423 return dividerSize; 424 } 425 426 427 432 public void setLeftComponent(Component comp) { 433 if (comp == null) { 434 if (leftComponent != null) { 435 remove(leftComponent); 436 leftComponent = null; 437 } 438 } else { 439 add(comp, JSplitPane.LEFT); 440 } 441 } 442 443 444 452 public Component getLeftComponent() { 453 return leftComponent; 454 } 455 456 457 464 public void setTopComponent(Component comp) { 465 setLeftComponent(comp); 466 } 467 468 469 474 public Component getTopComponent() { 475 return leftComponent; 476 } 477 478 479 487 public void setRightComponent(Component comp) { 488 if (comp == null) { 489 if (rightComponent != null) { 490 remove(rightComponent); 491 rightComponent = null; 492 } 493 } else { 494 add(comp, JSplitPane.RIGHT); 495 } 496 } 497 498 499 504 public Component getRightComponent() { 505 return rightComponent; 506 } 507 508 509 516 public void setBottomComponent(Component comp) { 517 setRightComponent(comp); 518 } 519 520 521 526 public Component getBottomComponent() { 527 return rightComponent; 528 } 529 530 531 549 public void setOneTouchExpandable(boolean newValue) { 550 boolean oldValue = oneTouchExpandable; 551 552 oneTouchExpandable = newValue; 553 oneTouchExpandableSet = true; 554 firePropertyChange(ONE_TOUCH_EXPANDABLE_PROPERTY, oldValue, newValue); 555 repaint(); 556 } 557 558 559 565 public boolean isOneTouchExpandable() { 566 return oneTouchExpandable; 567 } 568 569 570 581 public void setLastDividerLocation(int newLastLocation) { 582 int oldLocation = lastDividerLocation; 583 584 lastDividerLocation = newLastLocation; 585 firePropertyChange(LAST_DIVIDER_LOCATION_PROPERTY, oldLocation, 586 newLastLocation); 587 } 588 589 590 597 public int getLastDividerLocation() { 598 return lastDividerLocation; 599 } 600 601 602 618 public void setOrientation(int orientation) { 619 if ((orientation != VERTICAL_SPLIT) && 620 (orientation != HORIZONTAL_SPLIT)) { 621 throw new IllegalArgumentException ("JSplitPane: orientation must " + 622 "be one of " + 623 "JSplitPane.VERTICAL_SPLIT or " + 624 "JSplitPane.HORIZONTAL_SPLIT"); 625 } 626 627 int oldOrientation = this.orientation; 628 629 this.orientation = orientation; 630 firePropertyChange(ORIENTATION_PROPERTY, oldOrientation, orientation); 631 } 632 633 634 640 public int getOrientation() { 641 return orientation; 642 } 643 644 645 663 public void setContinuousLayout(boolean newContinuousLayout) { 664 boolean oldCD = continuousLayout; 665 666 continuousLayout = newContinuousLayout; 667 firePropertyChange(CONTINUOUS_LAYOUT_PROPERTY, oldCD, 668 newContinuousLayout); 669 } 670 671 672 678 public boolean isContinuousLayout() { 679 return continuousLayout; 680 } 681 682 700 public void setResizeWeight(double value) { 701 if (value < 0 || value > 1) { 702 throw new IllegalArgumentException ("JSplitPane weight must be between 0 and 1"); 703 } 704 double oldWeight = resizeWeight; 705 706 resizeWeight = value; 707 firePropertyChange(RESIZE_WEIGHT_PROPERTY, oldWeight, value); 708 } 709 710 716 public double getResizeWeight() { 717 return resizeWeight; 718 } 719 720 725 public void resetToPreferredSizes() { 726 SplitPaneUI ui = getUI(); 727 728 if (ui != null) { 729 ui.resetToPreferredSizes(this); 730 } 731 } 732 733 734 753 public void setDividerLocation(double proportionalLocation) { 754 if (proportionalLocation < 0.0 || 755 proportionalLocation > 1.0) { 756 throw new IllegalArgumentException ("proportional location must " + 757 "be between 0.0 and 1.0."); 758 } 759 if (getOrientation() == VERTICAL_SPLIT) { 760 setDividerLocation((int)((double)(getHeight() - getDividerSize()) * 761 proportionalLocation)); 762 } else { 763 setDividerLocation((int)((double)(getWidth() - getDividerSize()) * 764 proportionalLocation)); 765 } 766 } 767 768 769 783 public void setDividerLocation(int location) { 784 int oldValue = dividerLocation; 785 786 dividerLocation = location; 787 788 SplitPaneUI ui = getUI(); 790 791 if (ui != null) { 792 ui.setDividerLocation(this, location); 793 } 794 795 firePropertyChange(DIVIDER_LOCATION_PROPERTY, oldValue, location); 797 798 setLastDividerLocation(oldValue); 800 } 801 802 803 811 public int getDividerLocation() { 812 return dividerLocation; 813 } 814 815 816 826 public int getMinimumDividerLocation() { 827 SplitPaneUI ui = getUI(); 828 829 if (ui != null) { 830 return ui.getMinimumDividerLocation(this); 831 } 832 return -1; 833 } 834 835 836 844 public int getMaximumDividerLocation() { 845 SplitPaneUI ui = getUI(); 846 847 if (ui != null) { 848 return ui.getMaximumDividerLocation(this); 849 } 850 return -1; 851 } 852 853 854 861 public void remove(Component component) { 862 if (component == leftComponent) { 863 leftComponent = null; 864 } else if (component == rightComponent) { 865 rightComponent = null; 866 } 867 super.remove(component); 868 869 revalidate(); 871 repaint(); 872 } 873 874 875 884 public void remove(int index) { 885 Component comp = getComponent(index); 886 887 if (comp == leftComponent) { 888 leftComponent = null; 889 } else if (comp == rightComponent) { 890 rightComponent = null; 891 } 892 super.remove(index); 893 894 revalidate(); 896 repaint(); 897 } 898 899 900 905 public void removeAll() { 906 leftComponent = rightComponent = null; 907 super.removeAll(); 908 909 revalidate(); 911 repaint(); 912 } 913 914 915 927 public boolean isValidateRoot() { 928 return true; 929 } 930 931 932 962 protected void addImpl(Component comp, Object constraints, int index) 963 { 964 Component toRemove; 965 966 if (constraints != null && !(constraints instanceof String )) { 967 throw new IllegalArgumentException ("cannot add to layout: " + 968 "constraint must be a string " + 969 "(or null)"); 970 } 971 972 974 if (constraints == null) { 975 if (getLeftComponent() == null) { 976 constraints = JSplitPane.LEFT; 977 } else if (getRightComponent() == null) { 978 constraints = JSplitPane.RIGHT; 979 } 980 } 981 982 983 if (constraints != null && (constraints.equals(JSplitPane.LEFT) || 984 constraints.equals(JSplitPane.TOP))) { 985 toRemove = getLeftComponent(); 986 if (toRemove != null) { 987 remove(toRemove); 988 } 989 leftComponent = comp; 990 index = -1; 991 } else if (constraints != null && 992 (constraints.equals(JSplitPane.RIGHT) || 993 constraints.equals(JSplitPane.BOTTOM))) { 994 toRemove = getRightComponent(); 995 if (toRemove != null) { 996 remove(toRemove); 997 } 998 rightComponent = comp; 999 index = -1; 1000 } else if (constraints != null && 1001 constraints.equals(JSplitPane.DIVIDER)) { 1002 index = -1; 1003 } 1004 1005 1006 super.addImpl(comp, constraints, index); 1007 1008 revalidate(); 1010 repaint(); 1011 } 1012 1013 1014 1020 protected void paintChildren(Graphics g) { 1021 super.paintChildren(g); 1022 1023 SplitPaneUI ui = getUI(); 1024 1025 if (ui != null) { 1026 Graphics tempG = g.create(); 1027 ui.finishedPaintingChildren(this, tempG); 1028 tempG.dispose(); 1029 } 1030 } 1031 1032 1033 1038 private void writeObject(ObjectOutputStream s) throws IOException { 1039 s.defaultWriteObject(); 1040 if (getUIClassID().equals(uiClassID)) { 1041 byte count = JComponent.getWriteObjCounter(this); 1042 JComponent.setWriteObjCounter(this, --count); 1043 if (count == 0 && ui != null) { 1044 ui.installUI(this); 1045 } 1046 } 1047 } 1048 1049 void setUIProperty(String propertyName, Object value) { 1050 if (propertyName == "dividerSize") { 1051 if (!dividerSizeSet) { 1052 setDividerSize(((Number )value).intValue()); 1053 dividerSizeSet = false; 1054 } 1055 } else if (propertyName == "oneTouchExpandable") { 1056 if (!oneTouchExpandableSet) { 1057 setOneTouchExpandable(((Boolean )value).booleanValue()); 1058 oneTouchExpandableSet = false; 1059 } 1060 } else { 1061 super.setUIProperty(propertyName, value); 1062 } 1063 } 1064 1065 1066 1076 protected String paramString() { 1077 String orientationString = (orientation == HORIZONTAL_SPLIT ? 1078 "HORIZONTAL_SPLIT" : "VERTICAL_SPLIT"); 1079 String continuousLayoutString = (continuousLayout ? 1080 "true" : "false"); 1081 String oneTouchExpandableString = (oneTouchExpandable ? 1082 "true" : "false"); 1083 1084 return super.paramString() + 1085 ",continuousLayout=" + continuousLayoutString + 1086 ",dividerSize=" + dividerSize + 1087 ",lastDividerLocation=" + lastDividerLocation + 1088 ",oneTouchExpandable=" + oneTouchExpandableString + 1089 ",orientation=" + orientationString; 1090 } 1091 1092 1093 1094 1098 1099 1111 public AccessibleContext getAccessibleContext() { 1112 if (accessibleContext == null) { 1113 accessibleContext = new AccessibleJSplitPane(); 1114 } 1115 return accessibleContext; 1116 } 1117 1118 1119 1133 protected class AccessibleJSplitPane extends AccessibleJComponent 1134 implements AccessibleValue { 1135 1142 public AccessibleStateSet getAccessibleStateSet() { 1143 AccessibleStateSet states = super.getAccessibleStateSet(); 1144 if (getOrientation() == VERTICAL_SPLIT) { 1148 states.add(AccessibleState.VERTICAL); 1149 } else { 1150 states.add(AccessibleState.HORIZONTAL); 1151 } 1152 return states; 1153 } 1154 1155 1156 1164 public AccessibleValue getAccessibleValue() { 1165 return this; 1166 } 1167 1168 1169 1174 public Number getCurrentAccessibleValue() { 1175 return new Integer (getDividerLocation()); 1176 } 1177 1178 1179 1184 public boolean setCurrentAccessibleValue(Number n) { 1185 if (n == null) { 1187 return false; 1188 } 1189 setDividerLocation(n.intValue()); 1190 return true; 1191 } 1192 1193 1194 1199 public Number getMinimumAccessibleValue() { 1200 return new Integer (getUI().getMinimumDividerLocation( 1201 JSplitPane.this)); 1202 } 1203 1204 1205 1210 public Number getMaximumAccessibleValue() { 1211 return new Integer (getUI().getMaximumDividerLocation( 1212 JSplitPane.this)); 1213 } 1214 1215 1216 1223 public AccessibleRole getAccessibleRole() { 1224 return AccessibleRole.SPLIT_PANE; 1225 } 1226 } } 1228 | Popular Tags |