]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - 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
index 4b3873e0a3965998442ae6606b51ad5c2180fec7..f9645042bbc2b8b5fc97f85b4f041d2286f8cd2c 100644 (file)
-package org.simantics.document.linking.report;\r
-\r
-import java.awt.Font;\r
-import java.io.File;\r
-import java.util.HashMap;\r
-import java.util.List;\r
-import java.util.Map;\r
-\r
-import org.eclipse.core.runtime.IProgressMonitor;\r
-import org.eclipse.core.runtime.IStatus;\r
-import org.eclipse.core.runtime.NullProgressMonitor;\r
-import org.eclipse.core.runtime.Status;\r
-import org.eclipse.core.runtime.jobs.Job;\r
-import org.simantics.Simantics;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.Session;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.request.Read;\r
-import org.simantics.document.linking.Activator;\r
-import org.simantics.document.linking.report.html.HTMLDocument;\r
-import org.simantics.document.linking.report.pdf.PDFDocument;\r
-import org.simantics.document.linking.report.templates.CompleteStructureWriter;\r
-import org.simantics.document.linking.report.templates.DocumentStructureWriter;\r
-import org.simantics.document.linking.report.templates.ReportWriter;\r
-\r
-\r
-public class ExportToPDF {\r
-       public static final int DOCUMENT_BROWSE_STRCTURE = 0;\r
-       public static final int DOCUMENT_STRUCTURE = 1;\r
-       public static final int DIAGRAM_STRUCTURE = 2;\r
-       public static final int COMPLETE_STRUCTURE = 3;\r
-       \r
-       private Session session;\r
-       private Resource model;\r
-       \r
-       public ExportToPDF(Session session,Resource model) {\r
-               this.model = model;\r
-               this.session = session;\r
-       }\r
-       \r
-       public  void exportPDF(String filename, int report) {\r
-               ReportWriter<?> reportWriter = null;\r
-               switch (report) {\r
-               case DOCUMENT_BROWSE_STRCTURE:\r
-                       \r
-                       break;\r
-               case DOCUMENT_STRUCTURE:\r
-                       reportWriter = new DocumentStructureWriter();\r
-                       break;\r
-               case DIAGRAM_STRUCTURE:\r
-                       \r
-                       break;\r
-               case COMPLETE_STRUCTURE:\r
-                       reportWriter = new CompleteStructureWriter();\r
-                       break;\r
-               default:\r
-                       break;\r
-               }\r
-               ExportJob job = new ExportJob(filename, reportWriter);\r
-               job.setUser(true);\r
-               job.schedule();\r
-               \r
-       }\r
-       \r
-       private class ExportJob extends Job {\r
-               \r
-               String filename;\r
-               ReportWriter<?> reportWriter;\r
-               \r
-               public ExportJob(String filename, ReportWriter<?> reportWriter) {\r
-                       super(reportWriter.getName());\r
-                       this.filename = filename;\r
-                       this.reportWriter = reportWriter;\r
-               }\r
-               \r
-               @Override\r
-               protected IStatus run(IProgressMonitor monitor) {\r
-                       return export(filename, reportWriter, monitor);\r
-               }\r
-       }\r
-       \r
-       public <T> IStatus export(String filename, ReportWriter<T> reportWriter, IProgressMonitor monitor) {\r
-               \r
-               Document lineWriter = null;\r
-               try {\r
-                       if (filename.toLowerCase().endsWith(".pdf"))\r
-                               lineWriter = new PDFDocument(filename, new Font("Arial", Font.PLAIN, 10),new Font("Arial", Font.BOLD, 10));\r
-                       else if (filename.toLowerCase().endsWith(".html") ||\r
-                                       filename.toLowerCase().endsWith(".htm"))\r
-                               lineWriter = new HTMLDocument(new File(filename));\r
-                       else {\r
-                               throw new Exception("File has unknow extension " + filename);\r
-                       }\r
-                       export(lineWriter, reportWriter, monitor);\r
-                       \r
-               } catch (Exception e) {\r
-                       return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Could not generate report", e);\r
-               } finally {\r
-                       if (lineWriter != null) {\r
-                               try {\r
-                                       lineWriter.close();\r
-                               } catch (Exception e) {\r
-                                       e.printStackTrace();\r
-                               }\r
-                       }\r
-                       \r
-               }\r
-               String status  = "Report generated.";\r
-//             if (lineWriter instanceof PDFTableWriter) {\r
-//                     status += " Report size " + ((PDFTableWriter)lineWriter).getCurrentPageIndex() + " pages.";\r
-//             }\r
-               \r
-               return new Status(IStatus.OK, Activator.PLUGIN_ID, status);\r
-       }\r
-       \r
-       \r
-\r
-       \r
-       private <T> void export(final Document document, final ReportWriter<T> reportWriter, final IProgressMonitor monitor) throws DatabaseException{\r
-               session.syncRequest(new Read<Object>() {\r
-                       @Override\r
-                       public Object perform(ReadGraph graph) throws DatabaseException {\r
-                               \r
-                               try {\r
-                                       Map<Object, Object> context = new HashMap<Object, Object>();\r
-                                       context.put("model", model);\r
-                                       context.put(Document.class, document);\r
-                                       context.put(ReportWriter.class, reportWriter);\r
-                                       context.put("DocumentName", reportWriter.getName());\r
-                                       \r
-                                       reportWriter.start(graph, model, document, context);\r
-                                       \r
-                                       List<T> list = reportWriter.getReportItems(graph);\r
-                                       \r
-                               monitor.beginTask("Write Report", list.size());\r
-       \r
-                               T previous = null;\r
-                               T current = null;\r
-                               T next = null;\r
-                               for (int i = 0; i < list.size(); i++) {\r
-                                       current = list.get(i);\r
-                                       if (i > 0)\r
-                                               previous = list.get(i-1);\r
-                                       else\r
-                                               previous = null;\r
-                                       if (i < list.size()-1)\r
-                                               next = list.get(i+1);\r
-                                       else\r
-                                               next = null;\r
-                                       reportWriter.write(previous, current, next, document);\r
-                                       monitor.worked(1);\r
-                               }\r
-                               } catch (Exception e) {\r
-                                       throw new DatabaseException(e);\r
-                               }\r
-                       monitor.done();\r
-                       return null;\r
-                       }\r
-               });\r
-               \r
-       }\r
-       \r
-       public static void exportDocumentToPDF(Resource model, String filename) {\r
-           ExportToPDF exportPdf = new ExportToPDF(Simantics.getSession(), model);\r
-           exportPdf.export(filename, new DocumentStructureWriter(), new NullProgressMonitor());\r
-       }\r
-}\r
+package org.simantics.document.linking.report;
+
+import java.awt.Font;
+import java.io.File;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.core.runtime.jobs.Job;
+import org.simantics.Simantics;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.Session;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.request.Read;
+import org.simantics.document.linking.Activator;
+import org.simantics.document.linking.report.html.HTMLDocument;
+import org.simantics.document.linking.report.pdf.PDFDocument;
+import org.simantics.document.linking.report.templates.CompleteStructureWriter;
+import org.simantics.document.linking.report.templates.DocumentStructureWriter;
+import org.simantics.document.linking.report.templates.ReportWriter;
+
+
+public class ExportToPDF {
+       public static final int DOCUMENT_BROWSE_STRCTURE = 0;
+       public static final int DOCUMENT_STRUCTURE = 1;
+       public static final int DIAGRAM_STRUCTURE = 2;
+       public static final int COMPLETE_STRUCTURE = 3;
+       
+       private Session session;
+       private Resource model;
+       
+       public ExportToPDF(Session session,Resource model) {
+               this.model = model;
+               this.session = session;
+       }
+       
+       public  void exportPDF(String filename, int report) {
+               ReportWriter<?> reportWriter = null;
+               switch (report) {
+               case DOCUMENT_BROWSE_STRCTURE:
+                       
+                       break;
+               case DOCUMENT_STRUCTURE:
+                       reportWriter = new DocumentStructureWriter();
+                       break;
+               case DIAGRAM_STRUCTURE:
+                       
+                       break;
+               case COMPLETE_STRUCTURE:
+                       reportWriter = new CompleteStructureWriter();
+                       break;
+               default:
+                       break;
+               }
+               ExportJob job = new ExportJob(filename, reportWriter);
+               job.setUser(true);
+               job.schedule();
+               
+       }
+       
+       private class ExportJob extends Job {
+               
+               String filename;
+               ReportWriter<?> reportWriter;
+               
+               public ExportJob(String filename, ReportWriter<?> reportWriter) {
+                       super(reportWriter.getName());
+                       this.filename = filename;
+                       this.reportWriter = reportWriter;
+               }
+               
+               @Override
+               protected IStatus run(IProgressMonitor monitor) {
+                       return export(filename, reportWriter, monitor);
+               }
+       }
+       
+       public <T> IStatus export(String filename, ReportWriter<T> reportWriter, IProgressMonitor monitor) {
+               
+               Document lineWriter = null;
+               try {
+                       if (filename.toLowerCase().endsWith(".pdf")) //$NON-NLS-1$
+                               lineWriter = new PDFDocument(filename, new Font("Arial", Font.PLAIN, 10),new Font("Arial", Font.BOLD, 10)); //$NON-NLS-1$ //$NON-NLS-2$
+                       else if (filename.toLowerCase().endsWith(".html") || //$NON-NLS-1$
+                                       filename.toLowerCase().endsWith(".htm")) //$NON-NLS-1$
+                               lineWriter = new HTMLDocument(new File(filename));
+                       else {
+                               throw new Exception("File has unknow extension " + filename); //$NON-NLS-1$
+                       }
+                       export(lineWriter, reportWriter, monitor);
+                       
+               } catch (Exception e) {
+                       return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.ExportToPDF_ActivatorCouldNotGenerateReport, e);
+               } finally {
+                       if (lineWriter != null) {
+                               try {
+                                       lineWriter.close();
+                               } catch (Exception e) {
+                                       e.printStackTrace();
+                               }
+                       }
+                       
+               }
+               String status  = Messages.ExportToPDF_ReportGenerated;
+//             if (lineWriter instanceof PDFTableWriter) {
+//                     status += " Report size " + ((PDFTableWriter)lineWriter).getCurrentPageIndex() + " pages.";
+//             }
+               
+               return new Status(IStatus.OK, Activator.PLUGIN_ID, status);
+       }
+       
+       
+
+       
+       private <T> void export(final Document document, final ReportWriter<T> reportWriter, final IProgressMonitor monitor) throws DatabaseException{
+               session.syncRequest(new Read<Object>() {
+                       @Override
+                       public Object perform(ReadGraph graph) throws DatabaseException {
+                               
+                               try {
+                                       Map<Object, Object> context = new HashMap<Object, Object>();
+                                       context.put("model", model); //$NON-NLS-1$
+                                       context.put(Document.class, document);
+                                       context.put(ReportWriter.class, reportWriter);
+                                       context.put("DocumentName", reportWriter.getName()); //$NON-NLS-1$
+                                       
+                                       reportWriter.start(graph, model, document, context);
+                                       
+                                       List<T> list = reportWriter.getReportItems(graph);
+                                       
+                               monitor.beginTask(Messages.ExportToPDF_MonitorWriteReport, list.size());
+       
+                               T previous = null;
+                               T current = null;
+                               T next = null;
+                               for (int i = 0; i < list.size(); i++) {
+                                       current = list.get(i);
+                                       if (i > 0)
+                                               previous = list.get(i-1);
+                                       else
+                                               previous = null;
+                                       if (i < list.size()-1)
+                                               next = list.get(i+1);
+                                       else
+                                               next = null;
+                                       reportWriter.write(previous, current, next, document);
+                                       monitor.worked(1);
+                               }
+                               } catch (Exception e) {
+                                       throw new DatabaseException(e);
+                               }
+                       monitor.done();
+                       return null;
+                       }
+               });
+               
+       }
+       
+       public static void exportDocumentToPDF(Resource model, String filename) {
+           ExportToPDF exportPdf = new ExportToPDF(Simantics.getSession(), model);
+           exportPdf.export(filename, new DocumentStructureWriter(), new NullProgressMonitor());
+       }
+}