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