下面列出了javafx.scene.Node#prefWidth ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void adjustToolBarWidth() {
final double maxLength = 0.60 * chart.getWidth();
double length = 0.0;
for (Node node : this.getChildren()) {
length += node.prefWidth(DEFAULT_TOOLBAR_HEIGHT);
}
length += 4 * cornerRadius.get();
final double wrapLength = Math.min(maxLength, Math.max(length, 50));
this.setPrefWrapLength(wrapLength);
this.setMaxWidth(wrapLength);
this.setWidth(maxLength);
final int height = (int) Math.max(getHeight(), Math.max(getBoundsInParent().getHeight(), getBoundsInLocal().getHeight()));
this.setMinHeight(height);
this.setShape(ToolBarShapeHelper.getToolBarShape(wrapLength, height, cornerRadius.get()));
}
protected double computePrefHeight(double width, double topInset, double rightInset, double bottomInset, double leftInset) {
MultiSelect<E> control = getSkinnable();
double usedLineWidth = 0;
double hgap = control.getHgap().doubleValue();
double vgap = control.getVgap().doubleValue();
double prefHeight = getPrefRowHeight();
double y = prefHeight;
if (width == -1 && control.getWidth() > 0)
width = control.getWidth();
for (Node node : getChildren()) {
double prefWidth = node.prefWidth(prefHeight);
if (width > 0 && usedLineWidth + prefWidth > width && usedLineWidth > 0) {
usedLineWidth = 0;
y += prefHeight + vgap;
}
usedLineWidth += prefWidth + hgap;
}
return y;
}
protected void layoutChildren(double contentX, double contentY, double contentWidth, double contentHeight) {
double usedLineWidth = 0;
double hgap = getSkinnable().getHgap().doubleValue();
double vgap = getSkinnable().getVgap().doubleValue();
double prefHeight = getPrefRowHeight();
for (Node node : getChildren()) {
double prefWidth = node.prefWidth(prefHeight);
if (usedLineWidth + prefWidth > contentWidth && usedLineWidth > 0) {
usedLineWidth = 0;
contentY += prefHeight + vgap;
}
double x = usedLineWidth + contentX;
node.resizeRelocate(x, contentY, prefWidth, prefHeight);
usedLineWidth += prefWidth + hgap;
}
}
/** auto fit last node, useful for svg paths */
public void autoFitLastElement()
{
Node n = last();
double w = n.prefHeight(width);
double h = n.prefWidth(height);
double sx = width / w;
double sy = height / h;
double sc = Math.min(sx, sy);
n.setScaleX(sc);
n.setScaleY(sc);
Bounds b = n.getBoundsInLocal();
double dx = (width / 2.0) - b.getMinX() - (b.getWidth() / 2.0);
double dy = (height / 2.0) - b.getMinY() - (b.getHeight() / 2.0);
n.setTranslateX(dx);
n.setTranslateY(dy);
}
protected void layoutChildren(double x, double y, double w, double h) {
for (Node node : getChildren()) {
if (getSkinnable().getOrientation() == Orientation.VERTICAL) {
double prefHeight = node.prefHeight(-1);
node.resizeRelocate(x, y, w, prefHeight);
y += prefHeight;
} else {
double prefWidth = node.prefWidth(-1);
node.resizeRelocate(x, y, prefWidth, h);
x += prefWidth;
}
}
}
/**
* Compute pref width by placing equal amount of childen on each line, with a line height equal to the editors preferred
* height plus the vgap. This will not be correct in every case, depending on the difference in the other childrens
* preferred width, but it seems to be adequate.
*/
protected double computePrefWidth(double height, double topInset, double rightInset, double bottomInset, double leftInset) {
MultiSelect<E> control = getSkinnable();
double hgap = control.getHgap().doubleValue();
double vgap = control.getVgap().doubleValue();
double prefRowHeight = getPrefRowHeight();
int childCount = getChildren().size();
int rows = height <= 0 ? 1 : (int) Math.max(1, Math.floor((prefRowHeight + vgap) / height));
int perRow = (int) Math.ceil(childCount / rows);
double widestRow = 0;
int childPos = 0;
for (int rowCount = 0; rowCount < rows; rowCount++) {
double rowWidth = 0;
double childPosInRow = 0;
while (childPosInRow < perRow && childPos < childCount) {
Node child = getChildren().get(childPos);
rowWidth += child.prefWidth(prefRowHeight) + hgap;
childPos++;
childPosInRow++;
}
if (rowWidth > widestRow)
widestRow = rowWidth;
}
return widestRow + leftInset + rightInset - hgap;
}
public Helper()
{
Insets m = getInsets();
top = m.getTop();
bottom = m.getBottom();
left = m.getLeft();
right = m.getRight();
double lh = 0.0;
children = getChildren();
sz = children.size();
widths = new double[sz];
for(int i=0; i<sz; i++)
{
Node ch = children.get(i);
if(ch.isManaged())
{
double w = ch.prefWidth(-1);
widths[i] = w;
double h = ch.prefHeight(-1);
if(h > lh)
{
lh = h;
}
}
}
lineHeight = lh;
}
@Override protected void updateAllDetails() {
final Node node = (Node) getTarget();
// No property change events on these
resizableDetail.setValue(node != null ? Boolean.toString(node.isResizable()) : "-");
// boolean showResizable = node != null && node.isResizable();
resizableDetail.setIsDefault(node == null);
Orientation bias = null;
if (node != null) {
bias = node.getContentBias();
contentBiasDetail.setValue(bias != null ? bias.toString() : "none");
} else {
contentBiasDetail.setValue("-");
}
contentBiasDetail.setIsDefault(node == null || node.getContentBias() == null);
baselineDetail.setValue(node != null ? f.format(node.getBaselineOffset()) : "-");
baselineDetail.setIsDefault(node == null);
if (node != null) {
double minw = 0;
double minh = 0;
double prefw = 0;
double prefh = 0;
double maxw = 0;
double maxh = 0;
if (bias == null) {
minSizeDetail.setLabel("minWidth(-1)/minHeight(-1):");
prefSizeDetail.setLabel("prefWidth(-1)/prefHeight(-1):");
maxSizeDetail.setLabel("maxWidth(-1)/maxHeight(-1):");
minw = node.minWidth(-1);
minh = node.minHeight(-1);
prefw = node.prefWidth(-1);
prefh = node.prefHeight(-1);
maxw = node.maxWidth(-1);
maxh = node.maxHeight(-1);
} else if (bias == Orientation.HORIZONTAL) {
minSizeDetail.setLabel("minWidth(-1)/minHeight(w):");
prefSizeDetail.setLabel("prefWidth(-1)/prefHeight(w):");
maxSizeDetail.setLabel("maxWidth(-1)/maxHeight(w):");
minw = node.minWidth(-1);
minh = node.minHeight(minw);
prefw = node.prefWidth(-1);
prefh = node.prefHeight(prefw);
maxw = node.maxWidth(-1);
maxh = node.maxHeight(maxw);
} else { // VERTICAL
minSizeDetail.setLabel("minWidth(h)/minHeight(-1):");
prefSizeDetail.setLabel("prefWidth(h)/prefHeight(-1):");
maxSizeDetail.setLabel("maxWidth(h)/maxHeight(-1):");
minh = node.minHeight(-1);
minw = node.minWidth(minh);
prefh = node.prefHeight(-1);
prefw = node.prefWidth(prefh);
maxh = node.maxHeight(-1);
maxw = node.maxWidth(maxh);
}
minSizeDetail.setValue(f.format(minw) + " x " + f.format(minh));
prefSizeDetail.setValue(f.format(prefw) + " x " + f.format(prefh));
maxSizeDetail.setValue((maxw >= Double.MAX_VALUE ? "MAXVALUE" : f.format(maxw)) + " x " + (maxh >= Double.MAX_VALUE ? "MAXVALUE" : f.format(maxh)));
} else {
minSizeDetail.setValue("-");
prefSizeDetail.setValue("-");
maxSizeDetail.setValue("-");
}
final boolean fade = node == null || !node.isResizable();
minSizeDetail.setIsDefault(fade);
prefSizeDetail.setIsDefault(fade);
maxSizeDetail.setIsDefault(fade);
@SuppressWarnings("rawtypes") final ObservableMap map = node != null && node.hasProperties() ? node.getProperties() : null;
constraintsDetail.setValue(ConnectorUtils.serializePropertyMap(map));
constraintsDetail.setIsDefault(map == null || map.size() == 0);
updateDetail("*");
}
@Override
protected void layoutChildren(double contentX, double contentY, double contentWidth, double contentHeight) {
double leftContentWidth = 0;
if (getSkinnable().getLeftContent() != null) {
Node leftContent = getSkinnable().getLeftContent();
leftContentWidth = leftContent.prefWidth(contentHeight);
double leftContentHeight = Math.min(leftContent.prefHeight(leftContentWidth), contentHeight);
leftContent.resize(leftContentWidth, leftContentHeight);
if (leftContentAlignment.get().equals(VPos.TOP)) {
leftContent.relocate(contentX, contentY);
} else if (leftContentAlignment.get().equals(VPos.CENTER)) {
leftContent.relocate(contentX, contentY + (contentHeight - leftContentHeight) / 2);
} else {
leftContent.relocate(contentX, contentY + (contentHeight - leftContentHeight));
}
}
double rightContentWidth = 0;
if (getSkinnable().getRightContent() != null) {
Node rightContent = getSkinnable().getRightContent();
rightContentWidth = rightContent.prefWidth(contentHeight);
double rightContentHeight = Math.min(rightContent.prefHeight(rightContentWidth), contentHeight);
rightContent.resize(rightContentWidth, rightContentHeight);
if (rightContentAlignment.get().equals(VPos.TOP)) {
rightContent.relocate(contentX + contentWidth - rightContentWidth, contentY);
} else if (rightContentAlignment.get().equals(VPos.CENTER)) {
rightContent.relocate(contentX + contentWidth - rightContentWidth, contentY + (contentHeight - rightContentHeight) / 2);
} else {
rightContent.relocate(contentX + contentWidth - rightContentWidth, contentY + (contentHeight - rightContentHeight));
}
}
if (getSkinnable().getCenterContent() != null) {
Node centerContent = getSkinnable().getCenterContent();
double maxWidthForCenterContent = contentWidth - leftContentWidth - rightContentWidth - spacing.get().doubleValue() * 2;
double maxHeigthForCenterContent = contentHeight;
double centerContentWidth = Math.min(centerContent.maxWidth(contentHeight), maxWidthForCenterContent);
double centerContentHeight = Math.min(centerContent.maxHeight(centerContentWidth), maxHeigthForCenterContent);
centerContent.resize(centerContentWidth, centerContentHeight);
if (centerContentAlignment.get().equals(Pos.TOP_LEFT)) {
centerContent.relocate(contentX + leftContentWidth + spacing.get().doubleValue(), contentY);
} else if (centerContentAlignment.get().equals(Pos.TOP_CENTER)) {
centerContent.relocate(contentX + leftContentWidth + spacing.get().doubleValue() + (maxWidthForCenterContent - centerContentWidth) / 2, contentY);
} else if (centerContentAlignment.get().equals(Pos.TOP_RIGHT)) {
centerContent.relocate(contentX + leftContentWidth + spacing.get().doubleValue() + (maxWidthForCenterContent - centerContentWidth), contentY);
} else if (centerContentAlignment.get().equals(Pos.CENTER_LEFT)) {
centerContent.relocate(contentX + leftContentWidth + spacing.get().doubleValue(), contentY + (maxHeigthForCenterContent - centerContentHeight) / 2);
} else if (centerContentAlignment.get().equals(Pos.CENTER)) {
centerContent.relocate(contentX + leftContentWidth + spacing.get().doubleValue() + (maxWidthForCenterContent - centerContentWidth) / 2, contentY + (maxHeigthForCenterContent - centerContentHeight) / 2);
} else if (centerContentAlignment.get().equals(Pos.CENTER_RIGHT)) {
centerContent.relocate(contentX + leftContentWidth + spacing.get().doubleValue() + (maxWidthForCenterContent - centerContentWidth), contentY + (maxHeigthForCenterContent - centerContentHeight) / 2);
} else if (centerContentAlignment.get().equals(Pos.BOTTOM_LEFT)) {
centerContent.relocate(contentX + leftContentWidth + spacing.get().doubleValue(), contentY + (maxHeigthForCenterContent - centerContentHeight));
} else if (centerContentAlignment.get().equals(Pos.BOTTOM_CENTER)) {
centerContent.relocate(contentX + leftContentWidth + spacing.get().doubleValue() + (maxWidthForCenterContent - centerContentWidth) / 2, contentY + (maxHeigthForCenterContent - centerContentHeight));
} else {
centerContent.relocate(contentX + leftContentWidth + spacing.get().doubleValue() + (maxWidthForCenterContent - centerContentWidth), contentY + (maxHeigthForCenterContent - centerContentHeight));
}
}
}
@Override
public double prefLength(Node node, double breadth) {
return node.prefWidth(breadth);
}
@Override
public double prefBreadth(Node node) {
return node.prefWidth(-1);
}