]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.linking.ui/src/org/simantics/document/linking/report/ExportToPDF.java
Externalize org.simantics.document.linking.ui
[simantics/platform.git] / bundles / org.simantics.document.linking.ui / src / org / simantics / document / linking / report / ExportToPDF.java
1 package org.simantics.document.linking.report;
2
3 import java.awt.Font;
4 import java.io.File;
5 import java.util.HashMap;
6 import java.util.List;
7 import java.util.Map;
8
9 import org.eclipse.core.runtime.IProgressMonitor;
10 import org.eclipse.core.runtime.IStatus;
11 import org.eclipse.core.runtime.NullProgressMonitor;
12 import org.eclipse.core.runtime.Status;
13 import org.eclipse.core.runtime.jobs.Job;
14 import org.simantics.Simantics;
15 import org.simantics.db.ReadGraph;
16 import org.simantics.db.Resource;
17 import org.simantics.db.Session;
18 import org.simantics.db.exception.DatabaseException;
19 import org.simantics.db.request.Read;
20 import org.simantics.document.linking.Activator;
21 import org.simantics.document.linking.report.html.HTMLDocument;
22 import org.simantics.document.linking.report.pdf.PDFDocument;
23 import org.simantics.document.linking.report.templates.CompleteStructureWriter;
24 import org.simantics.document.linking.report.templates.DocumentStructureWriter;
25 import org.simantics.document.linking.report.templates.ReportWriter;
26
27
28 public class ExportToPDF {
29         public static final int DOCUMENT_BROWSE_STRCTURE = 0;
30         public static final int DOCUMENT_STRUCTURE = 1;
31         public static final int DIAGRAM_STRUCTURE = 2;
32         public static final int COMPLETE_STRUCTURE = 3;
33         
34         private Session session;
35         private Resource model;
36         
37         public ExportToPDF(Session session,Resource model) {
38                 this.model = model;
39                 this.session = session;
40         }
41         
42         public  void exportPDF(String filename, int report) {
43                 ReportWriter<?> reportWriter = null;
44                 switch (report) {
45                 case DOCUMENT_BROWSE_STRCTURE:
46                         
47                         break;
48                 case DOCUMENT_STRUCTURE:
49                         reportWriter = new DocumentStructureWriter();
50                         break;
51                 case DIAGRAM_STRUCTURE:
52                         
53                         break;
54                 case COMPLETE_STRUCTURE:
55                         reportWriter = new CompleteStructureWriter();
56                         break;
57                 default:
58                         break;
59                 }
60                 ExportJob job = new ExportJob(filename, reportWriter);
61                 job.setUser(true);
62                 job.schedule();
63                 
64         }
65         
66         private class ExportJob extends Job {
67                 
68                 String filename;
69                 ReportWriter<?> reportWriter;
70                 
71                 public ExportJob(String filename, ReportWriter<?> reportWriter) {
72                         super(reportWriter.getName());
73                         this.filename = filename;
74                         this.reportWriter = reportWriter;
75                 }
76                 
77                 @Override
78                 protected IStatus run(IProgressMonitor monitor) {
79                         return export(filename, reportWriter, monitor);
80                 }
81         }
82         
83         public <T> IStatus export(String filename, ReportWriter<T> reportWriter, IProgressMonitor monitor) {
84                 
85                 Document lineWriter = null;
86                 try {
87                         if (filename.toLowerCase().endsWith(".pdf")) //$NON-NLS-1$
88                                 lineWriter = new PDFDocument(filename, new Font("Arial", Font.PLAIN, 10),new Font("Arial", Font.BOLD, 10)); //$NON-NLS-1$ //$NON-NLS-2$
89                         else if (filename.toLowerCase().endsWith(".html") || //$NON-NLS-1$
90                                         filename.toLowerCase().endsWith(".htm")) //$NON-NLS-1$
91                                 lineWriter = new HTMLDocument(new File(filename));
92                         else {
93                                 throw new Exception("File has unknow extension " + filename); //$NON-NLS-1$
94                         }
95                         export(lineWriter, reportWriter, monitor);
96                         
97                 } catch (Exception e) {
98                         return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.ExportToPDF_ActivatorCouldNotGenerateReport, e);
99                 } finally {
100                         if (lineWriter != null) {
101                                 try {
102                                         lineWriter.close();
103                                 } catch (Exception e) {
104                                         e.printStackTrace();
105                                 }
106                         }
107                         
108                 }
109                 String status  = Messages.ExportToPDF_ReportGenerated;
110 //              if (lineWriter instanceof PDFTableWriter) {
111 //                      status += " Report size " + ((PDFTableWriter)lineWriter).getCurrentPageIndex() + " pages.";
112 //              }
113                 
114                 return new Status(IStatus.OK, Activator.PLUGIN_ID, status);
115         }
116         
117         
118
119         
120         private <T> void export(final Document document, final ReportWriter<T> reportWriter, final IProgressMonitor monitor) throws DatabaseException{
121                 session.syncRequest(new Read<Object>() {
122                         @Override
123                         public Object perform(ReadGraph graph) throws DatabaseException {
124                                 
125                                 try {
126                                         Map<Object, Object> context = new HashMap<Object, Object>();
127                                         context.put("model", model); //$NON-NLS-1$
128                                         context.put(Document.class, document);
129                                         context.put(ReportWriter.class, reportWriter);
130                                         context.put("DocumentName", reportWriter.getName()); //$NON-NLS-1$
131                                         
132                                         reportWriter.start(graph, model, document, context);
133                                         
134                                         List<T> list = reportWriter.getReportItems(graph);
135                                         
136                                 monitor.beginTask(Messages.ExportToPDF_MonitorWriteReport, list.size());
137         
138                                 T previous = null;
139                                 T current = null;
140                                 T next = null;
141                                 for (int i = 0; i < list.size(); i++) {
142                                         current = list.get(i);
143                                         if (i > 0)
144                                                 previous = list.get(i-1);
145                                         else
146                                                 previous = null;
147                                         if (i < list.size()-1)
148                                                 next = list.get(i+1);
149                                         else
150                                                 next = null;
151                                         reportWriter.write(previous, current, next, document);
152                                         monitor.worked(1);
153                                 }
154                                 } catch (Exception e) {
155                                         throw new DatabaseException(e);
156                                 }
157                         monitor.done();
158                         return null;
159                         }
160                 });
161                 
162         }
163         
164         public static void exportDocumentToPDF(Resource model, String filename) {
165             ExportToPDF exportPdf = new ExportToPDF(Simantics.getSession(), model);
166             exportPdf.export(filename, new DocumentStructureWriter(), new NullProgressMonitor());
167         }
168 }