Source for org.jfree.ui.NumberCellRenderer

   1: /* ========================================================================
   2:  * JCommon : a free general purpose class library for the Java(tm) platform
   3:  * ========================================================================
   4:  *
   5:  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
   6:  * 
   7:  * Project Info:  http://www.jfree.org/jcommon/index.html
   8:  *
   9:  * This library is free software; you can redistribute it and/or modify it 
  10:  * under the terms of the GNU Lesser General Public License as published by 
  11:  * the Free Software Foundation; either version 2.1 of the License, or 
  12:  * (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but 
  15:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
  16:  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
  17:  * License for more details.
  18:  *
  19:  * You should have received a copy of the GNU Lesser General Public
  20:  * License along with this library; if not, write to the Free Software
  21:  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
  22:  * USA.  
  23:  *
  24:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
  25:  * in the United States and other countries.]
  26:  * 
  27:  * -----------------------
  28:  * NumberCellRenderer.java
  29:  * -----------------------
  30:  * (C) Copyright 2000-2004, by Object Refinery Limited.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   -;
  34:  *
  35:  * $Id: NumberCellRenderer.java,v 1.7 2007/11/02 17:50:36 taqua Exp $
  36:  *
  37:  * Changes (from 26-Oct-2001)
  38:  * --------------------------
  39:  * 26-Oct-2001 : Changed package to com.jrefinery.ui.*;
  40:  * 11-Mar-2002 : Updated import statements (DG);
  41:  *
  42:  */
  43: 
  44: package org.jfree.ui;
  45: 
  46: import java.awt.Component;
  47: import java.text.NumberFormat;
  48: import javax.swing.JTable;
  49: import javax.swing.SwingConstants;
  50: import javax.swing.table.DefaultTableCellRenderer;
  51: 
  52: /**
  53:  * A table cell renderer that formats numbers with right alignment in each cell.
  54:  *
  55:  * @author David Gilbert
  56:  */
  57: public class NumberCellRenderer extends DefaultTableCellRenderer {
  58: 
  59:     /**
  60:      * Default constructor - builds a renderer that right justifies the 
  61:      * contents of a table cell.
  62:      */
  63:     public NumberCellRenderer() {
  64:         super();
  65:         setHorizontalAlignment(SwingConstants.RIGHT);
  66:     }
  67: 
  68:     /**
  69:      * Returns itself as the renderer. Supports the TableCellRenderer interface.
  70:      *
  71:      * @param table  the table.
  72:      * @param value  the data to be rendered.
  73:      * @param isSelected  a boolean that indicates whether or not the cell is 
  74:      *                    selected.
  75:      * @param hasFocus  a boolean that indicates whether or not the cell has 
  76:      *                  the focus.
  77:      * @param row  the (zero-based) row index.
  78:      * @param column  the (zero-based) column index.
  79:      *
  80:      * @return the component that can render the contents of the cell.
  81:      */
  82:     public Component getTableCellRendererComponent(final JTable table, 
  83:             final Object value, final boolean isSelected, 
  84:             final boolean hasFocus, final int row, final int column) {
  85: 
  86:         setFont(null);
  87:         final NumberFormat nf = NumberFormat.getNumberInstance();
  88:         if (value != null) {
  89:           setText(nf.format(value));
  90:         }
  91:         else {
  92:           setText("");
  93:         }
  94:         if (isSelected) {
  95:             setBackground(table.getSelectionBackground());
  96:         }
  97:         else {
  98:             setBackground(null);
  99:         }
 100:         return this;
 101:     }
 102: 
 103: }