Source for org.jfree.ui.DateCellRenderer

   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:  * DateCellRenderer.java
  29:  * ---------------------
  30:  * (C) Copyright 2003, 2004, by Object Refinery Limited.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   -;
  34:  *
  35:  * $Id: DateCellRenderer.java,v 1.7 2007/11/02 17:50:36 taqua Exp $
  36:  *
  37:  * Changes (from 26-Oct-2001)
  38:  * --------------------------
  39:  * 24-Jul-2003 : Version 1 (DG);
  40:  *
  41:  */
  42: 
  43: package org.jfree.ui;
  44: 
  45: import java.awt.Component;
  46: import java.text.DateFormat;
  47: import javax.swing.JTable;
  48: import javax.swing.SwingConstants;
  49: import javax.swing.table.DefaultTableCellRenderer;
  50: 
  51: /**
  52:  * A table cell renderer that formats dates.
  53:  *
  54:  * @author David Gilbert
  55:  */
  56: public class DateCellRenderer extends DefaultTableCellRenderer {
  57: 
  58:     /** The formatter. */
  59:     private DateFormat formatter;
  60:     
  61:     /**
  62:      * Default constructor.
  63:      */
  64:     public DateCellRenderer() {
  65:         this(DateFormat.getDateTimeInstance());
  66:     }
  67:     
  68:     /**
  69:      * Creates a new renderer.
  70:      * 
  71:      * @param formatter  the formatter.
  72:      */
  73:     public DateCellRenderer(final DateFormat formatter) {
  74:         super();
  75:         this.formatter = formatter;
  76:         setHorizontalAlignment(SwingConstants.CENTER);
  77:     }
  78: 
  79:     /**
  80:      * Returns itself as the renderer. Supports the TableCellRenderer interface.
  81:      *
  82:      * @param table  the table.
  83:      * @param value  the data to be rendered.
  84:      * @param isSelected  a boolean that indicates whether or not the cell is 
  85:      *                    selected.
  86:      * @param hasFocus  a boolean that indicates whether or not the cell has 
  87:      *                  the focus.
  88:      * @param row  the (zero-based) row index.
  89:      * @param column  the (zero-based) column index.
  90:      *
  91:      * @return the component that can render the contents of the cell.
  92:      */
  93:     public Component getTableCellRendererComponent(final JTable table, 
  94:             final Object value, final boolean isSelected, 
  95:             final boolean hasFocus, final int row, final int column) {
  96: 
  97:         setFont(null);
  98:         if (value != null) {
  99:           setText(this.formatter.format(value));
 100:         }
 101:         else {
 102:           setText("");
 103:         }
 104:         if (isSelected) {
 105:             setBackground(table.getSelectionBackground());
 106:         }
 107:         else {
 108:             setBackground(null);
 109:         }
 110:         return this;
 111:     }
 112: 
 113: }