001: package com.xoetrope.print;
002:
003: import java.awt.Container;
004: import java.awt.FontMetrics;
005: import java.awt.Graphics2D;
006: import java.awt.PageAttributes;
007: import java.awt.geom.Rectangle2D;
008: import java.awt.print.Book;
009: import java.awt.print.PageFormat;
010: import java.awt.print.PrinterJob;
011:
012: import net.xoetrope.xui.XPage;
013: import java.awt.Container;
014: import java.awt.print.Paper;
015:
016: /**
017: * A print engine for printing pages
018: *
019: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
020: * the GNU Public License (GPL), please see license.txt for more details. If
021: * you make commercial use of this software you must purchase a commercial
022: * license from Xoetrope.</p>
023: * <p> $Revision: 1.3 $</p>
024: */
025: public class Printout {
026: private PrinterJob printerJob;
027: private PageFormat pageFormat;
028: private Book book;
029: private int pageNumber;
030: private String headerStyle;
031:
032: private Object headers[] = new Object[3];
033: private Object footers[] = new Object[3];
034:
035: /**
036: * Position the header or footer to the left of the page
037: */
038: public static final int LEFT = 0;
039: /**
040: * Position the header or footer to the center of the page
041: */
042: public static final int CENTER = 1;
043: /**
044: * Position the header or footer to the right of the page
045: */
046: public static final int RIGHT = 2;
047:
048: /**
049: * Create a new printout
050: */
051: public Printout() {
052: printerJob = PrinterJob.getPrinterJob();
053: pageFormat = printerJob.defaultPage();
054: book = new Book();
055: headerStyle = "base";
056:
057: pageFormat.setOrientation(pageFormat.LANDSCAPE);
058: }
059:
060: /**
061: * Create a new printout
062: * @param paper the paper specification for this printout
063: */
064: public Printout(Paper paper) {
065: this ();
066: pageFormat.setPaper(paper);
067: }
068:
069: /**
070: * Get the default page format
071: * @return the default page format
072: */
073: public static PageFormat getDefaultPage() {
074: return PrinterJob.getPrinterJob().defaultPage();
075: }
076:
077: /**
078: * Add a page or container to the book being printed
079: * @param page the page or content
080: */
081: public void addPage(Container page) {
082: book.append(new PrintablePage(page, this ), pageFormat);
083: }
084:
085: /**
086: * Add a frameset to the printout. Each frame in the frameset will be printed
087: * @param frameset the frameset
088: */
089: public void addFrame(PrintableFrameSet frameset) {
090: book.append(frameset, pageFormat);
091: }
092:
093: /**
094: * Show the page format dialog. The dialog allows the setup of the page margins
095: * @return the updated page format
096: */
097: public PageFormat showPageFormat() {
098: pageFormat = printerJob.pageDialog(pageFormat);
099: return pageFormat;
100: }
101:
102: /**
103: * Initiate the printing
104: */
105: public void print() {
106: try {
107: int numPages = book.getNumberOfPages();
108: if (numPages > 0) {
109: PageAttributes pa = new PageAttributes();
110: pa
111: .setOrientationRequested(PageAttributes.OrientationRequestedType.LANDSCAPE);
112: pa.setOrigin(PageAttributes.OriginType.PRINTABLE);
113: pa
114: .setPrintQuality(PageAttributes.PrintQualityType.HIGH);
115: pa.setColor(PageAttributes.ColorType.COLOR);
116:
117: printerJob.setPageable(book);
118:
119: boolean result = printerJob.printDialog();
120: if (result)
121: printerJob.print();
122: }
123: } catch (Exception e) {
124: e.printStackTrace();
125: }
126: }
127:
128: /**
129: * Set the name that will appear in the operating system task list
130: * @param name the job name
131: */
132: public void setJobName(String name) {
133: printerJob.setJobName(name);
134: }
135:
136: /**
137: * Set the header
138: * @param text the header text
139: * @param position the text postition
140: */
141: public void setHeader(Object text, int position) {
142: headers[position] = text;
143: }
144:
145: /**
146: * Set the footer
147: * @param text the header text
148: * @param position the text postition
149: */
150: public void setFooter(Object text, int position) {
151: footers[position] = text;
152: }
153:
154: /**
155: * Set the name that will appear in the operating system task list
156: * @param top true to add the page numbers to the header, false for the footer
157: * @param position the text postition
158: * @param format the page number format
159: */
160: public void setPageNumbers(boolean top, int position, String format) {
161: if (top)
162: setHeader(new PageNumber(format), position);
163: else
164: setFooter(new PageNumber(format), position);
165: }
166:
167: /**
168: * Print the header or footer decorations for the document
169: * @param g the graphics context
170: * @param pf the page setup
171: * @param top true for header objects
172: * @param position the poition withing the header/footer
173: * @param x the x starting position of the decoration
174: * @param y the y starting position of the decoration
175: * @param fm the font metriucs of the header/footer font
176: * @return the decoration height
177: */
178: public int printDecoration(Graphics2D g, PageFormat pf,
179: boolean top, int position, double x, double y,
180: FontMetrics fm) {
181: String text = null;
182: if (top && (headers[position] != null))
183: text = headers[position].toString();
184: else if (footers[position] != null)
185: text = footers[position].toString();
186:
187: int fontHeight = fm.getHeight();
188: int cellW = (int) (pf.getImageableWidth() / 3.0);
189:
190: if (text != null) {
191: Rectangle2D rect = fm.getStringBounds(text, g);
192: int xOffset = (int) rect.getWidth();
193: if (position == CENTER)
194: xOffset = ((cellW - xOffset) / 2);
195: else if (position == RIGHT)
196: xOffset = (cellW - xOffset - 2);
197: else
198: xOffset = 0;
199: g.drawString(text, (int) (x + xOffset), (int) y
200: + fontHeight + fontHeight / 2);
201: return 2 * fontHeight;
202: }
203:
204: return 0;
205: }
206:
207: /**
208: * Set the style used for the headers and footers
209: * @param styleName the style name
210: */
211: public void setHeaderStyle(String styleName) {
212: headerStyle = styleName;
213: }
214:
215: /**
216: * Get the style used in the header and footer
217: * @return the style name
218: */
219: public String getHeaderStyle() {
220: return headerStyle;
221: }
222:
223: class PageNumber {
224: String format;
225:
226: public PageNumber(String baseStr) {
227: format = baseStr;
228: }
229:
230: public String toString() {
231: return format + (pageNumber + 1);
232: }
233: }
234: }
|