📄 grouplayout.java
字号:
// 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.remove(existingComponent);
if (newComponent.getParent() != host) {
host.add(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() {
if (host instanceof JComponent) {
((JComponent)host).revalidate();
} else {
host.invalidate();
}
host.repaint();
}
//
// LayoutManager
//
/**
* Notification that a <code>Component</code> has been added to
* the parent container. Developers should not invoke this method
* directly, instead you should use one of the <code>Group</code>
* methods to add a <code>Component</code>.
*
* @param name the string to be associated with the component
* @param component the <code>Component</code> to be added
*/
public void addLayoutComponent(String name, Component component) {
}
/**
* Notification that a <code>Component</code> has been removed from
* the parent container. You should not invoke this method
* directly, instead invoke <code>remove</code> on the parent
* <code>Container</code>.
*
* @param component the component to be removed
* @see java.awt.Component#remove
*/
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 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 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);
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.
Iterator componentInfo = componentInfos.values().iterator();
while (componentInfo.hasNext()) {
ComponentInfo info = (ComponentInfo)componentInfo.next();
Component c = info.getComponent();
info.setBounds(insets, width, ltr);
}
}
//
// LayoutManager2
//
/**
* Notification that a <code>Component</code> has been added to
* the parent container. You should not invoke this method
* directly, instead you should use one of the <code>Group</code>
* methods to add a <code>Component</code>.
*
* @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 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 (Iterator cis = componentInfos.values().iterator();
cis.hasNext();) {
ComponentInfo ci = (ComponentInfo)cis.next();
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() {
Iterator infos = componentInfos.values().iterator();
while (infos.hasNext()) {
ComponentInfo info = (ComponentInfo)infos.next();
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) {
List springs = group.springs;
for (int counter = springs.size() - 1; counter >= 0; counter--) {
Spring spring = (Spring)springs.get(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) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -