001: package com.xoetrope.print;
002:
003: import java.awt.Container;
004: import java.awt.Font;
005: import java.awt.FontMetrics;
006: import java.awt.Graphics;
007: import java.awt.Graphics2D;
008: import java.awt.print.PageFormat;
009: import java.awt.print.Printable;
010: import java.awt.print.PrinterException;
011: import net.xoetrope.xui.XProject;
012:
013: import net.xoetrope.xui.XProjectManager;
014: import net.xoetrope.xui.style.XStyle;
015:
016: /**
017: * A decoration of the XPage to allow printing. The class can also support printing in panels or other containers.
018: *
019: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
020: * the GNU Public License (GPL), please see license.txt for more details. If
021: * you make commercial use of this software you must purchase a commercial
022: * license from Xoetrope.</p>
023: * <p> $Revision: 1.4 $</p>
024: */
025: public class PrintablePage implements Printable {
026: /**
027: * The current page or container being printed
028: */
029: protected Container currentPage;
030: /**
031: * The PrintOut class to which this printable page belongs
032: */
033: protected Printout printout;
034:
035: /**
036: * The owner project and the context in which this object operates.
037: */
038: protected XProject currentProject = XProjectManager
039: .getCurrentProject();
040:
041: /**
042: * Create a new printable page
043: * @param page The page or container to be printed
044: * @param po The printout that will 'own' the printable page
045: */
046: public PrintablePage(Container page, Printout po) {
047: currentPage = page;
048: printout = po;
049: }
050:
051: /**
052: * Print the page
053: * @param g The graphics context
054: * @param pf the page format
055: * @param pageIndex the index of the page within the printout page set
056: * @throws java.awt.print.PrinterException Some printing exception occured
057: * @return Printable.PAGE_EXISTS on success
058: */
059: public int print(Graphics g, PageFormat pf, int pageIndex)
060: throws PrinterException {
061: Font f = currentProject.getStyleManager().getFont("base");
062: g.setFont(f);
063: g.setColor(currentProject.getStyleManager().getStyle("base")
064: .getStyleAsColor(XStyle.COLOR_FORE));
065: FontMetrics fm = g.getFontMetrics();
066:
067: double imageableX = pf.getImageableX();
068: double imageableY = pf.getImageableY();
069: double imageableW = pf.getImageableWidth();
070: double imageableH = pf.getImageableHeight();
071:
072: Graphics2D g2 = (Graphics2D) g;
073: int headerHeight = 0, footerHeight = 0;
074: for (int i = 0; i < 3; i++)
075: headerHeight = Math.max(headerHeight, printout
076: .printDecoration(g2, pf, true, i, imageableX + i
077: * imageableW / 3.0, imageableY, fm));
078:
079: for (int i = 0; i < 3; i++)
080: footerHeight = Math.max(footerHeight, printout
081: .printDecoration(g2, pf, false, i, imageableX + i
082: * imageableW / 3.0, imageableY + imageableH
083: - (headerHeight + footerHeight), fm));
084:
085: g2.translate(imageableX, imageableY);
086: g2.setClip(0, 0, currentPage.getWidth(), currentPage
087: .getHeight());
088:
089: // Setup the scaling
090: double scale;
091: imageableH -= (headerHeight + footerHeight);
092: if (pf.getOrientation() == PageFormat.LANDSCAPE)
093: scale = imageableH / currentPage.getHeight();
094: else
095: scale = imageableW / currentPage.getWidth();
096:
097: g2.translate(imageableX, imageableY + headerHeight);
098: g2.scale(scale, scale);
099: g2.setClip(0, 0, currentPage.getWidth(), currentPage
100: .getHeight());
101:
102: currentPage.print(g);
103: currentPage.printComponents(g);
104:
105: return Printable.PAGE_EXISTS;
106: }
107:
108: /**
109: * Get the page or container wrapped by this class.
110: * @return the wrapped container
111: */
112: public Container getPage() {
113: return currentPage;
114: }
115: }
|