]> gerrit.simantics Code Review - simantics/platform.git/blob
ce0c423e26ede5f1dfaeb7f9d3bdc18a1f768a7f
[simantics/platform.git] /
1 package org.simantics.document.linking.report.pdf;
2
3 import java.awt.Font;
4 import java.util.HashMap;
5 import java.util.Map;
6
7 import org.simantics.document.linking.report.Document;
8 import org.simantics.document.linking.report.DocumentElement;
9 import org.simantics.document.linking.report.DocumentItem;
10 import org.simantics.document.linking.report.DocumentTitlePage;
11 import org.simantics.document.linking.report.Table;
12 import org.simantics.document.linking.report.TableOfContents;
13 import org.simantics.document.linking.report.TextItem;
14 import org.simantics.document.linking.report.URLItem;
15 import org.simantics.document.linking.report.base.TextItemImpl;
16 import org.simantics.document.linking.report.base.URLItemImpl;
17 import org.simantics.utils.datastructures.Arrays;
18
19
20 /**
21  * PDF format reporter.
22  * 
23  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
24  *
25  */
26 public class PDFDocument implements Document {
27         private String filename;
28         
29         Map<TextSize, Font> fonts = new HashMap<TextSize, Font>();
30         
31         PDFPageStream contentStream;
32         PDFTable currentTable;
33         PDFTitlePage titlePage;
34         PDFTocElement toc;
35         
36         public PDFDocument(String filename) throws Exception {
37                 this.filename = filename;
38                 contentStream = new PDFPageStream();
39                 defaultFonts();
40                 contentStream.setDefaultFont(fonts.get(TextSize.SMALL));
41         }
42         
43         public PDFDocument(String filename, Font font) throws Exception {
44                 this.filename = filename;
45                 contentStream = new PDFPageStream();
46                 defaultFonts();
47                 fonts.put(TextSize.SMALL, font);
48         }
49         
50         public PDFDocument(String filename, Font font, Font headerFont) throws Exception {
51                 this.filename = filename;
52                 contentStream = new PDFPageStream();
53                 defaultFonts();
54                 fonts.put(TextSize.SMALL, font);
55                 fonts.put(TextSize.MEDIUM, headerFont);
56         }
57         
58         private void defaultFonts() {
59                 fonts.put(TextSize.TINY, new Font("Arial", Font.PLAIN, 6));
60                 fonts.put(TextSize.SMALL, new Font("Arial", Font.PLAIN, 8));
61                 fonts.put(TextSize.MEDIUM, new Font("Arial", Font.PLAIN, 10));
62                 fonts.put(TextSize.LARGE, new Font("Arial", Font.PLAIN, 16));
63                 fonts.put(TextSize.HUGE, new Font("Arial", Font.BOLD, 24));
64                 contentStream.setDefaultFont(fonts.get(TextSize.SMALL));
65         }
66         
67         Font getFont(TextSize size) {
68                 return fonts.get(size);
69         }
70         
71         @Override
72         public int getAvailableLines() {
73                 return contentStream.getAvailableLines();
74         }
75         
76         @Override
77         public int getCurrentLine() {
78                 return contentStream.getCurrentLine();
79         }
80         
81         public void close() throws Exception{
82                 if (toc != null) {
83                         // hackety hack.
84                         toc.create(contentStream, contentStream.getPages().get(0));
85                 }
86                 contentStream.close(filename);
87         }
88         
89         /* (non-Javadoc)
90          * @see org.simantics.document.linking.report.Document#nextPage()
91          */
92         @Override
93         public void nextPage() throws Exception{
94                 contentStream.nextPage();       
95         }
96         
97         @SuppressWarnings("unchecked")
98         @Override
99         public <T extends DocumentElement> T newElement(Class<T> cls, String...options) throws Exception {
100                 if (cls == Table.class) {
101                         return (T)newTable();
102                 } else if (cls == TableOfContents.class) {
103                         return (T)getOrCreateToc();
104                 } else if (cls == DocumentTitlePage.class) {
105                         if (titlePage != null)
106                                 throw new Exception("Report may contain only one title page");
107                         titlePage =  new PDFTitlePage(this);
108                         return (T)titlePage;
109                 }
110                 return null;
111         }
112         
113         @SuppressWarnings("unchecked")
114         @Override
115         public <T extends DocumentElement> T nextElement(Class<T> cls, String...options) throws Exception {
116                 if (cls == Table.class) {
117                         return (T)nextTable(Arrays.contains(options, Document.TOC));
118                 } else if (cls == TableOfContents.class) {
119                         return (T)getOrCreateToc();
120                 } 
121                 return null;
122         }
123         
124         @SuppressWarnings("unchecked")
125         @Override
126         public <T extends DocumentElement> T getCurrentElement(Class<T> cls) {
127                 if (cls == Table.class) {
128                         return (T)getCurrentTable();
129                 } else if (cls == TableOfContents.class) {
130                         return (T)toc;
131                 } else if (cls == DocumentTitlePage.class) {
132                         return (T)titlePage;
133                 }
134                 return null;
135         }
136         
137         @SuppressWarnings("unchecked")
138         @Override
139         public <T extends DocumentItem> T newItem(Class<T> cls, String... options)
140                         throws Exception {
141                 if (cls == TextItem.class)
142                         return (T)new TextItemImpl();
143                 if (cls == URLItem.class)
144                         return (T)new URLItemImpl();
145                 return null;
146         }       
147         
148         public Table nextTable(boolean id) throws Exception {
149                 if (currentTable!= null) {
150                         closeTable();
151                         currentTable = new PDFTable(currentTable);
152                         return currentTable;    
153                 } else {
154                         return newTable();
155                 }
156
157         }
158         
159         private void closeTable() throws Exception {
160                 if (currentTable != null && currentTable.currentLine > 0) {
161                         currentTable.setLinesVisible(false);
162                         currentTable.setHeaderVisible(false);
163                         currentTable.writeLine("");
164                 }
165         }
166         
167         
168         public Table newTable() throws Exception {
169                 closeTable();
170                 currentTable = new PDFTable(this,contentStream);
171                 return currentTable;
172         }
173         
174         
175         Table getCurrentTable() {
176                 return currentTable;
177         }
178         
179         TableOfContents getOrCreateToc() throws Exception{
180                 if (toc == null)
181                         toc = new PDFTocElement(this);
182                 return toc;
183         }
184         
185         
186
187
188 }