1 21 22 package org.armedbear.j; 23 24 import java.awt.Color ; 25 import java.awt.Font ; 26 import java.awt.FontMetrics ; 27 import java.awt.Graphics2D ; 28 import java.awt.Graphics ; 29 import java.awt.Toolkit ; 30 import java.awt.print.Paper ; 31 import java.util.Date ; 32 33 public final class PageFormat extends java.awt.print.PageFormat 34 { 35 private static final int INCH = 72; 36 37 private int linesPerPage; 38 private Font font; 39 private Font headerFont; 40 private Font footerFont; 41 private int lineHeight; 42 private String header; 43 private String 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 ().toString(); 65 String fontName = buffer.getStringProperty(Property.PRINTER_FONT_NAME); 66 int fontSize = buffer.getIntegerProperty(Property.PRINTER_FONT_SIZE); 67 if (fontSize <= 0) 68 fontSize = 10; font = new Font (fontName, Font.PLAIN, fontSize); 70 headerFont = footerFont = new Font (fontName, Font.BOLD, fontSize); 71 lineHeight = fontSize + 1; 72 Paper 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 FontMetrics fm = Toolkit.getDefaultToolkit().getFontMetrics(font); 78 height -= fm.getMaxDescent(); 79 linesPerPage = height / lineHeight; 80 if (header != null) 81 linesPerPage -= 2; 82 linesPerPage -= 2; } 84 85 public final Font 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 g, int pageIndex) 105 { 106 if (header != null) { 107 Graphics2D g2d = (Graphics2D ) 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 g, int pageIndex) 117 { 118 Graphics2D g2d = (Graphics2D ) 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 s = sb.toString(); 134 FontMetrics 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 |