View Javadoc
1   /*
2   
3       dsh-identify  Lightweight components for identifiable beans.
4       Copyright (c) 2003-2019 held jointly by the individual authors.
5   
6       This library is free software; you can redistribute it and/or modify it
7       under the terms of the GNU Lesser General Public License as published
8       by the Free Software Foundation; either version 3 of the License, or (at
9       your option) any later version.
10  
11      This library is distributed in the hope that it will be useful, but WITHOUT
12      ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or
13      FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14      License for more details.
15  
16      You should have received a copy of the GNU Lesser General Public License
17      along with this library;  if not, write to the Free Software Foundation,
18      Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA.
19  
20      > http://www.fsf.org/licensing/licenses/lgpl.html
21      > http://www.opensource.org/licenses/lgpl-license.php
22  
23  */
24  package org.dishevelled.identify;
25  
26  import java.awt.Component;
27  import java.awt.Color;
28  
29  import java.util.List;
30  
31  import javax.swing.JLabel;
32  import javax.swing.JTable;
33  import javax.swing.UIManager;
34  
35  import javax.swing.table.DefaultTableCellRenderer;
36  
37  /**
38   * Stripe table cell renderer.
39   *
40   * @since 1.2
41   * @author  Michael Heuer
42   */
43  public class StripeTableCellRenderer extends DefaultTableCellRenderer
44  {
45      /** Background color for odd rows. */
46      private Color oddRowBackgroundColor = DEFAULT_ODD_ROW_BACKGROUND_COLOR;
47  
48      /** Default background color for odd rows, <code>2a5703, 5% alpha</code>. */
49      public static final Color DEFAULT_ODD_ROW_BACKGROUND_COLOR = new Color(42, 87, 3, 12); // 2a5703, 5% alpha
50  
51      /**
52       * Return the background color for odd rows. Defaults to DEFAULT_ODD_ROW_BACKGROUND_COLOR.
53       *
54       * @return the background color for odd rows
55       */
56      public final Color getOddRowBackgroundColor()
57      {
58          return oddRowBackgroundColor;
59      }
60  
61      /**
62       * Set the background color for odd rows to <code>oddRowBackgroundColor</code>.
63       *
64       * <p>This is a bound property.</p>
65       *
66       * @param oddRowBackgroundColor background color for odd rows, must not be null
67       */
68      public final void setOddRowBackgroundColor(final Color oddRowBackgroundColor)
69      {
70          if (oddRowBackgroundColor == null)
71          {
72              throw new IllegalArgumentException("oddRowBackgroundColor must not be null");
73          }
74          Color oldOddRowBackgroundColor = this.oddRowBackgroundColor;
75          this.oddRowBackgroundColor = oddRowBackgroundColor;
76          firePropertyChange("oddRowBackgroundColor", oldOddRowBackgroundColor, this.oddRowBackgroundColor);
77      }
78  
79      @Override
80      public Component getTableCellRendererComponent(final JTable table, final Object value, final boolean isSelected, final boolean hasFocus, final int row, final int column)
81      {
82          JLabel label = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
83  
84          if (isSelected)
85          {
86              label.setForeground(UIManager.getColor("Table.selectionForeground"));
87              label.setBackground(UIManager.getColor("Table.selectionBackground"));
88          }
89          else
90          {
91              if (hasFocus)
92              {
93                  label.setForeground(UIManager.getColor("Table.focusCellForeground"));
94              }
95              else
96              {
97                  label.setForeground(UIManager.getColor("Table.foreground"));
98              }
99              if (row % 2 == 1)
100             {
101                 label.setBackground(oddRowBackgroundColor);
102             }
103             else
104             {
105                 if (hasFocus)
106                 {
107                     label.setBackground(UIManager.getColor("Table.focusCellBackground"));
108                 }
109                 else
110                 {
111                     label.setBackground(UIManager.getColor("Table.background"));
112                 }
113             }
114         }
115         return label;
116     }
117 
118     /**
119      * Install a stripe table cell renderer for the specified table.
120      *
121      * @param table table, must not be null
122      */
123     public static void install(final JTable table)
124     {
125         if (table == null)
126         {
127             throw new IllegalArgumentException("table must not be null");
128         }
129         StripeTableCellRenderertml#StripeTableCellRenderer">StripeTableCellRenderer renderer = new StripeTableCellRenderer();
130         table.setDefaultRenderer(Boolean.class, renderer);
131         table.setDefaultRenderer(Double.class, renderer);
132         table.setDefaultRenderer(Float.class, renderer);
133         table.setDefaultRenderer(Integer.class, renderer);
134         table.setDefaultRenderer(List.class, renderer);
135         table.setDefaultRenderer(Long.class, renderer);
136         table.setDefaultRenderer(String.class, renderer);
137     }
138 }