001: /*
002: * uDig - User Friendly Desktop Internet GIS client
003: * http://udig.refractions.net
004: * (C) 2004, Refractions Research Inc.
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: */
017: package net.refractions.udig.printing.ui.internal;
018:
019: import static net.refractions.udig.printing.ui.internal.PrintingPlugin.TRACE_PRINTING;
020: import java.awt.Graphics;
021: import java.awt.Graphics2D;
022: import java.awt.Shape;
023: import java.awt.geom.AffineTransform;
024: import java.awt.print.PageFormat;
025: import java.awt.print.Pageable;
026: import java.awt.print.Printable;
027: import java.awt.print.PrinterException;
028: import java.awt.print.PrinterJob;
029: import java.util.Iterator;
030:
031: import net.refractions.udig.printing.model.Box;
032: import net.refractions.udig.printing.model.Page;
033:
034: import org.eclipse.core.runtime.IProgressMonitor;
035:
036: /**
037: * The PrintingEngine takes a page and processes it, taking each Box and
038: * realizing its contents and sending the result to the printer.
039: *
040: * @author Richard Gould
041: */
042: public class PrintingEngine implements Pageable, Printable {
043:
044: Page diagram;
045: private IProgressMonitor monitor;
046: private PrinterJob printerJob;
047:
048: /**
049: * Constructs a PrintingEngine using the given Page
050: * @param page the Page to be printed
051: */
052: public PrintingEngine(Page diagram) {
053: this .diagram = diagram;
054: }
055:
056: /**
057: * Iterates through the Page's Boxes, drawing to the provided Graphics
058: * object
059: *
060: * @see java.awt.print.Printable#print(java.awt.Graphics, java.awt.print.PageFormat, int)
061: */
062: public int print(Graphics graphics, PageFormat pageFormat,
063: int pageIndex) throws PrinterException {
064:
065: if (pageIndex >= 1) {
066: return Printable.NO_SUCH_PAGE;
067: }
068:
069: final Graphics2D graphics2d = (Graphics2D) graphics;
070:
071: AffineTransform at = graphics2d.getTransform();
072: double dpi = at.getScaleX() * 72;
073:
074: if (PrintingPlugin.isDebugging(TRACE_PRINTING)) {
075: PrintingPlugin.log("Printing page " + pageIndex, null); //$NON-NLS-1$
076: System.out.println("PageFormat: " + pageFormat); //$NON-NLS-1$
077: System.out
078: .println("PageFormat height: " + pageFormat.getHeight()); //$NON-NLS-1$
079: System.out
080: .println("PageFormat width: " + pageFormat.getWidth()); //$NON-NLS-1$
081: System.out
082: .println("PageFormat imageableX,Y " + pageFormat.getImageableX() + ", " + pageFormat.getImageableY()); //$NON-NLS-1$ //$NON-NLS-2$
083: System.out
084: .println("PageFormat imageable height: " + pageFormat.getImageableHeight()); //$NON-NLS-1$
085: System.out
086: .println("PageFormat imageable width: " + pageFormat.getImageableWidth()); //$NON-NLS-1$
087: System.out
088: .println("PageFormat orientation (LANDSCAPE=" + PageFormat.LANDSCAPE + ", PORTRAIT=" + PageFormat.PORTRAIT + "): " + pageFormat.getOrientation()); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
089:
090: System.out
091: .println("Graphics: clip bounds: " + graphics2d.getClipBounds()); //$NON-NLS-1$
092: System.out.println("Transform: scaleX: " + at.getScaleX()); //$NON-NLS-1$
093: System.out.println("Transform: scaleY: " + at.getScaleY()); //$NON-NLS-1$
094: System.out.println("DPI?? : " + dpi); //$NON-NLS-1$
095: }
096:
097: graphics2d.translate(pageFormat.getImageableX(), pageFormat
098: .getImageableY());
099:
100: Iterator iter = diagram.getBoxes().iterator();
101: while (iter.hasNext()) {
102: Box box = (Box) iter.next();
103:
104: graphics2d.translate(box.getLocation().x,
105: box.getLocation().y);
106:
107: Shape clip = graphics2d.getClip();
108: graphics2d.setClip(0, 0, box.getSize().width,
109: box.getSize().height);
110:
111: box.getBoxPrinter().draw(graphics2d, monitor);
112:
113: graphics2d.setClip(clip);
114: graphics2d.setTransform(at);
115: }
116:
117: return Printable.PAGE_EXISTS;
118: }
119:
120: /**
121: *
122: * @param monitor
123: */
124: public void setMonitor(IProgressMonitor monitor) {
125: this .monitor = monitor;
126: }
127:
128: public int getNumberOfPages() {
129: return 1;
130: }
131:
132: public PageFormat getPageFormat(int pageIndex)
133: throws IndexOutOfBoundsException {
134: return this .printerJob.defaultPage();
135: }
136:
137: public Printable getPrintable(int pageIndex)
138: throws IndexOutOfBoundsException {
139: return this ;
140: }
141:
142: public void setPrinterJob(PrinterJob printerJob) {
143: this.printerJob = printerJob;
144: }
145: }
|