001: package com.xoetrope.print;
002:
003: import java.util.Vector;
004:
005: import java.awt.Container;
006: import java.awt.Dimension;
007: import java.awt.Font;
008: import java.awt.FontMetrics;
009: import java.awt.Graphics;
010: import java.awt.Graphics2D;
011: import java.awt.Point;
012: import java.awt.geom.AffineTransform;
013: import java.awt.print.PageFormat;
014: import java.awt.print.Printable;
015: import java.awt.print.PrinterException;
016: import net.xoetrope.xui.XProject;
017:
018: import net.xoetrope.xui.XProjectManager;
019: import net.xoetrope.xui.style.XStyle;
020:
021: /**
022: * A decoration of the container to allow printing when the page is contains multiple pages within frames
023: *
024: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
025: * the GNU Public License (GPL), please see license.txt for more details. If
026: * you make commercial use of this software you must purchase a commercial
027: * license from Xoetrope.</p>
028: * <p> $Revision: 1.4 $</p>
029: */
030: public class PrintableFrameSet implements Printable {
031: /**
032: * Storage for the frames of this page
033: */
034: protected Vector frames;
035:
036: /**
037: * The PrintOut object that owns this printable page
038: */
039: protected Printout printout;
040:
041: /**
042: * The owner project and the context in which this object operates.
043: */
044: protected XProject currentProject = XProjectManager
045: .getCurrentProject();
046:
047: /**
048: * Craete a new PrintableFrameSet
049: * @param po The owner PrintOut
050: */
051: public PrintableFrameSet(Printout po) {
052: frames = new Vector();
053: printout = po;
054: }
055:
056: /**
057: * Add a frame to the page.
058: * @param targetFrame the frame to add
059: */
060: public void addFrame(Container targetFrame) {
061: frames.addElement(targetFrame);
062: }
063:
064: /**
065: * Print the frameset.The frame elements are assumed to be XTargets, the
066: * coordinates of which are used to position each frame on the page. The
067: * content of each target area is then printed.
068: * @param g the graphics context
069: * @param pf the page format
070: * @param pageIndex the index of the page within the page set
071: * @throws java.awt.print.PrinterException some printing error
072: * @return Printable.PAGE_EXISTS on success
073: */
074: public int print(Graphics g, PageFormat pf, int pageIndex)
075: throws PrinterException {
076: Font f = currentProject.getStyleManager().getFont("base");
077: g.setFont(f);
078: g.setColor(currentProject.getStyleManager().getStyle("base")
079: .getStyleAsColor(XStyle.COLOR_FORE));
080: FontMetrics fm = g.getFontMetrics();
081:
082: int numFrames = frames.size();
083: if (pageIndex != 0)
084: return Printable.NO_SUCH_PAGE;
085:
086: double imageableX = pf.getImageableX();
087: double imageableY = pf.getImageableY();
088: double imageableW = pf.getImageableWidth();
089: double imageableH = pf.getImageableHeight();
090:
091: // Determine the maximum coordinates
092: int maxX = 0, maxY = 0;
093: for (int i = 0; i < numFrames; i++) {
094: Container currentTarget = (Container) frames.elementAt(i);
095: Point pos = currentTarget.getLocation();
096: Dimension size = currentTarget.getSize();
097: maxX = Math.max(maxX, pos.x + size.width);
098: maxY = Math.max(maxY, pos.y + size.height);
099: }
100:
101: Graphics2D g2 = (Graphics2D) g;
102:
103: // Draw the decorations
104: int headerHeight = 0, footerHeight = 10;
105: for (int i = 0; i < 3; i++)
106: headerHeight = Math.max(headerHeight, printout
107: .printDecoration(g2, pf, true, i, imageableX + i
108: * imageableW / 3.0, imageableY, fm));
109:
110: for (int i = 0; i < 3; i++)
111: footerHeight = Math.max(footerHeight, printout
112: .printDecoration(g2, pf, false, i, imageableX + i
113: * imageableW / 3.0, imageableY + imageableH
114: - footerHeight, fm));
115:
116: // Setup the scaling
117: double scale;
118: imageableH -= (headerHeight + footerHeight);
119: if (pf.getOrientation() == PageFormat.LANDSCAPE)
120: scale = imageableH / maxY;
121: else
122: scale = imageableW / maxX;
123:
124: AffineTransform transform = g2.getTransform();
125:
126: // Paint the individual elements
127: for (int i = 0; i < numFrames; i++) {
128: Container currentTarget = (Container) frames.elementAt(i);
129: Container currentPage = (Container) currentTarget
130: .getComponent(0);
131: Point pos = currentTarget.getLocation();
132:
133: g2.setTransform(transform);
134: g2.translate(imageableX + pos.x * scale, imageableY + pos.y
135: * scale + headerHeight);
136: g2.scale(scale, scale);
137: g2.setClip(0, 0, currentPage.getWidth(), currentPage
138: .getHeight());
139: currentPage.print(g);
140: currentPage.printComponents(g);
141: }
142:
143: return Printable.PAGE_EXISTS;
144: }
145: }
|