001: /*
002: * PageFormat.java
003: *
004: * Copyright (C) 2002 Peter Graves
005: * $Id: PageFormat.java,v 1.1.1.1 2002/09/24 16:08:38 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j;
023:
024: import java.awt.Color;
025: import java.awt.Font;
026: import java.awt.FontMetrics;
027: import java.awt.Graphics2D;
028: import java.awt.Graphics;
029: import java.awt.Toolkit;
030: import java.awt.print.Paper;
031: import java.util.Date;
032:
033: public final class PageFormat extends java.awt.print.PageFormat {
034: private static final int INCH = 72;
035:
036: private int linesPerPage;
037: private Font font;
038: private Font headerFont;
039: private Font footerFont;
040: private int lineHeight;
041: private String header;
042: private String date;
043: private int pageCount;
044:
045: public PageFormat(Editor editor, Region region) {
046: super ();
047: Buffer buffer = editor.getBuffer();
048: if (buffer.getFile() != null) {
049: header = buffer.getFile().netPath();
050: if (header.length() > 60)
051: header = buffer.getFile().getName();
052: if (region != null) {
053: FastStringBuffer sb = new FastStringBuffer(header);
054: sb.append(" (lines ");
055: sb.append(region.getBeginLine().lineNumber() + 1);
056: sb.append('-');
057: sb.append(region.getEndLine().lineNumber());
058: sb.append(')');
059: header = sb.toString();
060: }
061: }
062: date = new Date().toString();
063: String fontName = buffer
064: .getStringProperty(Property.PRINTER_FONT_NAME);
065: int fontSize = buffer
066: .getIntegerProperty(Property.PRINTER_FONT_SIZE);
067: if (fontSize <= 0)
068: fontSize = 10; // Default.
069: font = new Font(fontName, Font.PLAIN, fontSize);
070: headerFont = footerFont = new Font(fontName, Font.BOLD,
071: fontSize);
072: lineHeight = fontSize + 1;
073: Paper paper = getPaper();
074: paper.setImageableArea(0.5 * INCH, 0.5 * INCH, 7.5 * INCH,
075: 10 * INCH);
076: setPaper(paper);
077: int height = (int) getImageableHeight();
078: // Adjust for descenders on last line.
079: FontMetrics fm = Toolkit.getDefaultToolkit().getFontMetrics(
080: font);
081: height -= fm.getMaxDescent();
082: linesPerPage = height / lineHeight;
083: if (header != null)
084: linesPerPage -= 2;
085: linesPerPage -= 2; // Footer.
086: }
087:
088: public final Font getFont() {
089: return font;
090: }
091:
092: public final int getLinesPerPage() {
093: return linesPerPage;
094: }
095:
096: public final int getY(int i) {
097: int y = (int) getImageableY();
098: if (header != null)
099: y += lineHeight * 2;
100: y += lineHeight * (i + 1);
101: return y;
102: }
103:
104: public void printHeader(Graphics g, int pageIndex) {
105: if (header != null) {
106: Graphics2D g2d = (Graphics2D) g.create();
107: g2d.setPaint(Color.black);
108: g2d.setFont(headerFont);
109: int x = (int) getImageableX();
110: int y = (int) getImageableY() + lineHeight;
111: g2d.drawString(header, x, y);
112: }
113: }
114:
115: public void printFooter(Graphics g, int pageIndex) {
116: Graphics2D g2d = (Graphics2D) g.create();
117: g2d.setPaint(Color.black);
118: g2d.setFont(footerFont);
119: int x = (int) getImageableX();
120: int y = (int) getImageableY();
121: if (header != null)
122: y += lineHeight * 2;
123: y += lineHeight * (linesPerPage + 2);
124: g2d.drawString(date, x, y);
125: FastStringBuffer sb = new FastStringBuffer("Page ");
126: sb.append(pageIndex + 1);
127: if (pageCount != 0) {
128: sb.append(" of ");
129: sb.append(pageCount);
130: }
131: String s = sb.toString();
132: FontMetrics fm = Toolkit.getDefaultToolkit().getFontMetrics(
133: footerFont);
134: x = (int) getImageableX() + (int) getImageableWidth()
135: - fm.stringWidth(s);
136: g2d.drawString(s, x, y);
137: }
138:
139: public void setPageCount(int pageCount) {
140: this.pageCount = pageCount;
141: }
142: }
|