3745 lines · 140.5 KB · Java
RawDownload
    /**     * Returns {@code true} if gaps between the container and components that     * border the container are automatically created.     *     * @return {@code true} if gaps between the container and components that     *         border the container are automatically created     */    public boolean getAutoCreateContainerGaps() {        return autocreateContainerPadding;    }     /**     * Sets the {@code Group} that positions and sizes     * components along the horizontal axis.     *     * @param group the {@code Group} that positions and sizes     *        components along the horizontal axis     * @throws IllegalArgumentException if group is {@code null}     */    public void setHorizontalGroup(Group group) {        if (group == null) {            throw new IllegalArgumentException("Group must be non-null");        }        horizontalGroup = createTopLevelGroup(group);        invalidateHost();    }     /**     * Returns the {@code Group} that positions and sizes components     * along the horizontal axis.     *     * @return the {@code Group} responsible for positioning and     *         sizing component along the horizontal axis     */    private Group getHorizontalGroup() {        int index = 0;        if (horizontalGroup.springs.size() > 1) {            index = 1;        }        return (Group)horizontalGroup.springs.get(index);    }     /**     * Sets the {@code Group} that positions and sizes     * components along the vertical axis.     *     * @param group the {@code Group} that positions and sizes     *        components along the vertical axis     * @throws IllegalArgumentException if group is {@code null}     */    public void setVerticalGroup(Group group) {        if (group == null) {            throw new IllegalArgumentException("Group must be non-null");        }        verticalGroup = createTopLevelGroup(group);        invalidateHost();    }     /**     * Returns the {@code Group} that positions and sizes components     * along the vertical axis.     *     * @return the {@code Group} responsible for positioning and     *         sizing component along the vertical axis     */    private Group getVerticalGroup() {        int index = 0;        if (verticalGroup.springs.size() > 1) {            index = 1;        }        return (Group)verticalGroup.springs.get(index);    }     /**     * Wraps the user specified group in a sequential group.  If     * container gaps should be generated the necessary springs are     * added.     */    private Group createTopLevelGroup(Group specifiedGroup) {        SequentialGroup group = createSequentialGroup();        if (getAutoCreateContainerGaps()) {            group.addSpring(new ContainerAutoPreferredGapSpring());            group.addGroup(specifiedGroup);            group.addSpring(new ContainerAutoPreferredGapSpring());        } else {            group.addGroup(specifiedGroup);        }        return group;    }     /**     * Creates and returns a {@code SequentialGroup}.     *     * @return a new {@code SequentialGroup}     */    public SequentialGroup createSequentialGroup() {        return new SequentialGroup();    }     /**     * Creates and returns a {@code ParallelGroup} with an alignment of     * {@code Alignment.LEADING}.  This is a cover method for the more     * general {@code createParallelGroup(Alignment)} method.     *     * @return a new {@code ParallelGroup}     * @see #createParallelGroup(Alignment)     */    public ParallelGroup createParallelGroup() {        return createParallelGroup(Alignment.LEADING);    }     /**     * Creates and returns a {@code ParallelGroup} with the specified     * alignment.  This is a cover method for the more general {@code     * createParallelGroup(Alignment,boolean)} method with {@code true}     * supplied for the second argument.     *     * @param alignment the alignment for the elements of the group     * @throws IllegalArgumentException if {@code alignment} is {@code null}     * @return a new {@code ParallelGroup}     * @see #createBaselineGroup     * @see ParallelGroup     */    public ParallelGroup createParallelGroup(Alignment alignment) {        return createParallelGroup(alignment, true);    }     /**     * Creates and returns a {@code ParallelGroup} with the specified     * alignment and resize behavior. The {@code     * alignment} argument specifies how children elements are     * positioned that do not fill the group. For example, if a {@code     * ParallelGroup} with an alignment of {@code TRAILING} is given     * 100 and a child only needs 50, the child is     * positioned at the position 50 (with a component orientation of     * left-to-right).     * <p>     * Baseline alignment is only useful when used along the vertical     * axis. A {@code ParallelGroup} created with a baseline alignment     * along the horizontal axis is treated as {@code LEADING}.     * <p>     * Refer to {@link GroupLayout.ParallelGroup ParallelGroup} for details on     * the behavior of baseline groups.     *     * @param alignment the alignment for the elements of the group     * @param resizable {@code true} if the group is resizable; if the group     *        is not resizable the preferred size is used for the     *        minimum and maximum size of the group     * @throws IllegalArgumentException if {@code alignment} is {@code null}     * @return a new {@code ParallelGroup}     * @see #createBaselineGroup     * @see GroupLayout.ParallelGroup     */    public ParallelGroup createParallelGroup(Alignment alignment,            boolean resizable){        if (alignment == null) {            throw new IllegalArgumentException("alignment must be non null");        }         if (alignment == Alignment.BASELINE) {            return new BaselineGroup(resizable);        }        return new ParallelGroup(alignment, resizable);    }     /**     * Creates and returns a {@code ParallelGroup} that aligns it's     * elements along the baseline.     *     * @param resizable whether the group is resizable     * @param anchorBaselineToTop whether the baseline is anchored to     *        the top or bottom of the group     * @see #createBaselineGroup     * @see ParallelGroup     */    public ParallelGroup createBaselineGroup(boolean resizable,            boolean anchorBaselineToTop) {        return new BaselineGroup(resizable, anchorBaselineToTop);    }     /**     * Forces the specified components to have the same size     * regardless of their preferred, minimum or maximum sizes. Components that     * are linked are given the maximum of the preferred size of each of     * the linked components. For example, if you link two components with     * a preferred width of 10 and 20, both components are given a width of 20.     * <p>     * This can be used multiple times to force any number of     * components to share the same size.     * <p>     * Linked Components are not be resizable.     *     * @param components the {@code Component}s that are to have the same size     * @throws IllegalArgumentException if {@code components} is     *         {@code null}, or contains {@code null}     * @see #linkSize(int,Component[])     */    public void linkSize(Component... components) {        linkSize(SwingConstants.HORIZONTAL, components);        linkSize(SwingConstants.VERTICAL, components);    }     /**     * Forces the specified components to have the same size along the     * specified axis regardless of their preferred, minimum or     * maximum sizes. Components that are linked are given the maximum     * of the preferred size of each of the linked components. For     * example, if you link two components along the horizontal axis     * and the preferred width is 10 and 20, both components are given     * a width of 20.     * <p>     * This can be used multiple times to force any number of     * components to share the same size.     * <p>     * Linked {@code Component}s are not be resizable.     *     * @param components the {@code Component}s that are to have the same size     * @param axis the axis to link the size along; one of     *             {@code SwingConstants.HORIZONTAL} or     *             {@code SwingConstans.VERTICAL}     * @throws IllegalArgumentException if {@code components} is     *         {@code null}, or contains {@code null}; or {@code axis}     *          is not {@code SwingConstants.HORIZONTAL} or     *          {@code SwingConstants.VERTICAL}     */    public void linkSize(int axis, Component... components) {        if (components == null) {            throw new IllegalArgumentException("Components must be non-null");        }        for (int counter = components.length - 1; counter >= 0; counter--) {            Component c = components[counter];            if (components[counter] == null) {                throw new IllegalArgumentException(                        "Components must be non-null");            }            // Force the component to be added            getComponentInfo(c);        }        int glAxis;        if (axis == SwingConstants.HORIZONTAL) {            glAxis = HORIZONTAL;        } else if (axis == SwingConstants.VERTICAL) {            glAxis = VERTICAL;        } else {            throw new IllegalArgumentException("Axis must be one of " +                    "SwingConstants.HORIZONTAL or SwingConstants.VERTICAL");        }        LinkInfo master = getComponentInfo(                components[components.length - 1]).getLinkInfo(glAxis);        for (int counter = components.length - 2; counter >= 0; counter--) {            master.add(getComponentInfo(components[counter]));        }        invalidateHost();    }     /**     * Replaces an existing component with a new one.     *     * @param existingComponent the component that should be removed     *        and replaced with {@code newComponent}     * @param newComponent the component to put in     *        {@code existingComponent}'s place     * @throws IllegalArgumentException if either of the components are     *         {@code null} or {@code existingComponent} is not being managed     *         by this layout manager     */    public void replace(Component existingComponent, Component newComponent) {        if (existingComponent == null || newComponent == null) {            throw new IllegalArgumentException("Components must be non-null");        }        // Make sure all the components have been registered, otherwise we may        // not update the correct Springs.        if (springsChanged) {            registerComponents(horizontalGroup, HORIZONTAL);            registerComponents(verticalGroup, VERTICAL);        }        ComponentInfo info = componentInfos.remove(existingComponent);        if (info == null) {            throw new IllegalArgumentException("Component must already exist");        }        host.remove(existingComponent);        if (newComponent.getParent() != host) {            host.add(newComponent);        }        info.setComponent(newComponent);        componentInfos.put(newComponent, info);        invalidateHost();    }     /**     * Sets the {@code LayoutStyle} used to calculate the preferred     * gaps between components. A value of {@code null} indicates the     * shared instance of {@code LayoutStyle} should be used.     *     * @param layoutStyle the {@code LayoutStyle} to use     * @see LayoutStyle     */    public void setLayoutStyle(LayoutStyle layoutStyle) {        this.layoutStyle = layoutStyle;        invalidateHost();    }     /**     * Returns the {@code LayoutStyle} used for calculating the preferred     * gap between components. This returns the value specified to     * {@code setLayoutStyle}, which may be {@code null}.     *     * @return the {@code LayoutStyle} used for calculating the preferred     *         gap between components     */    public LayoutStyle getLayoutStyle() {        return layoutStyle;    }     private LayoutStyle getLayoutStyle0() {        LayoutStyle layoutStyle = getLayoutStyle();        if (layoutStyle == null) {            layoutStyle = LayoutStyle.getInstance();        }        return layoutStyle;    }     private void invalidateHost() {        if (host instanceof JComponent) {            ((JComponent)host).revalidate();        } else {            host.invalidate();        }        host.repaint();    }     //    // LayoutManager    //    /**     * Notification that a {@code Component} has been added to     * the parent container.  You should not invoke this method     * directly, instead you should use one of the {@code Group}     * methods to add a {@code Component}.     *     * @param name the string to be associated with the component     * @param component the {@code Component} to be added     */    public void addLayoutComponent(String name, Component component) {    }     /**     * Notification that a {@code Component} has been removed from     * the parent container.  You should not invoke this method     * directly, instead invoke {@code remove} on the parent     * {@code Container}.     *     * @param component the component to be removed     * @see java.awt.Component#remove     */    public void removeLayoutComponent(Component component) {        ComponentInfo info = componentInfos.remove(component);        if (info != null) {            info.dispose();            springsChanged = true;            isValid = false;        }    }     /**     * Returns the preferred size for the specified container.     *     * @param parent the container to return the preferred size for     * @return the preferred size for {@code parent}     * @throws IllegalArgumentException if {@code parent} is not     *         the same {@code Container} this was created with     * @throws IllegalStateException if any of the components added to     *         this layout are not in both a horizontal and vertical group     * @see java.awt.Container#getPreferredSize     */    public Dimension preferredLayoutSize(Container parent) {        checkParent(parent);        prepare(PREF_SIZE);        return adjustSize(horizontalGroup.getPreferredSize(HORIZONTAL),                verticalGroup.getPreferredSize(VERTICAL));    }     /**     * Returns the minimum size for the specified container.     *     * @param parent the container to return the size for     * @return the minimum size for {@code parent}     * @throws IllegalArgumentException if {@code parent} is not     *         the same {@code Container} that this was created with     * @throws IllegalStateException if any of the components added to     *         this layout are not in both a horizontal and vertical group     * @see java.awt.Container#getMinimumSize     */    public Dimension minimumLayoutSize(Container parent) {        checkParent(parent);        prepare(MIN_SIZE);        return adjustSize(horizontalGroup.getMinimumSize(HORIZONTAL),                verticalGroup.getMinimumSize(VERTICAL));    }     /**     * Lays out the specified container.     *     * @param parent the container to be laid out     * @throws IllegalStateException if any of the components added to     *         this layout are not in both a horizontal and vertical group     */    public void layoutContainer(Container parent) {        // Step 1: Prepare for layout.        prepare(SPECIFIC_SIZE);        Insets insets = parent.getInsets();        int width = parent.getWidth() - insets.left - insets.right;        int height = parent.getHeight() - insets.top - insets.bottom;        boolean ltr = isLeftToRight();        if (getAutoCreateGaps() || getAutoCreateContainerGaps() ||                hasPreferredPaddingSprings) {            // Step 2: Calculate autopadding springs            calculateAutopadding(horizontalGroup, HORIZONTAL, SPECIFIC_SIZE, 0,                    width);            calculateAutopadding(verticalGroup, VERTICAL, SPECIFIC_SIZE, 0,                    height);        }        // Step 3: set the size of the groups.        horizontalGroup.setSize(HORIZONTAL, 0, width);        verticalGroup.setSize(VERTICAL, 0, height);        // Step 4: apply the size to the components.        for (ComponentInfo info : componentInfos.values()) {            info.setBounds(insets, width, ltr);        }    }     //    // LayoutManager2    //    /**     * Notification that a {@code Component} has been added to     * the parent container.  You should not invoke this method     * directly, instead you should use one of the {@code Group}     * methods to add a {@code Component}.     *     * @param component the component added     * @param constraints description of where to place the component     */    public void addLayoutComponent(Component component, Object constraints) {    }     /**     * Returns the maximum size for the specified container.     *     * @param parent the container to return the size for     * @return the maximum size for {@code parent}     * @throws IllegalArgumentException if {@code parent} is not     *         the same {@code Container} that this was created with     * @throws IllegalStateException if any of the components added to     *         this layout are not in both a horizontal and vertical group     * @see java.awt.Container#getMaximumSize     */    public Dimension maximumLayoutSize(Container parent) {        checkParent(parent);        prepare(MAX_SIZE);        return adjustSize(horizontalGroup.getMaximumSize(HORIZONTAL),                verticalGroup.getMaximumSize(VERTICAL));    }     /**     * Returns the alignment along the x axis.  This specifies how     * the component would like to be aligned relative to other     * components.  The value should be a number between 0 and 1     * where 0 represents alignment along the origin, 1 is aligned     * the furthest away from the origin, 0.5 is centered, etc.     *     * @param parent the {@code Container} hosting this {@code LayoutManager}     * @throws IllegalArgumentException if {@code parent} is not     *         the same {@code Container} that this was created with     * @return the alignment; this implementation returns {@code .5}     */    public float getLayoutAlignmentX(Container parent) {        checkParent(parent);        return .5f;    }     /**     * Returns the alignment along the y axis.  This specifies how     * the component would like to be aligned relative to other     * components.  The value should be a number between 0 and 1     * where 0 represents alignment along the origin, 1 is aligned     * the furthest away from the origin, 0.5 is centered, etc.     *     * @param parent the {@code Container} hosting this {@code LayoutManager}     * @throws IllegalArgumentException if {@code parent} is not     *         the same {@code Container} that this was created with     * @return alignment; this implementation returns {@code .5}     */    public float getLayoutAlignmentY(Container parent) {        checkParent(parent);        return .5f;    }     /**     * Invalidates the layout, indicating that if the layout manager

Lines 501–1000 of 3745. Previous lines