⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tableprinter.java

📁 eq跨平台查询工具源码 eq跨平台查询工具源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * TablePrinter.java * * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or any later version. * * This program 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 for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. * */package org.executequery.print;import java.awt.Color;import java.awt.Dimension;import java.awt.Font;import java.awt.FontMetrics;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.Point;import java.awt.Rectangle;import java.awt.print.PageFormat;import java.awt.print.Printable;import java.awt.print.PrinterException;import java.text.MessageFormat;import java.util.Vector;import javax.swing.JTable;import javax.swing.JTable.PrintMode;import javax.swing.plaf.basic.BasicGraphicsUtils;import javax.swing.table.TableColumn;import javax.swing.table.TableColumnModel;import javax.swing.table.TableModel;import org.executequery.Constants;import org.executequery.GUIUtilities;import org.executequery.SystemUtilities;import org.underworldlabs.swing.table.PrintableTableModel;/* ---------------------------------------------------------- * CVS NOTE: Changes to the CVS repository prior to the  *           release of version 3.0.0beta1 has meant a  *           resetting of CVS revision numbers. * ---------------------------------------------------------- *//** * Simple <code>Printable</code> implementation for * <code>JTable</code>s. * * @author   Takis Diakoumis * @version  $Revision: 1.4 $ * @date     $Date: 2006/05/14 06:56:52 $ */public class TablePrinter implements Printable {                                     //Pageable {    private final String EMPTY = "";        protected int headerStatus = ALL_PAGES;    protected int maxNumPage = 1;    /** the table to be printed */    protected JTable table;        /** the page format for this printing */    protected PageFormat pageFormat;    /** the page header text */    private String pageHeaderText;        /** the table data font */    private Font plainFont;        /** the table header font */    private Font boldFont;        /** the footer font */    private Font footerFont;    /** the table header bg colour */    private Color headerBackground;        /** Indicates whether the first column in the table should be printed */    private boolean printFirstColumn;    /** table row height */    private int rowHeight;    /** table data row count */    private int rowCount;    // These constants indicate which pages should include column headers    public final static int ALL_PAGES = 0;    public final static int FIRST_PAGE_ONLY = 1;    public final static int NO_PAGES = 2;    public TablePrinter(JTable table, String pageHeaderText) {        this(table, pageHeaderText, true);    }    public TablePrinter(JTable table, String pageHeaderText, boolean printFirstColumn) {        this.table = table;        this.pageHeaderText = pageHeaderText;        this.printFirstColumn = printFirstColumn;                rowCount = table.getRowCount();        rowHeight = table.getRowHeight();                // init the fonts        String fontName = "monospaced";        plainFont = new Font(fontName, Font.PLAIN, 10);        boldFont = new Font(fontName, Font.BOLD, 10);        footerFont = new Font(fontName, Font.PLAIN, 9);                // colours        headerBackground = new Color(204,204,204);    }    /**     * Resets the state of the printable.<br>     * This is intended to be called when you want to begin     * the printing again from the beginning.     */    public void reset() {        _lastRow = 0;        _lastColumn = 0;        if (pages != null) {            pages.clear();        }    }        /**      * Returns the table set to be printed.     *     * @return the table being printed     */    public JTable getTable() {        return table;    }        /**     * Sets the table to be printed by this printable.     *     * @param table - the table to print     */    public void setTable(JTable table) {        this.table = table;    }    // don't like this - it only calls the table's paint method with the    // printers graphics object - includes renderers, editors etc... yuk!!    // also has some unreadable scaling for larger tables when FIT_WIDTH is set    // ...nice page breaking though    public int print_(Graphics graphics, PageFormat pageFormat, int pageIndex)        throws PrinterException {        PrintMode printMode = JTable.PrintMode.FIT_WIDTH;        if (table.getAutoResizeMode() == JTable.AUTO_RESIZE_OFF) {            printMode = JTable.PrintMode.NORMAL;        }                return table.getPrintable(printMode,                                   new MessageFormat(pageHeaderText),                                   new MessageFormat("Page {0}")).                print(graphics, pageFormat, pageIndex);    }    /** the last row printed in multi-page */    private int _lastRow = 0;        /** the last column printed in multi-page */    private int _lastColumn = 0;        private int lastPageIndex;        private Vector<TablePrintSegment> pages;        /**     * Prints the page at the specified index into the specified      * context in the specified format. A <code>PrinterJob</code> calls     * the <code>Printable</code> interface to request that a page be     * rendered into the context specified by      * <code>graphics</code>. The format of the page to be drawn is     * specified by <code>pageFormat</code>. The zero based index     * of the requested page is specified by <code>pageIndex</code>.      * If the requested page does not exist then this method returns     * NO_SUCH_PAGE; otherwise PAGE_EXISTS is returned.     * The <code>Graphics</code> class or subclass implements the     * interface to provide additional information.  If the      * <code>Printable</code> object aborts the print job then it throws      * a <code>PrinterException</code>.     *     * @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 <code>pageIndex</code> specifies a     *	       non-existent page.     * @exception java.awt.print.PrinterException     *         thrown when the print job is terminated.     */    public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)        throws PrinterException {        int lastRow = 0;        int lastColumn = 0;                TablePrintSegment segment = getPrintSegment(pageIndex);        if (segment != null) {            lastRow = segment.lastRow;            lastColumn = segment.lastColumn;        }                /*        Log.debug("page: "+pageIndex);        Log.debug("lastRow: "+lastRow);        Log.debug("lastColumn: "+lastColumn);                */        if (lastRow == -1 && lastColumn == -1) {            GUIUtilities.getStatusBar().setSecondLabelText("");            if (pages != null) {                pages.clear();            }            return NO_SUCH_PAGE;        }        lastPageIndex = pageIndex;        GUIUtilities.getStatusBar().setSecondLabelText(                "Printing page " +(pageIndex + 1));        Graphics2D g2d = (Graphics2D)graphics;        pageFormat = SystemUtilities.getPageFormat();                graphics.translate((int)pageFormat.getImageableX(),                           (int)pageFormat.getImageableY());        // determine the actual print width and height        int printWidth = (int)pageFormat.getImageableWidth();        int printHeight = (int)pageFormat.getImageableHeight();        /*        Log.debug("imageableHeight: " + pageFormat.getImageableHeight());        Log.debug("imageableWidth: " + pageFormat.getImageableWidth());        */        /*        g2d.setColor(Color.YELLOW);        g2d.fillRect(0,0,printWidth,printHeight);        */        // add the page number footer        g2d.setFont(footerFont);        g2d.setColor(Color.BLACK);        FontMetrics fm = g2d.getFontMetrics();        String footerText = "Page " + (pageIndex + 1);        int textX = (printWidth - fm.stringWidth(footerText)) / 2;        int textY = (printHeight - fm.getHeight());        g2d.drawString(footerText, textX, textY);                // reduce the printHeight to account for page number        printHeight -= (fm.getHeight() + 5);                //Log.debug("imageable x: "+pageFormat.getImageableX());        //Log.debug("imageable y: "+pageFormat.getImageableY());        // scale factor for each column if we are trying to fit        // all available columns on the one page        double colScaleFactor = 1;        // check of we need to scale the column widths        if (table.getAutoResizeMode() != JTable.AUTO_RESIZE_OFF) {            int tableWidth = table.getWidth();            if (tableWidth > printWidth) {                colScaleFactor = ((double)printWidth/(double)tableWidth);            }        }        g2d.setFont(plainFont);        g2d.setColor(Color.black);        fm = graphics.getFontMetrics();        int y = fm.getAscent();                // draw the title        if (pageIndex == 0) {            if (pageHeaderText != null) {                g2d.setClip(0, 0, printWidth, printHeight);                g2d.drawString(pageHeaderText, 0, y);                y += 20; // space between title and table headers            }        }        int firstColumn = 0;        TableColumnModel columnModel = table.getColumnModel();        int columnCount = columnModel.getColumnCount();        if (!printFirstColumn) {            firstColumn = 1;        }        // if we have left-over columns start there        if (lastColumn > 0) {            firstColumn = lastColumn;        }                // define an array to hold the col x positions        int x[] = new int[columnCount];        x[0] = 0;                // define an array to hold the col width values        int colWidths[] = new int[columnCount];        colWidths[0] = 0;                // table header font        g2d.setFont(boldFont);        fm = g2d.getFontMetrics();        // font ascent offset        int h = fm.getAscent();        Rectangle rect = new Rectangle();                if (pageIndex > 0) {            rect.y = rowHeight + h;        } else {            rect.y = y;        }        rect.height = rowHeight;                int totalWidth = 0;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -