krysalis-jplugin/src/java/org/krysalis/swingx PrintUtils.java,NONE,1.1

[email protected]
Newsgroups gmane.comp.krysalis.metamorphosis.cvs
Message-ID <[email protected]>
Update of /cvsroot/metamorphosis/krysalis-jplugin/src/java/org/krysalis/swingx
In directory sc8-pr-cvs1:/tmp/cvs-serv29474/src/java/org/krysalis/swingx

Added Files:
	PrintUtils.java 
Log Message:
Add printutils to be able to print easily a Component and 
handle the UI logic.

--- NEW FILE: PrintUtils.java ---
/*****************************************************************************
 * Copyright (C) The Krysalis project. All rights reserved.                  *
 * ------------------------------------------------------------------------- *
 * This software is published under the terms of the Krysalis Software       *
 * License version 1.1_01, a copy of which has been included  with this      *
 * distribution in the LICENSE file.                                         *
 *****************************************************************************/
package org.krysalis.swingx;

import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;

import javax.swing.RepaintManager;

/**
 * @author barozzink 2-ott-2003
 **/
public class PrintUtils {

    public static void print(Printable p) throws PrinterException {
        PrinterJob printJob = PrinterJob.getPrinterJob();
        printJob.setPrintable(p);
        if (printJob.printDialog()) {
            printJob.print();
        }
      }
      
    public static void print(Component component) throws PrinterException {
      print(new PrintUtils.PrintableComponent(component));
    }
    
    public static void disableDoubleBuffering(Component c) {
      RepaintManager.currentManager(c).setDoubleBufferingEnabled(false);
    }

    public static void enableDoubleBuffering(Component c) {
      RepaintManager.currentManager(c).setDoubleBufferingEnabled(true);
    }
      
    protected static class PrintableComponent implements Printable {

          protected Component componentToBePrinted = null;
          
          protected PrintableComponent(Component componentToBePrinted) {
              this.componentToBePrinted = componentToBePrinted;
          }
          
          public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
            if (pageIndex > 0) {
              return(NO_SUCH_PAGE);
            } else {
              Graphics2D g2 = (Graphics2D)g;
              g2.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
              disableDoubleBuffering(componentToBePrinted);
              componentToBePrinted.paint(g2);
              enableDoubleBuffering(componentToBePrinted);
              return(PAGE_EXISTS);
            }
          }
      }
    }



-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.