Package DisplayProject.printing

Source Code of DisplayProject.printing.PrintDocument

/*
Copyright (c) 2003-2008 ITerative Consulting Pty Ltd. All Rights Reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:

o Redistributions of source code must retain the above copyright notice, this list of conditions and
the following disclaimer.
 
o Redistributions in binary form must reproduce the above copyright notice, this list of conditions
and the following disclaimer in the documentation and/or other materials provided with the distribution.
   
o This jcTOOL Helper Class software, whether in binary or source form may not be used within,
or to derive, any other product without the specific prior written permission of the copyright holder

 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


*/
package DisplayProject.printing;

import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.Paper;

import javax.swing.JComponent;
import javax.swing.JPanel;

import DisplayProject.UIutils;
import DisplayProject.actions.BottomMargin;
import DisplayProject.actions.LeftMargin;
import DisplayProject.actions.RightMargin;
import DisplayProject.actions.TopMargin;
import Framework.TextData;

/**
* This class simulates the forte PrintDocument, and is based on the java.print.SimpleDoc
* @author Peter
* @author Craig Mitchell
*/
public class PrintDocument extends Book {
    private PrintOptionsDesc options = null;
    private JComponent workingPage;

    public PrintDocument(){
        super();
    }

    /**
     * Prints the current working page.
     * A component is wrapped in a BookComponentPrintable and stored
     * in the book.
     *
     * Actual printing is deferred till the document is closed
     */
    public void printWorkingPage(){
        UIutils.processGUIActions();

        // Set the working page to be the size of the paper
        PrintOptionsDesc options = getPrintOptions();
        Paper paper = options.getPaper();

        int topMargin = TopMargin.get(workingPage);
        int leftMargin = LeftMargin.get(workingPage);
        int bottomMargin = BottomMargin.get(workingPage);
        int rightMargin = RightMargin.get(workingPage);

        // CraigM:06/06/2008 - The paper.getImageable values seem to be way too big.  I'm guessing with my defaultMargin value.
//      double x = (leftMargin != 0) ? PrintUtilities.milsToPoints(leftMargin) : paper.getImageableX();
//      double y = (topMargin != 0) ? PrintUtilities.milsToPoints(topMargin) : paper.getImageableY();
//      double width = (rightMargin != 0) ? options.getWidth() - PrintUtilities.milsToPoints(rightMargin) - x : paper.getImageableWidth();
//      double height = (bottomMargin != 0) ? options.getHeight() - PrintUtilities.milsToPoints(bottomMargin) - y : paper.getImageableHeight();
        double defaultMargin = 35;
        double x = (leftMargin != 0) ? PrintUtilities.milsToPoints(leftMargin) : defaultMargin;
        double y = (topMargin != 0) ? PrintUtilities.milsToPoints(topMargin) : defaultMargin;
        double width = (rightMargin != 0) ? options.getWidth() - PrintUtilities.milsToPoints(rightMargin) - x : options.getWidth() - (defaultMargin*2);
        double height = (bottomMargin != 0) ? options.getHeight() - PrintUtilities.milsToPoints(bottomMargin) - y : options.getHeight() - (defaultMargin*2);

        // If we are in landscape mode, then we need to swap the width and height around.
        if (options.getOrientation() == PageFormat.LANDSCAPE) {
          double tmpWidth = width;
          width = height;
          height = tmpWidth;
        }

        // CraigM:30/05/2008 - The working page is now scaled in the BookComponentPrintable so we shouldn't set the size
        // workingPage.setSize((int)width, (int)height);
       
        paper.setImageableArea(x, y, width, height);
        options.setPaper(paper);

        this.append(new BookComponentPrintable(workingPage), getPrintOptions());
    }

    /**
     * Designates the working page to be printed
     * @param page
     * @param heightPolicy not implemented
     * @param widthPolicy not implemented
     * @param leftMargin in mils
     * @param rightMargin in mils
     * @param topMargin in mils
     * @param bottomMargin in mils
     */
    public void setWorkingPage(JPanel page,
            int heightPolicy, int widthPolicy,
            int leftMargin, int rightMargin,
            int topMargin, int bottomMargin) {
        workingPage = page;

        if (topMargin != 0) TopMargin.set(workingPage, topMargin);
        if (leftMargin != 0) LeftMargin.set(workingPage, leftMargin);
        if (bottomMargin != 0) BottomMargin.set(workingPage, bottomMargin);
        if (rightMargin != 0) RightMargin.set(workingPage, rightMargin);
    }

    public void setDocumentName(String name) {
        PrintUtilities.getPrinterJob().setJobName(name);
    }

    public void setDocumentName(TextData name){
        this.setDocumentName(name.toString());
    }

    public void setPrintOptions(PrintOptionsDesc options) {
      this.options = options;
    }

    public PrintOptionsDesc getPrintOptions(){
        if (this.options == null)
          this.options = PrintUtilities.getDefaultPrintOptions();
        return this.options;
    }
}
TOP

Related Classes of DisplayProject.printing.PrintDocument

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.