Sync git svn branch with SVN repository r33173.
authorTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Mon, 5 Sep 2016 20:08:56 +0000 (23:08 +0300)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Mon, 5 Sep 2016 20:08:56 +0000 (23:08 +0300)
refs #6475

bundles/org.simantics.export.core/META-INF/MANIFEST.MF
bundles/org.simantics.export.core/src/org/simantics/export/core/internal/Activator.java [new file with mode: 0644]
bundles/org.simantics.export.core/src/org/simantics/export/core/pdf/ExportPdfWriter.java
bundles/org.simantics.export.core/src/org/simantics/export/core/pdf/PdfExportPageEvent.java [new file with mode: 0644]
bundles/org.simantics.export.core/src/org/simantics/export/core/pdf/ServiceBasedPdfExportPageEvent.java [new file with mode: 0644]
bundles/org.simantics.modeling.ui/META-INF/MANIFEST.MF
bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/pdf/DiagramPrinter.java

index a194cf13392bee3b9baa7ac84513145e58d9d3da..eadcf7e08be678758677bb3c7e08cac3d32ac497 100644 (file)
@@ -24,3 +24,5 @@ Export-Package: org.simantics.export.core,
  org.simantics.export.core.manager,
  org.simantics.export.core.pdf,
  org.simantics.export.core.util
