1|package com.infocom.print;
  2|
  3|
  4|/**
  5| * Class: PFPrintPreview <p>
  6| *
  7| * Provide a print preview window for the print framework.
  8| * This class will take a <code>PFDocument</code> object
  9| * and display all the pages in the document on screen.<p>
 10| *
 11| * Here is a list of the functionnality offer to the user:
 12| *
 13| * <ul>
 14| * <li>Navigation between pages, first, previous, next and last page</li>
 15| * <li>Zoom-in/out, zoom the page in and out (NOT IMPLEMENTED YET)</li>
 16| * <li>Print the document directly with the print button</li>
 17| * </ul>
 18| *
 19| * @author Jean-Pierre Dube <jpdube@videotron.ca>
 20| * @version 1.0
 21| * @since 1.0
 22| * @see JFrame
 23| */
 24|
 25|
 26|import javax.swing.JFrame;
 27|import javax.swing.JPanel;
 28|import javax.swing.JButton;
 29|import javax.swing.JToolBar;
 30|import java.awt.BorderLayout;
 31|import java.awt.Color;
 32|import javax.swing.ImageIcon;
 33|import java.awt.Dimension;
 34|import javax.swing.border.EmptyBorder;
 35|import javax.swing.BorderFactory;
 36|import java.awt.Graphics;
 37|import java.awt.image.BufferedImage;
 38|import java.awt.Graphics2D;
 39|import java.awt.print.PageFormat;
 40|import java.awt.event.ActionListener;
 41|import java.awt.event.ActionEvent;
 42|import javax.swing.JScrollPane;
 43|import javax.swing.JScrollBar;
 44|
 45|
 46|public class PFPrintPreview extends JFrame {
 47|
 48|   //--- Private instances declarations
 49|   //----------------------------------
 50|
 51|   private JPanel viewPanel = new JPanel ();
 52|   private PagePanel pagePanel = new PagePanel ();
 53|
 54|   private BorderLayout mainLayout = new BorderLayout ();
 55|   private BorderLayout pageLayout = new BorderLayout ();
 56|
 57|   private JScrollBar verticalScrollBar = new JScrollBar (JScrollBar.VERTICAL);
 58|   private JScrollBar horizontalScrollBar = new JScrollBar (JScrollBar.HORIZONTAL);
 59|
 60|   private PFPrintPreviewToolBar toolbar = new PFPrintPreviewToolBar (this);
 61|
 62|   private Dimension preferredSize = new Dimension (500, 700);
 63|
 64|   private PFPage currentPage = null;
 65|   private PFDocument document = null;
 66|   private int pageIndex = 0;
 67|   private int zoom = 1;
 68|
 69|
 70|   /**
 71|    * Constructor: PFPrintPreview <p>
 72|    *
 73|    */
 74|   public PFPrintPreview (PFDocument parDocument) {
 75|
 76|      super ();
 77|
 78|      document = parDocument;
 79|      init ();
 80|
 81|   }
 82|
 83|
 84|   /**
 85|    * Method: setDocument <p>
 86|    *
 87|    * Set the document to preview
 88|    *
 89|    * @param parDocument a value of type PFDocument
 90|    */
 91|   public void setDocument () {
 92|
 93|
 94|
 95|   }
 96|
 97|
 98|   /**
 99|    * Method: zoom <p>
100|    *
101|    * Not implemented yet
102|    *
103|    * @param parZoomFactor a value of type int
104|    */
105|   public void setZoomIn (int parZoom) {
106|
107|      zoom = parZoom;
108|
109|   }
110|
111|
112|   /**
113|    * Method: setZoomOut <p>
114|    *
115|    * Not implemented yet
116|    *
117|    * @param parZoom a value of type int
118|    */
119|      public void setZoomOut (int parZoom) {
120|
121|      zoom = parZoom;
122|
123|   }
124|
125|
126|   /**
127|    * Method: getZoom <p>
128|    *
129|    * @return a value of type int
130|    */
131|      public int getZoom () {
132|
133|      return (zoom);
134|
135|   }
136|
137|
138|   /**
139|    * Method: nextPage <p>
140|    *
141|    * Preview the next page in the document
142|    *
143|    */
144|   public void nextPage () {
145|
146|      pageIndex++;
147|
148|      if (pageIndex > document.getPageCount () - 1)
149|         pageIndex--;
150|
151|      renderPage ();
152|
153|   }
154|
155|
156|   /**
157|    * Method: previousPage <p>
158|    *
159|    * Preview the previous page in the document
160|    *
161|    */
162|   public void previousPage () {
163|
164|      pageIndex--;
165|
166|      if (pageIndex < 0)
167|         pageIndex = 0;
168|
169|      renderPage ();
170|
171|   }
172|
173|
174|   /**
175|    * Method: firstPage <p>
176|    *
177|    * Preview the first page in the document
178|    *
179|    */
180|   public void firstPage () {
181|
182|      pageIndex = 0;
183|
184|      renderPage ();
185|   }
186|
187|
188|   /**
189|    * Method: lastPage <p>
190|    *
191|    * Preview the last page in the document
192|    *
193|    */
194|   public void lastPage () {
195|
196|      pageIndex = document.getPageCount () - 1;
197|
198|      renderPage ();
199|   }
200|
201|
202|   /**
203|    * Method: print <p>
204|    *
205|    * Print the document to the printer
206|    *
207|    */
208|   public void print () {
209|
210|      document.print ();
211|
212|   }
213|
214|
215|   /**
216|    * Method: renderPage <p>
217|    *
218|    * Ask the <code>pagePanel</code> to
219|    * render the current page
220|    *
221|    */
222|   private void renderPage () {
223|
224|      pagePanel.renderPage (document.getPage (pageIndex));
225|
226|   }
227|
228|
229|   /**
230|    * Method: getPreferredSize <p>
231|    *
232|    * @return a value of type Dimension
233|    */
234|   public Dimension getPreferredSize () {
235|
236|      return (preferredSize);
237|   }
238|
239|
240|   /**
241|    * Method: init <p>
242|    *
243|    * Init this class and all the Swing controls
244|    *
245|    */
246|   private void init () {
247|
248|
249|      //--- Init this frame
250|      this.getContentPane ().setLayout (mainLayout);
251|      this.getContentPane ().add (toolbar, BorderLayout.NORTH);
252|      this.getContentPane ().add (pagePanel, BorderLayout.CENTER);
253|      this.getContentPane ().add (verticalScrollBar, BorderLayout.EAST);
254|      this.getContentPane ().add (horizontalScrollBar, BorderLayout.SOUTH);
255|
256|      this.setTitle ("Print preview:" + document.getDocumentName ());
257|      this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
258|      this.pack ();
259|      this.show ();
260|
261|      //--- Init the pagePanel
262|      pagePanel.setBackground (Color.white);
263|      pagePanel.setBorder (BorderFactory.createLineBorder (Color.lightGray, 10));
264|      pagePanel.setPreferredSize (new Dimension (500, 900));
265|
266|      renderPage ();
267|   }
268|
269|
270|   /**
271|    * Class: PagePanel <p>
272|    *
273|    * Represent a page in the preview window
274|    *
275|    * @author Jean-Pierre Dube <jpdube@videotron.ca>
276|    * @version 1.0
277|    * @since 1.0
278|    * @see JPanel
279|    */
280|   private class PagePanel extends JPanel {
281|
282|      //--- Private instances declarations
283|      //----------------------------------
284|      private PFPage page;
285|      private int zoom = 1;
286|
287|
288|      /**
289|       * Constructor: PagePanel <p>
290|       *
291|       */
292|      public PagePanel () {
293|
294|         super ();
295|	
296|      }
297|
298|
299|      /**
300|       * Method: setZoomIn <p>
301|       *
302|       * @param parZoom a value of type int
303|       */
304|      public void setZoomIn (int parZoom) {
305|
306|         zoom = parZoom;
307|         this.setSize (this.getSize ().width * zoom, this.getSize ().height * zoom);
308|         repaint ();
309|
310|      }
311|
312|
313|      /**
314|       * Method: setZoomOut <p>
315|       *
316|       * @param parZoom a value of type int
317|       */
318|      public void setZoomOut (int parZoom) {
319|
320|         zoom = parZoom;
321|         this.setSize (this.getSize ().width / zoom, this.getSize ().height / zoom);
322|         repaint ();
323|      }
324|
325|
326|      /**
327|       * Method: renderPage <p>
328|       *
329|       * @param parPage a value of type PFPage
330|       */
331|      public void renderPage (PFPage parPage) {
332|
333|         page = parPage;
334|         repaint ();
335|      }
336|
337|
338|      /**
339|       * Method: paint <p>
340|       *
341|       * @param parG a value of type Graphics
342|       */
343|      public void paint (Graphics parG) {
344|
345|         super.paint (parG);
346|
347|         Dimension size = this.getSize ();
348|         double scaleFactor = 0.77;
349|
350|         BufferedImage doubleBuffer = new BufferedImage (size.width, size.height, BufferedImage.TYPE_INT_RGB);
351|         Graphics2D g2d = (Graphics2D) doubleBuffer.getGraphics ();
352|         g2d.setColor (Color.white);
353|         g2d.fillRect (0, 0, size.width, size.height);
354|         g2d.scale (scaleFactor, scaleFactor);
355|         g2d.setColor (Color.red);
356|         g2d.drawRect (0, 0, size.width, size.height);
357|
358|         if (page != null)
359|            page.print (g2d, new PageFormat (), 0);
360|
361|         if (doubleBuffer != null)
362|            parG.drawImage (doubleBuffer, 0, 0, this);
363|
364|      }
365|
366|   }
367|
368|
369|   /**
370|    * Class: PFPrintPreviewToolBar <p>
371|    *
372|    * This class responsability is to display the
373|    * toolbar and handle the user requests
374|    *
375|    * @author Jean-Pierre Dube <jpdube@videotron.ca>
376|    * @version 1.0
377|    * @since 1.0
378|    * @see JToolBar
379|    * @see ActionListener
380|    */
381|   public class PFPrintPreviewToolBar extends JToolBar implements ActionListener {
382|
383|      //--- Private instances declarations
384|      //----------------------------------
385|
386|      //--- Buttons
387|      private JButton firstPage = new JButton ();
388|      private JButton lastPage = new JButton ();
389|      private JButton nextPage = new JButton ();
390|      private JButton previousPage = new JButton ();
391|      private JButton zoomIn = new JButton ();
392|      private JButton zoomOut = new JButton ();
393|      private JButton print = new JButton ();
394|
395|      PFPrintPreview preview;
396|
397|
398|      /**
399|       * Constructor: PFPrintPreviewToolBar <p>
400|       *
401|       * @param parPreview a value of type PFPrintPreview
402|       */
403|      public PFPrintPreviewToolBar (PFPrintPreview parPreview) {
404|
405|         super ();
406|         preview = parPreview;
407|         init ();
408|      }
409|
410|
411|      /**
412|       * Method: init <p>
413|       *
414|       * Init this class and all the Swing controls
415|       *
416|       */
417|      private void init () {
418|
419|         //--- Init the buttons
420|         firstPage.setIcon (new ImageIcon (getClass ().getResource ("images/FirstPage.gif")));
421|         firstPage.setActionCommand ("firstPage");
422|         firstPage.addActionListener (this);
423|
424|         previousPage.setIcon (new ImageIcon (getClass ().getResource ("images/PreviousPage.gif")));
425|         previousPage.setActionCommand ("previousPage");
426|         previousPage.addActionListener (this);
427|
428|         nextPage.setIcon (new ImageIcon (getClass ().getResource ("images/NextPage.gif")));
429|         nextPage.setActionCommand ("nextPage");
430|         nextPage.addActionListener (this);
431|
432|         lastPage.setIcon (new ImageIcon (getClass ().getResource ("images/LastPage.gif")));
433|         lastPage.setActionCommand ("lastPage");
434|         lastPage.addActionListener (this);
435|
436|         zoomIn.setIcon (new ImageIcon (getClass ().getResource ("images/ZoomIn.gif")));
437|         zoomIn.setActionCommand ("zoomIn");
438|         zoomIn.addActionListener (this);
439|         zoomIn.setEnabled (false);
440|
441|         zoomOut.setIcon (new ImageIcon (getClass ().getResource ("images/ZoomOut.gif")));
442|         zoomOut.setActionCommand ("zoomOut");
443|         zoomOut.addActionListener (this);
444|         zoomOut.setEnabled (false);
445|
446|         print.setIcon (new ImageIcon (getClass ().getResource ("images/Print.gif")));
447|         print.setActionCommand ("print");
448|         print.addActionListener (this);
449|
450|
451|         //--- Init the toolbar
452|         this.add (firstPage);
453|         this.add (previousPage);
454|         this.add (nextPage);
455|         this.add (lastPage);
456|         this.add (zoomIn);
457|         this.add (zoomOut);
458|         this.add (print);
459|      }
460|
461|
462|      /**
463|       * Method: actionPerformed <p>
464|       *
465|       * @param parEvent a value of type ActionEvent
466|       */
467|      public void actionPerformed (ActionEvent parEvent) {
468|
469|         String command = parEvent.getActionCommand ();
470|
471|         if (command.equals ("nextPage"))
472|            preview.nextPage ();
473|
474|         else if (command.equals ("previousPage"))
475|            preview.previousPage ();
476|
477|         else if (command.equals ("firstPage"))
478|            preview.firstPage ();
479|
480|         else if (command.equals ("lastPage"))
481|            preview.lastPage ();
482|
483|         else if (command.equals ("print"))
484|            preview.print ();
485|
486|      }
487|
488|   }
489|}// PFPrintPreview