Coverage Report - org.dishevelled.iconbundle.impl.PNGIconBundle
 
Classes in this File Line Coverage Branch Coverage Complexity
PNGIconBundle
93%
81/87
97%
33/34
7.667
 
 1  
 /*
 2  
 
 3  
     dsh-iconbundle  Bundles of variants for icon images.
 4  
     Copyright (c) 2003-2011 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.iconbundle.impl;
 25  
 
 26  
 import java.io.File;
 27  
 import java.io.InputStream;
 28  
 import java.io.IOException;
 29  
 
 30  
 import java.awt.Color;
 31  
 import java.awt.Image;
 32  
 import java.awt.Component;
 33  
 import java.awt.Graphics2D;
 34  
 import java.awt.RenderingHints;
 35  
 
 36  
 import java.awt.image.BufferedImage;
 37  
 
 38  
 import java.awt.geom.Rectangle2D;
 39  
 import java.awt.geom.AffineTransform;
 40  
 
 41  
 import java.net.URL;
 42  
 
 43  
 import javax.imageio.ImageIO;
 44  
 
 45  
 import javax.imageio.stream.ImageInputStream;
 46  
 
 47  
 import javax.swing.UIManager;
 48  
 
 49  
 import org.dishevelled.iconbundle.IconSize;
 50  
 import org.dishevelled.iconbundle.IconState;
 51  
 import org.dishevelled.iconbundle.IconBundle;
 52  
 import org.dishevelled.iconbundle.IconTextDirection;
 53  
 
 54  
 /**
 55  
  * Icon bundle derived from a single base image in PNG format.
 56  
  *
 57  
  * @author  Michael Heuer
 58  
  * @version $Revision: 974 $ $Date: 2011-01-04 21:40:23 -0600 (Tue, 04 Jan 2011) $
 59  
  */
 60  
 public final class PNGIconBundle
 61  
     implements IconBundle
 62  
 {
 63  
     /** Base image name. */
 64  
     private final String baseImageName;
 65  
 
 66  
     /** Base image. */
 67  
     private final BufferedImage baseImage;
 68  
 
 69  
 
 70  
     /**
 71  
      * Create a new PNG icon bundle from a base image that will
 72  
      * be read from the specified file.
 73  
      *
 74  
      * @param file file, must not be null
 75  
      */
 76  
     public PNGIconBundle(final File file)
 77  14
     {
 78  14
         if (file == null)
 79  
         {
 80  1
             throw new IllegalArgumentException("file must not be null");
 81  
         }
 82  
 
 83  13
         baseImageName = null;
 84  
         try
 85  
         {
 86  13
             baseImage = ImageIO.read(file);
 87  
         }
 88  1
         catch (Exception e)
 89  
         {
 90  1
             throw new IllegalArgumentException("could not create base image; " + e.getMessage());
 91  12
         }
 92  12
     }
 93  
 
 94  
     /**
 95  
      * Create a new PNG icon bundle from a base image that will
 96  
      * be read from the specified input stream.
 97  
      *
 98  
      * @param inputStream input stream, must not be null
 99  
      */
 100  
     public PNGIconBundle(final InputStream inputStream)
 101  2
     {
 102  2
         if (inputStream == null)
 103  
         {
 104  1
             throw new IllegalArgumentException("inputStream must not be null");
 105  
         }
 106  
 
 107  1
         baseImageName = null;
 108  
         try
 109  
         {
 110  1
             baseImage = ImageIO.read(inputStream);
 111  
         }
 112  0
         catch (Exception e)
 113  
         {
 114  0
             throw new IllegalArgumentException("could not create base image; " + e.getMessage());
 115  1
         }
 116  1
     }
 117  
 
 118  
     /**
 119  
      * Create a new PNG icon bundle from a base image that will
 120  
      * be read from the specified image input stream.
 121  
      *
 122  
      * @param imageInputStream image input stream, must not be null
 123  
      */
 124  
     public PNGIconBundle(final ImageInputStream imageInputStream)
 125  2
     {
 126  2
         if (imageInputStream == null)
 127  
         {
 128  1
             throw new IllegalArgumentException("imageInputStream must not be null");
 129  
         }
 130  
 
 131  1
         baseImageName = null;
 132  
         try
 133  
         {
 134  1
             baseImage = ImageIO.read(imageInputStream);
 135  
         }
 136  0
         catch (Exception e)
 137  
         {
 138  0
             throw new IllegalArgumentException("could not create base image; " + e.getMessage());
 139  1
         }
 140  1
     }
 141  
 
 142  
     /**
 143  
      * Create a new PNG icon bundle from a base image that will
 144  
      * be read from the specified URL.
 145  
      *
 146  
      * @param url URL, must not be null
 147  
      */
 148  
     public PNGIconBundle(final URL url)
 149  3
     {
 150  3
         if (url == null)
 151  
         {
 152  1
             throw new IllegalArgumentException("url must not be null");
 153  
         }
 154  
 
 155  2
         baseImageName = null;
 156  
         try
 157  
         {
 158  2
             baseImage = ImageIO.read(url);
 159  
         }
 160  1
         catch (Exception e)
 161  
         {
 162  1
             throw new IllegalArgumentException("could not create base image; " + e.getMessage());
 163  1
         }
 164  1
     }
 165  
 
 166  
     /**
 167  
      * Create a new PNG icon bundle from a base image name.  The name
 168  
      * will be used to construct an URL to a classpath resource
 169  
      * <code>baseImageName + iconSize.toString() + ".png"</code>.
 170  
      *
 171  
      * @param baseImageName base image name, must not be null
 172  
      */
 173  
     public PNGIconBundle(final String baseImageName)
 174  3
     {
 175  3
         if (baseImageName == null)
 176  
         {
 177  1
             throw new IllegalArgumentException("baseImageName must not be null");
 178  
         }
 179  2
         baseImage = null;
 180  2
         this.baseImageName = baseImageName;
 181  2
     }
 182  
 
 183  
 
 184  
     /** {@inheritDoc} */
 185  
     public Image getImage(final Component component,
 186  
                           final IconTextDirection direction,
 187  
                           final IconState state,
 188  
                           final IconSize size)
 189  
     {
 190  2166
         if (direction == null)
 191  
         {
 192  1
             throw new IllegalArgumentException("direction must not be null");
 193  
         }
 194  2165
         if (state == null)
 195  
         {
 196  1
             throw new IllegalArgumentException("state must not be null");
 197  
         }
 198  2164
         if (size == null)
 199  
         {
 200  1
             throw new IllegalArgumentException("size must not be null");
 201  
         }
 202  
 
 203  2163
         BufferedImage image = null;
 204  2163
         if (baseImage == null)
 205  
         {
 206  
             try
 207  
             {
 208  7
                 URL url = getClass().getResource(baseImageName + size.toString() + ".png");
 209  7
                 image = (url == null) ? null : ImageIO.read(url);
 210  
             }
 211  0
             catch (IOException e)
 212  
             {
 213  
                 // ignore
 214  7
             }
 215  
         }
 216  
         else
 217  
         {
 218  2156
             int h = size.getHeight();
 219  2156
             int w = size.getWidth();
 220  
 
 221  2156
             image = new BufferedImage(w + 1, h + 1, BufferedImage.TYPE_INT_ARGB);
 222  2156
             Graphics2D g = image.createGraphics();
 223  
 
 224  2156
             g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
 225  2156
             g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
 226  2156
             g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
 227  
 
 228  2156
             Rectangle2D bounds = new Rectangle2D.Double(0.0d, 0.0d,
 229  
                                                         (double) baseImage.getWidth(),
 230  
                                                         (double) baseImage.getHeight());
 231  
 
 232  2156
             double d = 1.0d;
 233  2156
             if (bounds.getWidth() >= bounds.getHeight())
 234  
             {
 235  2156
                 d = w / bounds.getWidth();
 236  
             }
 237  
             else
 238  
             {
 239  0
                 d = h / bounds.getHeight();
 240  
             }
 241  
 
 242  2156
             AffineTransform at = new AffineTransform();
 243  
 
 244  
             // translate to center of icon size rect
 245  2156
             at.translate(w / 2.0d, h / 2.0d);
 246  
             // scale image to icon size rect
 247  2156
             at.scale(d, d);
 248  
             // translate center of image to center of icon size rect
 249  2156
             at.translate(-1 * bounds.getCenterX(), -1 * bounds.getCenterY());
 250  
 
 251  2156
             g.drawRenderedImage(baseImage, at);
 252  2156
             g.dispose();
 253  
         }
 254  
 
 255  2163
         if (IconState.ACTIVE == state)
 256  
         {
 257  308
             return IconBundleUtils.makeActive(image);
 258  
         }
 259  1855
         else if (IconState.MOUSEOVER == state)
 260  
         {
 261  308
             return IconBundleUtils.makeMouseover(image);
 262  
         }
 263  1547
         else if (IconState.SELECTED == state)
 264  
         {
 265  308
             Color selectionColor = UIManager.getColor("List.selectionBackground");
 266  308
             return IconBundleUtils.makeSelected(image, selectionColor);
 267  
         }
 268  1239
         else if (IconState.SELECTED_MOUSEOVER == state)
 269  
         {
 270  308
             Color selectionColor = UIManager.getColor("List.selectionBackground");
 271  308
             return IconBundleUtils.makeSelectedMouseover(image, selectionColor);
 272  
         }
 273  931
         else if (IconState.DRAGGING == state)
 274  
         {
 275  308
             return IconBundleUtils.makeDragging(image);
 276  
         }
 277  623
         else if (IconState.DISABLED == state)
 278  
         {
 279  308
             return IconBundleUtils.makeDisabled(image);
 280  
         }
 281  
         else
 282  
         {
 283  315
             return image;
 284  
         }
 285  
     }
 286  
 }