001: //The contents of this file are subject to the Mozilla Public License Version 1.1
002: //(the "License"); you may not use this file except in compliance with the
003: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
004: //
005: //Software distributed under the License is distributed on an "AS IS" basis,
006: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
007: //for the specific language governing rights and
008: //limitations under the License.
009: //
010: //The Original Code is "The Columba Project"
011: //
012: //The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
013: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
014: //
015: //All Rights Reserved.
016: package org.columba.core.print;
017:
018: import java.awt.print.Book;
019: import java.awt.print.Paper;
020: import java.awt.print.PrinterException;
021: import java.awt.print.PrinterJob;
022: import java.util.Enumeration;
023: import java.util.List;
024: import java.util.Vector;
025:
026: public class cDocument {
027: private List objects;
028: private List pages;
029: private cPrintObject header;
030: private cPrintObject footer;
031: private String docName;
032: private boolean uptodate;
033: private PrinterJob printJob;
034:
035: public cDocument() {
036: objects = new Vector();
037: pages = new Vector();
038: uptodate = false;
039:
040: printJob = PrinterJob.getPrinterJob();
041: }
042:
043: public int getPageCount() {
044: if (!uptodate) {
045: createPages();
046: }
047:
048: return pages.size();
049: }
050:
051: public void print() {
052: if (!uptodate) {
053: createPages();
054: }
055:
056: print(1, getPageCount());
057: }
058:
059: public void print(int startPage, int endPage) {
060: if (!uptodate) {
061: createPages();
062: }
063:
064: if (docName != null) {
065: printJob.setJobName(docName);
066: }
067:
068: Book book = new Book();
069:
070: for (int i = 0; i < endPage; i++) {
071: book.append((cPage) pages.get(i), printJob.defaultPage());
072: }
073:
074: printJob.setPageable(book);
075:
076: if (printJob.printDialog()) {
077: try {
078: printJob.print();
079: } catch (PrinterException e) {
080: e.printStackTrace();
081: }
082: }
083: }
084:
085: public void setHeader(cPrintObject h) {
086: header = h;
087: h.setType(cPrintObject.HEADER);
088: }
089:
090: public cPrintObject getHeader() {
091: return header;
092: }
093:
094: public cPrintObject getFooter() {
095: return footer;
096: }
097:
098: public void setFooter(cPrintObject f) {
099: footer = f;
100: f.setType(cPrintObject.FOOTER);
101: }
102:
103: public void setDocumentName(String n) {
104: docName = n;
105: }
106:
107: public int getPageNr(cPage p) {
108: if (!uptodate) {
109: createPages();
110: }
111:
112: return pages.indexOf(p) + 1;
113: }
114:
115: public void appendPrintObject(cPrintObject obj) {
116: objects.add(obj);
117: uptodate = false;
118: }
119:
120: private void createPages() {
121: pages.clear();
122:
123: Enumeration objEnum = ((Vector) objects).elements();
124:
125: Paper paper = printJob.defaultPage().getPaper();
126:
127: cCmUnit pWidth = new cCmUnit();
128: pWidth.setPoints(paper.getImageableWidth());
129:
130: cCmUnit pHeight = new cCmUnit();
131: pHeight.setPoints(paper.getImageableHeight());
132:
133: if (getHeader() != null) {
134: pHeight.subI(getHeader().getSize(pWidth).getHeight());
135: }
136:
137: if (getFooter() != null) {
138: pHeight.subI(getFooter().getSize(pWidth).getHeight());
139: }
140:
141: cUnit remainHeight;
142: cPrintObject remainObj;
143:
144: cPrintObject nextObj;
145: cUnit objHeight;
146:
147: cPage nPage = new cPage(this );
148: remainHeight = new cPointUnit(pHeight.getPoints());
149:
150: cUnit hLocation = new cCmUnit();
151:
152: nextObj = (cPrintObject) objEnum.nextElement();
153:
154: while (true) {
155: objHeight = nextObj.getSize(pWidth).getHeight();
156:
157: if (objHeight.getPoints() <= remainHeight.getPoints()) {
158: nextObj.setLocation(new cPoint(new cCmUnit(),
159: new cCmUnit(hLocation)));
160: remainHeight.setPoints(remainHeight.sub(objHeight)
161: .getPoints());
162: hLocation.setPoints(hLocation.add(objHeight)
163: .getPoints());
164: nPage.add(nextObj);
165:
166: if (objEnum.hasMoreElements()) {
167: nextObj = (cPrintObject) objEnum.nextElement();
168: } else {
169: break;
170: }
171: } else {
172: remainObj = nextObj.breakBlock(pWidth, remainHeight);
173:
174: if (remainObj != null) {
175: remainObj.setLocation(new cPoint(new cCmUnit(),
176: new cCmUnit(hLocation)));
177: nPage.add(remainObj);
178: }
179:
180: pages.add(nPage);
181: nPage = new cPage(this );
182: remainHeight.setPoints(pHeight.getPoints());
183: hLocation.setUnits(0);
184: }
185: }
186:
187: if (nPage.countObjects() != 0) {
188: pages.add(nPage);
189: }
190:
191: uptodate = true;
192: }
193: }
|