KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > render > print > PrintRenderer


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id: PrintRenderer.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.render.print;
21
22 import java.awt.geom.Rectangle2D JavaDoc;
23 import java.awt.print.PageFormat JavaDoc;
24 import java.awt.print.Pageable JavaDoc;
25 import java.awt.print.Paper JavaDoc;
26 import java.awt.print.Printable JavaDoc;
27 import java.awt.print.PrinterException JavaDoc;
28 import java.awt.print.PrinterJob JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.util.Vector JavaDoc;
31
32 import org.apache.fop.apps.FOPException;
33 import org.apache.fop.render.java2d.Java2DRenderer;
34
35 /**
36  * Renderer that prints through java.awt.PrintJob.
37  * The actual printing is handled by Java2DRenderer
38  * since both PrintRenderer and AWTRenderer need to
39  * support printing.
40  */

41 public class PrintRenderer extends Java2DRenderer implements Pageable JavaDoc {
42
43     private static final int EVEN_AND_ALL = 0;
44
45     private static final int EVEN = 1;
46
47     private static final int ODD = 2;
48
49     private int startNumber = 0;
50
51     private int endNumber = -1;
52
53     private int mode = EVEN_AND_ALL;
54
55     private int copies = 1;
56
57     private PrinterJob JavaDoc printerJob;
58
59     /**
60      * Creates a new PrintRenderer with the options set from system properties.
61      */

62     public PrintRenderer() {
63         initializePrinterJob();
64     }
65
66     /**
67      * Creates a new PrintRenderer and allows you to pass in a specific PrinterJob instance
68      * that this renderer should work with.
69      * @param printerJob the PrinterJob instance
70      */

71     public PrintRenderer(PrinterJob JavaDoc printerJob) {
72         this.printerJob = printerJob;
73         printerJob.setPageable(this);
74     }
75     
76     private void initializePrinterJob() throws IllegalArgumentException JavaDoc {
77         // read from command-line options
78
copies = getIntProperty("copies", 1);
79         startNumber = getIntProperty("start", 1) - 1;
80         endNumber = getIntProperty("end", -1);
81         String JavaDoc str = System.getProperty("even");
82         if (str != null) {
83             mode = Boolean.valueOf(str).booleanValue() ? EVEN : ODD;
84         }
85
86         printerJob = PrinterJob.getPrinterJob();
87         printerJob.setJobName("FOP Document");
88         printerJob.setCopies(copies);
89         if (System.getProperty("dialog") != null) {
90             if (!printerJob.printDialog()) {
91                 throw new IllegalArgumentException JavaDoc(
92                         "Printing cancelled by operator");
93             }
94         }
95         printerJob.setPageable(this);
96     }
97     
98     /** @return the PrinterJob instance that this renderer prints to */
99     public PrinterJob JavaDoc getPrinterJob() {
100         return this.printerJob;
101     }
102
103     /** @return the ending page number */
104     public int getEndNumber() {
105         return endNumber;
106     }
107     
108     /**
109      * Sets the number of the last page to be printed.
110      * @param end The ending page number
111      */

112     public void setEndPage(int end) {
113         this.endNumber = end;
114     }
115     
116     /** @return the starting page number */
117     public int getStartPage() {
118         return startNumber;
119     }
120     
121     /**
122      * Sets the number of the first page to be printed.
123      * @param start The starting page number
124      */

125     public void setStartPage(int start) {
126         this.startNumber = start;
127     }
128     
129     public void stopRenderer() throws IOException JavaDoc {
130         super.stopRenderer();
131
132         if (endNumber == -1) {
133             // was not set on command line
134
endNumber = getNumberOfPages();
135         }
136
137         Vector JavaDoc numbers = getInvalidPageNumbers();
138         for (int i = numbers.size() - 1; i > -1; i--) {
139             // removePage(Integer.parseInt((String)numbers.elementAt(i)));
140
}
141
142         try {
143             printerJob.print();
144         } catch (PrinterException JavaDoc e) {
145             log.error(e);
146             throw new IOException JavaDoc("Unable to print: " + e.getClass().getName()
147                     + ": " + e.getMessage());
148         }
149         clearViewportList();
150     }
151
152     public static int getIntProperty(String JavaDoc name, int def) {
153         String JavaDoc propValue = System.getProperty(name);
154         if (propValue != null) {
155             try {
156                 return Integer.parseInt(propValue);
157             } catch (Exception JavaDoc e) {
158                 return def;
159             }
160         } else {
161             return def;
162         }
163     }
164
165     private Vector JavaDoc getInvalidPageNumbers() {
166         Vector JavaDoc vec = new Vector JavaDoc();
167         int max = getNumberOfPages();
168         boolean isValid;
169         for (int i = 0; i < max; i++) {
170             isValid = true;
171             if (i < startNumber || i > endNumber) {
172                 isValid = false;
173             } else if (mode != EVEN_AND_ALL) {
174                 if (mode == EVEN && ((i + 1) % 2 != 0)) {
175                     isValid = false;
176                 } else if (mode == ODD && ((i + 1) % 2 != 1)) {
177                     isValid = false;
178                 }
179             }
180
181             if (!isValid) {
182                 vec.add(Integer.toString(i));
183             }
184         }
185         return vec;
186     }
187
188     /** @see java.awt.print.Pageable#getPageFormat(int) */
189     public PageFormat JavaDoc getPageFormat(int pageIndex)
190             throws IndexOutOfBoundsException JavaDoc {
191         try {
192             if (pageIndex >= getNumberOfPages()) {
193                 return null;
194             }
195     
196             PageFormat JavaDoc pageFormat = new PageFormat JavaDoc();
197     
198             Paper JavaDoc paper = new Paper JavaDoc();
199     
200             Rectangle2D JavaDoc dim = getPageViewport(pageIndex).getViewArea();
201             double width = dim.getWidth();
202             double height = dim.getHeight();
203     
204             // if the width is greater than the height assume lanscape mode
205
// and swap the width and height values in the paper format
206
if (width > height) {
207                 paper.setImageableArea(0, 0, height / 1000d, width / 1000d);
208                 paper.setSize(height / 1000d, width / 1000d);
209                 pageFormat.setOrientation(PageFormat.LANDSCAPE);
210             } else {
211                 paper.setImageableArea(0, 0, width / 1000d, height / 1000d);
212                 paper.setSize(width / 1000d, height / 1000d);
213                 pageFormat.setOrientation(PageFormat.PORTRAIT);
214             }
215             pageFormat.setPaper(paper);
216             return pageFormat;
217         } catch (FOPException fopEx) {
218             throw new IndexOutOfBoundsException JavaDoc(fopEx.getMessage());
219         }
220     }
221
222     public Printable JavaDoc getPrintable(int pageIndex)
223             throws IndexOutOfBoundsException JavaDoc {
224         return this;
225     }
226 }
227
Popular Tags