/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* An implementation of <code>Printable</code> for printing
* <code>JTable</code>s.
* <p>
* This implementation spreads table rows naturally in sequence
* across multiple pages, fitting as many rows as possible per page.
* The distribution of columns, on the other hand, is controlled by a
* printing mode parameter passed to the constructor. When
* <code>JTable.PrintMode.NORMAL</code> is used, the implementation
* handles columns in a similar manner to how it handles rows, spreading them
* across multiple pages (in an order consistent with the table's
* <code>ComponentOrientation</code>).
* When <code>JTable.PrintMode.FIT_WIDTH</code> is given, the implementation
* scales the output smaller if necessary, to ensure that all columns fit on
* the page. (Note that width and height are scaled equally, ensuring that the
* aspect ratio remains the same).
* <p>
* The portion of table printed on each page is headed by the
* appropriate section of the table's <code>JTableHeader</code>.
* <p>
* Header and footer text can be added to the output by providing
* <code>MessageFormat</code> instances to the constructor. The
* printing code requests Strings from the formats by calling
* their <code>format</code> method with a single parameter:
* an <code>Object</code> array containing a single element of type
* <code>Integer</code>, representing the current page number.
* <p>
* There are certain circumstances where this <code>Printable</code>
* cannot fit items appropriately, resulting in clipped output.
* These are:
* <ul>
* <li>In any mode, when the header or footer text is too wide to
* fit completely in the printable area. The implementation
* prints as much of the text as possible starting from the beginning,
* as determined by the table's <code>ComponentOrientation</code>.
* <li>In any mode, when a row is too tall to fit in the
* printable area. The upper most portion of the row
* is printed and no lower border is shown.
* <li>In <code>JTable.PrintMode.NORMAL</code> when a column
* is too wide to fit in the printable area. The center of the
* column is printed and no left and right borders are shown.
* </ul>
* <p>
* It is entirely valid for a developer to wrap this <code>Printable</code>
* inside another in order to create complex reports and documents. They may
* even request that different pages be rendered into different sized
* printable areas. The implementation was designed to handle this by
* performing most of its calculations on the fly. However, providing different
* sizes works best when <code>JTable.PrintMode.FIT_WIDTH</code> is used, or
* when only the printable width is changed between pages. This is because when
* it is printing a set of rows in <code>JTable.PrintMode.NORMAL</code> and the
* implementation determines a need to distribute columns across pages,
* it assumes that all of those rows will fit on each subsequent page needed
* to fit the columns.
* <p>
* It is the responsibility of the developer to ensure that the table is not
* modified in any way after this <code>Printable</code> is created (invalid
* modifications include changes in: size, renderers, or underlying data).
* The behavior of this <code>Printable</code> is undefined if the table is
* changed at any time after creation.
*
* @author Shannon Hickey
*/
/** The table to print. */
/** For quick reference to the table's header. */
/** For quick reference to the table's column model. */
/** To save multiple calculations of total column width. */
private int totalColWidth;
/** The printing mode of this printable. */
/** Provides the header text for the table. */
/** Provides the footer text for the table. */
/** The most recent page index asked to print. */
/** The next row to print. */
/** The next column to print. */
/** Used to store an area of the table to be printed. */
/** Used to store an area of the table's header to be printed. */
/** Saves the creation of multiple rectangles. */
/** Font size for the header text. */
/** Font size for the footer text. */
/** The font to use in rendering header text. */
/** The font to use in rendering footer text. */
/**
* Create a new <code>TablePrintable</code> for the given
* <code>JTable</code>. Header and footer text can be specified using the
* two <code>MessageFormat</code> parameters. When called upon to provide
* a String, each format is given the current page number.
*
* @param table the table to print
* @param printMode the printing mode for this printable
* @param headerFormat a <code>MessageFormat</code> specifying the text to
* be used in printing a header, or null for none
* @param footerFormat a <code>MessageFormat</code> specifying the text to
* be used in printing a footer, or null for none
* @throws IllegalArgumentException if passed an invalid print mode
*/
// the header clip height can be set once since it's unchanging
}
this.headerFormat = headerFormat;
this.footerFormat = footerFormat;
// derive the header and footer font from the table's font
}
/**
* Prints the specified page of the table into the given {@link Graphics}
* context, in the specified format.
*
* @param graphics the context into which the page is drawn
* @param pageFormat the size and orientation of the page being drawn
* @param pageIndex the zero based index of the page to be drawn
* @return PAGE_EXISTS if the page is rendered successfully, or
* NO_SUCH_PAGE if a non-existent page index is specified
* @throws PrinterException if an error causes printing to be aborted
*/
throws PrinterException {
// for easy access to these values
if (imgWidth <= 0) {
throw new PrinterException("Width of printable area is too small.");
}
// to pass the page number when formatting the header and footer text
// fetch the formatted header text, if any
if (headerFormat != null) {
}
// fetch the formatted footer text, if any
if (footerFormat != null) {
}
// to store the bounds of the header and footer text
// the amount of vertical space needed for the header and footer text
int headerTextSpace = 0;
int footerTextSpace = 0;
// the amount of vertical space available for printing the table
int availableSpace = imgHeight;
// if there's header text, find out how much space is needed for it
// and subtract that from the available space
if (headerText != null) {
graphics);
}
// if there's footer text, find out how much space is needed for it
// and subtract that from the available space
if (footerText != null) {
graphics);
}
if (availableSpace <= 0) {
throw new PrinterException("Height of printable area is too small.");
}
// depending on the print mode, we may need a scale factor to
// fit the table's entire width on the page
double sf = 1.0D;
totalColWidth > imgWidth) {
// if not, we would have thrown an acception previously
assert imgWidth > 0;
// it must be, according to the if-condition, since imgWidth > 0
assert totalColWidth > 1;
}
// dictated by the previous two assertions
assert sf > 0;
// This is in a loop for two reasons:
// First, it allows us to catch up in case we're called starting
// with a non-zero pageIndex. Second, we know that we can be called
// for the same page multiple times. The condition of this while
// loop acts as a check, ensuring that we don't attempt to do the
// calculations again when we are called subsequent times for the
// same page.
// if we are finished all columns in all rows
return NO_SUCH_PAGE;
}
// rather than multiplying every row and column by the scale factor
// in findNextClip, just pass a width and height that have already
// been divided by it
// calculate the area of the table to be printed for this page
last++;
}
// create a copy of the graphics so we don't affect the one given to us
// translate into the co-ordinate system of the pageFormat
// to save and store the transform
// if there's footer text, print it at the bottom of the imageable area
if (footerText != null) {
}
// if there's header text, print it at the top of the imageable area
// and then translate downwards
if (headerText != null) {
}
// constrain the table output to the available space
tempRect.x = 0;
tempRect.y = 0;
// if we have a scale factor, scale the graphics object to fit
// the entire width
if (sf != 1.0D) {
// otherwise, ensure that the current portion of the table is
// centered horizontally
} else {
}
// store the old transform and clip for later restoration
// if there's a table header, print the current section and
// then translate downwards
// restore the original transform and clip
// translate downwards
}
// print the current section of the table
// restore the original transform and clip
// draw a box around the table
// dispose the graphics copy
return PAGE_EXISTS;
}
/**
* A helper method that encapsulates common code for rendering the
* header and footer text.
*
* @param g2d the graphics to draw into
* @param text the text to draw, non null
* @param rect the bounding rectangle for this text,
* as calculated at the given font, non null
* @param font the font to draw the text in, non null
* @param imgWidth the width of the area to draw into
*/
int imgWidth) {
int tx;
// if the text is small enough to fit, center it
// otherwise, if the table is LTR, ensure the left side of
// the text shows; the right can be clipped
tx = 0;
// otherwise, ensure the right side of the text shows
} else {
}
}
/**
* Calculate the area of the table to be printed for
* the next page. This should only be called if there
* are rows and columns left to print.
*
* To avoid an infinite loop in printing, this will
* always put at least one cell on each page.
*
* @param pw the width of the area to print in
* @param ph the height of the area to print in
*/
// if we're ready to start a new set of rows
if (col == 0) {
if (ltr) {
// adjust clip to the left of the first column
clip.x = 0;
} else {
// adjust clip to the right of the first column
clip.x = totalColWidth;
}
// adjust clip to the top of the next set of rows
// adjust clip width and height to be zero
// fit as many rows as possible, and at least one
do {
break;
}
}
// we can short-circuit for JTable.PrintMode.FIT_WIDTH since
// we'll always fit all columns on the page
clip.x = 0;
return;
}
if (ltr) {
// adjust clip to the left of the next set of columns
}
// adjust clip width to be zero
// fit as many columns as possible, and at least one
do {
if (!ltr) {
}
// reset col to 0 to indicate we're finished all columns
col = 0;
break;
}
}
}