KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > print > cDocument


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14
//
15
//All Rights Reserved.
16
package org.columba.core.print;
17
18 import java.awt.print.Book JavaDoc;
19 import java.awt.print.Paper JavaDoc;
20 import java.awt.print.PrinterException JavaDoc;
21 import java.awt.print.PrinterJob JavaDoc;
22 import java.util.Enumeration JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Vector JavaDoc;
25
26
27 public class cDocument {
28     private List JavaDoc objects;
29     private List JavaDoc pages;
30     private cPrintObject header;
31     private cPrintObject footer;
32     private String JavaDoc docName;
33     private boolean uptodate;
34     private PrinterJob JavaDoc printJob;
35
36     public cDocument() {
37         objects = new Vector JavaDoc();
38         pages = new Vector JavaDoc();
39         uptodate = false;
40
41         printJob = PrinterJob.getPrinterJob();
42     }
43
44     public int getPageCount() {
45         if (!uptodate) {
46             createPages();
47         }
48
49         return pages.size();
50     }
51
52     public void print() {
53         if (!uptodate) {
54             createPages();
55         }
56
57         print(1, getPageCount());
58     }
59
60     public void print(int startPage, int endPage) {
61         if (!uptodate) {
62             createPages();
63         }
64
65         if (docName != null) {
66             printJob.setJobName(docName);
67         }
68
69         Book JavaDoc book = new Book JavaDoc();
70
71         for (int i = 0; i < endPage; i++) {
72             book.append((cPage) pages.get(i), printJob.defaultPage());
73         }
74
75         printJob.setPageable(book);
76
77         if (printJob.printDialog()) {
78             try {
79                 printJob.print();
80             } catch (PrinterException JavaDoc e) {
81                 e.printStackTrace();
82             }
83         }
84     }
85
86     public void setHeader(cPrintObject h) {
87         header = h;
88         h.setType(cPrintObject.HEADER);
89     }
90
91     public cPrintObject getHeader() {
92         return header;
93     }
94
95     public cPrintObject getFooter() {
96         return footer;
97     }
98
99     public void setFooter(cPrintObject f) {
100         footer = f;
101         f.setType(cPrintObject.FOOTER);
102     }
103
104     public void setDocumentName(String JavaDoc n) {
105         docName = n;
106     }
107
108     public int getPageNr(cPage p) {
109         if (!uptodate) {
110             createPages();
111         }
112
113         return pages.indexOf(p) + 1;
114     }
115
116     public void appendPrintObject(cPrintObject obj) {
117         objects.add(obj);
118         uptodate = false;
119     }
120
121     private void createPages() {
122         pages.clear();
123
124         Enumeration JavaDoc objEnum = ((Vector JavaDoc) objects).elements();
125
126         Paper JavaDoc paper = printJob.defaultPage().getPaper();
127
128         cCmUnit pWidth = new cCmUnit();
129         pWidth.setPoints(paper.getImageableWidth());
130
131         cCmUnit pHeight = new cCmUnit();
132         pHeight.setPoints(paper.getImageableHeight());
133
134         if (getHeader() != null) {
135             pHeight.subI(getHeader().getSize(pWidth).getHeight());
136         }
137
138         if (getFooter() != null) {
139             pHeight.subI(getFooter().getSize(pWidth).getHeight());
140         }
141
142         cUnit remainHeight;
143         cPrintObject remainObj;
144
145         cPrintObject nextObj;
146         cUnit objHeight;
147
148         cPage nPage = new cPage(this);
149         remainHeight = new cPointUnit(pHeight.getPoints());
150
151         cUnit hLocation = new cCmUnit();
152
153         nextObj = (cPrintObject) objEnum.nextElement();
154
155         while (true) {
156             objHeight = nextObj.getSize(pWidth).getHeight();
157
158             if (objHeight.getPoints() <= remainHeight.getPoints()) {
159                 nextObj.setLocation(new cPoint(new cCmUnit(),
160                         new cCmUnit(hLocation)));
161                 remainHeight.setPoints(remainHeight.sub(objHeight).getPoints());
162                 hLocation.setPoints(hLocation.add(objHeight).getPoints());
163                 nPage.add(nextObj);
164
165                 if (objEnum.hasMoreElements()) {
166                     nextObj = (cPrintObject) objEnum.nextElement();
167                 } else {
168                     break;
169                 }
170             } else {
171                 remainObj = nextObj.breakBlock(pWidth, remainHeight);
172
173                 if (remainObj != null) {
174                     remainObj.setLocation(new cPoint(new cCmUnit(),
175                             new cCmUnit(hLocation)));
176                     nPage.add(remainObj);
177                 }
178
179                 pages.add(nPage);
180                 nPage = new cPage(this);
181                 remainHeight.setPoints(pHeight.getPoints());
182                 hLocation.setUnits(0);
183             }
184         }
185
186         if (nPage.countObjects() != 0) {
187             pages.add(nPage);
188         }
189
190         uptodate = true;
191     }
192 }
193
Popular Tags