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.Graphics;
019: import java.awt.Graphics2D;
020: import java.awt.print.PageFormat;
021: import java.awt.print.Paper;
022: import java.awt.print.Printable;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.Vector;
026:
027: public class cPage implements Printable {
028: public static final int PORTRAIT = 1;
029:
030: public static final int LANDSCAPE = 2;
031:
032: private cUnit leftMargin;
033:
034: private cUnit rightMargin;
035:
036: private cUnit topMargin;
037:
038: private cUnit bottomMargin;
039:
040: private cUnit gutter;
041:
042: private List pageObjects;
043:
044: private cSize pageSize;
045:
046: private cDocument document;
047:
048: public cPage(cDocument d) {
049: pageObjects = new Vector();
050:
051: leftMargin = new cCmUnit();
052: rightMargin = new cCmUnit();
053: topMargin = new cCmUnit();
054: bottomMargin = new cCmUnit();
055: gutter = new cCmUnit();
056:
057: document = d;
058: }
059:
060: public int countObjects() {
061: return pageObjects.size();
062: }
063:
064: public int print(Graphics g, PageFormat pf, int pi) {
065: Graphics2D g2d = (Graphics2D) g;
066: Paper paper = pf.getPaper();
067:
068: leftMargin.setPoints(paper.getImageableX());
069: rightMargin.setPoints(paper.getWidth()
070: - (paper.getImageableX() + paper.getImageableWidth()));
071: bottomMargin.setPoints(paper.getHeight()
072: - (paper.getImageableY() + paper.getImageableHeight()));
073: topMargin.setPoints(paper.getImageableY());
074:
075: cPointUnit width = new cPointUnit(paper.getImageableWidth());
076: cPointUnit height = new cPointUnit(paper.getImageableHeight());
077:
078: pageSize = new cSize(width, height);
079:
080: cPrintObject header = document.getHeader();
081:
082: if (header != null) {
083: header.setPage(this );
084: header.print(g2d);
085: }
086:
087: for (Iterator it = pageObjects.iterator(); it.hasNext();) {
088: ((cPrintObject) it.next()).print(g2d);
089:
090: // for (int i = 0; i < pageObjects.size(); i++) {
091: // ((cPrintObject) pageObjects.get(i)).print(g2d);
092: }
093:
094: cPrintObject footer = document.getFooter();
095:
096: if (footer != null) {
097: footer.setPage(this );
098: footer.print(g2d);
099: }
100:
101: return PAGE_EXISTS;
102: }
103:
104: public cDocument getDocument() {
105: return document;
106: }
107:
108: public void setDocument(cDocument d) {
109: document = d;
110: }
111:
112: public void setLeftMargin(cUnit m) {
113: leftMargin = m;
114: }
115:
116: public void setRightMargin(cUnit m) {
117: rightMargin = m;
118: }
119:
120: public void setTopMargin(cUnit m) {
121: topMargin = m;
122: }
123:
124: public void setBottomMargin(cUnit m) {
125: bottomMargin = m;
126: }
127:
128: public void setGutter(cUnit m) {
129: gutter = m;
130: }
131:
132: public void add(cPrintObject po) {
133: po.setPage(this );
134: pageObjects.add(po);
135: }
136:
137: public cPoint getPrintableAreaOrigin() {
138: cPoint origin;
139: cUnit headerMargin = new cCmUnit();
140:
141: cPrintObject header = document.getHeader();
142:
143: if (header != null) {
144: headerMargin = header.getSize(pageSize.getWidth())
145: .getHeight();
146: }
147:
148: origin = new cPoint(leftMargin.add(gutter), topMargin
149: .add(headerMargin));
150:
151: return origin;
152: }
153:
154: public cSize getPrintableAreaSize() {
155: cUnit headerMargin = new cCmUnit();
156:
157: cPrintObject header = document.getHeader();
158:
159: if (header != null) {
160: headerMargin.addI(header.getSize(pageSize.getWidth())
161: .getHeight());
162: }
163:
164: cPrintObject footer = document.getFooter();
165:
166: if (footer != null) {
167: headerMargin.addI(footer.getSize(pageSize.getWidth())
168: .getHeight());
169: }
170:
171: return pageSize.subHeight(headerMargin);
172: }
173: }
|