1   package org.wcb.gui.util;
2   
3   import org.wcb.model.bd.LogbookDelegate;
4   import org.wcb.model.util.SpringUtil;
5   import org.wcb.model.service.IServicesConstants;
6   import org.wcb.model.vo.hibernate.Logbook;
7   import org.wcb.gui.table.model.PrintableLogbookModel;
8   import org.wcb.gui.table.LogbookPrintableTable;
9   
10  import java.awt.*;
11  import javax.swing.*;
12  import java.awt.print.*;
13  import java.text.MessageFormat;
14  import java.util.logging.Logger;
15  import java.util.logging.Level;
16  
17  /**
18   * <small>
19   * <p/>
20   * Copyright (c)  2006  wbogaardt.
21   * This library is free software; you can redistribute it and/or
22   * modify it under the terms of the GNU Lesser General Public
23   * License as published by the Free Software Foundation; either
24   * version 2.1 of the License, or (at your option) any later version.
25   *
26   * This library is distributed in the hope that it will be useful,
27   * but WITHOUT ANY WARRANTY; without even the implied warranty of
28   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
29   * Lesser General Public License for more details.
30   *
31   * You should have received a copy of the GNU Lesser General Public
32   * License along with this library; if not, write to the Free Software
33   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
34   * <p/>
35   * $File:  $ <br>
36   * $Change:  $ submitted by $Author: wbogaardt $ at $DateTime: Sep 14, 2006 8:02:59 AM $ <br>
37   * </small>
38   *
39   * @author wbogaardt
40   *         Utilitity to allow printing of 2d graphics in java.
41   */
42  
43  public class PrintUtilities implements Printable {
44  
45      private Logger LOG = Logger.getLogger(PrintUtilities.class.getName());
46      private Component componentToBePrinted;
47  
48      public static void printComponent(Component c) {
49          new PrintUtilities(c).print();
50      }
51  
52      public PrintUtilities(Component componentToBePrinted) {
53          this.componentToBePrinted = componentToBePrinted;
54      }
55  
56      public void print() {
57          PrinterJob printJob = PrinterJob.getPrinterJob();
58          printJob.setPrintable(this);
59          if (printJob.printDialog())
60              try {
61                  printJob.print();
62              } catch(PrinterException pe) {
63                  LOG.log(Level.WARNING, "Error printing: ",pe);
64              }
65      }
66  
67      public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
68          if (pageIndex > 0) {
69              return(NO_SUCH_PAGE);
70          } else {
71              Graphics2D g2d = (Graphics2D)g;
72              g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
73              disableDoubleBuffering(componentToBePrinted);
74              componentToBePrinted.paint(g2d);
75              enableDoubleBuffering(componentToBePrinted);
76              return(PAGE_EXISTS);
77          }
78      }
79  
80      public static void disableDoubleBuffering(Component c) {
81          RepaintManager currentManager = RepaintManager.currentManager(c);
82          currentManager.setDoubleBufferingEnabled(false);
83      }
84  
85      public static void enableDoubleBuffering(Component c) {
86          RepaintManager currentManager = RepaintManager.currentManager(c);
87          currentManager.setDoubleBufferingEnabled(true);
88      }
89  
90      public static void printTable() {
91          SwingUtilities.invokeLater(new Runnable() {
92             Logger LOG = Logger.getLogger(PrintUtilities.class.getName());
93              public void run() {
94                  LogbookDelegate del = (LogbookDelegate) SpringUtil.getApplicationContext().getBean(IServicesConstants.LOGBOOK_DELEGATE);
95                  java.util.List<Logbook> entries = del.getAllLogbookEntries();
96                  PrintableLogbookModel data = new PrintableLogbookModel(entries);
97  
98                  LogbookPrintableTable tablet = new LogbookPrintableTable(data);
99                  try {
100                     tablet.print(JTable.PrintMode.FIT_WIDTH, new MessageFormat("Pilot Logbook"), new MessageFormat("Page {0, number}"));
101                 }
102                 catch (PrinterException pe) {
103                     LOG.log(Level.WARNING, "Unable to print ",pe);
104                 }
105                 tablet.closeFrame();
106             }
107         });
108     }
109 }
110