Coverage Report - org.dishevelled.identify.IdTableCellRenderer
 
Classes in this File Line Coverage Branch Coverage Complexity
IdTableCellRenderer
100%
26/26
87%
7/8
1.833
 
 1  
 /*
 2  
 
 3  
     dsh-identify  Lightweight components for identifiable beans.
 4  
     Copyright (c) 2003-2012 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.Image;
 27  
 import java.awt.Component;
 28  
 import java.awt.ComponentOrientation;
 29  
 
 30  
 import javax.swing.JTable;
 31  
 import javax.swing.JLabel;
 32  
 import javax.swing.ImageIcon;
 33  
 
 34  
 import javax.swing.table.DefaultTableCellRenderer;
 35  
 
 36  
 import org.dishevelled.iconbundle.IconSize;
 37  
 import org.dishevelled.iconbundle.IconState;
 38  
 import org.dishevelled.iconbundle.IconBundle;
 39  
 import org.dishevelled.iconbundle.IconTextDirection;
 40  
 
 41  
 /**
 42  
  * Table cell renderer that displays the name property value
 43  
  * and appropriate icon from an icon bundle for a given bean.
 44  
  *
 45  
  * @author  Michael Heuer
 46  
  * @version $Revision: 1059 $ $Date: 2012-01-03 14:03:02 -0600 (Tue, 03 Jan 2012) $
 47  
  */
 48  
 public class IdTableCellRenderer
 49  
     extends DefaultTableCellRenderer
 50  
 {
 51  
     /** Default icon size. */
 52  1
     public static final IconSize DEFAULT_ICON_SIZE = IconSize.DEFAULT_16X16;
 53  
 
 54  
     /** Icon size. */
 55  
     private IconSize iconSize;
 56  
 
 57  
     /** ImageIcon wrapper for image from icon bundle. */
 58  
     private transient ImageIcon imageIcon;
 59  
 
 60  
 
 61  
     /**
 62  
      * Create a new table cell renderer for identifiable beans with
 63  
      * the default icon size.
 64  
      *
 65  
      * @see #DEFAULT_ICON_SIZE
 66  
      */
 67  
     public IdTableCellRenderer()
 68  
     {
 69  3
         this(DEFAULT_ICON_SIZE);
 70  3
     }
 71  
 
 72  
     /**
 73  
      * Create a new table cell renderer for identifiable beans with
 74  
      * the specified icon size.
 75  
      *
 76  
      * @param iconSize icon size, must not be null
 77  
      */
 78  
     public IdTableCellRenderer(final IconSize iconSize)
 79  
     {
 80  6
         super();
 81  6
         setIconSize(iconSize);
 82  5
     }
 83  
 
 84  
 
 85  
     /**
 86  
      * Return the icon size for this table cell renderer.
 87  
      *
 88  
      * @return the icon size for this table cell renderer
 89  
      */
 90  
     public final IconSize getIconSize()
 91  
     {
 92  4
         return iconSize;
 93  
     }
 94  
 
 95  
     /**
 96  
      * Set the icon size for this table cell renderer to <code>iconSize</code>.
 97  
      *
 98  
      * @param iconSize icon size, must not be null
 99  
      */
 100  
     public final void setIconSize(final IconSize iconSize)
 101  
     {
 102  8
         if (iconSize == null)
 103  
         {
 104  1
             throw new IllegalArgumentException("iconSize must not be null");
 105  
         }
 106  7
         this.iconSize = iconSize;
 107  7
     }
 108  
 
 109  
     /** {@inheritDoc} */
 110  
     public Component getTableCellRendererComponent(final JTable table,
 111  
                                                    final Object value,
 112  
                                                    final boolean isSelected,
 113  
                                                    final boolean hasFocus,
 114  
                                                    final int row,
 115  
                                                    final int column)
 116  
     {
 117  20
         JLabel label = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
 118  
 
 119  20
         String name = IdentifyUtils.getNameFor(value);
 120  20
         label.setText(name);
 121  
 
 122  20
         IconBundle iconBundle = IdentifyUtils.getIconBundleFor(value);
 123  
 
 124  20
         if (iconBundle == null)
 125  
         {
 126  8
             label.setIcon(null);
 127  
         }
 128  
         else
 129  
         {
 130  12
             IconTextDirection textDirection = determineTextDirection(label);
 131  12
             Image image = iconBundle.getImage(label, textDirection, IconState.NORMAL, iconSize);
 132  
 
 133  12
             if (imageIcon == null)
 134  
             {
 135  1
                 imageIcon = new ImageIcon(image);
 136  
             }
 137  
             else
 138  
             {
 139  11
                 imageIcon.setImage(image);
 140  
             }
 141  
 
 142  12
             label.setIcon(imageIcon);
 143  
         }
 144  
 
 145  20
         return label;
 146  
     }
 147  
 
 148  
     /**
 149  
      * Determine a text direction from the component orientation of
 150  
      * the specified label.
 151  
      *
 152  
      * @param label label
 153  
      * @return a text direction for the component orientation of
 154  
      *    the specified label
 155  
      */
 156  
     private IconTextDirection determineTextDirection(final JLabel label)
 157  
     {
 158  12
         ComponentOrientation componentOrientation = label.getComponentOrientation();
 159  12
         return componentOrientation.isLeftToRight() ? IconTextDirection.LEFT_TO_RIGHT : IconTextDirection.RIGHT_TO_LEFT;
 160  
     }
 161  
 }