Index: project.xml
===================================================================
RCS file: /cvsroot/displaytag/displaytag-examples/project.xml,v
retrieving revision 1.11
diff -u -r1.11 project.xml
--- project.xml 17 Sep 2005 08:01:11 -0000 1.11
+++ project.xml 20 Oct 2005 03:51:46 -0000
@@ -61,6 +61,15 @@
+The purpose of this example is two-fold: 1) to demonstrate exports that mimic the HTML data presentation. What you
+see rendered in the Excel, PDF, and RTF formats is what you see rendered as HTML, 2) to demonstrate how easy it is
+to implement this by applying the template method pattern to render the main table and decorate it. Refer to the following source listings.
+\n "); //$NON-NLS-1$
+ buffer.append("\n "); //$NON-NLS-1$
+ buffer.append("\n "); //$NON-NLS-1$
+ buffer.append("\n "); //$NON-NLS-1$
+ buffer.append("\n" //$NON-NLS-1$
+ + city + " Total: \n"); //$NON-NLS-1$
+ buffer.append(total);
+ buffer.append(" \n \n\n "); //$NON-NLS-1$
+ }
+
+ /**
+ * Write the table grand total in HTML.
+ * @param total The table grand total.
+ */
+ protected void writeGrandTotal(double total)
+ {
+ StringBuffer buffer = this.getStringBuffer();
+ buffer.append(" \n \n "); //$NON-NLS-1$
+ buffer.append(" "); //$NON-NLS-1$
+ }
+}
Index: src/main/java/org/displaytag/sample/ItextTotalWrapper.java
===================================================================
RCS file: src/main/java/org/displaytag/sample/ItextTotalWrapper.java
diff -N src/main/java/org/displaytag/sample/ItextTotalWrapper.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ src/main/java/org/displaytag/sample/ItextTotalWrapper.java 1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,144 @@
+/**
+ * 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.sample;
+
+import org.displaytag.decorator.itext.DecoratesItext;
+
+import com.lowagie.text.BadElementException;
+import com.lowagie.text.Cell;
+import com.lowagie.text.Chunk;
+import com.lowagie.text.Element;
+import com.lowagie.text.Font;
+import com.lowagie.text.FontFactory;
+import com.lowagie.text.Table;
+
+/**
+ * Same idea implemented in ItextTableWriter applied to decorators.
+ *
+ * @see org.displaytag.render.ItextTableWriter
+ * @author Jorge L. Barroso
+ * @version $Revision: $ ($Author: jbarroso $)
+ */
+public class ItextTotalWrapper extends TotalWrapperTemplate implements DecoratesItext
+{
+ /**
+ * The iText table in which the totals are rendered.
+ */
+ private Table table;
+
+ /**
+ * The iText font used to render the totals.
+ */
+ private Font font;
+
+ /**
+ * Set the table required to render the totals line.
+ * @param table The table required to render the totals line.
+ * @see org.displaytag.decorator.itext.DecoratesItext#setTable(com.lowagie.text.Table)
+ */
+ public void setTable(Table table)
+ {
+ this.table = table;
+ }
+
+ /**
+ * Set the font required to render the totals line.
+ * @param font The font required to render the totals line.
+ * @see org.displaytag.decorator.itext.DecoratesItext#setFont(com.lowagie.text.Font)
+ */
+ public void setFont(Font font)
+ {
+ this.font = font;
+ }
+
+ /**
+ * Writes cell border at bottom of cell.
+ */
+ public String startRow()
+ {
+ this.table.setDefaultCellBorder(Cell.BOTTOM);
+ return null;
+ }
+
+ /**
+ * Writes the city total line.
+ * @param city City name.
+ * @param total City total.
+ */
+ protected void writeCityTotal(String city, double total)
+ {
+ this.writeTotal(city, total);
+ }
+
+ /**
+ * Writes the table grand total
+ * @param total Table grand total
+ */
+ protected void writeGrandTotal(double total)
+ {
+ this.writeTotal("Grand", total);
+ }
+
+ /**
+ * Writes a total line.
+ * @param value Total message.
+ * @param total Total number.
+ */
+ private void writeTotal(String value, double total)
+ {
+ if (assertRequiredState())
+ {
+ try
+ {
+ this.font = FontFactory.getFont(this.font.getFamilyname(),
+ this.font.size(), Font.BOLD, this.font.color());
+ table.addCell(this.getCell(""));
+ table.addCell(this.getCell(""));
+ table.addCell(this.getCell("-------------"));
+ table.addCell(this.getCell(""));
+ //new row
+ table.addCell(this.getCell(""));
+ table.addCell(this.getCell(value + " Total:"));
+ table.addCell(this.getCell(total+""));
+ table.addCell(this.getCell(""));
+ }
+ catch (BadElementException e)
+ {
+ }
+ }
+ }
+
+ /**
+ * Obtain a cell with the given value.
+ * @param value Value to display in the cell.
+ * @return A cell with the given value.
+ * @throws BadElementException if an error occurs while generating the cell.
+ */
+ private Cell getCell(String value) throws BadElementException
+ {
+ Cell cell = new Cell(new Chunk(value, this.font));
+ cell.setLeading(8);
+ cell.setHorizontalAlignment(Element.ALIGN_LEFT);
+ return cell;
+ }
+
+ /**
+ * Asserts that the table and font properties needed have been set by the client.
+ *
+ * @return true if the required properties are not null; false otherwise.
+ */
+ private boolean assertRequiredState()
+ {
+ return this.table != null && this.font != null;
+ }
+}
Index: src/main/java/org/displaytag/sample/TotalWrapperTemplate.java
===================================================================
RCS file: src/main/java/org/displaytag/sample/TotalWrapperTemplate.java
diff -N src/main/java/org/displaytag/sample/TotalWrapperTemplate.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ src/main/java/org/displaytag/sample/TotalWrapperTemplate.java 1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,105 @@
+/**
+ * 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.sample;
+
+import java.util.List;
+
+import org.displaytag.decorator.TableDecorator;
+
+/**
+ * Same idea implemented in TableWriterTemplate applied to decorators.
+ *
+ * @see org.displaytag.render.TableWriterTemplate
+ * @author Jorge L. Barroso
+ * @version $Revision: $ ($Author: jbarroso $)
+ */
+public abstract class TotalWrapperTemplate extends TableDecorator
+{
+ /**
+ * total amount.
+ */
+ private double grandTotal;
+
+ /**
+ * total amount for city.
+ */
+ private double cityTotal;
+
+ /**
+ * Obtain the "); //$NON-NLS-1$
+ buffer.append("Grand Total: "); //$NON-NLS-1$
+ buffer.append(total);
+ buffer.append(" StringBuffer used to build the totals line.
+ */
+ private StringBuffer buffer;
+
+ /**
+ * After every row completes we evaluate to see if we should be drawing a new total line and summing the results
+ * from the previous group.
+ * @return String
+ */
+ public final String finishRow()
+ {
+ int listindex = ((List) getDecoratedObject()).indexOf(this.getCurrentRowObject());
+ ReportableListObject reportableObject = (ReportableListObject) this.getCurrentRowObject();
+ String nextCity;
+
+ this.cityTotal += reportableObject.getAmount();
+ this.grandTotal += reportableObject.getAmount();
+
+
+ if (listindex == ((List) getDecoratedObject()).size() - 1)
+ {
+ nextCity = "XXXXXX"; // Last row hack, it's only a demo folks... //$NON-NLS-1$
+ }
+ else
+ {
+ nextCity = ((ReportableListObject) ((List) getDecoratedObject()).get(listindex + 1)).getCity();
+ }
+
+ this.buffer = new StringBuffer(1000);
+
+ // City subtotals...
+ if (!nextCity.equals(reportableObject.getCity()))
+ {
+ writeCityTotal(reportableObject.getCity(), this.cityTotal);
+ this.cityTotal = 0;
+ }
+
+ // Grand totals...
+ if (getViewIndex() == ((List) getDecoratedObject()).size() - 1)
+ {
+ writeGrandTotal(this.grandTotal);
+ }
+
+ return buffer.toString();
+ }
+
+ /**
+ * Render the city total in the appropriate format.
+ * @param city The city name to render.
+ * @param total The city total to render.
+ */
+ abstract protected void writeCityTotal(String city, double total);
+
+ /**
+ * Render the grand total in the appropriate format.
+ * @param total The grand total to render.
+ */
+ abstract protected void writeGrandTotal(double total);
+
+ /**
+ * Obtain the StringBuffer used to build the totals line.
+ * @return The StringBuffer used to build the totals line.
+ */
+ protected StringBuffer getStringBuffer()
+ {
+ return this.buffer;
+ }
+}
Index: src/main/webapp/example-new-export.jsp
===================================================================
RCS file: src/main/webapp/example-new-export.jsp
diff -N src/main/webapp/example-new-export.jsp
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ src/main/webapp/example-new-export.jsp 1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,116 @@
+Examples >"What You See Is What You Get" Exports
+
+
+
+ Sample footer
+
+
+
+
+
+
+
+Why would one want to do this? When typical business users are presented with the + displaytag export facility, they usually expect the exported Excel or PDF to + look just like the HTML in their browser; they expect a WYSIWYG rendering. Yes, even + when exporting to Excel, users tend to expect the same look, feel, and structure + of the rendered HTML, instead of raw data. +
++(Note: the model state in this example changes with every view request, such that the data shown will change +with every request, but the report's structure remains the same in all formats.) +
++ What this table shows: You have a List who's objects are sorted and grouped by column A, column B and + column C, so instead of repeating columns A, B over and over again, it does a + grouping of those columns, and only shows data in those columns when it changes. + Think of reports... We use the this display tag as a key part of our + reporting framework. +
++ Grouping is straight-forward, simply make sure that your list that you are + providing is sorted appropriately, then indicate the grouping + order via the group attribute of the column tags. +
+ ++Potential and upcoming enhancements (Exit-Alpha Features): +
+ paging is used. + The ui may look like this: + + Export options: Excel | PDF | RTF | From: [1] To: [1] (default to this page) + Export options: Excel | PDF | RTF | From: [1] To: [max] (default to all) + + Defaults to exporting what's on the page currently. Default would be + configurable through property. ++
+The purpose of this example is two-fold: 1) to demonstrate exports that mimic the HTML data presentation. What you +see rendered in the Excel, PDF, and RTF formats is what you see rendered as HTML, 2) to demonstrate how easy it is +to implement this by applying the template method pattern to render the main table and decorate it. Refer to the following source listings. +
+Why would one want to do this? When typical business users are presented with the + displaytag export facility, they usually expect the exported Excel or PDF to + look just like the HTML in their browser; they expect a WYSIWYG rendering. Yes, even + when exporting to Excel, users tend to expect the same look, feel, and structure + of the rendered HTML, instead of raw data. +
++(Note: the model state in this example changes with every view request, such that the data shown will change +with every request, but the report's structure remains the same in all formats.) +
++ What this table shows: You have a List who's objects are sorted and grouped by column A, column B and + column C, so instead of repeating columns A, B over and over again, it does a + grouping of those columns, and only shows data in those columns when it changes. + Think of reports... We use the this display tag as a key part of our + reporting framework. +
++ Grouping is straight-forward, simply make sure that your list that you are + providing is sorted appropriately, then indicate the grouping + order via the group attribute of the column tags. +
+ ++Potential and upcoming enhancements (Exit-Alpha Features): +
+ paging is used. + The ui may look like this: + + Export options: Excel | PDF | RTF | From: [1] To: [1] (default to this page) + Export options: Excel | PDF | RTF | From: [1] To: [max] (default to all) + + Defaults to exporting what's on the page currently. Default would be + configurable through property. ++