View Javadoc

1   /*
2    * XMLCV - Gestion de CV sous forme XML
3    * 
4    * Copyright (C) 2004-2005, Damien Raude-Morvan
5    * 
6    * This file is part of XMLCV.
7    *
8    * XMLCV is free software; you can redistribute it and/or modify
9    * it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation; either version 2 of the License, or
11   * (at your option) any later version.
12   *
13   * XMLCV is distributed in the hope that it will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   * GNU General Public License for more details.
17   *
18   * You should have received a copy of the GNU General Public License
19   * along with XMLCV; if not, write to the Free Software
20   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21   * 
22   * Contacts :
23   * - Sébastien Mathy <smathy@tuxfamily.org>
24   * - Emmanuel Berre <eberre@tuxfamily.org>
25   * - Damien Raude-Morvan <drazzib@drazzib.com>
26   */
27  package org.xmlcv.gui.tables;
28  
29  import java.awt.Color;
30  import java.awt.Component;
31  
32  import javax.swing.JTable;
33  import javax.swing.table.DefaultTableCellRenderer;
34  
35  /***
36   * Use the DefaultTableCellRenderer (or specific renderer of the cell) and just
37   * change background color between even and odd row.
38   * 
39   * If the row is selected, don't change background color.
40   * 
41   * @version $Revision: 114 $
42   * @author <a href="mailto:drazzib@drazzib.com">Damien Raude-Morvan</a>
43   */
44  public class RowColorRenderer extends DefaultTableCellRenderer {
45  
46  	private static Color EVEN = new Color(255, 255, 255);
47  
48  	private static Color ODD = new Color(225, 225, 255);
49  
50  	public Component getTableCellRendererComponent(JTable table, Object value,
51  			boolean isSelected, boolean hasFocus, int row, int column) {
52  
53  		// Get the real Renderer for this table.
54  		super.getTableCellRendererComponent(table, value, isSelected, hasFocus,
55  				row, column);
56  
57  		// If this is the selected element, don't change background
58  		if (isSelected) {
59  			this.setBackground(table.getSelectionBackground());
60  		} else {
61  			// Change color between even and odd row
62  			this.setBackground(row % 2 == 0 ? RowColorRenderer.EVEN
63  					: RowColorRenderer.ODD);
64  		}
65  
66  		return this;
67  	}
68  }