Coverage Report - org.dishevelled.identify.IdMenu
 
Classes in this File Line Coverage Branch Coverage Complexity
IdMenu
70%
39/55
33%
6/18
1.909
 
 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.ComponentOrientation;
 27  
 
 28  
 import javax.swing.ImageIcon;
 29  
 import javax.swing.JMenu;
 30  
 
 31  
 import org.dishevelled.iconbundle.IconBundle;
 32  
 import org.dishevelled.iconbundle.IconSize;
 33  
 import org.dishevelled.iconbundle.IconState;
 34  
 import org.dishevelled.iconbundle.IconTextDirection;
 35  
 
 36  
 /**
 37  
  * An extension of JMenu that displays the name property value
 38  
  * and appropriate icon from an icon bundle for a given bean.
 39  
  *
 40  
  * @author  Michael Heuer
 41  
  * @version $Revision$ $Date$
 42  
  */
 43  
 public final class IdMenu
 44  
     extends JMenu
 45  
 {
 46  
     /** Default icon size. */
 47  1
     public static final IconSize DEFAULT_ICON_SIZE = IconSize.DEFAULT_16X16;
 48  
 
 49  
     /** Default icon text direction. */
 50  1
     private static final IconTextDirection DEFAULT_ICON_TEXT_DIRECTION = IconTextDirection.LEFT_TO_RIGHT;
 51  
 
 52  
     /** Icon size. */
 53  12
     private IconSize iconSize = DEFAULT_ICON_SIZE;
 54  
 
 55  
     /** Icon text direction. */
 56  12
     private IconTextDirection iconTextDirection = DEFAULT_ICON_TEXT_DIRECTION;
 57  
 
 58  
     /** Bound value property. */
 59  
     private Object value;
 60  
 
 61  
 
 62  
     /**
 63  
      * Create a new menu.
 64  
      */
 65  
     public IdMenu()
 66  
     {
 67  1
         this(null);
 68  1
     }
 69  
 
 70  
     /**
 71  
      * Create a new menu with the specified value.
 72  
      *
 73  
      * @param value value
 74  
      */
 75  
     public IdMenu(final Object value)
 76  
     {
 77  6
         super();
 78  6
         setValue(value);
 79  6
     }
 80  
 
 81  
     /**
 82  
      * Create a new menu item with the specified value and icon size.
 83  
      *
 84  
      * @param value value
 85  
      * @param iconSize icon size, must not be null
 86  
      */
 87  
     public IdMenu(final Object value, final IconSize iconSize)
 88  
     {
 89  6
         super();
 90  6
         setValue(value);
 91  
         // rebuilds again
 92  6
         setIconSize(iconSize);
 93  5
     }
 94  
 
 95  
 
 96  
     /**
 97  
      * Return the value for this menu.
 98  
      *
 99  
      * @return the value for this menu
 100  
      */
 101  
     public Object getValue()
 102  
     {
 103  0
         return value;
 104  
     }
 105  
 
 106  
     /**
 107  
      * Set the value for this menu to <code>value</code>.
 108  
      *
 109  
      * <p>This is a bound property.</p>
 110  
      *
 111  
      * @param value value for this menu
 112  
      */
 113  
     public void setValue(final Object value)
 114  
     {
 115  12
         Object oldValue = this.value;
 116  12
         this.value = value;
 117  12
         firePropertyChange("value", oldValue, this.value);
 118  12
         rebuild();
 119  12
     }
 120  
 
 121  
     /**
 122  
      * Return the icon size for this menu.
 123  
      *
 124  
      * @return the icon size for this menu
 125  
      */
 126  
     public IconSize getIconSize()
 127  
     {
 128  4
         return iconSize;
 129  
     }
 130  
 
 131  
     /**
 132  
      * Set the icon size for this menu to <code>iconSize</code>.
 133  
      *
 134  
      * <p>This is a bound property.</p>
 135  
      *
 136  
      * @param iconSize icon size, must not be null
 137  
      */
 138  
     public void setIconSize(final IconSize iconSize)
 139  
     {
 140  9
         if (iconSize == null)
 141  
         {
 142  2
             throw new IllegalArgumentException("iconSize must not be null");
 143  
         }
 144  7
         IconSize oldIconSize = this.iconSize;
 145  7
         this.iconSize = iconSize;
 146  
 
 147  7
         if (!this.iconSize.equals(oldIconSize))
 148  
         {
 149  4
             firePropertyChange("iconSize", oldIconSize, this.iconSize);
 150  4
             rebuild();
 151  
         }
 152  7
     }
 153  
 
 154  
     /**
 155  
      * Return the icon text direction for this menu.
 156  
      *
 157  
      * @return the icon text direction for this menu
 158  
      */
 159  
     public IconTextDirection getIconTextDirection()
 160  
     {
 161  0
         return iconTextDirection;
 162  
     }
 163  
 
 164  
     /** {@inheritDoc} */
 165  
     public void setComponentOrientation(final ComponentOrientation orientation)
 166  
     {
 167  0
         ComponentOrientation oldOrientation = getComponentOrientation();
 168  
 
 169  0
         if (!oldOrientation.equals(orientation))
 170  
         {
 171  0
             if (orientation != null)
 172  
             {
 173  0
                 iconTextDirection = orientation.isLeftToRight()
 174  
                     ? IconTextDirection.LEFT_TO_RIGHT : IconTextDirection.RIGHT_TO_LEFT;
 175  
 
 176  0
                 rebuild();
 177  
             }
 178  
         }
 179  
 
 180  0
         super.setComponentOrientation(orientation);
 181  0
     }
 182  
 
 183  
     /** {@inheritDoc} */
 184  
     public void applyComponentOrientation(final ComponentOrientation orientation)
 185  
     {
 186  0
         ComponentOrientation oldOrientation = getComponentOrientation();
 187  
 
 188  0
         if (!oldOrientation.equals(orientation))
 189  
         {
 190  0
             if (orientation != null)
 191  
             {
 192  0
                 iconTextDirection = orientation.isLeftToRight()
 193  
                     ? IconTextDirection.LEFT_TO_RIGHT : IconTextDirection.RIGHT_TO_LEFT;
 194  
 
 195  0
                 rebuild();
 196  
             }
 197  
         }
 198  
 
 199  0
         super.applyComponentOrientation(orientation);
 200  0
     }
 201  
 
 202  
     /**
 203  
      * Rebuild the text and icon properties of this menu
 204  
      * with the name property and icon bundle image of
 205  
      * <code>getValue()</code>, respectively.
 206  
      */
 207  
     private void rebuild()
 208  
     {
 209  16
         String name = IdentifyUtils.getNameFor(value);
 210  16
         setText(name);
 211  16
         IconBundle bndl = IdentifyUtils.getIconBundleFor(value);
 212  
 
 213  16
         if (bndl == null)
 214  
         {
 215  7
             setIcon(null);
 216  
         }
 217  
         else
 218  
         {
 219  9
             setIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.NORMAL, iconSize)));
 220  9
             setPressedIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.ACTIVE, iconSize)));
 221  9
             setSelectedIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.SELECTED, iconSize)));
 222  9
             setRolloverIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.MOUSEOVER, iconSize)));
 223  9
             setRolloverSelectedIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.SELECTED, iconSize)));
 224  9
             setDisabledIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.DISABLED, iconSize)));
 225  
         }
 226  16
     }
 227  
 }