1 /*******************************************************************************
2 ' * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.trend.util;
14 import java.awt.Graphics;
15 import java.awt.Graphics2D;
16 import java.awt.geom.Rectangle2D;
17 import java.awt.print.PageFormat;
18 import java.awt.print.Printable;
19 import java.awt.print.PrinterException;
20 import java.awt.print.PrinterJob;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.List;
27 import org.simantics.g2d.canvas.impl.CanvasContext;
28 import org.simantics.history.HistoryManager;
29 import org.simantics.trend.TrendInitializer;
30 import org.simantics.trend.configuration.TrendSpec;
31 import org.simantics.trend.configuration.Viewport;
32 import org.simantics.trend.impl.MilestoneSpec;
33 import org.simantics.trend.impl.TrendNode;
34 import org.simantics.utils.page.MarginUtils;
35 import org.simantics.utils.page.PageDesc;
36 import org.simantics.utils.page.PageOrientation;
37 import org.simantics.utils.threads.AWTThread;
39 import com.lowagie.text.Document;
40 import com.lowagie.text.DocumentException;
41 import com.lowagie.text.FontFactory;
42 import com.lowagie.text.Rectangle;
43 import com.lowagie.text.pdf.DefaultFontMapper;
44 import com.lowagie.text.pdf.FontMapper;
45 import com.lowagie.text.pdf.PdfContentByte;
46 import com.lowagie.text.pdf.PdfWriter;
48 public class PrintUtil {
52 List<PrintPage> pages = new ArrayList<PrintPage>();
56 pageDesc = new PageDesc("A4", PageOrientation.Landscape, 210, 297).withMargins( MarginUtils.MARGINS_10mm );
57 mapper = new DefaultFontMapper();
58 FontFactory.registerDirectories();
61 public void addTrendPage(TrendSpec trendSpec, HistoryManager historian, MilestoneSpec milestones, Viewport viewport)
63 pages.add( TrendPage.addTrendPage(trendSpec, historian, milestones, viewport) );
66 public void addTrendPage(TrendNode srcTrend) {
67 pages.add( TrendPage.createFrom(srcTrend));
70 public void setPageDesc(PageDesc pageDesc)
72 this.pageDesc = pageDesc;
75 public void print() throws PrinterException {
76 PrinterJob printJob = PrinterJob.getPrinterJob();
77 printJob.setPrintable( new Printable() {
79 public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
80 if (graphics instanceof Graphics2D == false) {
81 throw new PrinterException("Printer context does not support Graphics2D");
83 if (pageIndex < pages.size()) {
84 Graphics2D g2d = (Graphics2D) graphics;
86 g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
88 Rectangle2D rect = new Rectangle2D.Double(0, 0, pageFormat.getImageableWidth(), pageFormat.getImageableHeight());
89 //g2d.setClip( rect );
90 System.out.println("Printing page "+pageIndex+", pageFormat="+pageFormat.getImageableX()+", "+pageFormat.getImageableY()+", "+pageFormat.getImageableWidth()+", "+pageFormat.getImageableHeight());
91 PrintPage trend = pages.get(pageIndex);
92 trend.print(g2d, rect);
98 printJob.printDialog();
102 public void printPdf(File file) throws IOException
104 Document document = null;
105 FileOutputStream fos = null;
106 Graphics2D g2 = null;
109 Rectangle2D.Float pageArea = new Rectangle2D.Float();
110 pageDesc.getPageRectangle( pageArea );
112 // Content area in page
113 Rectangle2D.Float contentArea = new Rectangle2D.Float();
114 pageDesc.getMarginsRectangle( contentArea );
116 // step 1: creation of a document-object
117 Rectangle pageArea2 = new Rectangle(
118 (float) PageDesc.toPoints(pageArea.getMinX()),
119 (float) PageDesc.toPoints(pageArea.getMinY()),
120 (float) PageDesc.toPoints(pageArea.getWidth()),
121 (float) PageDesc.toPoints(pageArea.getHeight()) );
122 document = new Document( pageArea2 );
124 // step 2: creation of the writer
125 if ( !file.exists() ) file.createNewFile();
126 fos = new FileOutputStream( file, false );
129 writer = PdfWriter.getInstance(document, fos);
130 } catch (DocumentException e) {
131 throw new IOException(e);
134 // step 3: we open the document
138 for ( PrintPage page : pages )
140 if (!document.newPage()) break;
141 PdfContentByte cb = writer.getDirectContent();
142 g2 = cb.createGraphics( pageArea2.getWidth(), pageArea2.getHeight(), mapper);
144 Rectangle2D contentArea2 = new Rectangle2D.Float(
145 (float) PageDesc.toPoints(contentArea.getMinX()),
146 (float) PageDesc.toPoints(contentArea.getMinY()),
147 (float) PageDesc.toPoints(contentArea.getWidth()),
148 (float) PageDesc.toPoints(contentArea.getHeight()) );
150 page.print(g2, contentArea2);
155 if (g2 != null) g2.dispose();
156 if (document != null) document.close();
157 if (fos != null) fos.close();
158 } catch (Exception e) {}
162 interface PrintPage {
163 void print(Graphics2D g2d, Rectangle2D pageArea);
166 static class TrendPage implements PrintPage
171 public static TrendPage createFrom(TrendNode srcTrend)
173 TrendPage page = new TrendPage();
174 CanvasContext ctx = TrendInitializer.createDefaultCanvas(AWTThread.getThreadAccess(), srcTrend.historian, srcTrend.collector, null, srcTrend.getTrendSpec());
175 TrendNode trend = TrendInitializer.getTrendNode( ctx );
176 trend.setViewport( srcTrend.getViewport() );
177 page.autoscale = false;
178 if (srcTrend.milestones != null) trend.setMilestones( srcTrend.milestones );
179 trend.printing = true;
184 public static TrendPage addTrendPage(TrendSpec trendSpec, HistoryManager historian, MilestoneSpec milestones, Viewport viewport)
186 TrendPage page = new TrendPage();
187 CanvasContext ctx = TrendInitializer.createDefaultCanvas(AWTThread.getThreadAccess(), historian, null, null, trendSpec);
188 TrendNode trend = TrendInitializer.getTrendNode( ctx );
189 trend.printing = true;
190 if (milestones!=null) {
191 trend.setMilestones(milestones);
193 if ( viewport!=null ) {
194 trend.setViewport(viewport);
195 page.autoscale = false;
197 page.autoscale = true;
203 public void print(Graphics2D g2d, Rectangle2D pageArea)
205 Rectangle2D contentArea2 = new Rectangle2D.Float(
206 (float) PageDesc.toPoints(pageArea.getMinX()),
207 (float) PageDesc.toPoints(pageArea.getMinY()),
208 (float) PageDesc.toPoints(pageArea.getWidth()),
209 (float) PageDesc.toPoints(pageArea.getHeight()) );
211 java.awt.Rectangle r2 = g2d.getClipBounds();
212 java.awt.Rectangle r = g2d.getClipBounds();
216 // r = g2d.getClipBounds();
217 // System.out.println(r);
220 trend.setSize(r.width, r.height);
221 // trend.setSize(contentArea2.getWidth()*2, contentArea2.getHeight()*2);
222 // g2d.setClip( contentArea2 );
223 //trend.setSize(contentArea2.getWidth(), contentArea2.getHeight());
224 if ( autoscale ) trend.zoomOut();