synthsplitpaneui.java

来自「java jdk 1.4的源码」· Java 代码 · 共 2,043 行 · 第 1/5 页

JAVA
2,043
字号
            if(components[2] == null) {		sizes[2] = 0;	    }	    else {		sizes[2] = getSizeForPrimaryAxis(components[2].getPreferredSize());	    }        }	/**	 * Resets the size of the first component to <code>leftSize</code>,	 * and the right component to the remainder of the space.	 */	void setDividerLocation(int leftSize, int availableSize) {	    boolean          lValid = (components[0] != null &&				       components[0].isVisible());	    boolean          rValid = (components[1] != null &&				       components[1].isVisible());	    boolean          dValid = (components[2] != null && 				       components[2].isVisible());	    int              max = availableSize;	    if (dValid) {		max -= sizes[2];	    }	    leftSize = Math.max(0, Math.min(leftSize, max));	    if (lValid) {		if (rValid) {		    sizes[0] = leftSize;		    sizes[1] = max - leftSize;		}		else {		    sizes[0] = max;		    sizes[1] = 0;		}	    }	    else if (rValid) {		sizes[1] = max;		sizes[0] = 0;	    }	}	/**	 * Returns an array of the minimum sizes of the components.	 */	int[] getPreferredSizes() {	    int[]         retValue = new int[3];	    for (int counter = 0; counter < 3; counter++) {		if (components[counter] != null &&		    components[counter].isVisible()) {		    retValue[counter] = getPreferredSizeOfComponent			                (components[counter]);		}		else {		    retValue[counter] = -1;		}	    }	    return retValue;	}	/**	 * Returns an array of the minimum sizes of the components.	 */	int[] getMinimumSizes() {	    int[]         retValue = new int[3];	    for (int counter = 0; counter < 2; counter++) {		if (components[counter] != null &&		    components[counter].isVisible()) {		    retValue[counter] = getMinimumSizeOfComponent			                (components[counter]);		}		else {		    retValue[counter] = -1;		}	    }	    retValue[2] = (components[2] != null) ? 		getMinimumSizeOfComponent(components[2]) : -1;	    return retValue;	}	/**	 * Resets the components to their preferred sizes.	 */	void resetToPreferredSizes(int availableSize) {	    // Set the sizes to the preferred sizes (if fits), otherwise	    // set to min sizes and distribute any extra space.	    int[]       testSizes = getPreferredSizes();	    int         totalSize = 0;	    for (int counter = 0; counter < 3; counter++) {		if (testSizes[counter] != -1) {		    totalSize += testSizes[counter];		}	    }	    if (totalSize > availableSize) {		testSizes = getMinimumSizes();		totalSize = 0;		for (int counter = 0; counter < 3; counter++) {		    if (testSizes[counter] != -1) {			totalSize += testSizes[counter];		    }		}	    }	    setSizes(testSizes);	    distributeSpace(availableSize - totalSize, false);	}	/**	 * Distributes <code>space</code> between the two components 	 * (divider won't get any extra space) based on the weighting. This	 * attempts to honor the min size of the components.         *         * @param keepHidden if true and one of the components is 0x0         *                   it gets none of the extra space	 */	void distributeSpace(int space, boolean keepHidden) {	    boolean          lValid = (components[0] != null &&				       components[0].isVisible());	    boolean          rValid = (components[1] != null &&				       components[1].isVisible());            if (keepHidden) {                if (lValid && getSizeForPrimaryAxis(                                 components[0].getSize()) == 0) {                    lValid = false;                    if (rValid && getSizeForPrimaryAxis(                                     components[1].getSize()) == 0) {                        // Both aren't valid, force them both to be valid                        lValid = true;                    }                }                else if (rValid && getSizeForPrimaryAxis(                                   components[1].getSize()) == 0) {                    rValid = false;                }            }	    if (lValid && rValid) {		double        weight = splitPane.getResizeWeight();		int           lExtra = (int)(weight * (double)space);		int           rExtra = (space - lExtra);		sizes[0] += lExtra;		sizes[1] += rExtra;		int           lMin = getMinimumSizeOfComponent(components[0]);		int           rMin = getMinimumSizeOfComponent(components[1]);		boolean       lMinValid = (sizes[0] >= lMin);		boolean       rMinValid = (sizes[1] >= rMin);		if (!lMinValid && !rMinValid) {		    if (sizes[0] < 0) {			sizes[1] += sizes[0];			sizes[0] = 0;		    }		    else if (sizes[1] < 0) {			sizes[0] += sizes[1];			sizes[1] = 0;		    }		}		else if (!lMinValid) {		    if (sizes[1] - (lMin - sizes[0]) < rMin) {			// both below min, just make sure > 0			if (sizes[0] < 0) {			    sizes[1] += sizes[0];			    sizes[0] = 0;			}		    }		    else {			sizes[1] -= (lMin - sizes[0]);			sizes[0] = lMin;		    }		}		else if (!rMinValid) {		    if (sizes[0] - (rMin - sizes[1]) < lMin) {			// both below min, just make sure > 0			if (sizes[1] < 0) {			    sizes[0] += sizes[1];			    sizes[1] = 0;			}		    }		    else {			sizes[0] -= (rMin - sizes[1]);			sizes[1] = rMin;		    }		}		if (sizes[0] < 0) {		    sizes[0] = 0;		}		if (sizes[1] < 0) {		    sizes[1] = 0;		}	    }	    else if (lValid) {		sizes[0] = Math.max(0, sizes[0] + space);	    }	    else if (rValid) {		sizes[1] = Math.max(0, sizes[1] + space);	    }	}    }    /**     * Cursor used for HORIZONTAL_SPLIT splitpanes.     */    private static final Cursor horizontalCursor =                            Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR);    /**     * Cursor used for VERTICAL_SPLIT splitpanes.     */    private static final Cursor verticalCursor =                            Cursor.getPredefinedCursor(Cursor.S_RESIZE_CURSOR);    /**     * Default cursor.     */    private static final Cursor defaultCursor =                            Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR);    // * @(#)BasicSplitPaneDivider.java	1.45 01/12/03    private class Divider extends JComponent implements PropertyChangeListener{        /**         * Handles mouse dragging message to do the actual dragging.         */        protected DragController dragger;        /**         * Size of the divider.         */        protected int dividerSize = 0;        /**         * Divider that is used for noncontinuous layout mode.         */        protected Component hiddenDivider;        /**         * Handles mouse events from both this class, and the split pane.         * Mouse events are handled for the splitpane since you want to be able         * to drag when clicking on the border of the divider, which is not          * drawn by the divider.         */        protected MouseHandler mouseHandler;        /**         * True while the mouse is over the divider.         */        private boolean mouseOver;        /**         * Creates an instance of Divider. Registers this         * instance for mouse events and mouse dragged events.         */        public Divider(JSplitPane splitPane) {            setLayout(new DividerLayout());            int orientation = splitPane.getOrientation();            setCursor((orientation == JSplitPane.HORIZONTAL_SPLIT) ?                      horizontalCursor : verticalCursor);            mouseHandler = new MouseHandler();            splitPane.addMouseListener(mouseHandler);            splitPane.addMouseMotionListener(mouseHandler);            addMouseListener(mouseHandler);            addMouseMotionListener(mouseHandler);            splitPane.addPropertyChangeListener(this);        }        public String getUIClassID() {            return "SplitPaneDividerUI";        }        /**         * Sets the size of the divider to <code>newSize</code>. That is         * the width if the splitpane is <code>HORIZONTAL_SPLIT</code>, or         * the height of <code>VERTICAL_SPLIT</code>.         */        public void setDividerSize(int newSize) {            dividerSize = newSize;        }        /**         * Returns the size of the divider, that is the width if the splitpane         * is HORIZONTAL_SPLIT, or the height of VERTICAL_SPLIT.         */        public int getDividerSize() {            return dividerSize;        }        /**         * Returns dividerSize x dividerSize         */        public Dimension getPreferredSize() {            // Ideally this would return the size from the layout manager,            // but that could result in the layed out size being different from            // the dividerSize, which may break developers as well as            // SynthSplitPaneUI.            if (orientation == JSplitPane.HORIZONTAL_SPLIT) {                return new Dimension(getDividerSize(), 1);            }            return new Dimension(1, getDividerSize());        }        /**         * Returns dividerSize x dividerSize         */        public Dimension getMinimumSize() {            return getPreferredSize();        }        /**         * Returns true if the mouse is over the divider.         */        public boolean isMouseOver() {            return mouseOver;        }        /**         * Property change event, presumably from the JSplitPane, will message         * updateOrientation if necessary.         */        public void propertyChange(PropertyChangeEvent e) {            if (e.getSource() == splitPane) {                if (e.getPropertyName().equals(                                       JSplitPane.ORIENTATION_PROPERTY)) {                    orientation = splitPane.getOrientation();                    setCursor((orientation == JSplitPane.HORIZONTAL_SPLIT) ?                              horizontalCursor : verticalCursor);                    revalidate();                }            }        }        public void paintComponent(Graphics g) {            SynthContext context = getContext(splitPane,                                              Region.SPLIT_PANE_DIVIDER);            Rectangle bounds = getBounds();            bounds.x = bounds.y = 0;            SynthLookAndFeel.updateSubregion(context, g, bounds);            SynthPainter foreground = (SynthPainter)context.getStyle().                                      get(context, "foreground");            if (foreground != null) {                foreground.paint(context, "foreground", g, 0, 0, getWidth(),                                 getHeight());            }            context.dispose();        }        private int mapDirection(boolean isLeft) {            if (isLeft) {                if (splitPane.getOrientation() == JSplitPane.HORIZONTAL_SPLIT){                    return SwingConstants.WEST;                }                return SwingConstants.NORTH;            }            if (splitPane.getOrientation() == JSplitPane.HORIZONTAL_SPLIT){                return SwingConstants.EAST;            }            return SwingConstants.SOUTH;        }        /**         * MouseHandler is responsible for converting mouse events         * (released, dragged...) into the appropriate DragController          * methods.         * <p>         */        protected class MouseHandler extends MouseAdapter                             implements MouseMotionListener {            /**             * Starts the dragging session by creating the appropriate instance             * of DragController.             */            public void mousePressed(MouseEvent e) {                if ((e.getSource() == Divider.this ||                          e.getSource() == splitPane) &&                          dragger == null && splitPane.isEnabled()) {                    Component            newHiddenDivider =                                      getNonContinuousLayoutDivider();                    if (hiddenDivider != newHiddenDivider) {                        if (hiddenDivider != null) {                            hiddenDivider.removeMouseListener(this);                            hiddenDivider.removeMouseMotionListener(this);                        }                        hiddenDivider = newHiddenDivider;                        if (hiddenDivider != null) {                            hiddenDivider.addMouseMotionListener(this);                            hiddenDivider.addMouseListener(this);                        }                    }                    if (splitPane.getLeftComponent() != null &&                             splitPane.getRightComponent() != null) {                        if (orientation == JSplitPane.HORIZONTAL_SPLIT) {                            dragger = new DragController(e);                        }                        else {                            dragger = new 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?