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