Coverage Report - org.dishevelled.iconbundle.impl.SVGIconBundle
 
Classes in this File Line Coverage Branch Coverage Complexity
SVGIconBundle
94%
32/34
100%
20/20
8.333
 
 1  
 /*
 2  
 
 3  
     dsh-iconbundle  Bundles of variants for icon images.
 4  
     Copyright (c) 2003-2010 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  
 
 28  
 import java.net.URL;
 29  
 
 30  
 import java.awt.Image;
 31  
 import java.awt.Color;
 32  
 import java.awt.Component;
 33  
 
 34  
 import java.awt.image.BufferedImage;
 35  
 
 36  
 import javax.swing.UIManager;
 37  
 
 38  
 import org.dishevelled.iconbundle.IconSize;
 39  
 import org.dishevelled.iconbundle.IconState;
 40  
 import org.dishevelled.iconbundle.IconBundle;
 41  
 import org.dishevelled.iconbundle.IconTextDirection;
 42  
 
 43  
 /**
 44  
  * Icon bundle derived from a single scalable base image in SVG format.
 45  
  *
 46  
  * @author  Michael Heuer
 47  
  * @version $Revision: 741 $ $Date: 2010-01-02 20:23:15 -0600 (Sat, 02 Jan 2010) $
 48  
  */
 49  
 public final class SVGIconBundle
 50  
     implements IconBundle
 51  
 {
 52  
     /** SVG document url. */
 53  
     private URL url;
 54  
 
 55  
 
 56  
     /**
 57  
      * Create a new SVG icon bundle from a base scalable image that will
 58  
      * be read from the specified file.
 59  
      *
 60  
      * @param file file, must not be null
 61  
      */
 62  
     public SVGIconBundle(final File file)
 63  2
     {
 64  2
         if (file == null)
 65  
         {
 66  1
             throw new IllegalArgumentException("file must not be null");
 67  
         }
 68  
 
 69  
         try
 70  
         {
 71  1
             url = new URL("file://" + file.getAbsolutePath());
 72  
         }
 73  0
         catch (Exception e)
 74  
         {
 75  0
             throw new IllegalArgumentException("could not create base image URL: " + e.getMessage());
 76  1
         }
 77  1
     }
 78  
 
 79  
     /**
 80  
      * Create a new SVG icon bundle from a base scalable image that will
 81  
      * the specified URL.
 82  
      *
 83  
      * @param url URL, must not be null
 84  
      */
 85  
     public SVGIconBundle(final URL url)
 86  13
     {
 87  13
         if (url == null)
 88  
         {
 89  1
             throw new IllegalArgumentException("url must not be null");
 90  
         }
 91  
 
 92  12
         this.url = url;
 93  12
     }
 94  
 
 95  
 
 96  
     /** {@inheritDoc} */
 97  
     public Image getImage(final Component component,
 98  
                           final IconTextDirection direction,
 99  
                           final IconState state,
 100  
                           final IconSize size)
 101  
     {
 102  1851
         if (direction == null)
 103  
         {
 104  1
             throw new IllegalArgumentException("direction must not be null");
 105  
         }
 106  1850
         if (state == null)
 107  
         {
 108  1
             throw new IllegalArgumentException("state must not be null");
 109  
         }
 110  1849
         if (size == null)
 111  
         {
 112  1
             throw new IllegalArgumentException("size must not be null");
 113  
         }
 114  
 
 115  1848
         int height = size.getHeight();
 116  1848
         int width = size.getWidth();
 117  
 
 118  1848
         BufferedImage image = IconBundleUtils.readSVG(url, width, height);
 119  
 
 120  1848
         if (IconState.ACTIVE == state)
 121  
         {
 122  308
             return IconBundleUtils.makeActive(image);
 123  
         }
 124  1540
         else if (IconState.MOUSEOVER == state)
 125  
         {
 126  308
             return IconBundleUtils.makeMouseover(image);
 127  
         }
 128  1232
         else if (IconState.SELECTED == state)
 129  
         {
 130  308
             Color selectionColor = UIManager.getColor("List.selectionBackground");
 131  308
             return IconBundleUtils.makeSelected(image, selectionColor);
 132  
         }
 133  924
         else if (IconState.DRAGGING == state)
 134  
         {
 135  308
             return IconBundleUtils.makeDragging(image);
 136  
         }
 137  616
         else if (IconState.DISABLED == state)
 138  
         {
 139  308
             return IconBundleUtils.makeDisabled(image);
 140  
         }
 141  
         else
 142  
         {
 143  308
             return image;
 144  
         }
 145  
     }
 146  
 }