Coverage Report - org.dishevelled.identify.IdTreeCellRenderer
 
Classes in this File Line Coverage Branch Coverage Complexity
IdTreeCellRenderer
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.JTree;
 31  
 import javax.swing.JLabel;
 32  
 import javax.swing.ImageIcon;
 33  
 
 34  
 import javax.swing.tree.DefaultTreeCellRenderer;
 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  
  * Tree 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 IdTreeCellRenderer
 49  
     extends DefaultTreeCellRenderer
 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 tree cell renderer for identifiable beans with
 63  
      * the default icon size.
 64  
      *
 65  
      * @see #DEFAULT_ICON_SIZE
 66  
      */
 67  
     public IdTreeCellRenderer()
 68  
     {
 69  3
         this(DEFAULT_ICON_SIZE);
 70  3
     }
 71  
 
 72  
     /**
 73  
      * Create a new tree cell renderer for identifiable beans with
 74  
      * the specified icon size.
 75  
      *
 76  
      * @param iconSize icon size, must not be null
 77  
      */
 78  
     public IdTreeCellRenderer(final IconSize iconSize)
 79  
     {
 80  6
         super();
 81  6
         setIconSize(iconSize);
 82  5
     }
 83  
 
 84  
 
 85  
     /**
 86  
      * Return the icon size for this tree cell renderer.
 87  
      *
 88  
      * @return the icon size for this tree cell renderer
 89  
      */
 90  
     public final IconSize getIconSize()
 91  
     {
 92  4
         return iconSize;
 93  
     }
 94  
 
 95  
     /**
 96  
      * Set the icon size for this tree 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 getTreeCellRendererComponent(final JTree tree,
 111  
                                                   final Object value,
 112  
                                                   final boolean isSelected,
 113  
                                                   final boolean isExpanded,
 114  
                                                   final boolean isLeaf,
 115  
                                                   final int row,
 116  
                                                   final boolean hasFocus)
 117  
     {
 118  55
         JLabel label = (JLabel) super.getTreeCellRendererComponent(tree, value, isSelected, isExpanded, isLeaf, row, hasFocus);
 119  
 
 120  55
         String name = IdentifyUtils.getNameFor(value);
 121  55
         label.setText(name);
 122  
 
 123  55
         IconBundle iconBundle = IdentifyUtils.getIconBundleFor(value);
 124  
 
 125  55
         if (iconBundle == null)
 126  
         {
 127  22
             label.setIcon(null);
 128  
         }
 129  
         else
 130  
         {
 131  33
             IconTextDirection textDirection = determineTextDirection(label);
 132  33
             Image image = iconBundle.getImage(label, textDirection, IconState.NORMAL, iconSize);
 133  
 
 134  33
             if (imageIcon == null)
 135  
             {
 136  1
                 imageIcon = new ImageIcon(image);
 137  
             }
 138  
             else
 139  
             {
 140  32
                 imageIcon.setImage(image);
 141  
             }
 142  33
             label.setIcon(imageIcon);
 143  
         }
 144  
 
 145  55
         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  33
         ComponentOrientation componentOrientation = label.getComponentOrientation();
 159  33
         return componentOrientation.isLeftToRight() ? IconTextDirection.LEFT_TO_RIGHT : IconTextDirection.RIGHT_TO_LEFT;
 160  
     }
 161  
 }