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