Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

ComponentPrinter.java

Go to the documentation of this file.
00001 /*
00002  * ComponentPrinter.java
00003  *
00004  * Copyright (C) 2005 Project SQUID, http://www.cs.helsinki.fi/group/squid/
00005  *
00006  * This file is part of Ikayaki.
00007  *
00008  * Ikayaki is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * Ikayaki is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with Ikayaki; if not, write to the Free Software
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00021  */
00022 
00023 package ikayaki.util;
00024 
00025 import javax.swing.*;
00026 import java.awt.*;
00027 import java.awt.print.PageFormat;
00028 import java.awt.print.Printable;
00029 import java.awt.print.PrinterException;
00030 import java.awt.print.PrinterJob;
00031 
00037 public class ComponentPrinter implements Printable {
00038 
00042     private Component componentToBePrinted;
00043 
00047     private final int plotHeight = 200;
00048 
00049 
00055     public ComponentPrinter(Component componentToBePrinted) {
00056         this.componentToBePrinted = componentToBePrinted;
00057     }
00058 
00064     public static void printComponent(Component c) {
00065         new ComponentPrinter(c).print(null);
00066     }
00067 
00074     public static void printComponent(Component c, String jobName) {
00075         new ComponentPrinter(c).print(jobName);
00076     }
00077 
00081     public void print(String jobName) {
00082         if (jobName == null) {
00083             jobName = "Java Printing";
00084         }
00085         PrinterJob printJob = PrinterJob.getPrinterJob();
00086         printJob.setPrintable(this);
00087         System.out.println(printJob.getJobName());
00088         printJob.setJobName(jobName);
00089         if (printJob.printDialog()) {
00090             try {
00091                 printJob.print();
00092             } catch (PrinterException pe) {
00093                 System.out.println("Error printing: " + pe);
00094             }
00095         }
00096     }
00097 
00108     public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
00109         Dimension dim = componentToBePrinted.getSize();
00110         if (dim.getHeight() < (pageIndex * 695)) {
00111             return (NO_SUCH_PAGE);
00112         }
00113         Graphics2D g2d = (Graphics2D) g;
00114         if (dim.getHeight() - (695 * (pageIndex)) < plotHeight) {
00115             g2d.translate(pageFormat.getImageableX(),
00116                     pageFormat.getImageableY() - 695 * pageIndex + (plotHeight - (dim.getHeight() - (695 * (pageIndex)))));
00117         } else {
00118             g2d.translate(pageFormat.getImageableX(),
00119                     pageFormat.getImageableY() - 695 * pageIndex);
00120         }
00121         disableDoubleBuffering(componentToBePrinted);
00122         componentToBePrinted.paint(g2d);
00123         if (dim.getHeight() - (695 * (pageIndex + 1)) < plotHeight && dim.getHeight() - (695 * (pageIndex + 1)) > 0) {
00124             System.err.println("lets print page " + (pageIndex + 1));
00125             g2d.setColor(Color.white);
00126             g2d.fillRect(0, 695 * (pageIndex + 1) - (plotHeight - (int) dim.getHeight() + (695 * (pageIndex + 1))),
00127                     500, 700);
00128         }
00129         enableDoubleBuffering(componentToBePrinted);
00130         return (PAGE_EXISTS);
00131     }
00132 
00133 
00134     public static void disableDoubleBuffering(Component c) {
00135         RepaintManager currentManager = RepaintManager.currentManager(c);
00136         currentManager.setDoubleBufferingEnabled(false);
00137     }
00138 
00139     public static void enableDoubleBuffering(Component c) {
00140         RepaintManager currentManager = RepaintManager.currentManager(c);
00141         currentManager.setDoubleBufferingEnabled(true);
00142     }
00143 }

Generated on Fri May 6 16:00:33 2005 for Squid by  doxygen 1.4.1