View Javadoc

1   /*
2    *  UnBBayes
3    *  Copyright (C) 2002, 2008 Universidade de Brasilia - http://www.unb.br
4    *
5    *  This file is part of UnBBayes.
6    *
7    *  UnBBayes is free software: you can redistribute it and/or modify
8    *  it under the terms of the GNU General Public License as published by
9    *  the Free Software Foundation, either version 3 of the License, or
10   *  (at your option) any later version.
11   *
12   *  UnBBayes is distributed in the hope that it will be useful,
13   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   *  GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License
18   *  along with UnBBayes.  If not, see <http://www.gnu.org/licenses/>.
19   *
20   */
21  package unbbayes.controller;
22  
23  import java.awt.print.PageFormat;
24  import java.awt.print.Pageable;
25  import java.awt.print.Printable;
26  import java.awt.print.PrinterException;
27  import java.awt.print.PrinterJob;
28  import java.util.ResourceBundle;
29  
30  import javax.swing.JDialog;
31  import javax.swing.JOptionPane;
32  
33  /**
34   * Creating an instance of this class and printing it allows it to
35   * add a status dialog during printing. The print requests are
36   * simply delegated to the Pageable that actually contains the
37   * data to be printed, but by intercepting those calls, we can update
38   * the page number displayed in our dialog so that it indicates
39   * which page is currently being displayed.
40   */
41  public class PrintMonitor implements Pageable {
42  
43    protected PrinterJob printerJob;
44    protected Pageable pageable;
45    protected JOptionPane optionPane;
46    protected JDialog statusDialog;
47  
48    /** Load resource file from this package */
49    private static ResourceBundle resource = unbbayes.util.ResourceController.newInstance().getBundle(
50  		  unbbayes.controller.resources.ControllerResources.class.getName());
51  
52    public PrintMonitor(Pageable p) {
53      pageable = p;
54      printerJob = PrinterJob.getPrinterJob();
55      String[] options = {resource.getString("cancelOption")};
56      optionPane = new JOptionPane("",
57          JOptionPane.INFORMATION_MESSAGE,
58          JOptionPane.CANCEL_OPTION,
59          null, options);
60      statusDialog = optionPane.createDialog(null,
61          resource.getString("printerStatus"));
62    }
63  
64    /**
65     * Create a new thread and have it call the print() method.
66     * This ensures that the AWT event thread will be able to handle
67     * the Cancel button if it is pressed, and can cancel the print job.
68     */
69    public void performPrint(boolean showDialog)
70        throws PrinterException {
71      printerJob.setPageable(this);
72      if (showDialog) {
73        boolean isOk = printerJob.printDialog();
74        if (!isOk) return;
75      }
76      optionPane.setMessage(resource.getString("initializingPrinter"));
77      Thread t = new Thread(new Runnable() {
78        public void run() {
79          statusDialog.setVisible(true);
80          if (optionPane.getValue() !=
81              JOptionPane.UNINITIALIZED_VALUE) {
82            printerJob.cancel();
83          }
84        }
85      });
86      t.start();
87      printerJob.print();
88      statusDialog.setVisible(false);
89    }
90  
91    public int getNumberOfPages() {
92      return pageable.getNumberOfPages();
93    }
94  
95    /*
96     * Update our dialog message and delegate the getPrintable() call
97     */
98    public Printable getPrintable(int index) {
99      optionPane.setMessage(resource.getString("printingPage") + (index + 1));
100     return pageable.getPrintable(index);
101   }
102 
103   public PageFormat getPageFormat(int index) {
104     return pageable.getPageFormat(index);
105   }
106 }