+Bundle-ActivationPolicy: lazy
+Bundle-Activator: org.simantics.export.core.internal.Activator
diff --git a/bundles/org.simantics.export.core/src/org/simantics/export/core/internal/Activator.java b/bundles/org.simantics.export.core/src/org/simantics/export/core/internal/Activator.java
new file mode 100644 (file)
index 0000000..e07fa58
--- /dev/null
@@ -0,0 +1,28 @@
+package org.simantics.export.core.internal;\r
+\r
+import org.osgi.framework.BundleActivator;\r
+import org.osgi.framework.BundleContext;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ * @since 1.22.2\r
+ */\r
+public class Activator implements BundleActivator {\r
+\r
+       private static BundleContext context;\r
+\r
+       public static BundleContext getContext() {\r
+               return context;\r
+       }\r
+\r
+       @Override\r
+       public void start(BundleContext context) throws Exception {\r
+               Activator.context = context;\r
+       }\r
+\r
+       @Override\r
+       public void stop(BundleContext context) throws Exception {\r
+               Activator.context = null;\r
+       }\r
+\r
+}\r
index 7a490e642dd5ea601faa9324e783e7b4a41fb19f..6318e5de2d5bdb5d90cb40e274504a8b2b1df71d 100644 (file)
@@ -399,6 +399,7 @@ public class ExportPdfWriter {
                                this.pdfWriter = PdfWriter.getInstance(document, fos);\r
                                this.pdfWriter.setPdfVersion(PdfWriter.PDF_VERSION_1_7);\r
                                this.pdfWriter.setCompressionLevel( compressionLevel );\r
+                               this.pdfWriter.setPageEvent(new ServiceBasedPdfExportPageEvent());\r
                                this.document.open();\r
                                this.cb = this.pdfWriter.getDirectContent();\r
                                if (!this.document.newPage()) throw new ExportException("Failed to create new page.");\r
diff --git a/bundles/org.simantics.export.core/src/org/simantics/export/core/pdf/PdfExportPageEvent.java b/bundles/org.simantics.export.core/src/org/simantics/export/core/pdf/PdfExportPageEvent.java
new file mode 100644 (file)
index 0000000..60573fa
--- /dev/null
@@ -0,0 +1,9 @@
+package org.simantics.export.core.pdf;\r
+\r
+import com.lowagie.text.pdf.PdfPageEvent;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public interface PdfExportPageEvent extends PdfPageEvent {\r
+}\r
diff --git a/bundles/org.simantics.export.core/src/org/simantics/export/core/pdf/ServiceBasedPdfExportPageEvent.java b/bundles/org.simantics.export.core/src/org/simantics/export/core/pdf/ServiceBasedPdfExportPageEvent.java
new file mode 100644 (file)
index 0000000..c513654
--- /dev/null
@@ -0,0 +1,126 @@
+/*******************************************************************************\r
+ * Copyright (c) 2016 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     Semantum Oy - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.export.core.pdf;\r
+\r
+import java.util.Collection;\r
+import java.util.Collections;\r
+import java.util.HashMap;\r
+import java.util.Map;\r
+import java.util.function.Consumer;\r
+\r
+import org.osgi.framework.BundleContext;\r
+import org.osgi.framework.InvalidSyntaxException;\r
+import org.osgi.framework.ServiceReference;\r
+import org.simantics.Logger;\r
+import org.simantics.export.core.internal.Activator;\r
+\r
+import com.lowagie.text.Document;\r
+import com.lowagie.text.Paragraph;\r
+import com.lowagie.text.Rectangle;\r
+import com.lowagie.text.pdf.PdfPageEventHelper;\r
+import com.lowagie.text.pdf.PdfWriter;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ * @since 1.22.2\r
+ */\r
+public class ServiceBasedPdfExportPageEvent extends PdfPageEventHelper {\r
+\r
+       Map<String, PdfExportPageEvent> events;\r
+       \r
+       public ServiceBasedPdfExportPageEvent() {\r
+               // Read all page event contributions from the OSGi service\r
+               events = collectEvents();\r
+       }\r
+\r
+       private static Map<String, PdfExportPageEvent> collectEvents() {\r
+               BundleContext context = Activator.getContext();\r
+               Collection<ServiceReference<PdfExportPageEvent>> serviceReferences;\r
+               try {\r
+                       serviceReferences = context.getServiceReferences(PdfExportPageEvent.class, null);\r
+               } catch (InvalidSyntaxException e) {\r
+                       e.printStackTrace();\r
+                       serviceReferences = Collections.emptyList();\r
+               }\r
+               Map<String, PdfExportPageEvent> events = new HashMap<>(serviceReferences.size());\r
+               for (ServiceReference<PdfExportPageEvent> reference : serviceReferences) {\r
+                       PdfExportPageEvent event = context.getService(reference);\r
+                       String eventName = event.toString();\r
+                       events.put(eventName, event);\r
+               }\r
+               return events;\r
+       }\r
+\r
+       private void safeInvoke(String eventName, PdfExportPageEvent event, Consumer<PdfExportPageEvent> r) {\r
+               try {\r
+                       r.accept(event);\r
+               } catch (Exception e) {\r
+                       Logger.defaultLogError("Failed to invoke PdfExportPageEvent::" + eventName + " for " + event.toString(), e);\r
+               }\r
+       }\r
+\r
+       @Override\r
+       public void onChapter(PdfWriter writer, Document document, float paragraphPosition, Paragraph title) {\r
+               events.values().forEach(e -> safeInvoke("onChapter", e, ee -> ee.onChapter(writer, document, paragraphPosition, title)));\r
+       }\r
+\r
+       @Override\r
+       public void onChapterEnd(PdfWriter writer, Document document, float position) {\r
+               events.values().forEach(e -> safeInvoke("onChapterEnd", e, ee -> ee.onChapterEnd(writer, document, position)));\r
+       }\r
+\r
+       @Override\r
+       public void onGenericTag(PdfWriter writer, Document document, Rectangle rect, String text) {\r
+               events.values().forEach(e -> safeInvoke("onGenericTag", e, ee -> ee.onGenericTag(writer, document, rect, text)));\r
+       }\r
+\r
+       @Override\r
+       public void onOpenDocument(PdfWriter writer, Document document) {\r
+               events.values().forEach(e -> safeInvoke("onOpenDocument", e, ee -> ee.onOpenDocument(writer, document)));\r
+       }\r
+\r
+       @Override\r
+       public void onCloseDocument(PdfWriter writer, Document document) {\r
+               events.values().forEach(e -> safeInvoke("onCloseDocument", e, ee -> ee.onCloseDocument(writer, document)));\r
+       }\r
+\r
+       @Override\r
+       public void onParagraph(PdfWriter writer, Document document, float paragraphPosition) {\r
+               events.values().forEach(e -> safeInvoke("onParagraph", e, ee -> ee.onParagraph(writer, document, paragraphPosition)));\r
+       }\r
+\r
+       @Override\r
+       public void onParagraphEnd(PdfWriter writer, Document document, float paragraphPosition) {\r
+               events.values().forEach(e -> safeInvoke("onParagraphEnd", e, ee -> ee.onParagraphEnd(writer, document, paragraphPosition)));\r
+       }\r
+\r
+       @Override\r
+       public void onSection(PdfWriter writer, Document document, float paragraphPosition, int depth, Paragraph title) {\r
+               events.values().forEach(e -> safeInvoke("onSection", e, ee -> ee.onSection(writer, document, paragraphPosition, depth, title)));\r
+       }\r
+\r
+       @Override\r
+       public void onSectionEnd(PdfWriter writer, Document document, float position) {\r
+               events.values().forEach(e -> safeInvoke("onSectionEnd", e, ee -> ee.onSectionEnd(writer, document, position)));\r
+       }\r
+\r
+       @Override\r
+       public void onStartPage(PdfWriter writer, Document document) {\r
+               events.values().forEach(e -> safeInvoke("onStartPage", e, ee -> ee.onStartPage(writer, document)));\r
+       }\r
+\r
+       @Override\r
+       public void onEndPage(PdfWriter writer, Document document) {\r
+               events.values().forEach(e -> safeInvoke("onEndPage", e, ee -> ee.onEndPage(writer, document)));\r
+       }\r
+\r
+}\r
index 1b915e3a7a9e0daa82aaad885ec679e9d3aee27e..10ad43d9a99f44938ef9e44fa0bfb698039fbbea 100644 (file)
@@ -60,7 +60,8 @@ Require-Bundle: org.simantics.project;bundle-version="1.0.0",
  org.eclipse.swt,
  org.simantics.db.layer0,
  org.simantics.silk.ontology;bundle-version="1.1.0",
- org.simantics.image.ui;bundle-version="1.0.0"
+ org.simantics.image.ui;bundle-version="1.0.0",
+ org.simantics.export.core;bundle-version="1.0.0"
 Export-Package: org.simantics.modeling.ui,
  org.simantics.modeling.ui.actions,
  org.simantics.modeling.ui.chart.property,
index ccdf57fbe89f8ec655bef4fb1dd3b5b5f7ed8986..1b916fcb0b884524614d205b1da61851b70a236d 100644 (file)
@@ -30,6 +30,7 @@ import org.simantics.db.layer0.util.SessionGarbageCollection;
 import org.simantics.db.management.ISessionContext;\r
 import org.simantics.document.DocumentSettings;\r
 import org.simantics.document.DocumentUtils;\r
+import org.simantics.export.core.pdf.ServiceBasedPdfExportPageEvent;\r
 import org.simantics.modeling.requests.CollectionRequest;\r
 import org.simantics.modeling.requests.CollectionResult;\r
 import org.simantics.modeling.requests.Node;\r
@@ -115,6 +116,7 @@ public class DiagramPrinter {
                     document = new Document(pageSize);\r
                     writer = PdfWriter.getInstance(document, new FileOutputStream(exportPath));\r
                    writer.setPdfVersion(PdfWriter.PDF_VERSION_1_7);\r
+                   writer.setPageEvent(new ServiceBasedPdfExportPageEvent());\r
                    if ( exportPlan.attachTG ) {\r
                        writer.addViewerPreference(PdfName.USEATTACHMENTS, PdfBoolean.PDFTRUE);\r
                    }\r