01: /*
02: * PrintPainter.java
03: *
04: * Copyright (C) 2002 Peter Graves
05: * $Id: PrintPainter.java,v 1.1.1.1 2002/09/24 16:09:33 piso Exp $
06: *
07: * This program is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU General Public License
09: * as published by the Free Software Foundation; either version 2
10: * of the License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public License
18: * along with this program; if not, write to the Free Software
19: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20: */
21:
22: package org.armedbear.j;
23:
24: import java.awt.Graphics;
25: import java.awt.print.Printable;
26: import java.awt.print.PrinterException;
27:
28: public final class PrintPainter implements Printable {
29: private final Editor editor;
30: private final Buffer buffer;
31: private final Region region;
32: private int currentPage = -1;
33: private Line topLine;
34: private Line line;
35: private Line endLine;
36:
37: public PrintPainter(Editor editor) {
38: this .editor = editor;
39: buffer = editor.getBuffer();
40: region = null;
41: line = buffer.getFirstLine();
42: }
43:
44: public PrintPainter(Editor editor, Region region) {
45: this .editor = editor;
46: buffer = editor.getBuffer();
47: this .region = region;
48: line = region.getBeginLine();
49: endLine = region.getEndLine();
50: }
51:
52: public int print(Graphics g, java.awt.print.PageFormat pf,
53: int pageIndex) throws PrinterException {
54: if (pageIndex != currentPage) {
55: currentPage = pageIndex;
56: topLine = line;
57: } else
58: line = topLine;
59: if (topLine == null)
60: return NO_SUCH_PAGE;
61: PageFormat pageFormat = (PageFormat) pf;
62: int fieldWidth = 5; // Safe.
63: boolean printLineNumbers = buffer
64: .getBooleanProperty(Property.SHOW_LINE_NUMBERS);
65: if (printLineNumbers) {
66: int maxLineNumber = topLine.lineNumber()
67: + pageFormat.getLinesPerPage();
68: if (endLine != null
69: && endLine.lineNumber() + 1 < maxLineNumber)
70: maxLineNumber = endLine.lineNumber() + 1;
71: fieldWidth = String.valueOf(maxLineNumber).length();
72: }
73: g.setFont(pageFormat.getFont());
74: pageFormat.printHeader(g, pageIndex);
75: for (int i = 0; i < pageFormat.getLinesPerPage(); i++) {
76: if (line == null)
77: break;
78: if (line == endLine)
79: break;
80: String s = Utilities.detab(line.getText(), buffer
81: .getTabWidth());
82: if (printLineNumbers) {
83: FastStringBuffer sb = new FastStringBuffer(
84: Utilities.rightJustify(line.lineNumber() + 1,
85: fieldWidth));
86: sb.append(' ');
87: sb.append(s);
88: s = sb.toString();
89: }
90: int y = pageFormat.getY(i);
91: g.drawString(s, (int) pageFormat.getImageableX(), y);
92: line = line.next();
93: }
94: pageFormat.printFooter(g, pageIndex);
95: return Printable.PAGE_EXISTS;
96: }
97: }
|