Java Reference
In-Depth Information
Example 12−4: PrintableDocument.java (continued)
// Figure out its size and location
Shape kidshape = v.getChildAllocation(i, allocation);
if (kidshape == null) continue;
Rectangle2D kidbox = kidshape.getBounds2D();
// This is the Y coordinate of the bottom of the child
double kidpos = kidbox.getY() + kidbox.getHeight() - pageStart;
// If this is the first child of a group, then we want to ensure
// that it doesn't get left by itself at the bottom of a page.
// I.e. we want to prevent "widows"
if ((numkids > 1) && (i == 0)) {
// If it is not near the end of the page, then just move
// on to the next child
if (kidpos < printY + printHeight*MARGIN_ADJUST) continue;
// Otherwise, the child is near the bottom of the page, so
// break the page before this child and place this child on
// the new page.
breakPage(kidbox.getY());
continue;
}
// If this is the last child of a group, we don't want it to
// appear by itself at the top of a new page, so allow it to
// squeeze past the bottom margin if necessary. This helps to
// prevent "orphans"
if ((numkids > 1) && (i == numkids-1)) {
// If it fits normally, just move on to the next one
if (kidpos < printY + printHeight) continue;
// Otherwise, if it fits with extra space, then break the
// page at the end of the group
if (kidpos < printY + printHeight/MARGIN_ADJUST) {
breakPage(allocation.getY() + allocation.getHeight());
continue;
}
}
// If the child is not the first or last of a group, then we use
// the bottom margin strictly. If the child fits on the page,
// then move on to the next child.
if (kidpos < printY+printHeight) continue;
// If we get here, the child doesn't fit on this page. If it has
// no children, then break the page before this child and continue.
if (kid.getViewCount() == 0) {
breakPage(kidbox.getY());
continue;
}
// If we get here, then the child did not fit on the page, but it
// has kids of its own, so recurse to see if any of those kids
// will fit on the page.
paginate(kid, kidbox);
}
}
Search WWH ::




Custom Search