📄 grouplayout.java
字号:
Insets insets = host.getInsets();
return new Dimension(width + insets.left + insets.right,
height + insets.top + insets.bottom);
}
private void checkParent(Container parent) {
if (parent != host) {
throw new IllegalArgumentException(
"GroupLayout can only be used with one Container at a time");
}
}
/**
* Returns the <code>ComponentInfo</code> for the specified Component.
*/
private ComponentInfo getComponentInfo(Component component) {
ComponentInfo info = (ComponentInfo)componentInfos.get(component);
if (info == null) {
info = new ComponentInfo(component);
componentInfos.put(component, info);
if (component.getParent() != host) {
host.add(component);
}
}
return info;
}
/**
* Adjusts the autopadding springs for the horizontal and vertical
* groups. If <code>insert</code> is true this will insert auto padding
* springs, otherwise this will only adjust the springs that
* comprise auto preferred padding springs.
*/
private void insertAutopadding(boolean insert) {
horizontalGroup.insertAutopadding(HORIZONTAL, new ArrayList(1),
new ArrayList(1), new ArrayList(1), new ArrayList(1), insert);
verticalGroup.insertAutopadding(VERTICAL, new ArrayList(1),
new ArrayList(1), new ArrayList(1), new ArrayList(1), insert);
}
/**
* Returns true if the two Components have a common ParallelGroup ancestor
* along the particular axis.
*/
private boolean areParallelSiblings(Component source, Component target,
int axis) {
ComponentInfo sourceInfo = getComponentInfo(source);
ComponentInfo targetInfo = getComponentInfo(target);
Spring sourceSpring;
Spring targetSpring;
if (axis == HORIZONTAL) {
sourceSpring = sourceInfo.horizontalSpring;
targetSpring = targetInfo.horizontalSpring;
} else {
sourceSpring = sourceInfo.verticalSpring;
targetSpring = targetInfo.verticalSpring;
}
Set sourcePath = tmpParallelSet;
sourcePath.clear();
Spring spring = sourceSpring.getParent();
while (spring != null) {
sourcePath.add(spring);
spring = spring.getParent();
}
spring = targetSpring.getParent();
while (spring != null) {
if (sourcePath.contains(spring)) {
sourcePath.clear();
while (spring != null) {
if (spring instanceof ParallelGroup) {
return true;
}
spring = spring.getParent();
}
return false;
}
spring = spring.getParent();
}
sourcePath.clear();
return false;
}
private boolean isLeftToRight() {
return host.getComponentOrientation().isLeftToRight();
}
/**
* Spring consists of a range: min, pref and max a value some where in
* the middle of that and a location. Subclasses must override
* methods to get the min/max/pref and will likely want to override
* the <code>setSize</code> method. Spring automatically caches the
* min/max/pref. If the min/pref/max has internally changes, or needs
* to be updated you must invoked clear.
*/
abstract class Spring {
private int size;
private int min;
private int max;
private int pref;
private Spring parent;
private int alignment;
Spring() {
min = pref = max = UNSET;
}
/**
* Calculates and returns the minimum size.
*
* @param axis the axis of layout; one of HORIZONTAL or VERTICAL
* @return the minimum size
*/
abstract int calculateMinimumSize(int axis);
/**
* Calculates and returns the preferred size.
*
* @param axis the axis of layout; one of HORIZONTAL or VERTICAL
* @return the preferred size
*/
abstract int calculatePreferredSize(int axis);
/**
* Calculates and returns the minimum size.
*
* @param axis the axis of layout; one of HORIZONTAL or VERTICAL
* @return the minimum size
*/
abstract int calculateMaximumSize(int axis);
/**
* Sets the parent of this Spring.
*/
void setParent(Spring parent) {
this.parent = parent;
}
/**
* Returns the parent of this spring.
*/
Spring getParent() {
return parent;
}
// This is here purely as a conveniance for ParallelGroup to avoid
// having to track alignment separately.
void setAlignment(int alignment) {
this.alignment = alignment;
}
int getAlignment() {
return alignment;
}
/**
* Returns the minimum size.
*/
final int getMinimumSize(int axis) {
if (min == UNSET) {
min = constrain(calculateMinimumSize(axis));
}
return min;
}
/**
* Returns the preferred size.
*/
final int getPreferredSize(int axis) {
if (pref == UNSET) {
pref = constrain(calculatePreferredSize(axis));
}
return pref;
}
/**
* Returns the maximum size.
*/
final int getMaximumSize(int axis) {
if (max == UNSET) {
max = constrain(calculateMaximumSize(axis));
}
return max;
}
/**
* Resets the cached min/max/pref.
*/
void unset() {
size = min = pref = max = UNSET;
}
/**
* Sets the value and location of the spring. Subclasses
* will want to invoke super, then do any additional sizing.
*
* @param axis HORIZONTAL or VERTICAL
* @param origin of this Spring
* @param size of the Spring. If size is UNSET, this invokes
* clear.
*/
void setSize(int axis, int origin, int size) {
this.size = size;
if (size == UNSET) {
unset();
}
}
/**
* Returns the current size.
*/
int getSize() {
return size;
}
int constrain(int value) {
return Math.min(value, Short.MAX_VALUE);
}
int getBaseline() {
return -1;
}
int getBaselineResizeBehavior() {
return Baseline.BRB_OTHER;
}
final boolean isResizable(int axis) {
int min = getMinimumSize(axis);
int pref = getPreferredSize(axis);
return (min != pref || pref != getMaximumSize(axis));
}
/**
* Returns true if this Spring will ALWAYS have a zero size. This should
* NOT check the current size, rather it's meant to
* quickly test if this Spring will always have a zero size.
*/
abstract boolean willHaveZeroSize(boolean treatAutopaddingAsZeroSized);
}
/**
* Group provides for commonality between the two types of operations
* supported by <code>GroupLayout</code>: laying out components one
* after another (<code>SequentialGroup</code>) or layout on top
* of each other (<code>ParallelGroup</code>). Use one of
* <code>createSequentialGroup</code> or
* <code>createParallelGroup</code> to create one.
*/
public abstract class Group extends Spring {
// private int origin;
// private int size;
List springs;
Group() {
springs = new ArrayList();
}
int indexOf(Spring spring) {
return springs.indexOf(spring);
}
/**
* Adds the Spring to the list of <code>Spring</code>s and returns
* the receiver.
*/
Group addSpring(Spring spring) {
springs.add(spring);
spring.setParent(this);
if (!(spring instanceof AutopaddingSpring) ||
!((AutopaddingSpring)spring).getUserCreated()) {
springsChanged = true;
}
return this;
}
//
// Spring methods
//
void setSize(int axis, int origin, int size) {
super.setSize(axis, origin, size);
if (size == UNSET) {
for (int counter = springs.size() - 1; counter >= 0;
counter--) {
getSpring(counter).setSize(axis, origin, size);
}
} else {
setValidSize(axis, origin, size);
}
}
/**
* This is invoked from <code>setSize</code> if passed a value
* other than UNSET.
*/
abstract void setValidSize(int axis, int origin, int size);
int calculateMinimumSize(int axis) {
return calculateSize(axis, MIN_SIZE);
}
int calculatePreferredSize(int axis) {
return calculateSize(axis, PREF_SIZE);
}
int calculateMaximumSize(int axis) {
return calculateSize(axis, MAX_SIZE);
}
/**
* Used to compute how the two values representing two springs
* will be combined. For example, a group that layed things out
* one after the next would return <code>a + b</code>.
*/
abstract int operator(int a, int b);
/**
* Calculates the specified size. This is called from
* one of the <code>getMinimumSize0</code>,
* <code>getPreferredSize0</code> or
* <code>getMaximumSize0</code> methods. This will invoke
* to <code>operator</code> to combine the values.
*/
int calculateSize(int axis, int type) {
int count = springs.size();
if (count == 0) {
return 0;
}
if (count == 1) {
return getSpringSize(getSpring(0), axis, type);
}
int size = constrain(operator(getSpringSize(getSpring(0), axis, type),
getSpringSize(getSpring(1), axis, type)));
for (int counter = 2; counter < count; counter++) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -