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