KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * PageFormat.java
3  *
4  * Copyright (C) 2002 Peter Graves
5  * $Id: PageFormat.java,v 1.1.1.1 2002/09/24 16:08:38 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.Color JavaDoc;
25 import java.awt.Font JavaDoc;
26 import java.awt.FontMetrics JavaDoc;
27 import java.awt.Graphics2D JavaDoc;
28 import java.awt.Graphics JavaDoc;
29 import java.awt.Toolkit JavaDoc;
30 import java.awt.print.Paper JavaDoc;
31 import java.util.Date JavaDoc;
32
33 public final class PageFormat extends java.awt.print.PageFormat JavaDoc
34 {
35     private static final int INCH = 72;
36
37     private int linesPerPage;
38     private Font JavaDoc font;
39     private Font JavaDoc headerFont;
40     private Font JavaDoc footerFont;
41     private int lineHeight;
42     private String JavaDoc header;
43     private String JavaDoc date;
44     private int pageCount;
45
46     public PageFormat(Editor editor, Region region)
47     {
48         super();
49         Buffer buffer = editor.getBuffer();
50         if (buffer.getFile() != null) {
51             header = buffer.getFile().netPath();
52             if (header.length() > 60)
53                 header = buffer.getFile().getName();
54             if (region != null) {
55                 FastStringBuffer sb = new FastStringBuffer(header);
56                 sb.append(" (lines ");
57                 sb.append(region.getBeginLine().lineNumber() + 1);
58                 sb.append('-');
59                 sb.append(region.getEndLine().lineNumber());
60                 sb.append(')');
61                 header = sb.toString();
62             }
63         }
64         date = new Date JavaDoc().toString();
65         String JavaDoc fontName = buffer.getStringProperty(Property.PRINTER_FONT_NAME);
66         int fontSize = buffer.getIntegerProperty(Property.PRINTER_FONT_SIZE);
67         if (fontSize <= 0)
68             fontSize = 10; // Default.
69
font = new Font JavaDoc(fontName, Font.PLAIN, fontSize);
70         headerFont = footerFont = new Font JavaDoc(fontName, Font.BOLD, fontSize);
71         lineHeight = fontSize + 1;
72         Paper JavaDoc paper = getPaper();
73         paper.setImageableArea(0.5 * INCH, 0.5 * INCH, 7.5 * INCH, 10 * INCH);
74         setPaper(paper);
75         int height = (int) getImageableHeight();
76         // Adjust for descenders on last line.
77
FontMetrics JavaDoc fm = Toolkit.getDefaultToolkit().getFontMetrics(font);
78         height -= fm.getMaxDescent();
79         linesPerPage = height / lineHeight;
80         if (header != null)
81             linesPerPage -= 2;
82         linesPerPage -= 2; // Footer.
83
}
84
85     public final Font JavaDoc getFont()
86     {
87         return font;
88     }
89
90     public final int getLinesPerPage()
91     {
92         return linesPerPage;
93     }
94
95     public final int getY(int i)
96     {
97         int y = (int) getImageableY();
98         if (header != null)
99             y += lineHeight * 2;
100         y += lineHeight * (i+1);
101         return y;
102     }
103
104     public void printHeader(Graphics JavaDoc g, int pageIndex)
105     {
106         if (header != null) {
107             Graphics2D JavaDoc g2d = (Graphics2D JavaDoc) g.create();
108             g2d.setPaint(Color.black);
109             g2d.setFont(headerFont);
110             int x = (int) getImageableX();
111             int y = (int) getImageableY() + lineHeight;
112             g2d.drawString(header, x, y);
113         }
114     }
115
116     public void printFooter(Graphics JavaDoc g, int pageIndex)
117     {
118         Graphics2D JavaDoc g2d = (Graphics2D JavaDoc) g.create();
119         g2d.setPaint(Color.black);
120         g2d.setFont(footerFont);
121         int x = (int) getImageableX();
122         int y = (int) getImageableY();
123         if (header != null)
124             y += lineHeight * 2;
125         y += lineHeight * (linesPerPage + 2);
126         g2d.drawString(date, x, y);
127         FastStringBuffer sb = new FastStringBuffer("Page ");
128         sb.append(pageIndex + 1);
129         if (pageCount != 0) {
130             sb.append(" of ");
131             sb.append(pageCount);
132         }
133         String JavaDoc s = sb.toString();
134         FontMetrics JavaDoc fm = Toolkit.getDefaultToolkit().getFontMetrics(footerFont);
135         x = (int) getImageableX() + (int) getImageableWidth() - fm.stringWidth(s);
136         g2d.drawString(s, x, y);
137     }
138
139     public void setPageCount(int pageCount)
140     {
141         this.pageCount = pageCount;
142     }
143 }
144
Popular Tags