Coverage Report - org.dishevelled.identify.IdButton
 
Classes in this File Line Coverage Branch Coverage Complexity
IdButton
100%
78/78
75%
24/32
2.583
 
 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.Action;
 29  
 import javax.swing.ImageIcon;
 30  
 import javax.swing.JButton;
 31  
 import javax.swing.UIManager;
 32  
 
 33  
 import org.dishevelled.iconbundle.IconBundle;
 34  
 import org.dishevelled.iconbundle.IconSize;
 35  
 import org.dishevelled.iconbundle.IconState;
 36  
 import org.dishevelled.iconbundle.IconTextDirection;
 37  
 
 38  
 /**
 39  
  * An extension of JButton that displays the name property value
 40  
  * and appropriate icon from an icon bundle for an identifiable
 41  
  * action.
 42  
  *
 43  
  * @see IdentifiableAction
 44  
  *
 45  
  * @author  Michael Heuer
 46  
  * @version $Revision: 1059 $ $Date: 2012-01-03 14:03:02 -0600 (Tue, 03 Jan 2012) $
 47  
  */
 48  
 public final class IdButton
 49  
     extends JButton
 50  
 {
 51  
     /** Default icon size. */
 52  1
     public static final IconSize DEFAULT_ICON_SIZE = IconSize.DEFAULT_16X16;
 53  
 
 54  
     /** Default icon text direction. */
 55  1
     private static final IconTextDirection DEFAULT_ICON_TEXT_DIRECTION = IconTextDirection.LEFT_TO_RIGHT;
 56  
 
 57  
     /** Icon size. */
 58  31
     private IconSize iconSize = DEFAULT_ICON_SIZE;
 59  
 
 60  
     /** Icon text direction. */
 61  31
     private IconTextDirection iconTextDirection = DEFAULT_ICON_TEXT_DIRECTION;
 62  
 
 63  
 
 64  
     /**
 65  
      * Create a new button with the specified identifiable action.
 66  
      *
 67  
      * @param action identifiable action, must not be null
 68  
      */
 69  
     public IdButton(final IdentifiableAction action)
 70  
     {
 71  21
         super();
 72  
         // clear gradient for Metal/Ocean L&F
 73  21
         UIManager.put("Button.gradient", null);
 74  21
         UIManager.getDefaults().put("Button.gradient", null);
 75  21
         UIManager.getLookAndFeelDefaults().put("Button.gradient", null);
 76  
 
 77  21
         if (action == null)
 78  
         {
 79  2
             throw new IllegalArgumentException("action must not be null");
 80  
         }
 81  19
         setAction(action);
 82  19
     }
 83  
 
 84  
     /**
 85  
      * Create a new button with the specified identifiable action
 86  
      * and icon size.
 87  
      *
 88  
      * @param action identifiable action, must not be null
 89  
      * @param iconSize icon size, must not be null
 90  
      */
 91  
     public IdButton(final IdentifiableAction action, final IconSize iconSize)
 92  
     {
 93  10
         super();
 94  
         // clear gradient for Metal/Ocean L&F
 95  10
         UIManager.put("Button.gradient", null);
 96  10
         UIManager.getDefaults().put("Button.gradient", null);
 97  10
         UIManager.getLookAndFeelDefaults().put("Button.gradient", null);
 98  
 
 99  10
         if (action == null)
 100  
         {
 101  1
             throw new IllegalArgumentException("action must not be null");
 102  
         }
 103  9
         setIconSize(iconSize);
 104  8
         setAction(action);
 105  8
     }
 106  
 
 107  
 
 108  
     /**
 109  
      * Return the icon size for this button.
 110  
      *
 111  
      * @return the icon size for this button
 112  
      */
 113  
     public IconSize getIconSize()
 114  
     {
 115  4
         return iconSize;
 116  
     }
 117  
 
 118  
     /**
 119  
      * Set the icon size for this button to <code>iconSize</code>.
 120  
      *
 121  
      * <p>This is a bound property.</p>
 122  
      *
 123  
      * @param iconSize icon size, must not be null
 124  
      */
 125  
     public void setIconSize(final IconSize iconSize)
 126  
     {
 127  12
         if (iconSize == null)
 128  
         {
 129  2
             throw new IllegalArgumentException("iconSize must not be null");
 130  
         }
 131  10
         IconSize oldIconSize = this.iconSize;
 132  10
         this.iconSize = iconSize;
 133  
 
 134  10
         if (!this.iconSize.equals(oldIconSize))
 135  
         {
 136  10
             firePropertyChange("iconSize", oldIconSize, this.iconSize);
 137  10
             rebuild();
 138  
         }
 139  10
     }
 140  
 
 141  
     /**
 142  
      * Return the icon text direction for this button.
 143  
      *
 144  
      * @return the icon text direction for this button
 145  
      */
 146  
     IconTextDirection getIconTextDirection()
 147  
     {
 148  5
         return iconTextDirection;
 149  
     }
 150  
 
 151  
     /**
 152  
      * Display icons only.
 153  
      */
 154  
     public void displayIcon()
 155  
     {
 156  
         //setHideActionText(true);
 157  2
         setText(null);
 158  2
         rebuild();
 159  2
     }
 160  
 
 161  
     /**
 162  
      * Display text only.
 163  
      */
 164  
     public void displayText()
 165  
     {
 166  
         //setHideActionText(false);
 167  2
         Action action = getAction();
 168  2
         if ((action != null) && (action instanceof IdentifiableAction))
 169  
         {
 170  2
             IdentifiableAction identifiableAction = (IdentifiableAction) action;
 171  2
             setText(identifiableAction.getName());
 172  
         }
 173  2
         setIcon(null);
 174  2
         setPressedIcon(null);
 175  2
         setSelectedIcon(null);
 176  2
         setRolloverIcon(null);
 177  2
         setRolloverSelectedIcon(null);
 178  2
         setDisabledIcon(null);
 179  2
     }
 180  
 
 181  
     /**
 182  
      * Display icon and text.
 183  
      */
 184  
     public void displayIconAndText()
 185  
     {
 186  
         //setHideActionText(false);
 187  3
         Action action = getAction();
 188  3
         if ((action != null) && (action instanceof IdentifiableAction))
 189  
         {
 190  3
             IdentifiableAction identifiableAction = (IdentifiableAction) action;
 191  3
             setText(identifiableAction.getName());
 192  
         }
 193  3
         rebuild();
 194  3
     }
 195  
 
 196  
     /** {@inheritDoc} */
 197  
     public void setComponentOrientation(final ComponentOrientation orientation)
 198  
     {
 199  5
         ComponentOrientation oldOrientation = getComponentOrientation();
 200  
 
 201  5
         if (!oldOrientation.equals(orientation))
 202  
         {
 203  5
             if (orientation != null)
 204  
             {
 205  4
                 iconTextDirection = orientation.isLeftToRight()
 206  
                     ? IconTextDirection.LEFT_TO_RIGHT : IconTextDirection.RIGHT_TO_LEFT;
 207  
 
 208  4
                 rebuild();
 209  
             }
 210  
         }
 211  
 
 212  5
         super.setComponentOrientation(orientation);
 213  5
     }
 214  
 
 215  
     /** {@inheritDoc} */
 216  
     public void applyComponentOrientation(final ComponentOrientation orientation)
 217  
     {
 218  3
         ComponentOrientation oldOrientation = getComponentOrientation();
 219  
 
 220  3
         if (!oldOrientation.equals(orientation))
 221  
         {
 222  3
             if (orientation != null)
 223  
             {
 224  2
                 iconTextDirection = orientation.isLeftToRight()
 225  
                     ? IconTextDirection.LEFT_TO_RIGHT : IconTextDirection.RIGHT_TO_LEFT;
 226  
 
 227  2
                 rebuild();
 228  
             }
 229  
         }
 230  
 
 231  3
         super.applyComponentOrientation(orientation);
 232  2
     }
 233  
 
 234  
     /** {@inheritDoc} */
 235  
     protected void configurePropertiesFromAction(final Action action)
 236  
     {
 237  27
         super.configurePropertiesFromAction(action);
 238  27
         rebuild();
 239  27
     }
 240  
 
 241  
     /**
 242  
      * Rebuild the icons for this button from the icon bundle
 243  
      * provided by the identifiable action for this button.
 244  
      */
 245  
     private void rebuild()
 246  
     {
 247  48
         Action action = getAction();
 248  48
         if ((action != null) && (action instanceof IdentifiableAction))
 249  
         {
 250  40
             IdentifiableAction identifiableAction = (IdentifiableAction) action;
 251  40
             IconBundle bndl = identifiableAction.getIconBundle();
 252  40
             setIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.NORMAL, iconSize)));
 253  40
             setPressedIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.ACTIVE, iconSize)));
 254  40
             setSelectedIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.SELECTED, iconSize)));
 255  40
             setRolloverIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.MOUSEOVER, iconSize)));
 256  40
             setRolloverSelectedIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.SELECTED, iconSize)));
 257  
             //setDisabledIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.DISABLED, iconSize)));
 258  
         }
 259  48
     }
 260  
 }