📄 basicsplitpaneui.java
字号:
} /** * Returns the preferred size for the passed in component, * This is passed off to the current layoutmanager. */ public Dimension getPreferredSize(JComponent jc) { if(splitPane != null) return layoutManager.preferredLayoutSize(splitPane); return new Dimension(0, 0); } /** * Returns the minimum size for the passed in component, * This is passed off to the current layoutmanager. */ public Dimension getMinimumSize(JComponent jc) { if(splitPane != null) return layoutManager.minimumLayoutSize(splitPane); return new Dimension(0, 0); } /** * Returns the maximum size for the passed in component, * This is passed off to the current layoutmanager. */ public Dimension getMaximumSize(JComponent jc) { if(splitPane != null) return layoutManager.maximumLayoutSize(splitPane); return new Dimension(0, 0); } /** * Returns the insets. The insets are returned from the border insets * of the current border. */ public Insets getInsets(JComponent jc) { return null; } /** * Resets the layout manager based on orientation and messages it * with invalidateLayout to pull in appropriate Components. */ protected void resetLayoutManager() { if(orientation == JSplitPane.HORIZONTAL_SPLIT) { layoutManager = new BasicHorizontalLayoutManager(); } else { layoutManager = new BasicVerticalLayoutManager(); } splitPane.setLayout(layoutManager); layoutManager.updateComponents(); splitPane.revalidate(); splitPane.repaint(); } /** * Should be messaged before the dragging session starts, resets * lastDragLocation and dividerSize. */ protected void startDragging() { Component leftC = splitPane.getLeftComponent(); Component rightC = splitPane.getRightComponent(); ComponentPeer cPeer; beginDragDividerLocation = getDividerLocation(splitPane); draggingHW = false; if(leftC != null && (cPeer = leftC.getPeer()) != null && !(cPeer instanceof LightweightPeer)) { draggingHW = true; } else if(rightC != null && (cPeer = rightC.getPeer()) != null && !(cPeer instanceof LightweightPeer)) { draggingHW = true; } if(orientation == JSplitPane.HORIZONTAL_SPLIT) { setLastDragLocation(divider.getBounds().x); dividerSize = divider.getSize().width; if(!isContinuousLayout() && draggingHW) { nonContinuousLayoutDivider.setBounds (getLastDragLocation(), 0, dividerSize, splitPane.getHeight()); addHeavyweightDivider(); } } else { setLastDragLocation(divider.getBounds().y); dividerSize = divider.getSize().height; if(!isContinuousLayout() && draggingHW) { nonContinuousLayoutDivider.setBounds (0, getLastDragLocation(), splitPane.getWidth(), dividerSize); addHeavyweightDivider(); } } } /** * Messaged during a dragging session to move the divider to the * passed in location. If continuousLayout is true the location is * reset and the splitPane validated. */ protected void dragDividerTo(int location) { if(getLastDragLocation() != location) { if(isContinuousLayout()) { splitPane.setDividerLocation(location); setLastDragLocation(location); } else { int lastLoc = getLastDragLocation(); setLastDragLocation(location); if(orientation == JSplitPane.HORIZONTAL_SPLIT) { if(draggingHW) { nonContinuousLayoutDivider.setLocation( getLastDragLocation(), 0); } else { int splitHeight = splitPane.getHeight(); splitPane.repaint(lastLoc, 0, dividerSize, splitHeight); splitPane.repaint(location, 0, dividerSize, splitHeight); } } else { if(draggingHW) { nonContinuousLayoutDivider.setLocation(0, getLastDragLocation()); } else { int splitWidth = splitPane.getWidth(); splitPane.repaint(0, lastLoc, splitWidth, dividerSize); splitPane.repaint(0, location, splitWidth, dividerSize); } } } } } /** * Messaged to finish the dragging session. If not continuous display * the dividers location will be reset. */ protected void finishDraggingTo(int location) { dragDividerTo(location); setLastDragLocation(-1); if(!isContinuousLayout()) { Component leftC = splitPane.getLeftComponent(); Rectangle leftBounds = leftC.getBounds(); if (draggingHW) { if(orientation == JSplitPane.HORIZONTAL_SPLIT) { nonContinuousLayoutDivider.setLocation(-dividerSize, 0); } else { nonContinuousLayoutDivider.setLocation(0, -dividerSize); } splitPane.remove(nonContinuousLayoutDivider); } splitPane.setDividerLocation(location); } } /** * As of Java 2 platform v1.3 this method is no longer used. Instead * you should set the border on the divider. * <p> * Returns the width of one side of the divider border. * * @deprecated As of Java 2 platform v1.3, instead set the border on the * divider. */ protected int getDividerBorderSize() { return 1; } /** * LayoutManager for JSplitPanes that have an orientation of * HORIZONTAL_SPLIT. * <p> * This inner class is marked "public" due to a compiler bug. * This class should be treated as a "protected" inner class. * Instantiate it only within subclasses of BasicSplitPaneUI. */ public class BasicHorizontalLayoutManager implements LayoutManager2 { /* left, right, divider. (in this exact order) */ protected int[] sizes; protected Component[] components; /** Size of the splitpane the last time laid out. */ private int lastSplitPaneSize; /** True if resetToPreferredSizes has been invoked. */ private boolean doReset; /** Axis, 0 for horizontal, or 1 for veritcal. */ private int axis; BasicHorizontalLayoutManager() { this(0); } BasicHorizontalLayoutManager(int axis) { this.axis = axis; components = new Component[3]; components[0] = components[1] = components[2] = null; sizes = new int[3]; } // // LayoutManager // /** * Does the actual layout. */ public void layoutContainer(Container container) { Dimension containerSize = container.getSize(); // If the splitpane has a zero size then no op out of here. // If we execute this function now, we're going to cause ourselves // much grief. if (containerSize.height <= 0 || containerSize.width <= 0 ) { lastSplitPaneSize = 0; return; } int spDividerLocation = splitPane.getDividerLocation(); Insets insets = splitPane.getInsets(); int availableSize = getAvailableSize(containerSize, insets); int newSize = getSizeForPrimaryAxis(containerSize); int beginLocation = getDividerLocation(splitPane); int dOffset = getSizeForPrimaryAxis(insets, true); Dimension dSize = (components[2] == null) ? null : components[2].getPreferredSize(); if ((doReset && !dividerLocationIsSet) || spDividerLocation < 0) { resetToPreferredSizes(availableSize); } else if (lastSplitPaneSize <= 0 || availableSize == lastSplitPaneSize || !painted || (dSize != null && getSizeForPrimaryAxis(dSize) != sizes[2])) { if (dSize != null) { sizes[2] = getSizeForPrimaryAxis(dSize); } else { sizes[2] = 0; } setDividerLocation(spDividerLocation - dOffset, availableSize); dividerLocationIsSet = false; } else if (availableSize != lastSplitPaneSize) { distributeSpace(availableSize - lastSplitPaneSize, true); } doReset = false; dividerLocationIsSet = false; lastSplitPaneSize = availableSize; // Reset the bounds of each component int nextLocation = getInitialLocation(insets); int counter = 0; while (counter < 3) { if (components[counter] != null && components[counter].isVisible()) { setComponentToSize(components[counter], sizes[counter], nextLocation, insets, containerSize); nextLocation += sizes[counter]; } switch (counter) { case 0: counter = 2; break; case 2: counter = 1; break; case 1: counter = 3; break; } } if (painted) { // This is tricky, there is never a good time for us // to push the value to the splitpane, painted appears to // the best time to do it. What is really needed is // notification that layout has completed. int newLocation = getDividerLocation(splitPane); if (newLocation != (spDividerLocation - dOffset)) { int lastLocation = splitPane.getLastDividerLocation(); ignoreDividerLocationChange = true; try { splitPane.setDividerLocation(newLocation); // This is not always needed, but is rather tricky // to determine when... The case this is needed for // is if the user sets the divider location to some // bogus value, say 0, and the actual value is 1, the // call to setDividerLocation(1) will preserve the // old value of 0, when we really want the divider // location value before the call. This is needed for // the one touch buttons. splitPane.setLastDividerLocation(lastLocation); } finally { ignoreDividerLocationChange = false; } } } } /** * Adds the component at place. Place must be one of * JSplitPane.LEFT, RIGHT, TOP, BOTTOM, or null (for the * divider). */ public void addLayoutComponent(String place, Component component) { boolean isValid = true; if(place != null) { if(place.equals(JSplitPane.DIVIDER)) { /* Divider. */ components[2] = component; sizes[2] = getSizeForPrimaryAxis(component. getPreferredSize()); } else if(place.equals(JSplitPane.LEFT) || place.equals(JSplitPane.TOP)) { components[0] = component; sizes[0] = 0; } else if(place.equals(JSplitPane.RIGHT) || place.equals(JSplitPane.BOTTOM)) { components[1] = component; sizes[1] = 0; } else if(!place.equals( BasicSplitPaneUI.NON_CONTINUOUS_DIVIDER)) isValid = false; } else { isValid = false; } if(!isValid) throw new IllegalArgumentException("cannot add to layout: " + "unknown constraint: " + place); doReset = true; } /** * Returns the minimum size needed to contain the children. * The width is the sum of all the childrens min widths and * the height is the largest of the childrens minimum heights. */ public Dimension minimumLayoutSize(Container container) { int minPrimary = 0; int minSecondary = 0; Insets insets = splitPane.getInsets(); for (int counter=0; counter<3; counter++) { if(components[counter] != null) { Dimension minSize = components[counter].getMinimumSize(); int secSize = getSizeForSecondaryAxis(minSize); minPrimary += getSizeForPrimaryAxis(minSize); if(secSize > minSecondary) minSecondary = secSize; } } if(insets != null) { minPrimary += getSizeForPrimaryAxis(insets, true) + getSizeForPrimaryAxis(insets, false); minSecondary += getSizeForSecondaryAxis(insets, true) + getSizeForSecondaryAxis(insets, false); } if (axis == 0) { return new Dimension(minPrimary, minSecondary); } return new Dimension(minSecondary, minPrimary); } /** * Returns the preferred size needed to contain the children. * The width is the sum of all the childrens preferred widths and * the height is the largest of the childrens preferred heights. */ public Dimension preferredLayoutSize(Container container) { int prePrimary = 0; int preSecondary = 0; Insets insets = splitPane.getInsets(); for(int counter = 0; counter < 3; counter++) { if(components[counter] != null) { Dimension preSize = components[counter]. getPreferredSize(); int secSize = getSizeForSecondaryAxis(preSize); prePrimary += getSizeForPrimaryAxis(preSize); if(secSize > preSecondary) preSecondary = secSize;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -