Home » displaytag-1.1.1-src » org » displaytag » sample » decorators » [javadoc | source]

    1   /**
    2    * Licensed under the Artistic License; you may not use this file
    3    * except in compliance with the License.
    4    * You may obtain a copy of the License at
    5    *
    6    *      http://displaytag.sourceforge.net/license.html
    7    *
    8    * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
    9    * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
   10    * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
   11    */
   12   package org.displaytag.sample.decorators;
   13   
   14   import java.util.List;
   15   
   16   import org.apache.commons.lang.ObjectUtils;
   17   import org.displaytag.decorator.TableDecorator;
   18   import org.displaytag.sample.ReportableListObject;
   19   
   20   
   21   /**
   22    * Same idea implemented in TableWriterTemplate applied to decorators.
   23    * @see org.displaytag.render.TableWriterTemplate
   24    * @author Jorge L. Barroso
   25    * @version $Revision$ ($Author$)
   26    */
   27   public abstract class TotalWrapperTemplate extends TableDecorator
   28   {
   29   
   30       /**
   31        * total amount.
   32        */
   33       private double grandTotal;
   34   
   35       /**
   36        * total amount for city.
   37        */
   38       private double cityTotal;
   39   
   40       /**
   41        * Obtain the <code>StringBuffer</code> used to build the totals line.
   42        */
   43       private StringBuffer buffer;
   44   
   45       /**
   46        * After every row completes we evaluate to see if we should be drawing a new total line and summing the results
   47        * from the previous group.
   48        * @return String
   49        */
   50       public final String finishRow()
   51       {
   52           int listindex = ((List) getDecoratedObject()).indexOf(this.getCurrentRowObject());
   53           ReportableListObject reportableObject = (ReportableListObject) this.getCurrentRowObject();
   54           String nextCity = null;
   55   
   56           this.cityTotal += reportableObject.getAmount();
   57           this.grandTotal += reportableObject.getAmount();
   58   
   59           if (listindex != ((List) getDecoratedObject()).size() - 1)
   60           {
   61               nextCity = ((ReportableListObject) ((List) getDecoratedObject()).get(listindex + 1)).getCity();
   62           }
   63   
   64           this.buffer = new StringBuffer(1000);
   65   
   66           // City subtotals...
   67           if (!ObjectUtils.equals(nextCity, reportableObject.getCity()))
   68           {
   69               writeCityTotal(reportableObject.getCity(), this.cityTotal);
   70               this.cityTotal = 0;
   71           }
   72   
   73           // Grand totals...
   74           if (getViewIndex() == ((List) getDecoratedObject()).size() - 1)
   75           {
   76               writeGrandTotal(this.grandTotal);
   77           }
   78   
   79           return buffer.toString();
   80       }
   81   
   82       /**
   83        * Render the city total in the appropriate format.
   84        * @param city The city name to render.
   85        * @param total The city total to render.
   86        */
   87       abstract protected void writeCityTotal(String city, double total);
   88   
   89       /**
   90        * Render the grand total in the appropriate format.
   91        * @param total The grand total to render.
   92        */
   93       abstract protected void writeGrandTotal(double total);
   94   
   95       /**
   96        * Obtain the <code>StringBuffer</code> used to build the totals line.
   97        * @return The <code>StringBuffer</code> used to build the totals line.
   98        */
   99       protected StringBuffer getStringBuffer()
  100       {
  101           return this.buffer;
  102       }
  103   }

Home » displaytag-1.1.1-src » org » displaytag » sample » decorators » [javadoc | source]