Coverage Report - org.dishevelled.identify.StripeTableCellRenderer
 
Classes in this File Line Coverage Branch Coverage Complexity
StripeTableCellRenderer
44%
15/34
58%
7/12
3
 
 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  12
 public class StripeTableCellRenderer extends DefaultTableCellRenderer
 44  
 {
 45  
     /** Background color for odd rows. */
 46  12
     private Color oddRowBackgroundColor = DEFAULT_ODD_ROW_BACKGROUND_COLOR;
 47  
 
 48  
     /** Default background color for odd rows, <code>2a5703, 5% alpha</code>. */
 49  2
     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  0
         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  0
         if (oddRowBackgroundColor == null)
 71  
         {
 72  0
             throw new IllegalArgumentException("oddRowBackgroundColor must not be null");
 73  
         }
 74  0
         Color oldOddRowBackgroundColor = this.oddRowBackgroundColor;
 75  0
         this.oddRowBackgroundColor = oddRowBackgroundColor;
 76  0
         firePropertyChange("oddRowBackgroundColor", oldOddRowBackgroundColor, this.oddRowBackgroundColor);
 77  0
     }
 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  40
         JLabel label = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
 83  
 
 84  40
         if (isSelected)
 85  
         {
 86  20
             label.setForeground(UIManager.getColor("Table.selectionForeground"));
 87  20
             label.setBackground(UIManager.getColor("Table.selectionBackground"));
 88  
         }
 89  
         else
 90  
         {
 91  20
             if (hasFocus)
 92  
             {
 93  10
                 label.setForeground(UIManager.getColor("Table.focusCellForeground"));
 94  
             }
 95  
             else
 96  
             {
 97  10
                 label.setForeground(UIManager.getColor("Table.foreground"));
 98  
             }
 99  20
             if (row % 2 == 1)
 100  
             {
 101  0
                 label.setBackground(oddRowBackgroundColor);
 102  
             }
 103  
             else
 104  
             {
 105  20
                 if (hasFocus)
 106  
                 {
 107  10
                     label.setBackground(UIManager.getColor("Table.focusCellBackground"));
 108  
                 }
 109  
                 else
 110  
                 {
 111  10
                     label.setBackground(UIManager.getColor("Table.background"));
 112  
                 }
 113  
             }
 114  
         }
 115  40
         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  0
         if (table == null)
 126  
         {
 127  0
             throw new IllegalArgumentException("table must not be null");
 128  
         }
 129  0
         StripeTableCellRenderer renderer = new StripeTableCellRenderer();
 130  0
         table.setDefaultRenderer(Boolean.class, renderer);
 131  0
         table.setDefaultRenderer(Double.class, renderer);
 132  0
         table.setDefaultRenderer(Float.class, renderer);
 133  0
         table.setDefaultRenderer(Integer.class, renderer);
 134  0
         table.setDefaultRenderer(List.class, renderer);
 135  0
         table.setDefaultRenderer(Long.class, renderer);
 136  0
         table.setDefaultRenderer(String.class, renderer);
 137  0
     }
 138  
 }