Coverage Report - org.dishevelled.identify.IdCheckBoxMenuItem
 
Classes in this File Line Coverage Branch Coverage Complexity
IdCheckBoxMenuItem
0%
0/71
0%
0/34
2.615
 
 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.Action;
 29  
 import javax.swing.ImageIcon;
 30  
 import javax.swing.JCheckBoxMenuItem;
 31  
 
 32  
 import org.dishevelled.iconbundle.IconBundle;
 33  
 import org.dishevelled.iconbundle.IconSize;
 34  
 import org.dishevelled.iconbundle.IconState;
 35  
 import org.dishevelled.iconbundle.IconTextDirection;
 36  
 
 37  
 /**
 38  
  * An extension of JCheckBoxMenuItem that displays the name property value
 39  
  * and appropriate icon from an icon bundle for an identifiable
 40  
  * action.
 41  
  *
 42  
  * @see IdentifiableAction
 43  
  *
 44  
  * @author  Michael Heuer
 45  
  */
 46  
 public final class IdCheckBoxMenuItem
 47  
     extends JCheckBoxMenuItem
 48  
 {
 49  
     /** Default icon size. */
 50  0
     public static final IconSize DEFAULT_ICON_SIZE = IconSize.DEFAULT_16X16;
 51  
 
 52  
     /** Default icon text direction. */
 53  0
     private static final IconTextDirection DEFAULT_ICON_TEXT_DIRECTION = IconTextDirection.LEFT_TO_RIGHT;
 54  
 
 55  
     /** Icon size. */
 56  0
     private IconSize iconSize = DEFAULT_ICON_SIZE;
 57  
 
 58  
     /** Icon text direction. */
 59  0
     private IconTextDirection iconTextDirection = DEFAULT_ICON_TEXT_DIRECTION;
 60  
 
 61  
     /** Dirty flag. */
 62  0
     private transient boolean dirty = true;
 63  
 
 64  
 
 65  
     /**
 66  
      * Create a new check box menu item with the specified identifiable action.
 67  
      *
 68  
      * @param action identifiable action, must not be null
 69  
      */
 70  
     public IdCheckBoxMenuItem(final IdentifiableAction action)
 71  
     {
 72  0
         super();
 73  
 
 74  0
         if (action == null)
 75  
         {
 76  0
             throw new IllegalArgumentException("action must not be null");
 77  
         }
 78  0
         setAction(action);
 79  0
     }
 80  
 
 81  
     /**
 82  
      * Create a new check box menu item with the specified identifiable action and state.
 83  
      *
 84  
      * @param action identifiable action, must not be null
 85  
      * @param state true if this check box menu item is selected
 86  
      */
 87  
     public IdCheckBoxMenuItem(final IdentifiableAction action, final boolean state)
 88  
     {
 89  0
         super();
 90  
 
 91  0
         if (action == null)
 92  
         {
 93  0
             throw new IllegalArgumentException("action must not be null");
 94  
         }
 95  0
         setAction(action);
 96  0
         setState(state);
 97  0
     }
 98  
 
 99  
     /**
 100  
      * Create a new check box menu item with the specified identifiable action
 101  
      * and icon size.
 102  
      *
 103  
      * @param action identifiable action, must not be null
 104  
      * @param iconSize icon size, must not be null
 105  
      */
 106  
     public IdCheckBoxMenuItem(final IdentifiableAction action, final IconSize iconSize)
 107  
     {
 108  0
         super();
 109  
 
 110  0
         if (action == null)
 111  
         {
 112  0
             throw new IllegalArgumentException("action must not be null");
 113  
         }
 114  0
         setIconSize(iconSize);
 115  0
         setAction(action);
 116  0
     }
 117  
 
 118  
     /**
 119  
      * Create a new check box menu item with the specified identifiable action,
 120  
      * icon size, and state.
 121  
      *
 122  
      * @param action identifiable action, must not be null
 123  
      * @param iconSize icon size, must not be null
 124  
      * @param state true if this check box menu item is selected
 125  
      */
 126  
     public IdCheckBoxMenuItem(final IdentifiableAction action, final IconSize iconSize, final boolean state)
 127  
     {
 128  0
         super();
 129  
 
 130  0
         if (action == null)
 131  
         {
 132  0
             throw new IllegalArgumentException("action must not be null");
 133  
         }
 134  0
         setIconSize(iconSize);
 135  0
         setAction(action);
 136  0
         setState(state);
 137  0
     }
 138  
 
 139  
 
 140  
     /**
 141  
      * Return the icon size for this check box menu item.
 142  
      *
 143  
      * @return the icon size for this check box menu item
 144  
      */
 145  
     public IconSize getIconSize()
 146  
     {
 147  0
         return iconSize;
 148  
     }
 149  
 
 150  
     /**
 151  
      * Set the icon size for this check box menu item to <code>iconSize</code>.
 152  
      *
 153  
      * <p>This is a bound property.</p>
 154  
      *
 155  
      * @param iconSize icon size, must not be null
 156  
      */
 157  
     public void setIconSize(final IconSize iconSize)
 158  
     {
 159  0
         if (iconSize == null)
 160  
         {
 161  0
             throw new IllegalArgumentException("iconSize must not be null");
 162  
         }
 163  0
         IconSize oldIconSize = this.iconSize;
 164  0
         this.iconSize = iconSize;
 165  
 
 166  0
         if (!this.iconSize.equals(oldIconSize))
 167  
         {
 168  0
             firePropertyChange("iconSize", oldIconSize, this.iconSize);
 169  0
             setDirty(true);
 170  
         }
 171  0
     }
 172  
 
 173  
     /**
 174  
      * Return the icon text direction for this check box menu item.
 175  
      *
 176  
      * @return the icon text direction for this check box menu item
 177  
      */
 178  
     public IconTextDirection getIconTextDirection()
 179  
     {
 180  0
         return iconTextDirection;
 181  
     }
 182  
 
 183  
     @Override
 184  
     public void setComponentOrientation(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
                 setDirty(true);
 196  
             }
 197  
         }
 198  
 
 199  0
         super.setComponentOrientation(orientation);
 200  0
     }
 201  
 
 202  
     @Override
 203  
     public void applyComponentOrientation(final ComponentOrientation orientation)
 204  
     {
 205  0
         ComponentOrientation oldOrientation = getComponentOrientation();
 206  
 
 207  0
         if (!oldOrientation.equals(orientation))
 208  
         {
 209  0
             if (orientation != null)
 210  
             {
 211  0
                 iconTextDirection = orientation.isLeftToRight()
 212  
                     ? IconTextDirection.LEFT_TO_RIGHT : IconTextDirection.RIGHT_TO_LEFT;
 213  
 
 214  0
                 setDirty(true);
 215  
             }
 216  
         }
 217  
 
 218  0
         super.applyComponentOrientation(orientation);
 219  0
     }
 220  
 
 221  
     @Override
 222  
     protected void configurePropertiesFromAction(final Action action)
 223  
     {
 224  0
         super.configurePropertiesFromAction(action);
 225  
 
 226  0
         if (isDirty())
 227  
         {
 228  0
             rebuild();
 229  
         }
 230  0
     }
 231  
 
 232  
     /**
 233  
      * Set the dirty flag to the logical OR of <code>dirty</code>
 234  
      * and the previous dirty state.
 235  
      *
 236  
      * @param dirty dirty flag
 237  
      */
 238  
     private void setDirty(final boolean dirty)
 239  
     {
 240  0
         this.dirty = (this.dirty || dirty);
 241  0
     }
 242  
 
 243  
     /**
 244  
      * Return true if a rebuild of the icons for this check box menu item is necessary.
 245  
      *
 246  
      * @return true if a rebuild of the icons for this check box menu item is necessary
 247  
      */
 248  
     private boolean isDirty()
 249  
     {
 250  0
         return dirty;
 251  
     }
 252  
 
 253  
     /**
 254  
      * Rebuild the icons for this check box menu item from the icon bundle
 255  
      * provided by the identifiable action for this check box menu item.
 256  
      */
 257  
     private void rebuild()
 258  
     {
 259  0
         Action action = getAction();
 260  0
         if ((action != null) && (action instanceof IdentifiableAction))
 261  
         {
 262  0
             IdentifiableAction identifiableAction = (IdentifiableAction) action;
 263  0
             IconBundle bndl = identifiableAction.getIconBundle();
 264  0
             setIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.NORMAL, iconSize)));
 265  0
             setPressedIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.ACTIVE, iconSize)));
 266  0
             setSelectedIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.SELECTED, iconSize)));
 267  0
             setRolloverIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.MOUSEOVER, iconSize)));
 268  0
             setRolloverSelectedIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.SELECTED, iconSize)));
 269  
             //setDisabledIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.DISABLED, iconSize)));
 270  
         }
 271  0
         dirty = false;
 272  0
     }
 273  
 }