001: package net.sourceforge.squirrel_sql.client.util;
002:
003: /*
004: * This code below originates from Rob MacGrogan's blog here:
005: *
006: * http://www.developerdotstar.com/community/node/124
007: *
008: * My changes were minimal to support logging - Rob Manning
009: * This printing method is easy and preserves syntax highlighting, but it
010: * is not able to handle pagination correctly.
011: */
012:
013: /*
014: * Copied from this tutorial:
015: *
016: * http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/Swing-Tutorial-Printing.html
017: *
018: * And also from a post on the forums at java.swing.com. My apologies that do
019: * not have a link to that post, by my hat goes off to the poster because
020: * he/she figured out the sticky problem of paging properly when printing a
021: * Swing component.
022: */
023: import java.awt.Component;
024: import java.awt.Dimension;
025: import java.awt.Graphics;
026: import java.awt.Graphics2D;
027: import java.awt.print.PageFormat;
028: import java.awt.print.Printable;
029: import java.awt.print.PrinterException;
030: import java.awt.print.PrinterJob;
031:
032: import javax.swing.RepaintManager;
033:
034: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
035: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
036:
037: public class PrintUtilities implements Printable {
038:
039: private Component componentToBePrinted;
040:
041: /** Logger for this class. */
042: private final static ILogger s_log = LoggerController
043: .createLogger(PrintUtilities.class);
044:
045: public static void printComponent(Component c) {
046: new PrintUtilities(c).print();
047: }
048:
049: public PrintUtilities(Component componentToBePrinted) {
050: this .componentToBePrinted = componentToBePrinted;
051: }
052:
053: public void print() {
054: PrinterJob printJob = PrinterJob.getPrinterJob();
055: printJob.setPrintable(this );
056: if (printJob.printDialog()) {
057: try {
058: printJob.print();
059: } catch (PrinterException pe) {
060: s_log.error("Error printing", pe);
061: }
062: }
063: }
064:
065: public int print(Graphics g, PageFormat pf, int pageIndex) {
066: int response = NO_SUCH_PAGE;
067: Graphics2D g2 = (Graphics2D) g;
068: Dimension d = componentToBePrinted.getSize(); //get size of document
069: double panelWidth = d.width; //width in pixels
070: double panelHeight = d.height; //height in pixels
071: double pageHeight = pf.getImageableHeight(); //height of printer page
072: double pageWidth = pf.getImageableWidth(); //width of printer page
073: double scale = pageWidth / panelWidth;
074: int totalNumPages = (int) Math.ceil(scale * panelHeight
075: / pageHeight);
076: // make sure not print empty pages
077: if (pageIndex >= totalNumPages) {
078: response = NO_SUCH_PAGE;
079: } else {
080: // shift Graphic to line up with beginning of print-imageable region
081: g2.translate(pf.getImageableX(), pf.getImageableY());
082: // shift Graphic to line up with beginning of next page to print
083: g2.translate(0f, -pageIndex * pageHeight);
084: // scale the page so the width fits...
085: g2.scale(scale, scale);
086: // for faster printing, turn off double buffering
087: disableDoubleBuffering(componentToBePrinted);
088: componentToBePrinted.paint(g2); //repaint the page for printing
089: enableDoubleBuffering(componentToBePrinted);
090: response = Printable.PAGE_EXISTS;
091: }
092: return response;
093: }
094:
095: public static void disableDoubleBuffering(Component c) {
096: RepaintManager currentManager = RepaintManager
097: .currentManager(c);
098: currentManager.setDoubleBufferingEnabled(false);
099: }
100:
101: public static void enableDoubleBuffering(Component c) {
102: RepaintManager currentManager = RepaintManager
103: .currentManager(c);
104: currentManager.setDoubleBufferingEnabled(true);
105: }
106: }
|