Save This Page
Home » pentaho-reporting-engine-classic-0.8.10 » org » jfree » report » demo » invoice » [javadoc | source]
    1   /**
    2    * ========================================
    3    * JFreeReport : a free Java report library
    4    * ========================================
    5    *
    6    * Project Info:  http://www.jfree.org/jfreereport/index.html
    7    * Project Lead:  Thomas Morgner (taquera@sherito.org);
    8    *
    9    * (C) Copyright 2000-2004, by Simba Management Limited and Contributors.
   10    *
   11    * This library is free software; you can redistribute it and/or modify it under the terms
   12    * of the GNU Lesser General Public License as published by the Free Software Foundation;
   13    * either version 2.1 of the License, or (at your option) any later version.
   14    *
   15    * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
   16    * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
   17    * See the GNU Lesser General Public License for more details.
   18    *
   19    * You should have received a copy of the GNU Lesser General Public License along with this
   20    * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
   21    * Boston, MA 02111-1307, USA.
   22    *
   23    * ------------------------------
   24    * InvoiceTableModel.java
   25    * ------------------------------
   26    * (C)opyright 2004, by Thomas Morgner and Contributors.
   27    *
   28    * Original Author:  Thomas Morgner;
   29    * Contributor(s):   David Gilbert (for Simba Management Limited);
   30    *
   31    * $Id: InvoiceTableModel.java,v 1.1.2.2 2004/04/05 16:49:34 taqua Exp $
   32    *
   33    * Changes 
   34    * -------------------------
   35    * 26.03.2004 : Initial version
   36    *  
   37    */
   38   
   39   package org.jfree.report.demo.invoice;
   40   
   41   import java.util.ArrayList;
   42   import java.util.Date;
   43   import javax.swing.table.AbstractTableModel;
   44   
   45   public class InvoiceTableModel extends AbstractTableModel
   46   {
   47     private static Class[] COLUMN_TYPES =
   48       {
   49         Invoice.class,
   50         String.class, String.class, String.class, 
   51         String.class, String.class, String.class,
   52         String.class, Date.class, String.class,
   53         String.class, String.class, String.class,
   54         Float.class, Integer.class
   55       };
   56     
   57     private static String[] COLUMN_NAMES =
   58       {
   59         "invoice",
   60         "customer.firstName", "customer.lastName", "customer.street",
   61         "customer.town", "customer.postalCode", "customer.country",
   62         "customer.salutation", "invoice.date", "invoice.number",
   63         "article.name", "article.number", "article.details",
   64         "article.price", "article.count"
   65       };
   66   
   67     private transient Invoice[] invoicePerRow;
   68     private transient Article []articlesPerRow;
   69   
   70     private ArrayList invoices;
   71     private int totalSize;
   72   
   73     public InvoiceTableModel ()
   74     {
   75       invoices = new ArrayList();
   76     }
   77   
   78     public void addInvoice (final Invoice invoice)
   79     {
   80       invoices.add (invoice);
   81       invalidateCaches();
   82       fireTableDataChanged();
   83     }
   84   
   85     public void removeInvoice (final Invoice invoice)
   86     {
   87       invoices.remove (invoice);
   88       invalidateCaches();
   89       fireTableDataChanged();
   90     }
   91   
   92     public Invoice getInvoice (final int invoice)
   93     {
   94       return (Invoice) invoices.get (invoice);
   95     }
   96   
   97     public void invalidateCaches()
   98     {
   99       int size = 0;
  100       for (int i = 0; i < invoices.size(); i++)
  101       {
  102         final Invoice inv = getInvoice(i);
  103         size += inv.getArticleCount();
  104       }
  105       this.totalSize = size;
  106       this.invoicePerRow = null;
  107       this.articlesPerRow = null;
  108     }
  109   
  110     /**
  111      * Returns the number of columns in the model. A <code>JTable</code> uses this method to
  112      * determine how many columns it should create and display by default.
  113      *
  114      * @return the number of columns in the model
  115      *
  116      * @see #getRowCount
  117      */
  118     public int getColumnCount ()
  119     {
  120       return COLUMN_NAMES.length;
  121     }
  122   
  123     /**
  124      * Returns the number of rows in the model. A <code>JTable</code> uses this method to
  125      * determine how many rows it should display.  This method should be quick, as it is
  126      * called frequently during rendering.
  127      *
  128      * @return the number of rows in the model
  129      *
  130      * @see #getColumnCount
  131      */
  132     public int getRowCount ()
  133     {
  134       return totalSize;
  135     }
  136   
  137     /**
  138      * Returns a default name for the column using spreadsheet conventions: A, B, C, ... Z,
  139      * AA, AB, etc.  If <code>column</code> cannot be found, returns an empty string.
  140      *
  141      * @param column the column being queried
  142      * @return a string containing the default name of <code>column</code>
  143      */
  144     public String getColumnName (final int column)
  145     {
  146       return COLUMN_NAMES[column];
  147     }
  148   
  149     /**
  150      * Returns <code>Object.class</code> regardless of <code>columnIndex</code>.
  151      *
  152      * @param columnIndex the column being queried
  153      * @return the Object.class
  154      */
  155     public Class getColumnClass (final int columnIndex)
  156     {
  157       return COLUMN_TYPES[columnIndex];
  158     }
  159   
  160     private void fillCache()
  161     {
  162       if (invoicePerRow != null && articlesPerRow != null)
  163       {
  164         // nothing to do...
  165         return;
  166       }
  167       // ensure that we have enough space ...
  168       this.invoicePerRow = new Invoice[totalSize];
  169       this.articlesPerRow = new Article[totalSize];
  170   
  171   
  172       int currentRow = 0;
  173       final int invoiceSize = invoices.size();
  174       for (int i = 0; i < invoiceSize; i++)
  175       {
  176         final Invoice inv = (Invoice) invoices.get (i);
  177         final int articleCount = inv.getArticleCount();
  178         for (int ac = 0; ac < articleCount; ac++)
  179         {
  180           invoicePerRow[currentRow] = inv;
  181           articlesPerRow[currentRow] = inv.getArticle(ac);
  182           currentRow += 1;
  183         }
  184       }
  185     }
  186   
  187     /**
  188      * Returns the value for the cell at <code>columnIndex</code> and
  189      * <code>rowIndex</code>.
  190      *
  191      * @param	rowIndex	the row whose value is to be queried
  192      * @param	columnIndex the column whose value is to be queried
  193      * @return	the value Object at the specified cell
  194      */
  195     public Object getValueAt (final int rowIndex, final int columnIndex)
  196     {
  197       // just make sure we can access the invoices by the array
  198       fillCache();
  199       final Invoice inv = invoicePerRow[rowIndex];
  200       final Article art = articlesPerRow[rowIndex];
  201   
  202       switch (columnIndex)
  203       {
  204         case 0: return inv;
  205         case 1: return inv.getCustomer().getFirstName();
  206         case 2: return inv.getCustomer().getLastName();
  207         case 3: return inv.getCustomer().getStreet();
  208         case 4: return inv.getCustomer().getTown();
  209         case 5: return inv.getCustomer().getPostalCode();
  210         case 6: return inv.getCustomer().getCountry();
  211         case 7: return inv.getCustomer().getSalutation();
  212         case 8: return inv.getDate();
  213         case 9: return inv.getInvoiceNumber();
  214         case 10: return art.getName();
  215         case 11: return art.getArticleNumber();
  216         case 12: return art.getArticleDetails();
  217         case 13: return new Float(art.getPrice());
  218         case 14: return new Integer(inv.getArticleCount());
  219       }
  220       throw new IndexOutOfBoundsException("ColumnIndex");
  221     }
  222   }

Save This Page
Home » pentaho-reporting-engine-classic-0.8.10 » org » jfree » report » demo » invoice » [javadoc | source]