1   package org.wcb.gui.renderer;
2   
3   import org.wcb.model.vo.TotalLogEntriesVO;
4   
5   import javax.swing.*;
6   import javax.swing.table.TableCellRenderer;
7   import java.awt.*;
8   import java.text.DecimalFormat;
9   
10  /**
11   * <small>
12   * Copyright (c)  2006  wbogaardt.
13   * This library is free software; you can redistribute it and/or
14   * modify it under the terms of the GNU Lesser General Public
15   * License as published by the Free Software Foundation; either
16   * version 2.1 of the License, or (at your option) any later version.
17   *
18   * This library is distributed in the hope that it will be useful,
19   * but WITHOUT ANY WARRANTY; without even the implied warranty of
20   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21   * Lesser General Public License for more details.
22   *
23   * You should have received a copy of the GNU Lesser General Public
24   * License along with this library; if not, write to the Free Software
25   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
26   * <p/>
27   * $File:  $ <br>
28   * $Change:  $ submitted by $Author: wbogaardt $ at $DateTime: Apr 6, 2006 8:50:23 AM $ <br>
29   * </small>
30   *
31   * @author wbogaardt
32   * @version 1
33   *          Date: Apr 6, 2006
34   *          Time: 8:50:23 AM
35   */
36  
37  public class ColorRowsTableCellRender extends JLabel implements TableCellRenderer {
38  
39      final DecimalFormat formatter = new DecimalFormat("###,##0.0");
40  
41      private Color focusColor = new Color(51,139,204); // default is a dark blue
42      private Color evenRowColor = new Color(200,223,240); // default is a light blue
43      private Color totalRowColor = new Color(0xfff695); //gray.
44      private Color oddRowColor = Color.WHITE; //default is white
45  
46      public ColorRowsTableCellRender()
47      {
48          this.setOpaque(true);
49      }
50  
51      /**
52       * When user selects a row this is the color
53       * it should be.
54       * @param color Selected row color default is Color(51,139,204)
55       */
56      public void setFocusColor(Color color) {
57          focusColor = color;
58      }
59  
60      /**
61       * Even Color row color.
62       * @param color Row color default is Color(200,223,240)
63       */
64      public void setEvenRowColor(Color color) {
65          evenRowColor = color;
66      }
67  
68      /**
69       * Color for odd rows.
70       * @param color Row colro default is Color.WHITE;
71       */
72      public void setOddRowColor(Color color) {
73          oddRowColor = color;
74      }
75  
76      public Component getTableCellRendererComponent(
77              JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
78      { // in the api
79          if(isSelected || hasFocus) {
80              setBackground(focusColor);
81          } else {
82              if(value instanceof TotalLogEntriesVO)
83              {
84                  this.setText(value.toString());
85                  setBackground(totalRowColor);
86                  return this;
87              }
88              if(row%2 == 0)
89              {
90                  setBackground(evenRowColor); // gray for even no.
91  
92              }
93              else
94              {
95                  setBackground(oddRowColor); // blue for odd no.
96              }
97          }
98          if(value != null) { // improves checking for values that may be blank.
99              // value is a double lets format it a bit to pretty it up for the user.
100             if (value instanceof Double) {
101                 this.setText(formatter.format(value));
102             }
103             else {
104                 this.setText(value.toString());
105             }
106         } else {
107             this.setText("-");
108         }
109         this.setFont(table.getFont());
110         return this;
111     }
112 }