Source for org.jfree.report.modules.misc.tablemodel.PrintableTableModel

   1: /**
   2:  * ========================================
   3:  * JFreeReport : a free Java report library
   4:  * ========================================
   5:  *
   6:  * Project Info:  http://reporting.pentaho.org/
   7:  *
   8:  * (C) Copyright 2000-2007, by Object Refinery Limited, Pentaho Corporation and Contributors.
   9:  *
  10:  * This library is free software; you can redistribute it and/or modify it under the terms
  11:  * of the GNU Lesser General Public License as published by the Free Software Foundation;
  12:  * either version 2.1 of the License, or (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  15:  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16:  * See the GNU Lesser General Public License for more details.
  17:  *
  18:  * You should have received a copy of the GNU Lesser General Public License along with this
  19:  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  20:  * Boston, MA 02111-1307, USA.
  21:  *
  22:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  23:  * in the United States and other countries.]
  24:  *
  25:  * ------------
  26:  * $Id: PrintableTableModel.java 3525 2007-10-16 11:43:48Z tmorgner $
  27:  * ------------
  28:  * (C) Copyright 2000-2005, by Object Refinery Limited.
  29:  * (C) Copyright 2005-2007, by Pentaho Corporation.
  30:  */
  31: package org.jfree.report.modules.misc.tablemodel;
  32: 
  33: import javax.swing.event.TableModelListener;
  34: import javax.swing.table.TableModel;
  35: 
  36: /**
  37:  * A tablemodel that allows to override the column names. This is usefull
  38:  * in internationalized environments, where the tablemodel returns diffent
  39:  * columnnames depending on the current locale.
  40:  *
  41:  * @author LordOfCode
  42:  */
  43: public class PrintableTableModel implements TableModel
  44: {
  45: 
  46:   /** The original TableModel. */
  47:   private TableModel model;
  48:   /**
  49:    * The column keys to retrieve the internationalized names from the
  50:    * ResourceBundle.
  51:    */
  52:   private String[] i18nKeys;
  53: 
  54: 
  55:   public PrintableTableModel(final TableModel source, final String[] keys)
  56:   {
  57:     model = source;
  58:     i18nKeys = keys;
  59:   }
  60: 
  61:   public int getColumnCount()
  62:   {
  63:     return model.getColumnCount();
  64:   }
  65: 
  66:   public int getRowCount()
  67:   {
  68:     return model.getRowCount();
  69:   }
  70: 
  71: 
  72:   public boolean isCellEditable(final int rowIndex, final int columnIndex)
  73:   {
  74:     return model.isCellEditable(rowIndex, columnIndex);
  75:   }
  76: 
  77:   public Class getColumnClass(final int columnIndex)
  78:   {
  79:     return model.getColumnClass(columnIndex);
  80:   }
  81: 
  82:   public Object getValueAt(final int rowIndex, final int columnIndex)
  83:   {
  84:     return model.getValueAt(rowIndex, columnIndex);
  85:   }
  86: 
  87:   public void setValueAt(final Object aValue, final int rowIndex, final int columnIndex)
  88:   {
  89:     model.setValueAt(aValue, rowIndex, columnIndex);
  90:   }
  91: 
  92:   /**
  93:    * Retrieves the internationalized column name from the string array.
  94:    *
  95:    * @see TableModel#getColumnName(int)
  96:    */
  97:   public String getColumnName(final int columnIndex)
  98:   {
  99:     if (columnIndex < i18nKeys.length)
 100:     {
 101:       final String columnName = i18nKeys[columnIndex];
 102:       if (columnName != null)
 103:       {
 104:         return columnName;
 105:       }
 106:     }
 107:     return model.getColumnName(columnIndex);
 108:   }
 109: 
 110:   public void addTableModelListener(final TableModelListener l)
 111:   {
 112:     model.addTableModelListener(l);
 113:   }
 114: 
 115:   public void removeTableModelListener(final TableModelListener l)
 116:   {
 117:     model.removeTableModelListener(l);
 118:   }
 119: }