01: /*
02: * (C) Copyright IBM Corp. 1998-2004. All Rights Reserved.
03: *
04: * The program is provided "as is" without any warranty express or
05: * implied, including the warranty of non-infringement and the implied
06: * warranties of merchantibility and fitness for a particular purpose.
07: * IBM will not be liable for any damages suffered by you as a result
08: * of using the Program. In no event will IBM be liable for any
09: * special, indirect or consequential damages or lost profits even if
10: * IBM has been advised of the possibility of their occurrence. IBM
11: * will not be liable for any third party claims against you.
12: */
13: // Requires Java2
14: package com.ibm.richtext.print;
15:
16: import com.ibm.richtext.styledtext.MConstText;
17: import com.ibm.richtext.textlayout.attributes.AttributeMap;
18:
19: import java.awt.Frame;
20: import java.awt.Graphics;
21: import java.awt.Rectangle;
22:
23: import java.awt.print.PageFormat;
24: import java.awt.print.Printable;
25: import java.awt.print.PrinterJob;
26: import java.awt.print.PrinterException;
27:
28: final class PrintContext implements Printable {
29:
30: static final String COPYRIGHT = "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";
31:
32: private MConstTextPrintable fPrintable;
33:
34: PrintContext(MConstText text, AttributeMap defaultStyles,
35: PageFormat pf) {
36:
37: int width = (int) Math.round(pf.getImageableWidth());
38: int height = (int) Math.round(pf.getImageableHeight());
39: int left = (((int) Math.round(pf.getWidth())) - width) / 2;
40: int top = (((int) Math.round(pf.getHeight())) - height) / 2;
41:
42: Rectangle pageRect = new Rectangle(left, top, width, height);
43: fPrintable = new MConstTextPrintable(text, defaultStyles,
44: pageRect);
45: }
46:
47: public int print(Graphics graphics, PageFormat format, int pageIndex)
48: throws PrinterException {
49:
50: if (false)
51: throw new PrinterException("save trees");
52:
53: if (fPrintable.print(graphics, pageIndex) == MConstTextPrintable.PAGE_EXISTS) {
54: return PAGE_EXISTS;
55: } else {
56: return NO_SUCH_PAGE;
57: }
58: }
59:
60: static void userPrintText(MConstText text,
61: AttributeMap defaultStyles, Frame frame, String jobTitle) {
62:
63: PrinterJob job = PrinterJob.getPrinterJob();
64: job.setJobName(jobTitle);
65: if (job.printDialog()) {
66: job.setPrintable(new PrintContext(text, defaultStyles, job
67: .defaultPage()));
68: try {
69: job.print();
70: } catch (PrinterException e) {
71: System.out.println("Printer exception: " + e);
72: }
73: }
74: }
75: }
|