KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > PrintPainter


1 /*
2  * PrintPainter.java
3  *
4  * Copyright (C) 2002 Peter Graves
5  * $Id: PrintPainter.java,v 1.1.1.1 2002/09/24 16:09:33 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * 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 JavaDoc;
25 import java.awt.print.Printable JavaDoc;
26 import java.awt.print.PrinterException JavaDoc;
27
28 public final class PrintPainter implements Printable JavaDoc
29 {
30     private final Editor editor;
31     private final Buffer buffer;
32     private final Region region;
33     private int currentPage = -1;
34     private Line topLine;
35     private Line line;
36     private Line endLine;
37
38     public PrintPainter(Editor editor)
39     {
40         this.editor = editor;
41         buffer = editor.getBuffer();
42         region = null;
43         line = buffer.getFirstLine();
44     }
45
46     public PrintPainter(Editor editor, Region region)
47     {
48         this.editor = editor;
49         buffer = editor.getBuffer();
50         this.region = region;
51         line = region.getBeginLine();
52         endLine = region.getEndLine();
53     }
54
55     public int print(Graphics JavaDoc g, java.awt.print.PageFormat JavaDoc pf, int pageIndex) throws PrinterException JavaDoc
56     {
57         if (pageIndex != currentPage) {
58             currentPage = pageIndex;
59             topLine = line;
60         } else
61             line = topLine;
62         if (topLine == null)
63             return NO_SUCH_PAGE;
64         PageFormat pageFormat = (PageFormat) pf;
65         int fieldWidth = 5; // Safe.
66
boolean printLineNumbers = buffer.getBooleanProperty(Property.SHOW_LINE_NUMBERS);
67         if (printLineNumbers) {
68             int maxLineNumber = topLine.lineNumber() + pageFormat.getLinesPerPage();
69             if (endLine != null && 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 JavaDoc s = Utilities.detab(line.getText(), buffer.getTabWidth());
81             if (printLineNumbers) {
82                 FastStringBuffer sb = new FastStringBuffer(Utilities.rightJustify(line.lineNumber() + 1, fieldWidth));
83                 sb.append(' ');
84                 sb.append(s);
85                 s = sb.toString();
86             }
87             int y = pageFormat.getY(i);
88             g.drawString(s, (int) pageFormat.getImageableX(), y);
89             line = line.next();
90         }
91         pageFormat.printFooter(g, pageIndex);
92         return Printable.PAGE_EXISTS;
93     }
94 }
95
Popular Tags