From 15f7f5152a1edff983e26a7b78b2cf95e2f19e57 Mon Sep 17 00:00:00 2001 From: Tuukka Lehtonen Date: Mon, 5 Sep 2016 23:08:56 +0300 Subject: [PATCH] Sync git svn branch with SVN repository r33173. refs #6475 --- .../META-INF/MANIFEST.MF | 2 + .../export/core/internal/Activator.java | 28 ++++ .../export/core/pdf/ExportPdfWriter.java | 1 + .../export/core/pdf/PdfExportPageEvent.java | 9 ++ .../pdf/ServiceBasedPdfExportPageEvent.java | 126 ++++++++++++++++++ .../META-INF/MANIFEST.MF | 3 +- .../modeling/ui/pdf/DiagramPrinter.java | 2 + 7 files changed, 170 insertions(+), 1 deletion(-) create mode 100644 bundles/org.simantics.export.core/src/org/simantics/export/core/internal/Activator.java create mode 100644 bundles/org.simantics.export.core/src/org/simantics/export/core/pdf/PdfExportPageEvent.java create mode 100644 bundles/org.simantics.export.core/src/org/simantics/export/core/pdf/ServiceBasedPdfExportPageEvent.java diff --git a/bundles/org.simantics.export.core/META-INF/MANIFEST.MF b/bundles/org.simantics.export.core/META-INF/MANIFEST.MF index a194cf133..eadcf7e08 100644 --- a/bundles/org.simantics.export.core/META-INF/MANIFEST.MF +++ b/bundles/org.simantics.export.core/META-INF/MANIFEST.MF @@ -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 index 000000000..e07fa5863 --- /dev/null +++ b/bundles/org.simantics.export.core/src/org/simantics/export/core/internal/Activator.java @@ -0,0 +1,28 @@ +package org.simantics.export.core.internal; + +import org.osgi.framework.BundleActivator; +import org.osgi.framework.BundleContext; + +/** + * @author Tuukka Lehtonen + * @since 1.22.2 + */ +public class Activator implements BundleActivator { + + private static BundleContext context; + + public static BundleContext getContext() { + return context; + } + + @Override + public void start(BundleContext context) throws Exception { + Activator.context = context; + } + + @Override + public void stop(BundleContext context) throws Exception { + Activator.context = null; + } + +} diff --git a/bundles/org.simantics.export.core/src/org/simantics/export/core/pdf/ExportPdfWriter.java b/bundles/org.simantics.export.core/src/org/simantics/export/core/pdf/ExportPdfWriter.java index 7a490e642..6318e5de2 100644 --- a/bundles/org.simantics.export.core/src/org/simantics/export/core/pdf/ExportPdfWriter.java +++ b/bundles/org.simantics.export.core/src/org/simantics/export/core/pdf/ExportPdfWriter.java @@ -399,6 +399,7 @@ public class ExportPdfWriter { this.pdfWriter = PdfWriter.getInstance(document, fos); this.pdfWriter.setPdfVersion(PdfWriter.PDF_VERSION_1_7); this.pdfWriter.setCompressionLevel( compressionLevel ); + this.pdfWriter.setPageEvent(new ServiceBasedPdfExportPageEvent()); this.document.open(); this.cb = this.pdfWriter.getDirectContent(); if (!this.document.newPage()) throw new ExportException("Failed to create new page."); 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 index 000000000..60573fa11 --- /dev/null +++ b/bundles/org.simantics.export.core/src/org/simantics/export/core/pdf/PdfExportPageEvent.java @@ -0,0 +1,9 @@ +package org.simantics.export.core.pdf; + +import com.lowagie.text.pdf.PdfPageEvent; + +/** + * @author Tuukka Lehtonen + */ +public interface PdfExportPageEvent extends PdfPageEvent { +} 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 index 000000000..c513654df --- /dev/null +++ b/bundles/org.simantics.export.core/src/org/simantics/export/core/pdf/ServiceBasedPdfExportPageEvent.java @@ -0,0 +1,126 @@ +/******************************************************************************* + * Copyright (c) 2016 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Semantum Oy - initial API and implementation + *******************************************************************************/ +package org.simantics.export.core.pdf; + +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.function.Consumer; + +import org.osgi.framework.BundleContext; +import org.osgi.framework.InvalidSyntaxException; +import org.osgi.framework.ServiceReference; +import org.simantics.Logger; +import org.simantics.export.core.internal.Activator; + +import com.lowagie.text.Document; +import com.lowagie.text.Paragraph; +import com.lowagie.text.Rectangle; +import com.lowagie.text.pdf.PdfPageEventHelper; +import com.lowagie.text.pdf.PdfWriter; + +/** + * @author Tuukka Lehtonen + * @since 1.22.2 + */ +public class ServiceBasedPdfExportPageEvent extends PdfPageEventHelper { + + Map events; + + public ServiceBasedPdfExportPageEvent() { + // Read all page event contributions from the OSGi service + events = collectEvents(); + } + + private static Map collectEvents() { + BundleContext context = Activator.getContext(); + Collection> serviceReferences; + try { + serviceReferences = context.getServiceReferences(PdfExportPageEvent.class, null); + } catch (InvalidSyntaxException e) { + e.printStackTrace(); + serviceReferences = Collections.emptyList(); + } + Map events = new HashMap<>(serviceReferences.size()); + for (ServiceReference reference : serviceReferences) { + PdfExportPageEvent event = context.getService(reference); + String eventName = event.toString(); + events.put(eventName, event); + } + return events; + } + + private void safeInvoke(String eventName, PdfExportPageEvent event, Consumer r) { + try { + r.accept(event); + } catch (Exception e) { + Logger.defaultLogError("Failed to invoke PdfExportPageEvent::" + eventName + " for " + event.toString(), e); + } + } + + @Override + public void onChapter(PdfWriter writer, Document document, float paragraphPosition, Paragraph title) { + events.values().forEach(e -> safeInvoke("onChapter", e, ee -> ee.onChapter(writer, document, paragraphPosition, title))); + } + + @Override + public void onChapterEnd(PdfWriter writer, Document document, float position) { + events.values().forEach(e -> safeInvoke("onChapterEnd", e, ee -> ee.onChapterEnd(writer, document, position))); + } + + @Override + public void onGenericTag(PdfWriter writer, Document document, Rectangle rect, String text) { + events.values().forEach(e -> safeInvoke("onGenericTag", e, ee -> ee.onGenericTag(writer, document, rect, text))); + } + + @Override + public void onOpenDocument(PdfWriter writer, Document document) { + events.values().forEach(e -> safeInvoke("onOpenDocument", e, ee -> ee.onOpenDocument(writer, document))); + } + + @Override + public void onCloseDocument(PdfWriter writer, Document document) { + events.values().forEach(e -> safeInvoke("onCloseDocument", e, ee -> ee.onCloseDocument(writer, document))); + } + + @Override + public void onParagraph(PdfWriter writer, Document document, float paragraphPosition) { + events.values().forEach(e -> safeInvoke("onParagraph", e, ee -> ee.onParagraph(writer, document, paragraphPosition))); + } + + @Override + public void onParagraphEnd(PdfWriter writer, Document document, float paragraphPosition) { + events.values().forEach(e -> safeInvoke("onParagraphEnd", e, ee -> ee.onParagraphEnd(writer, document, paragraphPosition))); + } + + @Override + public void onSection(PdfWriter writer, Document document, float paragraphPosition, int depth, Paragraph title) { + events.values().forEach(e -> safeInvoke("onSection", e, ee -> ee.onSection(writer, document, paragraphPosition, depth, title))); + } + + @Override + public void onSectionEnd(PdfWriter writer, Document document, float position) { + events.values().forEach(e -> safeInvoke("onSectionEnd", e, ee -> ee.onSectionEnd(writer, document, position))); + } + + @Override + public void onStartPage(PdfWriter writer, Document document) { + events.values().forEach(e -> safeInvoke("onStartPage", e, ee -> ee.onStartPage(writer, document))); + } + + @Override + public void onEndPage(PdfWriter writer, Document document) { + events.values().forEach(e -> safeInvoke("onEndPage", e, ee -> ee.onEndPage(writer, document))); + } + +} diff --git a/bundles/org.simantics.modeling.ui/META-INF/MANIFEST.MF b/bundles/org.simantics.modeling.ui/META-INF/MANIFEST.MF index 1b915e3a7..10ad43d9a 100644 --- a/bundles/org.simantics.modeling.ui/META-INF/MANIFEST.MF +++ b/bundles/org.simantics.modeling.ui/META-INF/MANIFEST.MF @@ -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, diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/pdf/DiagramPrinter.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/pdf/DiagramPrinter.java index ccdf57fbe..1b916fcb0 100644 --- a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/pdf/DiagramPrinter.java +++ b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/pdf/DiagramPrinter.java @@ -30,6 +30,7 @@ import org.simantics.db.layer0.util.SessionGarbageCollection; import org.simantics.db.management.ISessionContext; import org.simantics.document.DocumentSettings; import org.simantics.document.DocumentUtils; +import org.simantics.export.core.pdf.ServiceBasedPdfExportPageEvent; import org.simantics.modeling.requests.CollectionRequest; import org.simantics.modeling.requests.CollectionResult; import org.simantics.modeling.requests.Node; @@ -115,6 +116,7 @@ public class DiagramPrinter { document = new Document(pageSize); writer = PdfWriter.getInstance(document, new FileOutputStream(exportPath)); writer.setPdfVersion(PdfWriter.PDF_VERSION_1_7); + writer.setPageEvent(new ServiceBasedPdfExportPageEvent()); if ( exportPlan.attachTG ) { writer.addViewerPreference(PdfName.USEATTACHMENTS, PdfBoolean.PDFTRUE); } -- 2.47.1