📄 grouplayout.java.svn-base
字号:
linkSize0(components, VERTICAL); } invalidateHost(); } private void linkSize0(Component[] components, int axis) { LinkInfo master = getComponentInfo( components[components.length - 1]).getLinkInfo(axis); for (int counter = components.length - 2; counter >= 0; counter--) { master.add(getComponentInfo(components[counter])); } } /** * Removes an existing component replacing it with the specified component. * * @param existingComponent the Component that should be removed and * replaced with newComponent * @param newComponent the Component to put in existingComponents place * @throws IllegalArgumentException is either of the Components are null or * if 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 = (ComponentInfo)componentInfos. remove(existingComponent); if (info == null) { throw new IllegalArgumentException("Component must already exist"); } host.removeComponent(existingComponent); if (newComponent.getParent() != host) { host.addComponent(newComponent); } info.setComponent(newComponent); componentInfos.put(newComponent, info); invalidateHost(); } /** * Sets the LayoutStyle this GroupLayout is to use. A value of null can * be used to indicate the shared instance of LayoutStyle should be used. * * @param layoutStyle the LayoutStyle to use */ public void setLayoutStyle(LayoutStyle layoutStyle) { this.layoutStyle = layoutStyle; invalidateHost(); } /** * Returns the LayoutStyle instance to use * * @return the LayoutStyle instance to use */ public LayoutStyle getLayoutStyle() { return layoutStyle; } private LayoutStyle getLayoutStyle0() { LayoutStyle layoutStyle = getLayoutStyle(); if (layoutStyle == null) { layoutStyle = LayoutStyle.getSharedInstance(); } return layoutStyle; } private void invalidateHost() { host.invalidate(); host.repaint(); } /** * Notification that a <code>Component</code> has been removed from * the parent container. You should not invoke this method * directly, instead invoke <code>removeComponent</code> on the parent * <code>Container</code>. * * @param component the component to be removed * @see Container#removeComponent */ public void removeLayoutComponent(Component component) { ComponentInfo info = (ComponentInfo)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 size for * @throws IllegalArgumentException if <code>parent</code> is not * the same <code>Container</code> 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 Container#getPreferredSize */ public Dimension getPreferredSize(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 size for * @throws IllegalArgumentException if <code>parent</code> is not * the same <code>Container</code> 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); int insetLeft = parent.getStyle().getMargin(Component.LEFT); int insetTop = parent.getStyle().getMargin(Component.TOP); int insetRight = parent.getStyle().getMargin(Component.RIGHT); int insetBottom = parent.getStyle().getMargin(Component.BOTTOM); int width = parent.getWidth() - insetLeft - insetRight; int height = parent.getHeight() - insetTop - insetBottom; 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. Enumeration componentInfo = componentInfos.elements(); while (componentInfo.hasMoreElements()) { ComponentInfo info = (ComponentInfo)componentInfo.nextElement(); Component c = info.getComponent(); info.setBounds(insetLeft, insetTop, width, ltr); } } /** * Returns the maximum size for the specified container. * * @param parent the container to return size for * @throws IllegalArgumentException if <code>parent</code> is not * the same <code>Container</code> 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 Container hosting this LayoutManager * @throws IllegalArgumentException if <code>parent</code> is not * the same <code>Container</code> that this was created with * @return alignment */ /*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 Container hosting this LayoutManager * @throws IllegalArgumentException if <code>parent</code> is not * the same <code>Container</code> that this was created with * @return alignment */ /*public float getLayoutAlignmentY(Container parent) { checkParent(parent); return .5f; }*/ /** * Invalidates the layout, indicating that if the layout manager * has cached information it should be discarded. * * @param parent Container hosting this LayoutManager * @throws IllegalArgumentException if <code>parent</code> is not * the same <code>Container</code> that this was created with */ /*public void invalidateLayout(Container parent) { checkParent(parent); // invalidateLayout is called from Container.invalidate, which // does NOT grab the treelock. All other methods do. To make sure // there aren't any possible threading problems we grab the tree lock // here. //synchronized(parent.getTreeLock()) { isValid = false; //} }*/ private void prepare(int sizeType) { boolean visChanged = false; // Step 1: If not-valid, clear springs and update visibility. if (!isValid) { isValid = true; horizontalGroup.setSize(HORIZONTAL, UNSET, UNSET); verticalGroup.setSize(VERTICAL, UNSET, UNSET); for (Enumeration cis = componentInfos.elements(); cis.hasMoreElements();) { ComponentInfo ci = (ComponentInfo)cis.nextElement(); if (ci.updateVisibility()) { visChanged = true; } ci.clearCachedSize(); } } // Step 2: Make sure components are bound to ComponentInfos if (springsChanged) { registerComponents(horizontalGroup, HORIZONTAL); registerComponents(verticalGroup, VERTICAL); } // Step 3: Adjust the autopadding. This removes existing // autopadding, then recalculates where it should go. if (springsChanged || visChanged) { checkComponents(); horizontalGroup.removeAutopadding(); verticalGroup.removeAutopadding(); if (getAutocreateGaps()) { insertAutopadding(true); } else if (hasPreferredPaddingSprings || getAutocreateContainerGaps()) { insertAutopadding(false); } springsChanged = false; } // Step 4: (for min/pref/max size calculations only) calculate the // autopadding. This invokes for unsetting the calculated values, then // recalculating them. // If sizeType == SPECIFIC_SIZE, it indicates we're doing layout, this // step will be done later on. if (sizeType != SPECIFIC_SIZE && (getAutocreateGaps() || getAutocreateContainerGaps() || hasPreferredPaddingSprings)) { calculateAutopadding(horizontalGroup, HORIZONTAL, sizeType, 0, 0); calculateAutopadding(verticalGroup, VERTICAL, sizeType, 0, 0); } } private void calculateAutopadding(Group group, int axis, int sizeType, int origin, int size) { group.unsetAutopadding(); switch(sizeType) { case MIN_SIZE: size = group.getMinimumSize(axis); break; case PREF_SIZE: size = group.getPreferredSize(axis); break; case MAX_SIZE: size = group.getMaximumSize(axis); break; } group.setSize(axis, origin, size); group.calculateAutopadding(axis); } private void checkComponents() { Enumeration infos = componentInfos.elements(); while (infos.hasMoreElements()) { ComponentInfo info = (ComponentInfo)infos.nextElement(); if (info.horizontalSpring == null) { throw new IllegalStateException(info.component + " is not attached to a horizontal group"); } if (info.verticalSpring == null) { throw new IllegalStateException(info.component + " is not attached to a vertical group"); } } } private void registerComponents(Group group, int axis) { Vector springs = group.springs; for (int counter = springs.size() - 1; counter >= 0; counter--) { Spring spring = (Spring)springs.elementAt(counter); if (spring instanceof ComponentSpring) { ((ComponentSpring)spring).installIfNecessary(axis); } else if (spring instanceof Group) { registerComponents((Group)spring, axis); } } } private Dimension adjustSize(int width, int height) { int insetLeft = host.getStyle().getMargin(Component.LEFT); int insetTop = host.getStyle().getMargin(Component.TOP); int insetRight = host.getStyle().getMargin(Component.RIGHT); int insetBottom = host.getStyle().getMargin(Component.BOTTOM); return new Dimension(width + insetLeft + insetRight, height + insetTop + insetBottom); } private void checkParent(Container parent) { if (parent != host) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -