001    /*
002     * Copyright (C) 2008-2010 by Holger Arndt
003     *
004     * This file is part of the Universal Java Matrix Package (UJMP).
005     * See the NOTICE file distributed with this work for additional
006     * information regarding copyright ownership and licensing.
007     *
008     * UJMP is free software; you can redistribute it and/or modify
009     * it under the terms of the GNU Lesser General Public License as
010     * published by the Free Software Foundation; either version 2
011     * of the License, or (at your option) any later version.
012     *
013     * UJMP is distributed in the hope that it will be useful,
014     * but WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016     * GNU Lesser General Public License for more details.
017     *
018     * You should have received a copy of the GNU Lesser General Public
019     * License along with UJMP; if not, write to the
020     * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
021     * Boston, MA  02110-1301  USA
022     */
023    
024    package org.ujmp.itext;
025    
026    import java.awt.Component;
027    import java.awt.Graphics2D;
028    import java.io.File;
029    import java.io.FileOutputStream;
030    import java.util.logging.Level;
031    import java.util.logging.Logger;
032    
033    import javax.swing.JFileChooser;
034    
035    import org.ujmp.core.UJMP;
036    import org.ujmp.core.enums.FileFormat;
037    import org.ujmp.gui.interfaces.CanRenderGraph;
038    
039    import com.itextpdf.text.Document;
040    import com.itextpdf.text.Rectangle;
041    import com.itextpdf.text.pdf.DefaultFontMapper;
042    import com.itextpdf.text.pdf.PdfContentByte;
043    import com.itextpdf.text.pdf.PdfTemplate;
044    import com.itextpdf.text.pdf.PdfWriter;
045    
046    public abstract class ExportPDF {
047    
048            private static final Logger logger = Logger.getLogger(ExportPDF.class
049                            .getName());
050    
051            public static final File selectFile() {
052                    return selectFile(null);
053            }
054    
055            public static final File selectFile(Component c) {
056                    JFileChooser chooser = new JFileChooser();
057                    chooser.setFileFilter(FileFormat.PDF.getFileFilter());
058                    int returnVal = chooser.showOpenDialog(c);
059                    if (returnVal == JFileChooser.APPROVE_OPTION) {
060                            File file = chooser.getSelectedFile();
061                            if (!file.getAbsolutePath().toLowerCase().endsWith(".pdf"))
062                                    file = new File(file.getAbsolutePath() + ".pdf");
063                            return file;
064                    }
065                    return null;
066            }
067    
068            public static final void save(File file, Component c) {
069                    save(file, c, c.getWidth(), c.getHeight());
070            }
071    
072            public static final void save(File file, Component c, int width, int height) {
073                    if (file == null) {
074                            logger.log(Level.WARNING, "no file selected");
075                            return;
076                    }
077                    if (c == null) {
078                            logger.log(Level.WARNING, "no component provided");
079                            return;
080                    }
081                    try {
082                            Document document = new Document(new Rectangle(width, height));
083                            PdfWriter writer = PdfWriter.getInstance(document,
084                                            new FileOutputStream(file.getAbsolutePath()));
085                            document.addAuthor("UJMP v" + UJMP.UJMPVERSION);
086                            document.open();
087                            PdfContentByte cb = writer.getDirectContent();
088                            PdfTemplate tp = cb.createTemplate(width, height);
089                            Graphics2D g2 = tp.createGraphics(width, height,
090                                            new DefaultFontMapper());
091                            if (c instanceof CanRenderGraph) {
092                                    ((CanRenderGraph) c).renderGraph(g2);
093                            } else {
094                                    c.paint(g2);
095                            }
096                            g2.dispose();
097                            cb.addTemplate(tp, 0, 0);
098                            document.close();
099                            writer.close();
100                    } catch (Exception e) {
101                            logger.log(Level.WARNING, "could not save PDF file", e);
102                    }
103            }
104    }