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