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

📄 textprinter.java

📁 eq跨平台查询工具源码 eq跨平台查询工具源码
💻 JAVA
字号:
/* * TextPrinter.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.Font;import java.awt.FontMetrics;import java.awt.Graphics;import java.awt.print.PageFormat;import java.awt.print.Printable;import java.util.StringTokenizer;import java.util.Vector;import org.underworldlabs.util.SystemProperties;/* ---------------------------------------------------------- * CVS NOTE: Changes to the CVS repository prior to the  *           release of version 3.0.0beta1 has meant a  *           resetting of CVS revision numbers. * ---------------------------------------------------------- *//** * * @author   Takis Diakoumis * @version  $Revision: 1.3 $ * @date     $Date: 2006/05/14 06:56:52 $ */public class TextPrinter implements Printable {        /** The text to be printed */    private String text;        /** The font to print with */    private Font printFont;    /** Holds the lines of text */    private Vector lines;        /** The tab sizes in spaces */    private int tabSize;        /** <p>Constructs a new text printer with the specified text.     *     *  @param the text to be printed     */    public TextPrinter(String text) {        this.text = text;        printFont = new Font("monospaced", Font.PLAIN, 10);        tabSize = SystemProperties.getIntProperty("user", "editor.tab.spaces");    }        public int print(Graphics g, PageFormat pageFormat, int pageIndex) {                g.translate((int)pageFormat.getImageableX(),                    (int)pageFormat.getImageableY());                int printWidth = (int)pageFormat.getImageableWidth();        int printHeight = (int)pageFormat.getImageableHeight();        g.setClip(0, 0, printWidth, printHeight);                g.setColor(Color.BLACK);        g.setFont(printFont);                FontMetrics fm = g.getFontMetrics();        int fontHeight = fm.getHeight();        if (lines == null) {            lines = getLines(fm, printWidth);        }        // print the page number        String footerText = "Page " + (pageIndex + 1);        int textX = (printWidth - fm.stringWidth(footerText)) / 2;        int textY = (printHeight - fontHeight);        g.drawString(footerText, textX, textY);        // reduce the printHeight to account for page number        printHeight -= (fontHeight + 10);                int numLines = lines.size();        int linesPerPage = Math.max(printHeight / fontHeight, 1);        int numPages = (int)Math.ceil((double)numLines / (double)linesPerPage);                if (numPages == 0) {            numPages = 1;        }        if (pageIndex >= numPages) {            lines = null;            return NO_SUCH_PAGE;        }                int x = 0;        int y = fm.getAscent();        int lineIndex = linesPerPage * pageIndex;                while (lineIndex < numLines && y < printHeight) {            String str = (String)lines.get(lineIndex);            g.drawString(str, x, y);            y += fontHeight;            lineIndex++;        }                return PAGE_EXISTS;    }        protected Vector getLines(FontMetrics fm, int wPage) {                Vector v = new Vector();                String prevToken = "";        StringTokenizer st = new StringTokenizer(text, "\n\r", true);                String empty = "";        String carriageReturn = "\r";        String newLine = "\n";        String tab = "\t";        String spaceTab = " \t";        String space = " ";                while (st.hasMoreTokens()) {                        String line = st.nextToken();                        if (line.equals(carriageReturn)) {                continue;            }                        // StringTokenizer will ignore empty lines,            // so it's a bit tricky to get them...            if (line.equals(newLine) && prevToken.equals(newLine)) {                v.add(empty);            }                        prevToken = line;                        if (line.equals(newLine)) {                continue;            }                        StringTokenizer st2 = new StringTokenizer(line, spaceTab, true);            String line2 = empty;                        while (st2.hasMoreTokens()) {                String token = st2.nextToken();                                if (token.equals(tab)) {                    int numSpaces = tabSize - line2.length() % tabSize;                    token = empty;                                        for (int k = 0; k < numSpaces; k++) {                        token += space;                    }                                    }                                int lineLength = fm.stringWidth(line2 + token);                                if (lineLength > wPage && line2.length() > 0) {                    v.add(line2);                    line2 = token.trim();                    continue;                }                                line2 += token;                            }                        v.add(line2);                    }                return v;    }    }

⌨️ 快捷键说明

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