]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.export.core/src/org/simantics/export/core/pdf/ServiceBasedPdfExportPageEvent.java
ConstantLabelDecorationRule to avoid Display in creating FontDescriptors
[simantics/platform.git] / bundles / org.simantics.export.core / src / org / simantics / export / core / pdf / ServiceBasedPdfExportPageEvent.java
1 /*******************************************************************************
2  * Copyright (c) 2016 Association for Decentralized Information Management
3  * in 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  *     Semantum Oy - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.export.core.pdf;
13
14 import java.util.Collection;
15 import java.util.Collections;
16 import java.util.HashMap;
17 import java.util.Map;
18 import java.util.function.Consumer;
19
20 import org.osgi.framework.BundleContext;
21 import org.osgi.framework.InvalidSyntaxException;
22 import org.osgi.framework.ServiceReference;
23 import org.simantics.export.core.internal.Activator;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 import com.lowagie.text.Document;
28 import com.lowagie.text.Paragraph;
29 import com.lowagie.text.Rectangle;
30 import com.lowagie.text.pdf.PdfPageEventHelper;
31 import com.lowagie.text.pdf.PdfWriter;
32
33 /**
34  * @author Tuukka Lehtonen
35  * @since 1.22.2
36  */
37 public class ServiceBasedPdfExportPageEvent extends PdfPageEventHelper {
38     private static final Logger LOGGER = LoggerFactory.getLogger(ServiceBasedPdfExportPageEvent.class);
39
40         Map<String, PdfExportPageEvent> events;
41         
42         public ServiceBasedPdfExportPageEvent() {
43                 // Read all page event contributions from the OSGi service
44                 events = collectEvents();
45         }
46
47         private static Map<String, PdfExportPageEvent> collectEvents() {
48                 BundleContext context = Activator.getContext();
49                 Collection<ServiceReference<PdfExportPageEvent>> serviceReferences;
50                 try {
51                         serviceReferences = context.getServiceReferences(PdfExportPageEvent.class, null);
52                 } catch (InvalidSyntaxException e) {
53                         e.printStackTrace();
54                         serviceReferences = Collections.emptyList();
55                 }
56                 Map<String, PdfExportPageEvent> events = new HashMap<>(serviceReferences.size());
57                 for (ServiceReference<PdfExportPageEvent> reference : serviceReferences) {
58                         PdfExportPageEvent event = context.getService(reference);
59                         String eventName = event.toString();
60                         events.put(eventName, event);
61                 }
62                 return events;
63         }
64
65         private void safeInvoke(String eventName, PdfExportPageEvent event, Consumer<PdfExportPageEvent> r) {
66                 try {
67                         r.accept(event);
68                 } catch (Exception e) {
69                         LOGGER.error("Failed to invoke PdfExportPageEvent::" + eventName + " for " + event.toString(), e);
70                 }
71         }
72
73         @Override
74         public void onChapter(PdfWriter writer, Document document, float paragraphPosition, Paragraph title) {
75                 events.values().forEach(e -> safeInvoke("onChapter", e, ee -> ee.onChapter(writer, document, paragraphPosition, title)));
76         }
77
78         @Override
79         public void onChapterEnd(PdfWriter writer, Document document, float position) {
80                 events.values().forEach(e -> safeInvoke("onChapterEnd", e, ee -> ee.onChapterEnd(writer, document, position)));
81         }
82
83         @Override
84         public void onGenericTag(PdfWriter writer, Document document, Rectangle rect, String text) {
85                 events.values().forEach(e -> safeInvoke("onGenericTag", e, ee -> ee.onGenericTag(writer, document, rect, text)));
86         }
87
88         @Override
89         public void onOpenDocument(PdfWriter writer, Document document) {
90                 events.values().forEach(e -> safeInvoke("onOpenDocument", e, ee -> ee.onOpenDocument(writer, document)));
91         }
92
93         @Override
94         public void onCloseDocument(PdfWriter writer, Document document) {
95                 events.values().forEach(e -> safeInvoke("onCloseDocument", e, ee -> ee.onCloseDocument(writer, document)));
96         }
97
98         @Override
99         public void onParagraph(PdfWriter writer, Document document, float paragraphPosition) {
100                 events.values().forEach(e -> safeInvoke("onParagraph", e, ee -> ee.onParagraph(writer, document, paragraphPosition)));
101         }
102
103         @Override
104         public void onParagraphEnd(PdfWriter writer, Document document, float paragraphPosition) {
105                 events.values().forEach(e -> safeInvoke("onParagraphEnd", e, ee -> ee.onParagraphEnd(writer, document, paragraphPosition)));
106         }
107
108         @Override
109         public void onSection(PdfWriter writer, Document document, float paragraphPosition, int depth, Paragraph title) {
110                 events.values().forEach(e -> safeInvoke("onSection", e, ee -> ee.onSection(writer, document, paragraphPosition, depth, title)));
111         }
112
113         @Override
114         public void onSectionEnd(PdfWriter writer, Document document, float position) {
115                 events.values().forEach(e -> safeInvoke("onSectionEnd", e, ee -> ee.onSectionEnd(writer, document, position)));
116         }
117
118         @Override
119         public void onStartPage(PdfWriter writer, Document document) {
120                 events.values().forEach(e -> safeInvoke("onStartPage", e, ee -> ee.onStartPage(writer, document)));
121         }
122
123         @Override
124         public void onEndPage(PdfWriter writer, Document document) {
125                 events.values().forEach(e -> safeInvoke("onEndPage", e, ee -> ee.onEndPage(writer, document)));
126         }
127
128 }