[displaytag-cvs] displaytag/src/main/java/org/displaytag/export DefaultItextExportView.java,NONE,1.1
Brought to you by:
fgiust
|
From: fabrizio g. <fg...@us...> - 2005-10-16 20:35:02
|
Update of /cvsroot/displaytag/displaytag/src/main/java/org/displaytag/export In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv18315/src/main/java/org/displaytag/export Modified Files: ExportViewFactory.java Added Files: DefaultItextExportView.java DefaultRtfExportView.java DefaultPdfExportView.java Log Message: committing DISPL-245 WYSIWYG Exports - from Jorge L. Barroso + some changes and fixes --- NEW FILE: DefaultItextExportView.java --- /** * Licensed under the Artistic License; you may not use this file * except in compliance with the License. * You may obtain a copy of the License at * * http://displaytag.sourceforge.net/license.html * * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. */ package org.displaytag.export; import java.io.OutputStream; import javax.servlet.jsp.JspException; import org.displaytag.Messages; import org.displaytag.exception.BaseNestableJspTagException; import org.displaytag.exception.SeverityEnum; import org.displaytag.model.TableModel; import org.displaytag.render.ItextTableWriter; import com.lowagie.text.Document; import com.lowagie.text.DocumentException; import com.lowagie.text.PageSize; import com.lowagie.text.Table; /** * Exporter using iText: subclasses export to any of the iText document types, such as PDF and RTF. * @author Jorge L. Barroso * @version $Revision$ ($Author$) */ public abstract class DefaultItextExportView implements BinaryExportView { /** * TableModel to render. */ private TableModel model; /** * @see org.displaytag.export.ExportView#setParameters(TableModel, boolean, boolean, boolean) */ public void setParameters(TableModel tableModel, boolean exportFullList, boolean includeHeader, boolean decorateValues) { this.model = tableModel; } /** * @see org.displaytag.export.BaseExportView#getMimeType() Meant to be overwritten by subclasses. * @return null */ public String getMimeType() { return null; } /** * @see org.displaytag.export.BinaryExportView#doExport(OutputStream) */ public void doExport(OutputStream out) throws JspException { try { Document document = new Document(PageSize.A4.rotate(), 60, 60, 40, 40); this.initItextWriter(document, out); document.open(); Table table = new Table(this.model.getNumberOfColumns()); ItextTableWriter writer = new ItextTableWriter(table, document); writer.writeTable(this.model, "-1"); document.add(table); document.close(); } catch (Exception e) { throw new ItextGenerationException(e); } } /** * Initializes the iText writer used by export view to write iText document, such as PDF or RTF iText writer. * @param document The iText document to be written. * @param out The output stream to which the document is written. * @throws DocumentException If something goes wrong during initialization. */ protected abstract void initItextWriter(Document document, OutputStream out) throws DocumentException; /** * Wraps iText-generated exceptions. * @author Fabrizio Giustina * @version $Revision$ ($Author$) */ static class ItextGenerationException extends BaseNestableJspTagException { /** * D1597A17A6. */ private static final long serialVersionUID = 899149338534L; /** * Instantiate a new PdfGenerationException with a fixed message and the given cause. * @param cause Previous exception */ public ItextGenerationException(Throwable cause) { super(DefaultItextExportView.class, Messages.getString("DefaultItextExportView.errorexporting"), cause); //$NON-NLS-1$ this.initCause(cause); } /** * @see org.displaytag.exception.BaseNestableJspTagException#getSeverity() */ public SeverityEnum getSeverity() { return SeverityEnum.ERROR; } } } --- NEW FILE: DefaultPdfExportView.java --- /** * Licensed under the Artistic License; you may not use this file * except in compliance with the License. * You may obtain a copy of the License at * * http://displaytag.sourceforge.net/license.html * * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. */ package org.displaytag.export; import java.io.IOException; import java.io.OutputStream; import com.lowagie.text.Document; import com.lowagie.text.DocumentException; import com.lowagie.text.pdf.BaseFont; import com.lowagie.text.pdf.PdfContentByte; import com.lowagie.text.pdf.PdfPTable; import com.lowagie.text.pdf.PdfPageEventHelper; import com.lowagie.text.pdf.PdfTemplate; import com.lowagie.text.pdf.PdfWriter; /** * PDF exporter using iText. * @author Jorge L. Barroso * @version $Revision$ ($Author$) */ public class DefaultPdfExportView extends DefaultItextExportView { /** * @see org.displaytag.export.BaseExportView#getMimeType() * @return "application/pdf" */ public String getMimeType() { return "application/pdf"; //$NON-NLS-1$ } /** * Initializes the PDF writer this export view uses to write the table document. * @param document The iText document to be written. * @param out The output stream to which the document is written. * @throws DocumentException If something goes wrong during initialization. */ protected void initItextWriter(Document document, OutputStream out) throws DocumentException { PdfWriter.getInstance(document, out).setPageEvent(new PageNumber()); } /** * Prints a page number at the bottom of each page. Based on * http://itextdocs.lowagie.com/examples/com/lowagie/examples/directcontent/pageevents/PageNumbersWatermark.java * @author Jorge L. Barroso * @version $Revision$ ($Author$) */ private static class PageNumber extends PdfPageEventHelper { /** * @see com.lowagie.text.pdf.PdfPageEventHelper#onEndPage(com.lowagie.text.pdf.PdfWriter, * com.lowagie.text.Document) */ public void onEndPage(PdfWriter writer, Document document) { /** The headertable. */ PdfPTable table = new PdfPTable(2); /** A template that will hold the total number of pages. */ PdfTemplate tpl = writer.getDirectContent().createTemplate(100, 100); /** The font that will be used. */ BaseFont helv = null; try { helv = BaseFont.createFont("Helvetica", BaseFont.WINANSI, false); } catch (DocumentException e) { } catch (IOException e) { } PdfContentByte cb = writer.getDirectContent(); cb.saveState(); // write the headertable table.setTotalWidth(document.right() - document.left()); table.writeSelectedRows(0, -1, document.left(), document.getPageSize().height() - 50, cb); // compose the footer String text = "Page " + writer.getPageNumber(); float textSize = helv.getWidthPoint(text, 12); float textBase = document.bottom() - 20; cb.beginText(); cb.setFontAndSize(helv, 12); float adjust = helv.getWidthPoint("0", 12); cb.setTextMatrix(document.right() - textSize - adjust, textBase); cb.showText(text); cb.endText(); cb.addTemplate(tpl, document.right() - adjust, textBase); cb.saveState(); } } } Index: ExportViewFactory.java =================================================================== RCS file: /cvsroot/displaytag/displaytag/src/main/java/org/displaytag/export/ExportViewFactory.java,v retrieving revision 1.13 retrieving revision 1.14 diff -C2 -d -r1.13 -r1.14 *** ExportViewFactory.java 10 Dec 2004 22:46:58 -0000 1.13 --- ExportViewFactory.java 16 Oct 2005 20:34:37 -0000 1.14 *************** *** 104,108 **** { log.warn(Messages.getString("ExportViewFactory.noclassdef" //$NON-NLS-1$ ! , new Object[]{name, viewClassName, e.getMessage()})); return; } --- 104,109 ---- { log.warn(Messages.getString("ExportViewFactory.noclassdef" //$NON-NLS-1$ ! , ! new Object[]{name, viewClassName, e.getMessage()})); return; } *************** *** 127,131 **** { log.warn(Messages.getString("ExportViewFactory.noclassdef" //$NON-NLS-1$ ! , new Object[]{name, viewClassName, e.getMessage()})); return; } --- 128,133 ---- { log.warn(Messages.getString("ExportViewFactory.noclassdef" //$NON-NLS-1$ ! , ! new Object[]{name, viewClassName, e.getMessage()})); return; } --- NEW FILE: DefaultRtfExportView.java --- /** * Licensed under the Artistic License; you may not use this file * except in compliance with the License. * You may obtain a copy of the License at * * http://displaytag.sourceforge.net/license.html * * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. */ package org.displaytag.export; import java.io.OutputStream; import com.lowagie.text.Document; import com.lowagie.text.rtf.RtfWriter2; /** * RTF exporter using iText. * @author Jorge L. Barroso * @version $Revision$ ($Author$) */ public class DefaultRtfExportView extends DefaultItextExportView { /** * @see org.displaytag.export.BaseExportView#getMimeType() * @return "application/rtf" */ public String getMimeType() { return "application/rtf"; //$NON-NLS-1$ } /** * Initializes the RTF writer this export view uses to write the table document. * @param document The iText document to be written. * @param out The output stream to which the document is written. */ protected void initItextWriter(Document document, OutputStream out) { RtfWriter2.getInstance(document, out); } } |