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