]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.export.core/src/org/simantics/export/core/pdf/ExportPdfFormat.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.export.core / src / org / simantics / export / core / pdf / ExportPdfFormat.java
1 package org.simantics.export.core.pdf;\r
2 \r
3 import java.io.File;\r
4 import java.io.FileNotFoundException;\r
5 import java.io.FileOutputStream;\r
6 import java.io.IOException;\r
7 import java.util.ArrayList;\r
8 import java.util.List;\r
9 import java.util.StringTokenizer;\r
10 import java.util.concurrent.atomic.AtomicBoolean;\r
11 import java.util.regex.Pattern;\r
12 \r
13 import org.eclipse.core.runtime.IProduct;\r
14 import org.eclipse.core.runtime.Platform;\r
15 import org.eclipse.core.runtime.preferences.DefaultScope;\r
16 import org.eclipse.core.runtime.preferences.InstanceScope;\r
17 import org.osgi.service.prefs.Preferences;\r
18 import org.simantics.databoard.Accessors;\r
19 import org.simantics.databoard.Bindings;\r
20 import org.simantics.databoard.Datatypes;\r
21 import org.simantics.databoard.accessor.RecordAccessor;\r
22 import org.simantics.databoard.accessor.UnionAccessor;\r
23 import org.simantics.databoard.accessor.error.AccessorConstructionException;\r
24 import org.simantics.databoard.accessor.error.AccessorException;\r
25 import org.simantics.databoard.accessor.error.ReferenceException;\r
26 import org.simantics.databoard.accessor.reference.ChildReference;\r
27 import org.simantics.databoard.accessor.reference.LabelReference;\r
28 import org.simantics.databoard.binding.mutable.Variant;\r
29 import org.simantics.databoard.forms.DataboardForm;\r
30 import org.simantics.databoard.type.DoubleType;\r
31 import org.simantics.databoard.type.RecordType;\r
32 import org.simantics.databoard.type.StringType;\r
33 import org.simantics.databoard.type.UnionType;\r
34 import org.simantics.export.core.ExportContext;\r
35 import org.simantics.export.core.error.ExportException;\r
36 import org.simantics.export.core.intf.FormatClass;\r
37 import org.simantics.export.core.manager.Content;\r
38 import org.simantics.export.core.util.ExporterUtils;\r
39 import org.simantics.utils.page.MarginUtils;\r
40 import org.simantics.utils.page.MarginUtils.Margin;\r
41 import org.simantics.utils.page.MarginUtils.Margins;\r
42 import org.simantics.utils.page.PageDesc;\r
43 import org.simantics.utils.page.PageOrientation;\r
44 \r
45 import com.lowagie.text.Document;\r
46 import com.lowagie.text.DocumentException;\r
47 import com.lowagie.text.FontFactory;\r
48 import com.lowagie.text.Rectangle;\r
49 import com.lowagie.text.pdf.DefaultFontMapper;\r
50 import com.lowagie.text.pdf.PdfBoolean;\r
51 import com.lowagie.text.pdf.PdfCopy;\r
52 import com.lowagie.text.pdf.PdfName;\r
53 import com.lowagie.text.pdf.PdfWriter;\r
54 \r
55 /**\r
56  * Represents the actions to Pdf File format in Export Core.\r
57  *\r
58  * @author toni.kalajainen@semantum.fi\r
59  */\r
60 public class ExportPdfFormat implements FormatClass {\r
61 \r
62         public static final Pattern PATTERN_PDF = Pattern.compile(".*\\.pdf$", Pattern.CASE_INSENSITIVE);\r
63         \r
64         // Options references\r
65         public static String S_PDF = "Portable Document Format (PDF)";\r
66         public static ChildReference P_PDF_TITLE = ChildReference.parsePath(S_PDF+"/Title");\r
67         public static ChildReference P_PDF_AUTHOR = ChildReference.parsePath(S_PDF+"/Author");\r
68         public static ChildReference P_PDF_SUBJECT = ChildReference.parsePath(S_PDF+"/Subject");\r
69         public static ChildReference P_PDF_KEYWORDS = ChildReference.parsePath(S_PDF+"/Keywords");\r
70         public static ChildReference P_PDF_COMPRESSION = ChildReference.parsePath(S_PDF+"/Compression");\r
71         \r
72         public static String S_SIGN = "Digital Signature";\r
73         public static LabelReference P_SIGN = new LabelReference( S_SIGN );\r
74         public static ChildReference P_SIGN_KEYSTORE = ChildReference.concatenate(P_SIGN, new LabelReference("Keystore") );     \r
75         public static ChildReference P_SIGN_KEYSTOREPASSWORD = ChildReference.concatenate(P_SIGN, new LabelReference("Keystore Password"));\r
76         public static ChildReference P_SIGN_PRIVATEKEYPASSWORD = ChildReference.concatenate(P_SIGN, new LabelReference("Private Key Password"));\r
77         public static ChildReference P_SIGN_LOCATION = ChildReference.concatenate(P_SIGN, new LabelReference("Sign Location")); \r
78         public static ChildReference P_SIGN_REASON = ChildReference.concatenate(P_SIGN, new LabelReference("Sign Reason"));     \r
79 \r
80         public static String S_PAGE = "Page Settings";\r
81         public static ChildReference P_PAGE = new LabelReference( S_PAGE );\r
82         public static ChildReference P_PAGE_PAPERSIZE = ChildReference.concatenate(P_PAGE, new LabelReference("Size"));\r
83         public static ChildReference P_PAGE_ORIENTATION = ChildReference.concatenate(P_PAGE, new LabelReference("Orientation"));\r
84         public static ChildReference P_PAGE_MARGIN_LEFT = ChildReference.concatenate(P_PAGE, new LabelReference("Margin Left"));\r
85         public static ChildReference P_PAGE_MARGIN_RIGHT = ChildReference.concatenate(P_PAGE, new LabelReference("Margin Right"));\r
86         public static ChildReference P_PAGE_MARGIN_TOP = ChildReference.concatenate(P_PAGE, new LabelReference("Margin Top"));\r
87         public static ChildReference P_PAGE_MARGIN_BOTTOM = ChildReference.concatenate(P_PAGE, new LabelReference("Margin Bottom"));\r
88         \r
89         // Preference page IDs\r
90     public static String PAGE_PLUGIN_ID = "org.simantics.modeling.ui";\r
91         \r
92     public static String P_DEFAULT_PAGE_SIZE        = "page.default.size";\r
93     \r
94     private static final AtomicBoolean fontFactoryInitialized = new AtomicBoolean();\r
95     \r
96     public static final RecordType pdfOptions;\r
97 \r
98     static {\r
99         pdfOptions = new RecordType();          \r
100         DoubleType mm = new DoubleType();\r
101         mm.setUnit("mm");\r
102         \r
103             // PDF metadata         \r
104         RecordType pdfRecord = new RecordType();\r
105             pdfRecord.addComponent("Title", Datatypes.STRING );\r
106             pdfRecord.addComponent("Author", Datatypes.STRING );\r
107             pdfRecord.addComponent("Subject", Datatypes.STRING );\r
108             pdfRecord.addComponent("Keywords", DataboardForm.TEXTBOX );     \r
109             // Formatter options \r
110             pdfRecord.addComponent("Compression", UnionType.newEnum("0 (No compression)", "1", "2", "3", "4", "5", "6", "7", "8", "9 (Best)"));\r
111             \r
112             // PDF Encryption\r
113         RecordType pdfSign = new RecordType();\r
114             StringType certificate = DataboardForm.fileOpenDialog("PKCS#12 keystore (.p12)", "*.p12", "PFX (.pfx)", "*.pfx"); //, "Privacy Enhanced Mail (.pem)", ".pem" \r
115             pdfSign.addComponent("Keystore", certificate);\r
116             pdfSign.addComponent("Keystore Password", DataboardForm.PASSWORD);\r
117             pdfSign.addComponent("Private Key Password", DataboardForm.PASSWORD);\r
118             pdfSign.addComponent("Sign Location", Datatypes.STRING);        \r
119             pdfSign.addComponent("Sign Reason", Datatypes.STRING);                  \r
120             \r
121             PageDesc[] descs = PageDesc.PDF_ITEMS;\r
122             String[] pageSizesStr = new String[ descs.length ];\r
123             for ( int i=0; i<descs.length; i++ ) pageSizesStr[i] = descs[i].getText();\r
124             UnionType pageSizes = UnionType.newEnum("A0", "A1", "A2", "A3", "A4", "A5", "A6", "A7", "A8", "A9", "A10");\r
125             UnionType orientation = UnionType.newEnum("Portrait", "Landscape");\r
126             RecordType pagePrefs = new RecordType();\r
127             pagePrefs.addComponent("Size", pageSizes);\r
128             pagePrefs.addComponent("Orientation", orientation);\r
129             pagePrefs.addComponent("Margin Left", mm);\r
130             pagePrefs.addComponent("Margin Right", mm);\r
131             pagePrefs.addComponent("Margin Top", mm);\r
132             pagePrefs.addComponent("Margin Bottom", mm);\r
133             //pagePrefs.metadata.put("style", "dialog");\r
134             \r
135             pdfOptions.addComponent(S_PDF, pdfRecord);\r
136             pdfOptions.addComponent(S_SIGN, pdfSign);\r
137             pdfOptions.addComponent(S_PAGE, pagePrefs);\r
138     }\r
139     \r
140         @Override\r
141         public RecordType options(ExportContext context) throws ExportException {\r
142                 return pdfOptions;              \r
143         }\r
144 \r
145         @Override\r
146         public Object createFile(ExportContext ctx, File outputFile, Variant options) throws ExportException {\r
147                 try {\r
148                         RecordAccessor ra = Accessors.getAccessor( options );\r
149                         \r
150             if (fontFactoryInitialized.compareAndSet(false, true)) {\r
151                 // Only register directories once.\r
152                 FontFactory.registerDirectories();\r
153             }\r
154             \r
155                         ExportPdfWriter writer = new ExportPdfWriter();\r
156                         \r
157                         Rectangle pageSize = null;\r
158                         writer.document = new Document(pageSize);\r
159                         writer.outputFile = outputFile;\r
160                         writer.fos = new FileOutputStream(writer.outputFile);\r
161                         writer.pdfCopy = new PdfCopy(writer.document, writer.fos); \r
162                         writer.pdfCopy.setPdfVersion(PdfWriter.PDF_VERSION_1_7);\r
163                         writer.fontMapper = new DefaultFontMapper();\r
164                         writer.options = options;\r
165                         writer.ctx = ctx;\r
166 \r
167                         String creator = getCreator();\r
168                         writer.document.addCreator(creator);\r
169 \r
170                         String s; \r
171                         s = (String) ra.getValue(P_PDF_TITLE, Bindings.STRING);\r
172                         if ( s!=null ) writer.document.addTitle(s);                     \r
173 \r
174                         s = (String) ra.getValue(P_PDF_AUTHOR, Bindings.STRING);\r
175                         if ( s!=null ) writer.document.addAuthor(s);\r
176 \r
177                         s = (String) ra.getValue(P_PDF_SUBJECT, Bindings.STRING);\r
178                         if ( s!=null ) writer.document.addSubject(s);\r
179 \r
180                         s = (String) ra.getValue(P_PDF_KEYWORDS, Bindings.STRING);\r
181                         if ( s!=null ) writer.document.addKeywords(s);\r
182 \r
183                         // Compression Level\r
184                         try {                   \r
185                         UnionAccessor ua = ra.getComponent(P_PDF_COMPRESSION);\r
186                         int tag = ua.getTag();                  \r
187                         writer.pdfCopy.setCompressionLevel( tag );\r
188                         writer.compressionLevel = tag;\r
189                 } catch (ReferenceException re) {                       \r
190                         }\r
191 \r
192                         // PageSize\r
193                         {                               \r
194                                 Margins margins = MarginUtils.MARGINS_10mm;\r
195                                 Double left = (Double) ra.getValue(P_PAGE_MARGIN_LEFT, Bindings.DOUBLE);\r
196                                 if ( left!=null ) margins = margins.withSide(Margins.LEFT, new Margin(0, 0, left));\r
197                                 Double right = (Double) ra.getValue(P_PAGE_MARGIN_RIGHT, Bindings.DOUBLE);\r
198                                 if ( right!=null ) margins = margins.withSide(Margins.RIGHT, new Margin(0, 0, right));\r
199                                 Double bottom = (Double) ra.getValue(P_PAGE_MARGIN_BOTTOM, Bindings.DOUBLE);\r
200                                 if ( bottom!=null ) margins = margins.withSide(Margins.BOTTOM, new Margin(0, 0, bottom));\r
201                                 Double top = (Double) ra.getValue(P_PAGE_MARGIN_TOP, Bindings.DOUBLE);\r
202                                 if ( top!=null ) margins = margins.withSide(Margins.TOP, new Margin(0, 0, top));\r
203                                 \r
204                                 String paperName = "A4";\r
205                                 try {                   \r
206                                 UnionAccessor ua = ra.getComponent(P_PAGE_PAPERSIZE);\r
207                                 int tag = ua.getTag();\r
208                                 paperName = ua.type().getComponent(tag).name;                           \r
209                         } catch (ReferenceException re) {                       \r
210                                 }\r
211                                 \r
212                                 PageOrientation pageOrientation = PageOrientation.Portrait;\r
213                                 try {                   \r
214                                 UnionAccessor ua = ra.getComponent(P_PAGE_ORIENTATION);\r
215                                 int tag = ua.getTag();\r
216                                 String str = ua.type().getComponent(tag).name;\r
217                                 pageOrientation = PageOrientation.valueOf( str );\r
218                         } catch (ReferenceException re) {                       \r
219                                 }\r
220                                 \r
221                                 PageDesc pd = PageDesc.getByName(paperName);\r
222                                 pd = pd.withOrientation(pageOrientation);\r
223                                 pd = pd.withMargins(margins);\r
224                                 writer.margins = margins;\r
225                                 writer.defaultPageDesc = pd;\r
226                                 writer.defaultRectangle = ExportPdfWriter.toRectangle( writer.defaultPageDesc );\r
227                                 writer.document.setPageSize( writer.defaultRectangle );\r
228                         }\r
229                         \r
230                         // Add Developer Extension - not used\r
231 //              PdfName companyName = new PdfName("SMTC");          \r
232 //          PdfDeveloperExtension ext = new PdfDeveloperExtension(companyName, PdfWriter.PDF_VERSION_1_7, 3);\r
233 //          writer.addDeveloperExtension( ext );\r
234                         \r
235                         \r
236             writer.document.open();\r
237             writer.cb = writer.pdfCopy.getDirectContent();\r
238                         \r
239                         return writer;\r
240                 } catch (FileNotFoundException e) {\r
241                         throw new ExportException( e );\r
242                 } catch (DocumentException e) {\r
243                         throw new ExportException( e );\r
244                 } catch (AccessorConstructionException e) {\r
245                         throw new ExportException( e );\r
246                 } catch (AccessorException e) {\r
247                         throw new ExportException( e );\r
248                 }\r
249         }\r
250 \r
251         @Override\r
252         public Object openFile(ExportContext context, File inputFile, Variant options) throws ExportException {\r
253                 try {\r
254                         return new ImportPdfReader(inputFile);\r
255                 } catch (IOException e) {\r
256                         throw new ExportException(e);\r
257                 }\r
258         }\r
259         \r
260         @Override\r
261         public List<String> validate(ExportContext context, Variant options) throws ExportException {\r
262                 List<String> problems = new ArrayList<String>();\r
263                 \r
264                 try {\r
265                         RecordAccessor ra = Accessors.getAccessor( options );\r
266 \r
267                         // Assert the keystore file exists, if set.\r
268                         String keystoreFileName = (String) ra.getValue(P_SIGN_KEYSTORE, Bindings.STRING);                                                       \r
269                         if ( keystoreFileName != null && !keystoreFileName.isEmpty() ) {\r
270                                 File keystoreFile = new File( keystoreFileName );\r
271                                 if ( !keystoreFile.exists() ) {\r
272                                         problems.add( "Keystore file was not found: "+keystoreFile );\r
273                                 }\r
274                         }\r
275                         \r
276                 } catch (AccessorConstructionException e) {\r
277                         throw new ExportException( e );\r
278                 } catch (AccessorException e) {\r
279                         throw new ExportException( e );\r
280                 }\r
281                 \r
282                 return problems;\r
283         }\r
284 \r
285         @Override\r
286         public void closeFile(ExportContext context, Object handle) throws ExportException {\r
287                 if ( handle instanceof ImportPdfReader ) {\r
288                         ImportPdfReader reader = (ImportPdfReader) handle;\r
289                         reader.close();\r
290                 }\r
291                 \r
292                 if ( handle instanceof ExportPdfWriter ) {\r
293                         ExportPdfWriter writer = (ExportPdfWriter) handle;\r
294         \r
295                         writer.close();\r
296                         \r
297                         try {\r
298                                 RecordAccessor ra = Accessors.getAccessor( writer.options );\r
299                                 \r
300                                 String keystoreFileName = (String) ra.getValue(P_SIGN_KEYSTORE, Bindings.STRING);                               \r
301                                 String keystorePassword = (String) ra.getValue(P_SIGN_KEYSTOREPASSWORD, Bindings.STRING);\r
302                                 String privateKeyPassword = (String) ra.getValue(P_SIGN_PRIVATEKEYPASSWORD, Bindings.STRING);\r
303                                 String signLocation = (String) ra.getValue(P_SIGN_LOCATION, Bindings.STRING);\r
304                                 String signReason = (String) ra.getValue(P_SIGN_REASON, Bindings.STRING);\r
305                                 \r
306                                 if ( keystoreFileName != null && !keystoreFileName.isEmpty() ) \r
307                                 {\r
308                                         File keystoreFile = new File( keystoreFileName );\r
309                                         if ( !keystoreFile.exists() ) {\r
310                                                 throw new ExportException( "Keystore file was not found: "+keystoreFile );\r
311                                         }\r
312                                         writer.sign(keystoreFile, keystorePassword, privateKeyPassword, signLocation, signReason);\r
313                                 }\r
314         \r
315                         } catch (AccessorConstructionException e) {\r
316                                 throw new ExportException( e.getClass().getName()+": "+e.getMessage() );\r
317                         } catch (AccessorException e) {\r
318                                 throw new ExportException( e.getClass().getName()+": "+e.getMessage() );\r
319                         }\r
320                         \r
321                         // Encrypt\r
322                     /*\r
323                     writer.setEncryption(\r
324                                 new Certificate[] {}, \r
325                                 new int[] {PdfWriter.ALLOW_FILL_IN|PdfWriter.ALLOW_PRINTING}, \r
326                                 PdfWriter.STANDARD_ENCRYPTION_128);\r
327                     //writer.setEncryption(PdfWriter.STANDARD_ENCRYPTION_128, "", "password", PdfWriter.ALLOW_FILL_IN|PdfWriter.ALLOW_PRINTING|PdfWriter.ALLOW_COPY|PdfWriter.ALLOW_ASSEMBLY);\r
328                         \r
329                         int permission = PdfWriter.ALLOW_FILL_IN|PdfWriter.ALLOW_PRINTING|PdfWriter.ALLOW_COPY|PdfWriter.ALLOW_ASSEMBLY;\r
330                         PdfEncryption crypto = new PdfEncryption();\r
331                         //for (Certificate c : chain) crypto.addRecipient(c, permission);\r
332                         //crypto.addRecipient(chain[2], permission);\r
333                 crypto.setCryptoMode(PdfWriter.ENCRYPTION_AES_128, 0);\r
334                         crypto.setupByEncryptionKey(key.getEncoded(), key.getEncoded().length*8);\r
335                 crypto.getEncryptionDictionary();\r
336                         */\r
337                 }\r
338         }\r
339 \r
340         @Override\r
341         public void addAttachment(ExportContext ctx, Object handle, List<Content> attachments) throws ExportException {\r
342                 ExportPdfWriter writer = (ExportPdfWriter) handle; \r
343 \r
344         writer.pdfCopy.addViewerPreference(PdfName.USEATTACHMENTS, PdfBoolean.PDFTRUE);\r
345                 for ( Content content : attachments ) {\r
346                         writer.addAttachment( content );\r
347                 }               \r
348         }\r
349         \r
350         @Override\r
351         public void savePref(Variant options, Preferences contentScopeNode, Preferences workbenchScopeNode) throws ExportException {\r
352         try {\r
353                         RecordAccessor ra = Accessors.getAccessor(options);                     \r
354                         String s; \r
355                         \r
356                         s = (String) ra.getValue(P_PDF_TITLE, Bindings.STRING);\r
357                         if ( s!=null ) contentScopeNode.put(P_PDF_TITLE.tail().toString(), s);\r
358 \r
359                         s = (String) ra.getValue(P_PDF_AUTHOR, Bindings.STRING);\r
360                         if ( s!=null ) workbenchScopeNode.put(P_PDF_AUTHOR.tail().toString(), s);\r
361 \r
362                         s = (String) ra.getValue(P_PDF_SUBJECT, Bindings.STRING);\r
363                         if ( s!=null ) contentScopeNode.put(P_PDF_SUBJECT.tail().toString(), s);\r
364 \r
365                         s = (String) ra.getValue(P_PDF_KEYWORDS, Bindings.STRING);\r
366                         if ( s!=null ) contentScopeNode.put(P_PDF_KEYWORDS.tail().toString(), s);\r
367 \r
368                         s = (String) ra.getValue(P_SIGN_KEYSTORE, Bindings.STRING);\r
369                         if ( s!=null ) workbenchScopeNode.put(P_SIGN_KEYSTORE.tail().toString(), s);\r
370 \r
371                         s = (String) ra.getValue(P_SIGN_KEYSTOREPASSWORD, Bindings.STRING);\r
372                         if ( s!=null ) workbenchScopeNode.put(P_SIGN_KEYSTOREPASSWORD.tail().toString(), s);\r
373 \r
374                         s = (String) ra.getValue(P_SIGN_PRIVATEKEYPASSWORD, Bindings.STRING);\r
375                         if ( s!=null ) workbenchScopeNode.put(P_SIGN_PRIVATEKEYPASSWORD.tail().toString(), s);\r
376 \r
377                         s = (String) ra.getValue(P_SIGN_LOCATION, Bindings.STRING);\r
378                         if ( s!=null ) workbenchScopeNode.put(P_SIGN_LOCATION.tail().toString(), s);\r
379 \r
380                         s = (String) ra.getValue(P_SIGN_REASON, Bindings.STRING);\r
381                         if ( s!=null ) workbenchScopeNode.put(P_SIGN_REASON.tail().toString(), s);\r
382                         \r
383                         try {                   \r
384                         UnionAccessor ua = ra.getComponent(P_PDF_COMPRESSION);\r
385                         int tag = ua.getTag();\r
386                         workbenchScopeNode.putInt(P_PDF_COMPRESSION.tail().toString(), tag);\r
387                 } catch (ReferenceException re) {                       \r
388                         }\r
389                         \r
390                         Double d = (Double) ra.getValue(P_PAGE_MARGIN_LEFT, Bindings.DOUBLE);\r
391                         if ( d!=null ) contentScopeNode.putDouble(P_PAGE_MARGIN_LEFT.tail().toString(), d);\r
392 \r
393                         d = (Double) ra.getValue(P_PAGE_MARGIN_RIGHT, Bindings.DOUBLE);\r
394                         if ( d!=null ) contentScopeNode.putDouble(P_PAGE_MARGIN_RIGHT.tail().toString(), d);\r
395                         \r
396                         d = (Double) ra.getValue(P_PAGE_MARGIN_BOTTOM, Bindings.DOUBLE);\r
397                         if ( d!=null ) contentScopeNode.putDouble(P_PAGE_MARGIN_BOTTOM.tail().toString(), d);\r
398 \r
399                         d = (Double) ra.getValue(P_PAGE_MARGIN_TOP, Bindings.DOUBLE);\r
400                         if ( d!=null ) contentScopeNode.putDouble(P_PAGE_MARGIN_TOP.tail().toString(), d);\r
401                         \r
402                         try {                   \r
403                         UnionAccessor ua = ra.getComponent(P_PAGE_PAPERSIZE);\r
404                         int tag = ua.getTag();\r
405                         contentScopeNode.putInt(P_PAGE_PAPERSIZE.tail().toString(), tag);\r
406                 } catch (ReferenceException re) {                       \r
407                         }\r
408                         \r
409                         try {                   \r
410                         UnionAccessor ua = ra.getComponent(P_PAGE_ORIENTATION);\r
411                         int tag = ua.getTag();\r
412                         contentScopeNode.putInt(P_PAGE_ORIENTATION.tail().toString(), tag);\r
413                 } catch (ReferenceException re) {                       \r
414                         }\r
415                         \r
416                 } catch (AccessorConstructionException e) {\r
417                         throw new ExportException(e);\r
418                 } catch (AccessorException e) {\r
419                         throw new ExportException(e);\r
420                 }\r
421         }\r
422 \r
423         @Override\r
424         public void loadPref(Variant options, Preferences contentScopeNode, Preferences workbenchScopeNode) throws ExportException {\r
425         try {\r
426                         RecordAccessor ra = Accessors.getAccessor(options);                     \r
427                         String s;\r
428                         \r
429                         s = contentScopeNode.get(P_PDF_TITLE.tail().toString(), null);\r
430                         if ( s!=null ) ra.setValue(P_PDF_TITLE, Bindings.STRING, s);\r
431                 \r
432                         s = workbenchScopeNode.get(P_PDF_AUTHOR.tail().toString(), null);\r
433                         if ( s!=null ) ra.setValue(P_PDF_AUTHOR, Bindings.STRING, s);\r
434                 \r
435                         s = contentScopeNode.get(P_PDF_SUBJECT.tail().toString(), null);\r
436                         if ( s!=null ) ra.setValue(P_PDF_SUBJECT, Bindings.STRING, s);\r
437                 \r
438                         s = contentScopeNode.get(P_PDF_KEYWORDS.tail().toString(), null);\r
439                         if ( s!=null ) ra.setValue(P_PDF_KEYWORDS, Bindings.STRING, s);\r
440                 \r
441                         s = workbenchScopeNode.get(P_SIGN_KEYSTORE.tail().toString(), null);\r
442                         if ( s!=null ) ra.setValue(P_SIGN_KEYSTORE, Bindings.STRING, s);\r
443                 \r
444                         s = workbenchScopeNode.get(P_SIGN_KEYSTOREPASSWORD.tail().toString(), null);\r
445                         if ( s!=null ) ra.setValue(P_SIGN_KEYSTOREPASSWORD, Bindings.STRING, s);\r
446                 \r
447                         s = workbenchScopeNode.get(P_SIGN_PRIVATEKEYPASSWORD.tail().toString(), null);\r
448                         if ( s!=null ) ra.setValue(P_SIGN_PRIVATEKEYPASSWORD, Bindings.STRING, s);\r
449 \r
450                         s = workbenchScopeNode.get(P_SIGN_LOCATION.tail().toString(), null);\r
451                         if ( s!=null ) ra.setValue(P_SIGN_LOCATION, Bindings.STRING, s);\r
452                         \r
453                         s = workbenchScopeNode.get(P_SIGN_REASON.tail().toString(), null);\r
454                         if ( s!=null ) ra.setValue(P_SIGN_REASON, Bindings.STRING, s);\r
455 \r
456                         int tag = workbenchScopeNode.getInt(P_PDF_COMPRESSION.tail().toString(), -1);\r
457                         if ( tag>=0 )\r
458                 try {                   \r
459                         UnionAccessor ua = ra.getComponent(P_PDF_COMPRESSION);\r
460                         ua.setComponentValue(tag, Bindings.VOID, null);\r
461                 } catch (ReferenceException re) {                       \r
462                         }\r
463                 \r
464                 \r
465 \r
466                         s = contentScopeNode.get(P_PAGE_MARGIN_LEFT.tail().toString(), null);\r
467                         if ( s!=null ) ra.setValue(P_PAGE_MARGIN_LEFT, Bindings.DOUBLE, Double.valueOf(s));\r
468                 \r
469                         s = contentScopeNode.get(P_PAGE_MARGIN_RIGHT.tail().toString(), null);\r
470                         if ( s!=null ) ra.setValue(P_PAGE_MARGIN_RIGHT, Bindings.DOUBLE, Double.valueOf(s));\r
471                 \r
472                         s = contentScopeNode.get(P_PAGE_MARGIN_TOP.tail().toString(), null);\r
473                         if ( s!=null ) ra.setValue(P_PAGE_MARGIN_TOP, Bindings.DOUBLE, Double.valueOf(s));\r
474                 \r
475                         s = contentScopeNode.get(P_PAGE_MARGIN_BOTTOM.tail().toString(), null);\r
476                         if ( s!=null ) ra.setValue(P_PAGE_MARGIN_BOTTOM, Bindings.DOUBLE, Double.valueOf(s));\r
477                         \r
478                         tag = contentScopeNode.getInt(P_PAGE_PAPERSIZE.tail().toString(), -1);\r
479                         if ( tag>=0 )\r
480                 try {                   \r
481                         UnionAccessor ua = ra.getComponent(P_PAGE_PAPERSIZE);\r
482                         ua.setComponentValue(tag, Bindings.VOID, null);\r
483                 } catch (ReferenceException re) {                       \r
484                         }\r
485                         \r
486                         tag = contentScopeNode.getInt(P_PAGE_ORIENTATION.tail().toString(), -1);\r
487                         if ( tag>=0 )\r
488                 try {                   \r
489                         UnionAccessor ua = ra.getComponent(P_PAGE_ORIENTATION);\r
490                         ua.setComponentValue(tag, Bindings.VOID, null);\r
491                 } catch (ReferenceException re) {                       \r
492                         }\r
493                 \r
494                 \r
495                 } catch (AccessorConstructionException e) {\r
496                         throw new ExportException(e);\r
497                 } catch (AccessorException e) {\r
498                         throw new ExportException(e);\r
499                 }\r
500         }\r
501 \r
502         @Override\r
503         public void fillDefaultPrefs( ExportContext ctx, Variant options) throws ExportException {\r
504         try {\r
505                         RecordAccessor ra = Accessors.getAccessor(options);     \r
506 \r
507                         Preferences instPrefs = InstanceScope.INSTANCE.getNode( PAGE_PLUGIN_ID );\r
508                         Preferences defaultPrefs = DefaultScope.INSTANCE.getNode( PAGE_PLUGIN_ID );\r
509                         \r
510                         String value = ExporterUtils.getPrefString(instPrefs, defaultPrefs, P_DEFAULT_PAGE_SIZE);\r
511                 if ( value != null ) {\r
512                         try {\r
513                     StringTokenizer tok = new StringTokenizer( value );\r
514                     String w = tok.nextToken().trim();\r
515                     String h = tok.nextToken().trim();\r
516                     String orientation = tok.nextToken().trim();\r
517                     if ( orientation!=null ) {\r
518                                 try {                                                           \r
519                                 UnionAccessor ua = ra.getComponent(P_PAGE_ORIENTATION);\r
520                                 int index = ua.type().getComponentIndex2(orientation);\r
521                                 if ( index<0 ) index = 1;\r
522                                 ua.setComponentValue(index, Bindings.VOID, new Object());\r
523                         } catch (ReferenceException re) {                                                                       \r
524                                 }\r
525                     }\r
526                     String centering = tok.nextToken().trim();\r
527                     String pagesize = tok.nextToken("\u0001").trim();\r
528                     ExporterUtils.setUnionValue(ra, P_PAGE_PAPERSIZE, pagesize);\r
529                     \r
530                     String margins = tok.nextToken().trim();                                \r
531                     if ( margins != null ) {\r
532                         String[] split = margins.split(";");\r
533                         if (split.length == 4) {\r
534                                 for (int i=0; i<4; i++) {\r
535                                         String[] tok2 = split[i].split(" ");\r
536                                         String controlRelative = tok2[0];\r
537                                         String controlAbsolute = tok2[1];\r
538                                         String diagramAbsolute = tok2[2];\r
539                                         \r
540                                         ChildReference ref = null;\r
541                                         switch (i) {\r
542                                                 case 0: ref = P_PAGE_MARGIN_TOP; break;\r
543                                                 case 1: ref = P_PAGE_MARGIN_BOTTOM; break;\r
544                                                 case 2: ref = P_PAGE_MARGIN_LEFT; break;\r
545                                                 case 3: ref = P_PAGE_MARGIN_RIGHT; break;\r
546                                         }\r
547                                         double margin_mm = Double.valueOf(diagramAbsolute);\r
548                                                 ra.setValue(ref, Bindings.DOUBLE, margin_mm);                                                           \r
549                                 }                                               \r
550                         }\r
551                     }\r
552                         } catch( Exception e ) {\r
553                                 // This is not so important that pdf export should fail.\r
554                                 e.printStackTrace();\r
555                         }\r
556 \r
557                 }\r
558                         \r
559                 String username = System.getProperty("user.name");\r
560                 if ( username != null ) ra.setValue(P_PDF_AUTHOR, Bindings.STRING, username);\r
561 \r
562                 ra.setValue(P_SIGN_REASON, Bindings.STRING, "Document Created");\r
563                 ExporterUtils.setUnionValue(ra, P_PDF_COMPRESSION, 9);          \r
564                 \r
565                 } catch (AccessorConstructionException e) {\r
566                         throw new ExportException(e);\r
567                 } catch (AccessorException e) {\r
568                         throw new ExportException(e);\r
569                 }\r
570         }       \r
571 \r
572         public static String getCreator() {\r
573                 String creator = null;\r
574                 IProduct product = Platform.getProduct();\r
575                 if (product != null) {\r
576                         creator = product.getDescription();\r
577                         if (creator == null) {\r
578                                 creator = product.getName();\r
579                         }\r
580                 }\r
581                 if (creator == null) {\r
582                         creator = "Simantics";\r
583                 }\r
584                 return creator;\r
585         }\r
586 \r
587         public static boolean isPdf(String filename) {\r
588                 return PATTERN_PDF.matcher(filename).matches();\r
589         }\r
590         \r
591         public static boolean isPdf(File file) {\r
592                 return PATTERN_PDF.matcher(file.getName()).matches();\r
593         }\r
594         \r
595 }\r