View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  package org.xnap.gui.table;
20  
21  import java.awt.*;
22  import javax.swing.*;
23  import javax.swing.table.*;
24  
25  import org.apache.log4j.Logger;
26  
27  /***
28   * Sets a different background color for every other row in the table.
29   */
30  public class ColoredTable extends JTable
31  {
32  	//--- Constant(s) ---
33  	
34  	//--- Data field(s) ---
35  	
36      private static Logger logger = Logger.getLogger(ColoredTable.class);
37  	
38  	private Color oddColor;
39  	private Color evenColor;
40  
41  	//--- Constructor(s) ---
42  
43  	public ColoredTable(TableModel dm, TableColumnModel cm)
44  	{
45  		super(dm, cm);
46  		updateColors();
47  	}
48  	
49      public ColoredTable()
50  	{
51  		updateColors();
52  	}
53  
54      //--- Method(s) ---
55  
56  	public Color getBackgroundColor(int row)
57  	{
58  		return (row % 2 == 1) ? oddColor : evenColor;
59  	}
60  
61  	private void updateColors()
62  	{
63  		evenColor = UIManager.getColor("Table.background");
64  		//Color odd = UIManager.getColor("Label.background");	
65  		oddColor = (evenColor != null) 
66  			? new Color(Math.max((int)(evenColor.getRed()   * 0.9), 0), 
67  						Math.max((int)(evenColor.getGreen() * 0.9), 0),
68  						Math.max((int)(evenColor.getBlue()  * 0.9), 0))
69  			: null;
70  	}
71  
72  	public void updateUI() 
73  	{
74  		super.updateUI();
75  		updateColors();
76  	}		
77  
78  	/***
79  	 * The idea for this implementation stems from limewire's
80  	 * JMultilineToolTipTreeTable, thanks guys.
81  	 */
82  	public Component prepareRenderer(TableCellRenderer renderer, int row,
83  									 int column)
84  	{
85  		Component c = super.prepareRenderer(renderer, row, column);
86  
87  		boolean isSelected = isCellSelected(row, column);
88  		boolean rowIsAnchor = 
89  			(selectionModel.getAnchorSelectionIndex() == row);
90  		boolean colIsAnchor =
91  			(columnModel.getSelectionModel().getAnchorSelectionIndex()
92  			 == column);
93  		boolean hasFocus = (rowIsAnchor && colIsAnchor) && hasFocus();
94  		if (!isSelected && !hasFocus) {
95  			c.setBackground(getBackgroundColor(row));
96  		}
97  		return c;
98  	}
99  }