GNU Classpath (0.19) | ||
Frames | No Frames |
1: /* JSplitPane.java -- 2: Copyright (C) 2004 Free Software Foundation, Inc. 3: 4: This file is part of GNU Classpath. 5: 6: GNU Classpath is free software; you can redistribute it and/or modify 7: it under the terms of the GNU General Public License as published by 8: the Free Software Foundation; either version 2, or (at your option) 9: any later version. 10: 11: GNU Classpath is distributed in the hope that it will be useful, but 12: WITHOUT ANY WARRANTY; without even the implied warranty of 13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14: General Public License for more details. 15: 16: You should have received a copy of the GNU General Public License 17: along with GNU Classpath; see the file COPYING. If not, write to the 18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 19: 02110-1301 USA. 20: 21: Linking this library statically or dynamically with other modules is 22: making a combined work based on this library. Thus, the terms and 23: conditions of the GNU General Public License cover the whole 24: combination. 25: 26: As a special exception, the copyright holders of this library give you 27: permission to link this library with independent modules to produce an 28: executable, regardless of the license terms of these independent 29: modules, and to copy and distribute the resulting executable under 30: terms of your choice, provided that you also meet, for each linked 31: independent module, the terms and conditions of the license of that 32: module. An independent module is a module which is not derived from 33: or based on this library. If you modify this library, you may extend 34: this exception to your version of the library, but you are not 35: obligated to do so. If you do not wish to do so, delete this 36: exception statement from your version. */ 37: 38: 39: package javax.swing; 40: 41: import java.awt.Component; 42: import java.awt.Graphics; 43: 44: import javax.accessibility.Accessible; 45: import javax.accessibility.AccessibleContext; 46: import javax.accessibility.AccessibleRole; 47: import javax.accessibility.AccessibleStateSet; 48: import javax.accessibility.AccessibleValue; 49: import javax.swing.plaf.SplitPaneUI; 50: 51: /** 52: * This class implements JSplitPane. It is used to divide two components. By 53: * dragging the SplitPane's divider, the user can resize the two components. 54: * Note that the divider cannot resize a component to smaller than it's 55: * minimum size. 56: */ 57: public class JSplitPane extends JComponent implements Accessible 58: { 59: /** 60: * DOCUMENT ME! 61: */ 62: // FIXME: This inner class is a complete stub and must be implemented 63: // properly. 64: protected class AccessibleJSplitPane extends JComponent.AccessibleJComponent 65: implements AccessibleValue 66: { 67: private static final long serialVersionUID = -1788116871416305366L; 68: 69: /** 70: * Creates a new AccessibleJSplitPane object. 71: */ 72: protected AccessibleJSplitPane() 73: { 74: // Nothing to do here. 75: } 76: 77: /** 78: * DOCUMENT ME! 79: * 80: * @return DOCUMENT ME! 81: */ 82: public AccessibleStateSet getAccessibleStateSet() 83: { 84: return null; 85: } 86: 87: /** 88: * DOCUMENT ME! 89: * 90: * @return DOCUMENT ME! 91: */ 92: public AccessibleRole getAccessibleRole() 93: { 94: return null; 95: } 96: 97: /** 98: * DOCUMENT ME! 99: * 100: * @return DOCUMENT ME! 101: */ 102: public AccessibleValue getAccessibleValue() 103: { 104: return null; 105: } 106: 107: /** 108: * DOCUMENT ME! 109: * 110: * @return DOCUMENT ME! 111: */ 112: public Number getCurrentAccessibleValue() 113: { 114: return null; 115: } 116: 117: /** 118: * DOCUMENT ME! 119: * 120: * @param value0 DOCUMENT ME! 121: * 122: * @return DOCUMENT ME! 123: */ 124: public boolean setCurrentAccessibleValue(Number value0) 125: { 126: return false; 127: } 128: 129: /** 130: * DOCUMENT ME! 131: * 132: * @return DOCUMENT ME! 133: */ 134: public Number getMinimumAccessibleValue() 135: { 136: return null; 137: } 138: 139: /** 140: * DOCUMENT ME! 141: * 142: * @return DOCUMENT ME! 143: */ 144: public Number getMaximumAccessibleValue() 145: { 146: return null; 147: } 148: } 149: 150: private static final long serialVersionUID = -5634142046175988380L; 151: 152: /** The constraints string used to add components to the bottom. */ 153: public static final String BOTTOM = "bottom"; 154: 155: /** The property fired when the continuousLayout property changes. */ 156: public static final String CONTINUOUS_LAYOUT_PROPERTY = "continuousLayout"; 157: 158: /** The property fired when the divider property changes. */ 159: public static final String DIVIDER = "divider"; 160: 161: /** The property fired when the divider location property changes. */ 162: public static final String DIVIDER_LOCATION_PROPERTY = "dividerLocation"; 163: 164: /** The property fired when the divider size property changes. */ 165: public static final String DIVIDER_SIZE_PROPERTY = "dividerSize"; 166: 167: /** 168: * The value of the orientation when the components are split horizontally. 169: */ 170: public static final int HORIZONTAL_SPLIT = 1; 171: 172: /** The property fired when the last divider location property changes. */ 173: public static final String LAST_DIVIDER_LOCATION_PROPERTY = "lastDividerLocation"; 174: 175: /** The constraints string used to add components to the left. */ 176: public static final String LEFT = "left"; 177: 178: /** The property fired when the one touch expandable property changes. */ 179: public static final String ONE_TOUCH_EXPANDABLE_PROPERTY = "oneTouchExpandable"; 180: 181: /** The property fired when the orientation property changes. */ 182: public static final String ORIENTATION_PROPERTY = "orientation"; 183: 184: /** The property fired when the resize weight property changes. */ 185: public static final String RESIZE_WEIGHT_PROPERTY = "resizeWeight"; 186: 187: /** The constraints string used to add components to the right. */ 188: public static final String RIGHT = "right"; 189: 190: /** The constraints string used to add components to the top. */ 191: public static final String TOP = "top"; 192: 193: /** The value of the orientation when the components are split vertically. */ 194: public static final int VERTICAL_SPLIT = 0; 195: 196: /** Whether the JSplitPane uses continuous layout. */ 197: protected boolean continuousLayout; 198: 199: /** Whether the JSplitPane uses one touch expandable buttons. */ 200: protected boolean oneTouchExpandable = false; 201: 202: // This is the master dividerSize variable and sets the BasicSplitPaneDivider one accordingly 203: 204: /** The size of the divider. */ 205: protected int dividerSize = 10; 206: 207: /** The last location of the divider given by the UI. */ 208: protected int lastDividerLocation; 209: 210: /** The orientation of the JSplitPane. */ 211: protected int orientation; 212: 213: /** The component on the top or left. */ 214: protected Component leftComponent; 215: 216: /** The component on the right or bottom. */ 217: protected Component rightComponent; 218: 219: /** Determines how extra space should be allocated. */ 220: private transient double resizeWeight; 221: 222: /** 223: * Creates a new JSplitPane object with the given orientation, layout mode, 224: * and left and right components. 225: * 226: * @param newOrientation The orientation to use. 227: * @param newContinuousLayout The layout mode to use. 228: * @param newLeftComponent The left component. 229: * @param newRightComponent The right component. 230: * 231: * @throws IllegalArgumentException DOCUMENT ME! 232: */ 233: public JSplitPane(int newOrientation, boolean newContinuousLayout, 234: Component newLeftComponent, Component newRightComponent) 235: { 236: if (newOrientation != HORIZONTAL_SPLIT && newOrientation != VERTICAL_SPLIT) 237: throw new IllegalArgumentException("orientation is invalid."); 238: orientation = newOrientation; 239: continuousLayout = newContinuousLayout; 240: setLeftComponent(newLeftComponent); 241: setRightComponent(newRightComponent); 242: 243: updateUI(); 244: } 245: 246: /** 247: * Creates a new JSplitPane object using nonContinuousLayout mode, the given 248: * orientation and left and right components. 249: * 250: * @param newOrientation The orientation to use. 251: * @param newLeftComponent The left component. 252: * @param newRightComponent The right component. 253: */ 254: public JSplitPane(int newOrientation, Component newLeftComponent, 255: Component newRightComponent) 256: { 257: this(newOrientation, false, newLeftComponent, newRightComponent); 258: } 259: 260: /** 261: * Creates a new JSplitPane object with the given layout mode and 262: * orientation. 263: * 264: * @param newOrientation The orientation to use. 265: * @param newContinuousLayout The layout mode to use. 266: */ 267: public JSplitPane(int newOrientation, boolean newContinuousLayout) 268: { 269: this(newOrientation, newContinuousLayout, null, null); 270: } 271: 272: /** 273: * Creates a new JSplitPane object using a nonContinuousLayout mode and the 274: * given orientation. 275: * 276: * @param newOrientation The orientation to use. 277: */ 278: public JSplitPane(int newOrientation) 279: { 280: this(newOrientation, false, null, null); 281: } 282: 283: /** 284: * Creates a new JSplitPane object using HORIZONTAL_SPLIT and a 285: * nonContinuousLayout mode. 286: */ 287: public JSplitPane() 288: { 289: this(HORIZONTAL_SPLIT, false, null, null); 290: } 291: 292: /** 293: * This method adds a component to the JSplitPane. The constraints object is 294: * a string that identifies where this component should go. If the 295: * constraints is not a known one, it will throw an 296: * IllegalArgumentException. The valid constraints are LEFT, TOP, RIGHT, 297: * BOTTOM and DIVIDER. 298: * 299: * @param comp The component to add. 300: * @param constraints The constraints string to use. 301: * @param index Where to place to component in the list of components. 302: * 303: * @throws IllegalArgumentException When the constraints is not a known identifier. 304: */ 305: protected void addImpl(Component comp, Object constraints, int index) 306: { 307: int left = 0; 308: int right = 1; 309: int div = 2; 310: int place; 311: if (constraints == null) 312: { 313: if (leftComponent == null) 314: constraints = LEFT; 315: else if (rightComponent == null) 316: constraints = RIGHT; 317: } 318: 319: if (constraints instanceof String) 320: { 321: String placement = (String) constraints; 322: 323: if (placement.equals(BOTTOM) || placement.equals(RIGHT)) 324: { 325: if (rightComponent != null) 326: remove(rightComponent); 327: rightComponent = comp; 328: } 329: else if (placement.equals(LEFT) || placement.equals(TOP)) 330: { 331: if (leftComponent != null) 332: remove(leftComponent); 333: leftComponent = comp; 334: } 335: else if (placement.equals(DIVIDER)) 336: constraints = null; 337: else 338: throw new IllegalArgumentException("Constraints is not a known identifier."); 339: 340: super.addImpl(comp, constraints, index); 341: } 342: invalidate(); 343: layout(); 344: } 345: 346: /** 347: * DOCUMENT ME! 348: * 349: * @return DOCUMENT ME! 350: */ 351: public AccessibleContext getAccessibleContext() 352: { 353: if (accessibleContext == null) 354: accessibleContext = new AccessibleJSplitPane(); 355: 356: return accessibleContext; 357: } 358: 359: /** 360: * This method returns the bottom component. 361: * 362: * @return The bottom component. 363: */ 364: public Component getBottomComponent() 365: { 366: return rightComponent; 367: } 368: 369: /** 370: * This method returns the location of the divider. This method is passed to 371: * the UI. 372: * 373: * @return The location of the divider. 374: */ 375: public int getDividerLocation() 376: { 377: if (ui != null) 378: return ((SplitPaneUI) ui).getDividerLocation(this); 379: else 380: return -1; 381: } 382: 383: /** 384: * This method returns the size of the divider. 385: * 386: * @return The size of the divider. 387: */ 388: public int getDividerSize() 389: { 390: return dividerSize; 391: } 392: 393: /** 394: * This method returns the last divider location. 395: * 396: * @return The last divider location. 397: */ 398: public int getLastDividerLocation() 399: { 400: return lastDividerLocation; 401: } 402: 403: /** 404: * This method returns the left component. 405: * 406: * @return The left component. 407: */ 408: public Component getLeftComponent() 409: { 410: return leftComponent; 411: } 412: 413: /** 414: * This method returns the maximum divider location. This method is passed 415: * to the UI. 416: * 417: * @return DOCUMENT ME! 418: */ 419: public int getMaximumDividerLocation() 420: { 421: if (ui != null) 422: return ((SplitPaneUI) ui).getMaximumDividerLocation(this); 423: else 424: return -1; 425: } 426: 427: /** 428: * This method returns the minimum divider location. This method is passed 429: * to the UI. 430: * 431: * @return The minimum divider location. 432: */ 433: public int getMinimumDividerLocation() 434: { 435: if (ui != null) 436: return ((SplitPaneUI) ui).getMinimumDividerLocation(this); 437: else 438: return -1; 439: } 440: 441: /** 442: * This method returns the orientation that the JSplitPane is using. 443: * 444: * @return The current orientation. 445: */ 446: public int getOrientation() 447: { 448: return orientation; 449: } 450: 451: /** 452: * This method returns the current resize weight. 453: * 454: * @return The current resize weight. 455: */ 456: public double getResizeWeight() 457: { 458: return resizeWeight; 459: } 460: 461: /** 462: * This method returns the right component. 463: * 464: * @return The right component. 465: */ 466: public Component getRightComponent() 467: { 468: return rightComponent; 469: } 470: 471: /** 472: * This method returns the top component. 473: * 474: * @return The top component. 475: */ 476: public Component getTopComponent() 477: { 478: return leftComponent; 479: } 480: 481: /** 482: * This method returns the UI. 483: * 484: * @return The UI. 485: */ 486: public SplitPaneUI getUI() 487: { 488: return (SplitPaneUI) ui; 489: } 490: 491: /** 492: * This method returns true if the JSplitPane is using a continuousLayout. 493: * 494: * @return True if using a continuousLayout. 495: */ 496: public boolean isContinuousLayout() 497: { 498: return continuousLayout; 499: } 500: 501: /** 502: * This method returns true if the divider has one touch expandable buttons. 503: * 504: * @return True if one touch expandable is used. 505: */ 506: public boolean isOneTouchExpandable() 507: { 508: return oneTouchExpandable; 509: } 510: 511: /** 512: * This method returns true. 513: * 514: * @return true. 515: */ 516: public boolean isValidateRoot() 517: { 518: return true; 519: } 520: 521: /** 522: * This method overrides JComponent's paintChildren so the UI can be 523: * messaged when the children have finished painting. 524: * 525: * @param g The Graphics object to paint with. 526: */ 527: protected void paintChildren(Graphics g) 528: { 529: super.paintChildren(g); 530: if (ui != null) 531: ((SplitPaneUI) ui).finishedPaintingChildren(this, g); 532: } 533: 534: /** 535: * This method returns a String that describes this JSplitPane. The string 536: * is primarily used for debugging purposes. 537: * 538: * @return A String used for debugging purposes. 539: */ 540: protected String paramString() 541: { 542: return "JSplitPane"; 543: } 544: 545: /** 546: * This method removes the given component from the JSplitPane. 547: * 548: * @param component The Component to remove. 549: */ 550: public void remove(Component component) 551: { 552: if (component == leftComponent) 553: leftComponent = null; 554: else if (component == rightComponent) 555: rightComponent = null; 556: super.remove(component); 557: } 558: 559: /** 560: * This method removes the component at the given index. 561: * 562: * @param index The index of the component to remove. 563: */ 564: public void remove(int index) 565: { 566: Component component = getComponent(index); 567: if (component == leftComponent) 568: leftComponent = null; 569: else if (component == rightComponent) 570: rightComponent = null; 571: super.remove(index); 572: } 573: 574: /** 575: * This method removes all components from the JSplitPane. 576: */ 577: public void removeAll() 578: { 579: leftComponent = null; 580: rightComponent = null; 581: super.removeAll(); 582: } 583: 584: /** 585: * This method resets all children of the JSplitPane to their preferred 586: * sizes. 587: */ 588: public void resetToPreferredSizes() 589: { 590: if (ui != null) 591: ((SplitPaneUI) ui).resetToPreferredSizes(this); 592: } 593: 594: /** 595: * This method sets the bottom component. 596: * 597: * @param comp The Component to be placed at the bottom. 598: */ 599: public void setBottomComponent(Component comp) 600: { 601: if (comp != null) 602: add(comp, BOTTOM); 603: else 604: add(new JButton("right button"), BOTTOM); 605: } 606: 607: /** 608: * This method sets the layout mode for the JSplitPane. 609: * 610: * @param newContinuousLayout Whether the JSplitPane is in continuousLayout 611: * mode. 612: */ 613: public void setContinuousLayout(boolean newContinuousLayout) 614: { 615: if (newContinuousLayout != continuousLayout) 616: { 617: boolean oldValue = continuousLayout; 618: continuousLayout = newContinuousLayout; 619: firePropertyChange(CONTINUOUS_LAYOUT_PROPERTY, oldValue, 620: continuousLayout); 621: } 622: } 623: 624: /** 625: * This method sets the location of the divider. A value of 0 sets the 626: * divider to the farthest left. A value of 1 sets the divider to the 627: * farthest right. 628: * 629: * @param proportionalLocation A double that describes the location of the 630: * divider. 631: * 632: * @throws IllegalArgumentException DOCUMENT ME! 633: */ 634: public void setDividerLocation(double proportionalLocation) 635: { 636: if (proportionalLocation > 1 || proportionalLocation < 0) 637: throw new IllegalArgumentException("proportion has to be between 0 and 1."); 638: 639: int max = (orientation == HORIZONTAL_SPLIT) ? getWidth() : getHeight(); 640: setDividerLocation((int) (proportionalLocation * max)); 641: } 642: 643: /** 644: * This method sets the location of the divider. 645: * 646: * @param location The location of the divider. 647: */ 648: public void setDividerLocation(int location) 649: { 650: if (ui != null && location != getDividerLocation()) 651: { 652: int oldLocation = getDividerLocation(); 653: ((SplitPaneUI) ui).setDividerLocation(this, location); 654: firePropertyChange(DIVIDER_LOCATION_PROPERTY, oldLocation, location); 655: } 656: } 657: 658: /** 659: * This method sets the size of the divider. 660: * 661: * @param newSize The size of the divider. 662: */ 663: public void setDividerSize(int newSize) 664: { 665: if (newSize != dividerSize) 666: { 667: int oldSize = dividerSize; 668: dividerSize = newSize; 669: firePropertyChange(DIVIDER_SIZE_PROPERTY, oldSize, dividerSize); 670: } 671: } 672: 673: // This doesn't appear to do anything when set from user side. 674: // so it probably is only used from the UI side to change the 675: // lastDividerLocation var. 676: 677: /** 678: * This method sets the last location of the divider. 679: * 680: * @param newLastLocation The last location of the divider. 681: */ 682: public void setLastDividerLocation(int newLastLocation) 683: { 684: if (newLastLocation != lastDividerLocation) 685: { 686: int oldValue = lastDividerLocation; 687: lastDividerLocation = newLastLocation; 688: firePropertyChange(LAST_DIVIDER_LOCATION_PROPERTY, oldValue, 689: lastDividerLocation); 690: } 691: } 692: 693: /** 694: * This method sets the left component. 695: * 696: * @param comp The left component. 697: */ 698: public void setLeftComponent(Component comp) 699: { 700: if (comp != null) 701: add(comp, LEFT); 702: else 703: add(new JButton("left button"), LEFT); 704: } 705: 706: /** 707: * This method sets whether the divider has one touch expandable buttons. 708: * The one touch expandable buttons can expand the size of either component 709: * to the maximum allowed size. 710: * 711: * @param newValue Whether the divider will have one touch expandable 712: * buttons. 713: */ 714: public void setOneTouchExpandable(boolean newValue) 715: { 716: if (newValue != oneTouchExpandable) 717: { 718: boolean oldValue = oneTouchExpandable; 719: oneTouchExpandable = newValue; 720: firePropertyChange(ONE_TOUCH_EXPANDABLE_PROPERTY, oldValue, 721: oneTouchExpandable); 722: } 723: } 724: 725: /** 726: * This method sets the orientation of the JSplitPane. 727: * 728: * @param orientation The orientation of the JSplitPane. 729: * 730: * @throws IllegalArgumentException DOCUMENT ME! 731: */ 732: public void setOrientation(int orientation) 733: { 734: if (orientation != HORIZONTAL_SPLIT && orientation != VERTICAL_SPLIT) 735: throw new IllegalArgumentException("orientation must be one of VERTICAL_SPLIT, HORIZONTAL_SPLIT"); 736: if (orientation != this.orientation) 737: { 738: int oldOrientation = this.orientation; 739: this.orientation = orientation; 740: firePropertyChange(ORIENTATION_PROPERTY, oldOrientation, 741: this.orientation); 742: } 743: } 744: 745: /** 746: * This method determines how extra space will be distributed among the left 747: * and right components. A value of 0 will allocate all extra space to the 748: * right component. A value of 1 indicates that all extra space will go to 749: * the left component. A value in between 1 and 0 will split the space 750: * accordingly. 751: * 752: * @param value The resize weight. 753: */ 754: public void setResizeWeight(double value) 755: { 756: resizeWeight = value; 757: } 758: 759: /** 760: * This method sets the right component. 761: * 762: * @param comp The right component. 763: */ 764: public void setRightComponent(Component comp) 765: { 766: if (comp != null) 767: add(comp, RIGHT); 768: else 769: add(new JButton("right button"), RIGHT); 770: } 771: 772: /** 773: * This method sets the top component. 774: * 775: * @param comp The top component. 776: */ 777: public void setTopComponent(Component comp) 778: { 779: if (comp != null) 780: add(comp, TOP); 781: else 782: add(new JButton("left button"), TOP); 783: } 784: 785: /** 786: * This method sets the UI used by the JSplitPane. 787: * 788: * @param ui The UI to use. 789: */ 790: public void setUI(SplitPaneUI ui) 791: { 792: super.setUI(ui); 793: } 794: 795: /** 796: * This method resets the UI to the one specified by the current Look and 797: * Feel. 798: */ 799: public void updateUI() 800: { 801: setUI((SplitPaneUI) UIManager.getUI(this)); 802: invalidate(); 803: repaint(); 804: } 805: 806: /** 807: * This method returns a string identifier to determine which UI class it 808: * needs. 809: * 810: * @return A string that identifies it's UI class. 811: */ 812: public String getUIClassID() 813: { 814: return "SplitPaneUI"; 815: } 816: }
GNU Classpath (0.19) |