jdk/src/java.desktop/share/classes/javax/swing/TablePrintable.java
author psadhukhan
Thu, 22 Dec 2016 18:30:27 +0530
changeset 43077 785bbcd9812a
parent 34397 86a74cb6c903
permissions -rw-r--r--
8170349: The printed content is beyond the borders Reviewed-by: alexsch, aniyogi
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     2
 * Copyright (c) 2003, 2008, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.swing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import javax.swing.table.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.print.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.geom.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.text.MessageFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * An implementation of <code>Printable</code> for printing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * <code>JTable</code>s.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * This implementation spreads table rows naturally in sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * across multiple pages, fitting as many rows as possible per page.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * The distribution of columns, on the other hand, is controlled by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * printing mode parameter passed to the constructor. When
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <code>JTable.PrintMode.NORMAL</code> is used, the implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * handles columns in a similar manner to how it handles rows, spreading them
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * across multiple pages (in an order consistent with the table's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <code>ComponentOrientation</code>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * When <code>JTable.PrintMode.FIT_WIDTH</code> is given, the implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * scales the output smaller if necessary, to ensure that all columns fit on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * the page. (Note that width and height are scaled equally, ensuring that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * aspect ratio remains the same).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * The portion of table printed on each page is headed by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * appropriate section of the table's <code>JTableHeader</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * Header and footer text can be added to the output by providing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <code>MessageFormat</code> instances to the constructor. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * printing code requests Strings from the formats by calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * their <code>format</code> method with a single parameter:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * an <code>Object</code> array containing a single element of type
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * <code>Integer</code>, representing the current page number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * There are certain circumstances where this <code>Printable</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * cannot fit items appropriately, resulting in clipped output.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * These are:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *   <li>In any mode, when the header or footer text is too wide to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *       fit completely in the printable area. The implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *       prints as much of the text as possible starting from the beginning,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *       as determined by the table's <code>ComponentOrientation</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *   <li>In any mode, when a row is too tall to fit in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *       printable area. The upper most portion of the row
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *       is printed and no lower border is shown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *   <li>In <code>JTable.PrintMode.NORMAL</code> when a column
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *       is too wide to fit in the printable area. The center of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *       column is printed and no left and right borders are shown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * It is entirely valid for a developer to wrap this <code>Printable</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * inside another in order to create complex reports and documents. They may
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * even request that different pages be rendered into different sized
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * printable areas. The implementation was designed to handle this by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * performing most of its calculations on the fly. However, providing different
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * sizes works best when <code>JTable.PrintMode.FIT_WIDTH</code> is used, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * when only the printable width is changed between pages. This is because when
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * it is printing a set of rows in <code>JTable.PrintMode.NORMAL</code> and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * implementation determines a need to distribute columns across pages,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * it assumes that all of those rows will fit on each subsequent page needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * to fit the columns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * It is the responsibility of the developer to ensure that the table is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * modified in any way after this <code>Printable</code> is created (invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * modifications include changes in: size, renderers, or underlying data).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * The behavior of this <code>Printable</code> is undefined if the table is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * changed at any time after creation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * @author  Shannon Hickey
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
class TablePrintable implements Printable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    /** The table to print. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    private JTable table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /** For quick reference to the table's header. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    private JTableHeader header;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /** For quick reference to the table's column model. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private TableColumnModel colModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /** To save multiple calculations of total column width. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private int totalColWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /** The printing mode of this printable. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    private JTable.PrintMode printMode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /** Provides the header text for the table. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    private MessageFormat headerFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /** Provides the footer text for the table. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    private MessageFormat footerFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /** The most recent page index asked to print. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    private int last = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    /** The next row to print. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    private int row = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /** The next column to print. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    private int col = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    /** Used to store an area of the table to be printed. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    private final Rectangle clip = new Rectangle(0, 0, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /** Used to store an area of the table's header to be printed. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    private final Rectangle hclip = new Rectangle(0, 0, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /** Saves the creation of multiple rectangles. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    private final Rectangle tempRect = new Rectangle(0, 0, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    /** Vertical space to leave between table and header/footer text. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    private static final int H_F_SPACE = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /** Font size for the header text. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    private static final float HEADER_FONT_SIZE = 18.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /** Font size for the footer text. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    private static final float FOOTER_FONT_SIZE = 12.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /** The font to use in rendering header text. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    private Font headerFont;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    /** The font to use in rendering footer text. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    private Font footerFont;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * Create a new <code>TablePrintable</code> for the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * <code>JTable</code>. Header and footer text can be specified using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * two <code>MessageFormat</code> parameters. When called upon to provide
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * a String, each format is given the current page number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @param  table         the table to print
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @param  printMode     the printing mode for this printable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @param  headerFormat  a <code>MessageFormat</code> specifying the text to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *                       be used in printing a header, or null for none
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @param  footerFormat  a <code>MessageFormat</code> specifying the text to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *                       be used in printing a footer, or null for none
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @throws IllegalArgumentException if passed an invalid print mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    public TablePrintable(JTable table,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                          JTable.PrintMode printMode,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                          MessageFormat headerFormat,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                          MessageFormat footerFormat) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        this.table = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        header = table.getTableHeader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        colModel = table.getColumnModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        totalColWidth = colModel.getTotalColumnWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        if (header != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            // the header clip height can be set once since it's unchanging
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            hclip.height = header.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        this.printMode = printMode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        this.headerFormat = headerFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        this.footerFormat = footerFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        // derive the header and footer font from the table's font
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        headerFont = table.getFont().deriveFont(Font.BOLD,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                                                HEADER_FONT_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        footerFont = table.getFont().deriveFont(Font.PLAIN,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                                                FOOTER_FONT_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * Prints the specified page of the table into the given {@link Graphics}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * context, in the specified format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * @param   graphics    the context into which the page is drawn
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @param   pageFormat  the size and orientation of the page being drawn
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * @param   pageIndex   the zero based index of the page to be drawn
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * @return  PAGE_EXISTS if the page is rendered successfully, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     *          NO_SUCH_PAGE if a non-existent page index is specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * @throws  PrinterException if an error causes printing to be aborted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                                                       throws PrinterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        // for easy access to these values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        final int imgWidth = (int)pageFormat.getImageableWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        final int imgHeight = (int)pageFormat.getImageableHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        if (imgWidth <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            throw new PrinterException("Width of printable area is too small.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        // to pass the page number when formatting the header and footer text
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   216
        Object[] pageNumber = new Object[]{Integer.valueOf(pageIndex + 1)};
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        // fetch the formatted header text, if any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        String headerText = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        if (headerFormat != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            headerText = headerFormat.format(pageNumber);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        // fetch the formatted footer text, if any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        String footerText = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        if (footerFormat != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            footerText = footerFormat.format(pageNumber);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        // to store the bounds of the header and footer text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        Rectangle2D hRect = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        Rectangle2D fRect = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        // the amount of vertical space needed for the header and footer text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        int headerTextSpace = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        int footerTextSpace = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        // the amount of vertical space available for printing the table
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        int availableSpace = imgHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        // if there's header text, find out how much space is needed for it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        // and subtract that from the available space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        if (headerText != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            graphics.setFont(headerFont);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            hRect = graphics.getFontMetrics().getStringBounds(headerText,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                                                              graphics);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            headerTextSpace = (int)Math.ceil(hRect.getHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            availableSpace -= headerTextSpace + H_F_SPACE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        // if there's footer text, find out how much space is needed for it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        // and subtract that from the available space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        if (footerText != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            graphics.setFont(footerFont);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            fRect = graphics.getFontMetrics().getStringBounds(footerText,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                                                              graphics);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            footerTextSpace = (int)Math.ceil(fRect.getHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            availableSpace -= footerTextSpace + H_F_SPACE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        if (availableSpace <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            throw new PrinterException("Height of printable area is too small.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        // depending on the print mode, we may need a scale factor to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        // fit the table's entire width on the page
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        double sf = 1.0D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        if (printMode == JTable.PrintMode.FIT_WIDTH &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                totalColWidth > imgWidth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            // if not, we would have thrown an acception previously
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            assert imgWidth > 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            // it must be, according to the if-condition, since imgWidth > 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            assert totalColWidth > 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            sf = (double)imgWidth / (double)totalColWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        // dictated by the previous two assertions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        assert sf > 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        // This is in a loop for two reasons:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        // First, it allows us to catch up in case we're called starting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        // with a non-zero pageIndex. Second, we know that we can be called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        // for the same page multiple times. The condition of this while
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        // loop acts as a check, ensuring that we don't attempt to do the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        // calculations again when we are called subsequent times for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        // same page.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        while (last < pageIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            // if we are finished all columns in all rows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            if (row >= table.getRowCount() && col == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                return NO_SUCH_PAGE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            // rather than multiplying every row and column by the scale factor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            // in findNextClip, just pass a width and height that have already
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            // been divided by it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            int scaledWidth = (int)(imgWidth / sf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            int scaledHeight = (int)((availableSpace - hclip.height) / sf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            // calculate the area of the table to be printed for this page
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            findNextClip(scaledWidth, scaledHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
34397
86a74cb6c903 8081491: The case print incomplete.
psadhukhan
parents: 25859
diff changeset
   306
            if (!((table.getBounds()).intersects(clip))) {
86a74cb6c903 8081491: The case print incomplete.
psadhukhan
parents: 25859
diff changeset
   307
                return NO_SUCH_PAGE;
86a74cb6c903 8081491: The case print incomplete.
psadhukhan
parents: 25859
diff changeset
   308
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            last++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        // create a copy of the graphics so we don't affect the one given to us
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        Graphics2D g2d = (Graphics2D)graphics.create();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        // translate into the co-ordinate system of the pageFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        // to save and store the transform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        AffineTransform oldTrans;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        // if there's footer text, print it at the bottom of the imageable area
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        if (footerText != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            oldTrans = g2d.getTransform();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            g2d.translate(0, imgHeight - footerTextSpace);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            printText(g2d, footerText, fRect, footerFont, imgWidth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            g2d.setTransform(oldTrans);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        // if there's header text, print it at the top of the imageable area
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        // and then translate downwards
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        if (headerText != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            printText(g2d, headerText, hRect, headerFont, imgWidth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            g2d.translate(0, headerTextSpace + H_F_SPACE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        // constrain the table output to the available space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        tempRect.x = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        tempRect.y = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        tempRect.width = imgWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        tempRect.height = availableSpace;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        g2d.clip(tempRect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        // if we have a scale factor, scale the graphics object to fit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        // the entire width
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        if (sf != 1.0D) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            g2d.scale(sf, sf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        // otherwise, ensure that the current portion of the table is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        // centered horizontally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            int diff = (imgWidth - clip.width) / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            g2d.translate(diff, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        // store the old transform and clip for later restoration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        oldTrans = g2d.getTransform();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        Shape oldClip = g2d.getClip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        // if there's a table header, print the current section and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        // then translate downwards
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        if (header != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            hclip.x = clip.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            hclip.width = clip.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            g2d.translate(-hclip.x, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            g2d.clip(hclip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            header.print(g2d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            // restore the original transform and clip
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            g2d.setTransform(oldTrans);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            g2d.setClip(oldClip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            // translate downwards
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            g2d.translate(0, hclip.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        // print the current section of the table
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        g2d.translate(-clip.x, -clip.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        g2d.clip(clip);
43077
785bbcd9812a 8170349: The printed content is beyond the borders
psadhukhan
parents: 34397
diff changeset
   383
785bbcd9812a 8170349: The printed content is beyond the borders
psadhukhan
parents: 34397
diff changeset
   384
        // set a property so that BasicTableUI#paint can know JTable printMode
785bbcd9812a 8170349: The printed content is beyond the borders
psadhukhan
parents: 34397
diff changeset
   385
        // is FIT_WIDTH since TablePrintable.printMode is not accessible from BasicTableUI
785bbcd9812a 8170349: The printed content is beyond the borders
psadhukhan
parents: 34397
diff changeset
   386
        if (printMode == JTable.PrintMode.FIT_WIDTH) {
785bbcd9812a 8170349: The printed content is beyond the borders
psadhukhan
parents: 34397
diff changeset
   387
            table.putClientProperty("Table.printMode", JTable.PrintMode.FIT_WIDTH);
785bbcd9812a 8170349: The printed content is beyond the borders
psadhukhan
parents: 34397
diff changeset
   388
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        table.print(g2d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        // restore the original transform and clip
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        g2d.setTransform(oldTrans);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        g2d.setClip(oldClip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        // draw a box around the table
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        g2d.setColor(Color.BLACK);
34397
86a74cb6c903 8081491: The case print incomplete.
psadhukhan
parents: 25859
diff changeset
   397
86a74cb6c903 8081491: The case print incomplete.
psadhukhan
parents: 25859
diff changeset
   398
        // compute the visible portion of table and draw the rect around it
86a74cb6c903 8081491: The case print incomplete.
psadhukhan
parents: 25859
diff changeset
   399
        Rectangle visibleBounds = clip.intersection(table.getBounds());
86a74cb6c903 8081491: The case print incomplete.
psadhukhan
parents: 25859
diff changeset
   400
        Point upperLeft = visibleBounds.getLocation();
86a74cb6c903 8081491: The case print incomplete.
psadhukhan
parents: 25859
diff changeset
   401
        Point lowerRight = new Point(visibleBounds.x + visibleBounds.width,
86a74cb6c903 8081491: The case print incomplete.
psadhukhan
parents: 25859
diff changeset
   402
                                     visibleBounds.y + visibleBounds.height);
86a74cb6c903 8081491: The case print incomplete.
psadhukhan
parents: 25859
diff changeset
   403
86a74cb6c903 8081491: The case print incomplete.
psadhukhan
parents: 25859
diff changeset
   404
        int rMin = table.rowAtPoint(upperLeft);
86a74cb6c903 8081491: The case print incomplete.
psadhukhan
parents: 25859
diff changeset
   405
        int rMax = table.rowAtPoint(lowerRight);
86a74cb6c903 8081491: The case print incomplete.
psadhukhan
parents: 25859
diff changeset
   406
        if (rMin == -1) {
86a74cb6c903 8081491: The case print incomplete.
psadhukhan
parents: 25859
diff changeset
   407
            rMin = 0;
86a74cb6c903 8081491: The case print incomplete.
psadhukhan
parents: 25859
diff changeset
   408
        }
86a74cb6c903 8081491: The case print incomplete.
psadhukhan
parents: 25859
diff changeset
   409
        if (rMax == -1) {
86a74cb6c903 8081491: The case print incomplete.
psadhukhan
parents: 25859
diff changeset
   410
            rMax = table.getRowCount();
86a74cb6c903 8081491: The case print incomplete.
psadhukhan
parents: 25859
diff changeset
   411
        }
86a74cb6c903 8081491: The case print incomplete.
psadhukhan
parents: 25859
diff changeset
   412
        int rowHeight = 0;
86a74cb6c903 8081491: The case print incomplete.
psadhukhan
parents: 25859
diff changeset
   413
        for(int visrow = rMin; visrow < rMax; visrow++) {
86a74cb6c903 8081491: The case print incomplete.
psadhukhan
parents: 25859
diff changeset
   414
            rowHeight += table.getRowHeight(visrow);
86a74cb6c903 8081491: The case print incomplete.
psadhukhan
parents: 25859
diff changeset
   415
        }
43077
785bbcd9812a 8170349: The printed content is beyond the borders
psadhukhan
parents: 34397
diff changeset
   416
        // If PrintMode is FIT_WIDTH, then draw rect for entire column width while
785bbcd9812a 8170349: The printed content is beyond the borders
psadhukhan
parents: 34397
diff changeset
   417
        // printing irrespective of how many columns are visible in console
785bbcd9812a 8170349: The printed content is beyond the borders
psadhukhan
parents: 34397
diff changeset
   418
        if (printMode == JTable.PrintMode.FIT_WIDTH) {
785bbcd9812a 8170349: The printed content is beyond the borders
psadhukhan
parents: 34397
diff changeset
   419
            g2d.drawRect(0, 0, clip.width, hclip.height + rowHeight);
785bbcd9812a 8170349: The printed content is beyond the borders
psadhukhan
parents: 34397
diff changeset
   420
        } else {
785bbcd9812a 8170349: The printed content is beyond the borders
psadhukhan
parents: 34397
diff changeset
   421
            g2d.drawRect(0, 0, visibleBounds.width, hclip.height + rowHeight);
785bbcd9812a 8170349: The printed content is beyond the borders
psadhukhan
parents: 34397
diff changeset
   422
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
43077
785bbcd9812a 8170349: The printed content is beyond the borders
psadhukhan
parents: 34397
diff changeset
   424
        // clear the property
785bbcd9812a 8170349: The printed content is beyond the borders
psadhukhan
parents: 34397
diff changeset
   425
        if (printMode == JTable.PrintMode.FIT_WIDTH) {
785bbcd9812a 8170349: The printed content is beyond the borders
psadhukhan
parents: 34397
diff changeset
   426
            table.putClientProperty("Table.printMode", null);
785bbcd9812a 8170349: The printed content is beyond the borders
psadhukhan
parents: 34397
diff changeset
   427
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        // dispose the graphics copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        g2d.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        return PAGE_EXISTS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * A helper method that encapsulates common code for rendering the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * header and footer text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * @param  g2d       the graphics to draw into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * @param  text      the text to draw, non null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * @param  rect      the bounding rectangle for this text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     *                   as calculated at the given font, non null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * @param  font      the font to draw the text in, non null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * @param  imgWidth  the width of the area to draw into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    private void printText(Graphics2D g2d,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                           String text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                           Rectangle2D rect,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                           Font font,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                           int imgWidth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            int tx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            // if the text is small enough to fit, center it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            if (rect.getWidth() < imgWidth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                tx = (int)((imgWidth - rect.getWidth()) / 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            // otherwise, if the table is LTR, ensure the left side of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            // the text shows; the right can be clipped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            } else if (table.getComponentOrientation().isLeftToRight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                tx = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            // otherwise, ensure the right side of the text shows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                tx = -(int)(Math.ceil(rect.getWidth()) - imgWidth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            int ty = (int)Math.ceil(Math.abs(rect.getY()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            g2d.setColor(Color.BLACK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            g2d.setFont(font);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            g2d.drawString(text, tx, ty);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * Calculate the area of the table to be printed for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * the next page. This should only be called if there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * are rows and columns left to print.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * To avoid an infinite loop in printing, this will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * always put at least one cell on each page.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * @param  pw  the width of the area to print in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * @param  ph  the height of the area to print in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    private void findNextClip(int pw, int ph) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        final boolean ltr = table.getComponentOrientation().isLeftToRight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        // if we're ready to start a new set of rows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        if (col == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            if (ltr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                // adjust clip to the left of the first column
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                clip.x = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                // adjust clip to the right of the first column
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                clip.x = totalColWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            // adjust clip to the top of the next set of rows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            clip.y += clip.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            // adjust clip width and height to be zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            clip.width = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            clip.height = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            // fit as many rows as possible, and at least one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            int rowCount = table.getRowCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            int rowHeight = table.getRowHeight(row);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                clip.height += rowHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                if (++row >= rowCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                rowHeight = table.getRowHeight(row);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            } while (clip.height + rowHeight <= ph);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        // we can short-circuit for JTable.PrintMode.FIT_WIDTH since
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        // we'll always fit all columns on the page
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        if (printMode == JTable.PrintMode.FIT_WIDTH) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            clip.x = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            clip.width = totalColWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        if (ltr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            // adjust clip to the left of the next set of columns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            clip.x += clip.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        // adjust clip width to be zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        clip.width = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        // fit as many columns as possible, and at least one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        int colCount = table.getColumnCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        int colWidth = colModel.getColumn(col).getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            clip.width += colWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            if (!ltr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                clip.x -= colWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            if (++col >= colCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                // reset col to 0 to indicate we're finished all columns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                col = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            colWidth = colModel.getColumn(col).getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        } while (clip.width + colWidth <= pw);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
}