View Javadoc

1   /**
2    *  Licensed under GPL. For more information, see
3    *    http://jaxodraw.sourceforge.net/license.html
4    *  or the LICENSE file in the jaxodraw distribution.
5    */
6   package net.sf.jaxodraw.plugin.pdf.itext;
7   
8   import com.lowagie.text.Document;
9   import com.lowagie.text.DocumentException;
10  //import com.lowagie.text.FontFactory;
11  import com.lowagie.text.Rectangle;
12  import com.lowagie.text.pdf.BaseFont;
13  //import com.lowagie.text.pdf.DefaultFontMapper;
14  import com.lowagie.text.pdf.FontMapper;
15  import com.lowagie.text.pdf.PdfContentByte;
16  import com.lowagie.text.pdf.PdfWriter;
17  
18  import java.awt.Font;
19  import java.awt.Graphics2D;
20  import java.awt.event.ActionEvent;
21  import java.awt.event.ActionListener;
22  
23  import java.io.File;
24  import java.io.FileOutputStream;
25  import java.io.IOException;
26  
27  import javax.swing.JButton;
28  import javax.swing.JComponent;
29  import javax.swing.JLabel;
30  import javax.swing.JPanel;
31  import javax.swing.JTextField;
32  
33  import net.sf.jaxodraw.io.JaxoPreview;
34  import net.sf.jaxodraw.plugin.JaxoExportPlugin;
35  import net.sf.jaxodraw.plugin.JaxoPluginExecutionException;
36  
37  
38  /** A plugin that exports JaxoGraphs to PDF. */
39  public class PDFExportJaxoPlugin extends JaxoExportPlugin {
40  
41      /** The unique plugin id. */
42      private static final String PLUGIN_ID = PDFExportJaxoPlugin.class.getName();
43  
44      /** The version number of this plugin. */
45      private static final String PLUGIN_VERSION = "1.0-0";
46  
47      /** The default file extension for the export format. */
48      private static final String EXTENSION = "pdf";
49  
50      /** The property key for the pdf.viewer property. */
51      private static final String PDF_VIEWER = "pdf.viewer";
52  
53      /** Default length for input text fields. */
54      private static final int TEXT_FIELD_LENGTH = 30;
55  
56        //
57       // internationalized strings
58      //
59  
60      /** The (internationalized) name of the export format. */
61      private String formatName;
62  
63      /** The (internationalized) description of the export format. */
64      private String fileDescription;
65  
66      /** The (internationalized) description of the plugin. */
67      private String description;
68  
69        //
70       // private fields
71      //
72  
73      /** The configuration panel. */
74      private JComponent panel;
75  
76      /** The text field for the pdf.viewer property. */
77      private JTextField pdfViewerTextField;
78  
79  
80  
81      /** Constructor: intialize internationalized strings. */
82      public PDFExportJaxoPlugin() {
83          registerDictionary(PDFExportJaxoPlugin.class);
84          initLang();
85      }
86  
87      /** {@inheritDoc} */
88      public final String pluginId() {
89          return PLUGIN_ID;
90      }
91  
92      /** {@inheritDoc} */
93      public String description() {
94          return description;
95      }
96  
97      /** {@inheritDoc} */
98      public String version() {
99          return PLUGIN_VERSION;
100     }
101 
102     /** {@inheritDoc} */
103     public final String getFormatName() {
104         return formatName;
105     }
106 
107     /** {@inheritDoc} */
108     public final String getFileExtension() {
109         return EXTENSION;
110     }
111 
112     /** {@inheritDoc} */
113     public final String getShortName() {
114         return EXTENSION;
115     }
116 
117     /** {@inheritDoc} */
118     public final String getFileExtensionDescription() {
119         return fileDescription;
120     }
121 
122     /** {@inheritDoc} */
123     public final boolean makeAvailableAtRuntime() {
124         try {
125             Class.forName("com.lowagie.text.pdf.PdfWriter");
126         } catch (ClassNotFoundException e) {
127             getLog().warn(
128                 "No itext classes found, PDF export will not be available!");
129             getLog().debug(e);
130             return false;
131         }
132 
133         return true;
134     }
135 
136     /** {@inheritDoc} */
137     public final void commitConfiguration() {
138         setProperty(PDF_VIEWER, pdfViewerTextField.getText().trim());
139     }
140 
141     /** {@inheritDoc} */
142     public final JComponent getConfigurationPanel() {
143         if (panel != null) {
144             return panel;
145         }
146 
147         pdfViewerTextField =
148             new JTextField(getProperty(PDF_VIEWER), TEXT_FIELD_LENGTH);
149 
150         JLabel pdfViewerLabel =
151             new JLabel(getLang().translate("PDFExportJaxoPlugin.PDF_viewer"));
152 
153         JButton saveButton = new JButton(getLang().translate("PDFExportJaxoPlugin.Save"));
154         saveButton.addActionListener(new ActionListener() {
155                     public void actionPerformed(final ActionEvent e) {
156                         commitConfiguration();
157                         storeProperties();
158                     }
159                 });
160 
161         panel = new JPanel();
162         panel.add(pdfViewerLabel);
163         panel.add(pdfViewerTextField);
164         panel.add(saveButton);
165 
166         return panel;
167     }
168 
169     /** {@inheritDoc} */
170     public final String getWarningForGraph() {
171         if (getGraph().containsLatexText()) {
172             return getLaTeXTextWarning();
173         }
174 
175         return null;
176     }
177 
178     /** {@inheritDoc} */
179     public final void updateLanguage() {
180         initLang();
181     }
182 
183     /** {@inheritDoc} */
184     protected final void exportTo(final String fileName)
185         throws JaxoPluginExecutionException {
186 
187         java.awt.Rectangle bb = getGraph().getBounds();
188 
189         if (bb == null) {
190             bb = new java.awt.Rectangle();
191         }
192 
193         int w = bb.width;
194         int h = bb.height;
195 
196         Document document = new Document(new Rectangle(w, h));
197 
198         try {
199             PdfWriter writer = PdfWriter.getInstance(document,
200                 new FileOutputStream(fileName));
201 
202             document.open();
203 
204             PdfContentByte cb = writer.getDirectContent();
205             FontMapper arialuni = new FontMapper() {
206 
207                 public BaseFont awtToPdf(Font font) {
208                     try {
209                         return BaseFont.createFont(font.getFontName(), BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
210                     } catch (DocumentException e) {
211                         e.printStackTrace();
212                     } catch (IOException e) {
213                         e.printStackTrace();
214                     }
215                     return null;
216                 }
217 
218                 public Font pdfToAwt(BaseFont font, int size) {
219                     return new java.awt.Font(font.getPostscriptFontName(),
220                             java.awt.Font.PLAIN, size);
221                 }
222 
223             };
224 
225             Graphics2D g2 = cb.createGraphics(w, h, arialuni);
226             //g2.setFont(arialuni.pdfToAwt(null, 12));
227             g2.translate(-bb.x, -bb.y);
228 
229             paintGraph(g2, true);
230 
231             g2.dispose();
232         } catch (DocumentException e) {
233             throw new JaxoPluginExecutionException(
234                 errorDialogMessage(fileName), e, this);
235         } catch (IOException e) {
236             throw new JaxoPluginExecutionException(
237                 errorDialogMessage(fileName), e, this);
238         }
239 
240         document.close();
241     }
242 
243     /**
244      * {@inheritDoc}
245      * sameWindow is not used yet.
246      */
247     public final void preview(final JaxoPreview p, final boolean sameWindow) {
248         String pdfViewer = getProperty(PDF_VIEWER);
249 
250         if (pdfViewer == null || pdfViewer.trim().length() == 0) {
251             showErrorDialog(getLang().translate("PDFExportJaxoPlugin.No_pdf_viewer_specified"));
252             return;
253         }
254 
255         String previewFileName = "Jaxo_tmp.pdf";
256         File previewFile = new File(JaxoPreview.USER_DIR, previewFileName);
257         previewFile.deleteOnExit();
258 
259         try {
260             exportTo(previewFileName);
261         } catch (JaxoPluginExecutionException e) {
262             setFailure(e); // this logs in debug mode
263             if (e.getMessage() != null) {
264                 showErrorDialog(e.getMessage());
265             }
266             return;
267         }
268 
269         p.setTitle(getLang().message("PDFExportJaxoPlugin.preview%0Title",
270             getShortName()));
271 
272         try {
273             Runtime.getRuntime().exec(new String[]{
274                     pdfViewer, previewFile.toString()
275                 });
276         } catch (IOException e) {
277             setFailure(e);
278         }
279     }
280 
281       //
282      // private methods
283     //
284 
285     /** Updates all internationalized strings to the current language. */
286     private void initLang() {
287         formatName = getLang().translate("PDFExportJaxoPlugin.formatName");
288         fileDescription = getLang().translate("PDFExportJaxoPlugin.fileDescription");
289         description = getLang().translate("PDFExportJaxoPlugin.description");
290     }
291 }