]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.trend/src/org/simantics/trend/util/PrintUtil.java
Fixed route graph splitting and diagram mapping race condition problem
[simantics/platform.git] / bundles / org.simantics.trend / src / org / simantics / trend / util / PrintUtil.java
1 /*******************************************************************************
2 ' * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
3  * Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.trend.util;
13
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;
21 import java.io.File;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.List;
26
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;
38
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;
47
48 public class PrintUtil {
49
50         PageDesc pageDesc;
51     FontMapper mapper;
52     List<PrintPage> pages = new ArrayList<PrintPage>();
53         
54     public PrintUtil()
55     {
56         pageDesc = new PageDesc("A4", PageOrientation.Landscape, 210, 297).withMargins( MarginUtils.MARGINS_10mm );
57         mapper = new DefaultFontMapper();
58         FontFactory.registerDirectories();
59     }
60     
61     public void addTrendPage(TrendSpec trendSpec, HistoryManager historian, MilestoneSpec milestones, Viewport viewport)
62     {
63         pages.add( TrendPage.addTrendPage(trendSpec, historian, milestones, viewport) );
64     }
65     
66     public void addTrendPage(TrendNode srcTrend) {
67         pages.add( TrendPage.createFrom(srcTrend));
68     }
69     
70         public void setPageDesc(PageDesc pageDesc)
71         {
72                 this.pageDesc = pageDesc;
73     }
74         
75         public void print() throws PrinterException {
76                 PrinterJob printJob = PrinterJob.getPrinterJob();
77                 printJob.setPrintable( new Printable() {
78                         @Override
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");
82                                 }
83                                 if (pageIndex < pages.size()) {
84                                         Graphics2D g2d = (Graphics2D) graphics;
85
86                                         g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());                  
87                                         
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);
93                                         
94                                         return PAGE_EXISTS;
95                                 }
96                                 return NO_SUCH_PAGE;
97                         }} );
98                 printJob.printDialog();
99                 printJob.print();
100         }       
101         
102         public void printPdf(File file) throws IOException
103         {               
104                 Document document = null;
105                 FileOutputStream fos = null;
106                 Graphics2D g2 = null;
107                 try {
108                         // Page area
109                         Rectangle2D.Float pageArea = new Rectangle2D.Float();
110                         pageDesc.getPageRectangle( pageArea );
111
112                         // Content area in page
113                         Rectangle2D.Float contentArea = new Rectangle2D.Float();
114                         pageDesc.getMarginsRectangle( contentArea );
115         
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 );
123           
124                         // step 2: creation of the writer
125                         if ( !file.exists() ) file.createNewFile();
126                         fos = new FileOutputStream( file, false );
127                         PdfWriter writer;
128                         try {
129                                 writer = PdfWriter.getInstance(document, fos);
130                         } catch (DocumentException e) {
131                                 throw new IOException(e);
132                         }
133             
134                         // step 3: we open the document
135                         document.open();
136
137                         // step 4:
138                         for ( PrintPage page : pages ) 
139                         {
140                                 if (!document.newPage()) break;
141                                 PdfContentByte cb = writer.getDirectContent();
142                                 g2 = cb.createGraphics( pageArea2.getWidth(), pageArea2.getHeight(), mapper);
143                                 
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()) );
149         
150                                 page.print(g2, contentArea2);
151                         }
152                         
153         } finally {
154                 try {
155                         if (g2 != null) g2.dispose();
156                         if (document != null) document.close();
157                         if (fos != null) fos.close();
158                 } catch (Exception e) {}
159         }
160         }
161
162         interface PrintPage {
163                 void print(Graphics2D g2d, Rectangle2D pageArea);
164         }
165         
166         static class TrendPage implements PrintPage
167         {
168                 boolean autoscale;
169                 TrendNode trend;
170                 
171                 public static TrendPage createFrom(TrendNode srcTrend)
172                 {
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;
180                 page.trend = trend;
181                 return page;
182                 }
183                 
184             public static TrendPage addTrendPage(TrendSpec trendSpec, HistoryManager historian, MilestoneSpec milestones, Viewport viewport)
185             {
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);
192                 }
193                 if ( viewport!=null ) {
194                         trend.setViewport(viewport);
195                         page.autoscale = false;
196                 } else {
197                         page.autoscale = true;
198                 }
199                 page.trend = trend;
200                 return page;
201             }
202                                 
203                 public void print(Graphics2D g2d, Rectangle2D pageArea)
204                 {
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()) );
210
211                         java.awt.Rectangle r2 = g2d.getClipBounds();
212                         java.awt.Rectangle r = g2d.getClipBounds();
213                         r.width *= 2;
214                         r.height *= 2;
215                         g2d.scale(0.5, 0.5);
216 //                      r = g2d.getClipBounds();
217 //                      System.out.println(r);
218                         g2d.setClip(r);
219                         try {
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();
225                                 trend.layout();
226                                 trend.render( g2d );
227                         } finally {
228                                 g2d.scale(2, 2);
229                                 g2d.setClip(r2);
230                         }
231                 }
232         }
233         
234 }