1|package com.infocom.print;
  2|
  3|
  4|/**
  5| * Class: PFDocument <p>
  6| *
  7| * This class is at the top of the print framework hierarchy
  8| * To use this framework you must create an instance of this
  9| * class and then add pages to it. This class is also
 10| * responsible for instantiating the print process as well
 11| * as all the export features built-in the framework.
 12| *
 13| * @author Jean-Pierre Dube <jpdube@videotron.ca>
 14| * @version 1.0
 15| * @since 1.0
 16| */
 17|
 18|import java.awt.print.PrinterJob;
 19|import java.awt.print.Book;
 20|import java.awt.print.PageFormat;
 21|import java.util.Vector;
 22|import java.util.LinkedList;
 23|
 24|
 25|public class PFDocument {
 26|
 27|   //--- Private instances declarations
 28|   //----------------------------------
 29|
 30|   //--- Page data structure
 31|   private Vector pageCollection = new Vector ();
 32|
 33|   //--- Strings
 34|   private String documentName = "";
 35|   private String exportFilename = "";
 36|
 37|   //--- Print dialogs
 38|   private boolean showPrintDialog = false;
 39|   private boolean showPageDialog = false;
 40|
 41|   //--- Headers and footers
 42|   private PFPrintObject header = null;
 43|   private PFPrintObject footer = null;
 44|   private PFPrintObject firstPageHeader = null;
 45|   private PFPrintObject firstPageFooter = null;
 46|
 47|
 48|   /**
 49|    * Constructor: PFDocument <p>
 50|    *
 51|    * Default constructor
 52|    *
 53|    */
 54|   public PFDocument () {
 55|
 56|   }
 57|
 58|
 59|   /**
 60|    * Method: addPage <p>
 61|    *
 62|    * Add a page to this document, the page must be
 63|    * an instance of the <code>PFPage</code> class.
 64|    *
 65|    * @param parPage a value of type PFPage
 66|    */
 67|   public void addPage (PFPage parPage) {
 68|
 69|      if (parPage != null) {
 70|         pageCollection.add (parPage);
 71|         parPage.setDocument (this);
 72|      }
 73|   }
 74|
 75|
 76|   /**
 77|    * Method: addPage <p>
 78|    *
 79|    * Insert a page in this document.
 80|    *
 81|    * @param parPage a value of type PFPage, the page to  insert in the document
 82|    * @param parPageNo a value of type int, insertion point in the document
 83|    */
 84|   public void addPage (PFPage parPage, int parPageNo) {
 85|
 86|      if (parPage != null) {
 87|         pageCollection.insertElementAt (parPage, parPageNo);
 88|         parPage.setDocument (this);
 89|      }
 90|   }
 91|
 92|
 93|   /**
 94|    * Method: removePage <p>
 95|    *
 96|    * Remove a page using the page number
 97|    *
 98|    * @param parPageNo a value of type int, the page number to remove
 99|    */
100|   public void removePage (int parPageNo) {
101|
102|      if (parPageNo >= 0 && parPageNo < pageCollection.size ())
103|         pageCollection.remove (parPageNo);
104|
105|   }
106|
107|
108|   /**
109|    * Method: removePage <p>
110|    *
111|    * Remove a page using an instance of the page
112|    *
113|    * @param parPage a value of type PFPage, the instance of the page to remove
114|    */
115|   public void removePage (PFPage parPage) {
116|
117|      if (parPage != null)
118|         pageCollection.remove (parPage);
119|
120|   }
121|
122|
123|   /**
124|    * Method: getPage <p>
125|    *
126|    * Get a page using the page number as the reference.
127|    *
128|    * @param parPageNo a value of type int, the page number
129|    *
130|    * @return a value of type PFPage
131|    */
132|   public PFPage getPage (int parPageNo) {
133|
134|      if (parPageNo >= 0 && parPageNo < pageCollection.size ())
135|         return ((PFPage) pageCollection.get (parPageNo));
136|      else
137|         return (null);
138|
139|   }
140|
141|
142|   /**
143|    * Method: getPageCount <p>
144|    *
145|    * Return the number of pages currently in the document.
146|    * The page count is zero based.
147|    *
148|    * @return a value of type int
149|    */
150|   public int getPageCount () {
151|
152|      return (pageCollection.size ());
153|
154|   }
155|
156|
157|   /**
158|    * Method: setDocumentName <p>
159|    *
160|    * Give a name to this document. The document name
161|    * is used by the operating system print queue to
162|    * identify this print job. The document name is also
163|    * used by the <code>PfPrintPreview</code> class
164|    * to identify the document.
165|    *
166|    * @param parName a value of type String, the name of the document
167|    */
168|   public void setDocumentName (String parName) {
169|
170|      documentName = parName;
171|   }
172|
173|
174|   /**
175|    * Method: getDocumentName <p>
176|    *
177|    * Return the name of this document.
178|    *
179|    * @return a value of type String
180|    */
181|   public String getDocumentName () {
182|
183|      return (documentName);
184|
185|   }
186|
187|
188|   /**
189|    * Method: showPrintDialog <p>
190|    *
191|    * Set the showPrintDialog flag. The print dialog
192|    * will not be shown immediatly. It will only be shown
193|    * when the print process is instantiated.
194|    *
195|    * @param parShowPrintDialog a value of type boolean
196|    *
197|    */
198|   public void showPrintDialog (boolean parShowPrintDialog) {
199|
200|      showPrintDialog = parShowPrintDialog;
201|
202|   }
203|
204|
205|   /**
206|    * Method: <p>
207|    *
208|    * Set the showPage dialog flag. The page dialog that will
209|    * be showned is the print framework dialog, not the one
210|    * provided by the Java Print API.
211|    *
212|    * @param parShowPageDialog a value of type boolean
213|    */
214|   public void showPageDialog (boolean parShowPageDialog) {
215|
216|      showPageDialog = parShowPageDialog;
217|
218|   }
219|
220|
221|   /**
222|    * Method: print <p>
223|    *
224|    * Print the whole document
225|    *
226|    */
227|   public void print () {
228|
229|      print (0, pageCollection.size ());
230|
231|   }
232|
233|
234|   /**
235|    * Method: print <p>
236|    *
237|    * Print a page range. You must supply the start
238|    * and end page to be printed. Appropriate dialogs
239|    * will be shown if necessary.
240|    *
241|    * @param parStartPage the first page to print
242|    * @param parEndPage the last page
243|    */
244|   public void print (int parStartPage, int parEndPage) {
245|
246|      PFPage page;
247|      PFPageFormat pageFormat = new PFPageFormat ();
248|      int i;
249|
250|      //--- Require
251|      if (parEndPage > pageCollection.size ())
252|         parEndPage = pageCollection.size ();
253|
254|      if (parStartPage < 0)
255|         parStartPage = 0;
256|
257|      //--- Create a new PrinterJob object
258|      PrinterJob printJob = PrinterJob.getPrinterJob ();
259|      printJob.setJobName (documentName);
260|
261|      //--- Show the page dialog
262|      if (showPageDialog) {
263|         PFPageSetupDialog pageSetupDialog = new PFPageSetupDialog (pageFormat);
264|         pageFormat = pageSetupDialog.showDialog ();
265|      }
266|
267|      //--- Create a new book to add pages to
268|      Book book = new Book ();
269|
270|      //--- Add the pages to the book
271|      for (i = parStartPage; i < parEndPage; i++) {
272|         page = (PFPage) pageCollection.get (i);
273|
274|         if (showPageDialog)
275|            book.append (page, pageFormat.getPageFormat ());
276|         else
277|            book.append (page, page.getPageFormat ().getPageFormat ());
278|      }
279|
280|      //--- Tell the printJob to use the book as the pageable object
281|      printJob.setPageable (book);
282|
283|      //--- Show the print dialog box. If the user click the
284|      //--- print button we then proceed to print else we abort.
285|      try {
286|         if (showPrintDialog) {
287|            if (printJob.printDialog()) {
288|               printJob.print();
289|            }
290|         }
291|         else
292|            printJob.print ();
293|
294|      } catch (Exception PrintException) {
295|         PrintException.printStackTrace();
296|      }
297|   }
298|
299|
300|   /**
301|    * Method: printPDF <p>
302|    *
303|    * Export the current document to the PDF
304|    * file format.
305|    *
306|    * @param parFilename a value of type String
307|    */
308|   public void printPDF (String parFilename) {
309|
310|      //--- To be implemented by you
311|
312|   }
313|
314|
315|   /**
316|    * Method: printHTML <p>
317|    *
318|    * Export the current document to the HTML
319|    * file format
320|    *
321|    * @param parFilename a value of type String
322|    */
323|   public void printHTML (String parFilename) {
324|
325|      //--- To be implemented by you
326|
327|   }
328|
329|
330|   /**
331|    * Method: printPreview <p>
332|    *
333|    * Show this document in the print preview window
334|    *
335|    */
336|   public void printPreview () {
337|
338|      PFPrintPreview printPreview = new PFPrintPreview (this);
339|
340|   }
341|
342|
343|   /**
344|    * Method: setHeader <p>
345|    *
346|    * Set the page header for this document
347|    *
348|    */
349|   public void setHeader (PFPrintObject parHeader) {
350|
351|      header = parHeader;
352|
353|   }
354|
355|
356|   /**
357|    * Method: setFooter <p>
358|    *
359|    * Set the page footer for this document
360|    *
361|    */
362|   public void setFooter (PFPrintObject parFooter) {
363|
364|      footer = parFooter;
365|
366|   }
367|
368|
369|   /**
370|    * Method: setFirstPageHeader <p>
371|    *
372|    * Set the page header for the first page only
373|    *
374|    * @param parHeader a value of type PFPrintObject
375|    */
376|   public void setFirstPageHeader (PFPrintObject parHeader) {
377|
378|      firstPageHeader = parHeader;
379|
380|   }
381|
382|
383|   /**
384|    * Method: setFirstPageFooter <p>
385|    *
386|    * Set the page footer for the first page only
387|    *
388|    * @param parFooter a value of type PFPrintObject
389|    */
390|   public void setFirstPageFooter (PFPrintObject parFooter) {
391|
392|      firstPageFooter = parFooter;
393|   }
394|
395|
396|   /**
397|    * Method: getHeader <p>
398|    *
399|    * Get the page haader
400|    *
401|    * @return a value of type PFPrintObject
402|    */
403|   public PFPrintObject getHeader () {
404|
405|      return (header);
406|   }
407|
408|
409|   /**
410|    * Method: getFooter <p>
411|    *
412|    * Get the page footer
413|    *
414|    * @return a value of type PFPrintObject
415|    */
416|   public PFPrintObject getFooter () {
417|
418|      return (footer);
419|   }
420|
421|
422|   /**
423|    * Method: getHeader <p>
424|    *
425|    * @param parPage a value of type PFPage
426|    * @return a value of type PFPrintObject
427|    */
428|   public PFPrintObject getHeader (PFPage parPage) {
429|
430|      if (getPageNo (parPage) == 0)
431|         return (firstPageHeader);
432|      else
433|         return (header);
434|
435|   }
436|
437|
438|   /**
439|    * Method: getFooter <p>
440|    *
441|    * @param parPage a value of type PFPage
442|    * @return a value of type PFPrintObject
443|    */
444|   public PFPrintObject getFooter (PFPage parPage) {
445|
446|      if (getPageNo (parPage) == 0)
447|         return (firstPageFooter);
448|      else
449|         return (footer);
450|
451|   }
452|
453|
454|   /**
455|    * Method: getPageNo <p>
456|    *
457|    * Return the page number for the page passed in
458|    * parameters.
459|    *
460|    * @param parPage a value of type PFPage
461|    * @return a value of type int
462|    */
463|   public int getPageNo (PFPage parPage) {
464|
465|      return (pageCollection.indexOf (parPage));
466|   }
467|}// PFDocument