KickJava   Java API By Example, From Geeks To Geeks.

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


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.Graphics JavaDoc;
19 import java.awt.Graphics2D JavaDoc;
20 import java.awt.print.PageFormat JavaDoc;
21 import java.awt.print.Paper JavaDoc;
22 import java.awt.print.Printable JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Vector JavaDoc;
26
27 public class cPage implements Printable JavaDoc {
28     public static final int PORTRAIT = 1;
29
30     public static final int LANDSCAPE = 2;
31
32     private cUnit leftMargin;
33
34     private cUnit rightMargin;
35
36     private cUnit topMargin;
37
38     private cUnit bottomMargin;
39
40     private cUnit gutter;
41
42     private List JavaDoc pageObjects;
43
44     private cSize pageSize;
45
46     private cDocument document;
47
48     public cPage(cDocument d) {
49         pageObjects = new Vector JavaDoc();
50
51         leftMargin = new cCmUnit();
52         rightMargin = new cCmUnit();
53         topMargin = new cCmUnit();
54         bottomMargin = new cCmUnit();
55         gutter = new cCmUnit();
56
57         document = d;
58     }
59
60     public int countObjects() {
61         return pageObjects.size();
62     }
63
64     public int print(Graphics JavaDoc g, PageFormat JavaDoc pf, int pi) {
65         Graphics2D JavaDoc g2d = (Graphics2D JavaDoc) g;
66         Paper JavaDoc paper = pf.getPaper();
67
68         leftMargin.setPoints(paper.getImageableX());
69         rightMargin.setPoints(paper.getWidth()
70                 - (paper.getImageableX() + paper.getImageableWidth()));
71         bottomMargin.setPoints(paper.getHeight()
72                 - (paper.getImageableY() + paper.getImageableHeight()));
73         topMargin.setPoints(paper.getImageableY());
74
75         cPointUnit width = new cPointUnit(paper.getImageableWidth());
76         cPointUnit height = new cPointUnit(paper.getImageableHeight());
77
78         pageSize = new cSize(width, height);
79
80         cPrintObject header = document.getHeader();
81
82         if (header != null) {
83             header.setPage(this);
84             header.print(g2d);
85         }
86
87         for (Iterator JavaDoc it = pageObjects.iterator(); it.hasNext();) {
88             ((cPrintObject) it.next()).print(g2d);
89
90             // for (int i = 0; i < pageObjects.size(); i++) {
91
// ((cPrintObject) pageObjects.get(i)).print(g2d);
92
}
93
94         cPrintObject footer = document.getFooter();
95
96         if (footer != null) {
97             footer.setPage(this);
98             footer.print(g2d);
99         }
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()).getHeight();
145         }
146
147         origin = new cPoint(leftMargin.add(gutter), topMargin.add(headerMargin));
148
149         return origin;
150     }
151
152     public cSize getPrintableAreaSize() {
153         cUnit headerMargin = new cCmUnit();
154
155         cPrintObject header = document.getHeader();
156
157         if (header != null) {
158             headerMargin.addI(header.getSize(pageSize.getWidth()).getHeight());
159         }
160
161         cPrintObject footer = document.getFooter();
162
163         if (footer != null) {
164             headerMargin.addI(footer.getSize(pageSize.getWidth()).getHeight());
165         }
166
167         return pageSize.subHeight(headerMargin);
168     }
169 }
170
Popular Tags