Read and respond to this message at: 
https://sourceforge.net/forum/message.php?msg_id=6078162
By: chikkubhai

As for the following example, I wanted to include sub-totals and totals in the
PDF export, try it in your application and it just wouldn't. I wrote my own
class MyITextTotalWrapper.java as shown below showing how you can control what
you want to see in your PDF export. I also made some changes to my
DefaultItextExportView.java  doExport() method to remove and set the export
output formatting!

Another class for example, ItextTableWriter.java, decides what style and format
is applied and is probably the class you are looking for! Let me know if you
want more hints, I was even thinking of committing my changes or raise
a JIRA/update/patch kind of a thing so that other's could make use of it!

All of this was possible after getting inspired with the WYSIWYG kind of 
exporting
in the displaytable live examples section:

package displaytag.sample.decorators;

import java.text.MessageFormat;
import java.util.Iterator;
import java.util.List;

import org.displaytag.model.HeaderCell;

import com.lowagie.text.BadElementException;
import com.lowagie.text.Cell;
import com.lowagie.text.Chunk;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Table;

// Referenced classes of package org.displaytag.sample.decorators:
//            TotalWrapperTemplate

// This uses the existing  TotalTableDecorator which creates the subTotal
// and Total fields as required

public class MyITextTotalWrapper extends MyTotalTableDecoratorTemplate
                implements 
org.displaytag.render.ItextTableWriter.ItextDecorator {

        private Table table;
        private Font font;
        
        public MyITextTotalWrapper() {
        }

        public void setTable(Table table) {
                this.table = table;
        }
        
        public void setFont(Font font) {
                this.font = font;
        }
        
        // The type MyITextTotalWrapper must implement the inherited abstract 
method 
        // MyTotalTableDecoratorTemplate.createTotalRow(boolean)
        protected void createTotalRow(boolean grandTotal) {
                
                writeTotal(grandTotal);
                System.out.println("ITextTotalWrapper: inside createTotalRow() 
method");
                subTotals.clear();
        }

        private void writeTotal(Boolean grandTotal) {
                if (assertRequiredState())
                        try {
                                font = 
FontFactory.getFont(font.getFamilyname(), font.size(),
                                                1, font.color());
                                
                                List headerCells = 
tableModel.getHeaderCellList();
                                
                                // Iterate every header cell
                                for (Iterator it = headerCells.iterator(); 
it.hasNext(); )
                                {
                                        // Construct a new pdf cell element
                                        HeaderCell cell = (HeaderCell) 
it.next();
                                        Cell cellToBeAdded = getCell("");
                                        
                                        // If the headerCell is having an 
isTotaled
                                        if (cell.isTotaled())
                                        {
                                                // Get the propertyName
                                                String totalPropertyName = 
cell.getBeanPropertyName();
                                                
                                                // Get the total or sub-total 
values for the propertyName
                                                Object total = grandTotal ? 
grandTotals.get(totalPropertyName)
                                                                : 
subTotals.get(totalPropertyName);
                                                
                                                // Update the currentCell with 
its sub-total or total
                                                cellToBeAdded = getCell(total + 
"");
                                        }
                                        
                                        // As there must be one group by set 
with group = "1", it cannot
                                        // have the sub-total enabled on that 
field!
                                        // I think it is possible to do both on 
that field also such as
                                        // ytd_wages sub-total: 
ytd_wages_subTotalValue
                                        // I think this part over writes the 
possibility where the user
                                        // accidentally sets the total="true" 
along with group="1"
                                        // it will only show the label and not 
the total if it is a group by field
                                        
                                        // Adds the subTotal label or totalLabel
                                        // Under the group=1 property column
                                        if (groupPropertyName != null
                                                        && 
groupPropertyName.equals(cell.getBeanPropertyName()))
                                                cellToBeAdded = 
getCell((grandTotal ? totalLabel : MessageFormat.format(
                                                                subtotalLabel, 
new Object[] { previousValues
                                                                                
.get(groupPropertyName) })));
                                        
                                        // Finally add the cell for each 
iteration
                                        table.addCell(cellToBeAdded);
                                }
                                
                        } catch (BadElementException e) {
                                e.printStackTrace();
                }
        }

        private Cell getCell(String value) throws BadElementException {
                Cell cell = new Cell(new Chunk(value, font));
                cell.setLeading(8F);
                cell.setHorizontalAlignment(0);
                return cell;
        }

        private boolean assertRequiredState() {
                return table != null && font != null;
        }

}


______________________________________________________________________
You are receiving this email because you elected to monitor this forum.
To stop monitoring this forum, login to SourceForge.net and visit: 
https://sourceforge.net/forum/unmonitor.php?forum_id=249318

------------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It is the best place to buy or sell services for
just about anything Open Source.
http://p.sf.net/sfu/Xq1LFB
_______________________________________________
displaytag-user mailing list
displaytag-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/displaytag-user

Reply via email to