Coverage Report - org.dishevelled.iconbundle.impl.ShapeIconBundle
 
Classes in this File Line Coverage Branch Coverage Complexity
ShapeIconBundle
100%
55/55
100%
28/28
14.5
 
 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.awt.Color;
 27  
 import java.awt.Image;
 28  
 import java.awt.Paint;
 29  
 import java.awt.Shape;
 30  
 import java.awt.Stroke;
 31  
 import java.awt.Component;
 32  
 import java.awt.Graphics2D;
 33  
 import java.awt.RenderingHints;
 34  
 
 35  
 import java.awt.geom.Rectangle2D;
 36  
 
 37  
 import java.awt.image.BufferedImage;
 38  
 
 39  
 import javax.swing.UIManager;
 40  
 
 41  
 import org.dishevelled.iconbundle.IconSize;
 42  
 import org.dishevelled.iconbundle.IconState;
 43  
 import org.dishevelled.iconbundle.IconBundle;
 44  
 import org.dishevelled.iconbundle.IconTextDirection;
 45  
 
 46  
 /**
 47  
  * Shape icon bundle.
 48  
  *
 49  
  * @author  Michael Heuer
 50  
  * @version $Revision: 974 $ $Date: 2011-01-04 21:40:23 -0600 (Tue, 04 Jan 2011) $
 51  
  */
 52  
 public final class ShapeIconBundle
 53  
     implements IconBundle
 54  
 {
 55  
     /** Shape. */
 56  
     private final Shape shape;
 57  
 
 58  
     /** Stroke. */
 59  
     private final Stroke stroke;
 60  
 
 61  
     /** Paint. */
 62  
     private final Paint fillPaint;
 63  
 
 64  
     /** Stroke paint. */
 65  
     private final Paint strokePaint;
 66  
 
 67  
 
 68  
     /**
 69  
      * Create a new shape icon bundle with the specified shape,
 70  
      * stroke, and paints.
 71  
      *
 72  
      * @param shape shape, must not be null
 73  
      * @param stroke stroke
 74  
      * @param fillPaint fill paint
 75  
      * @param strokePaint stroke paint
 76  
      */
 77  
     public ShapeIconBundle(final Shape shape,
 78  
                            final Stroke stroke,
 79  
                            final Paint fillPaint,
 80  
                            final Paint strokePaint)
 81  33
     {
 82  33
         if (shape == null)
 83  
         {
 84  1
             throw new IllegalArgumentException("shape must not be null");
 85  
         }
 86  32
         if (stroke == null)
 87  
         {
 88  1
             throw new IllegalArgumentException("stroke must not be null");
 89  
         }
 90  31
         if (fillPaint == null)
 91  
         {
 92  1
             throw new IllegalArgumentException("fillPaint must not be null");
 93  
         }
 94  30
         if (strokePaint == null)
 95  
         {
 96  1
             throw new IllegalArgumentException("strokePaint must not be null");
 97  
         }
 98  
 
 99  29
         this.shape = shape;
 100  29
         this.stroke = stroke;
 101  29
         this.fillPaint = fillPaint;
 102  29
         this.strokePaint = strokePaint;
 103  29
     }
 104  
 
 105  
 
 106  
     /** {@inheritDoc} */
 107  
     public Image getImage(final Component component,
 108  
                           final IconTextDirection direction,
 109  
                           final IconState state,
 110  
                           final IconSize size)
 111  
     {
 112  4217
         if (direction == null)
 113  
         {
 114  1
             throw new IllegalArgumentException("direction must not be null");
 115  
         }
 116  4216
         if (state == null)
 117  
         {
 118  1
             throw new IllegalArgumentException("state must not be null");
 119  
         }
 120  4215
         if (size == null)
 121  
         {
 122  1
             throw new IllegalArgumentException("size must not be null");
 123  
         }
 124  
 
 125  4214
         int h = size.getHeight();
 126  4214
         int w = size.getWidth();
 127  
 
 128  4214
         BufferedImage image = new BufferedImage(w + 1, h + 1, BufferedImage.TYPE_INT_ARGB);
 129  4214
         Graphics2D g = image.createGraphics();
 130  
 
 131  4214
         g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
 132  4214
         g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
 133  
 
 134  4214
         Rectangle2D bounds = shape.getBounds2D();
 135  
 
 136  4214
         double d = 1.0d;
 137  4214
         if (bounds.getWidth() >= bounds.getHeight())
 138  
         {
 139  392
             d = w / bounds.getWidth();
 140  
         }
 141  
         else
 142  
         {
 143  3822
             d = h / bounds.getHeight();
 144  
         }
 145  
 
 146  
         // translate to center of icon size rect
 147  4214
         g.translate(w / 2.0d, h / 2.0d);
 148  
         // scale shape to icon size rect
 149  4214
         g.scale(d, d);
 150  
         // translate center of shape to center of icon size rect
 151  4214
         g.translate(-1 * bounds.getCenterX(), -1 * bounds.getCenterY());
 152  
 
 153  4214
         g.setPaint(fillPaint);
 154  4214
         g.fill(shape);
 155  
 
 156  4214
         g.setStroke(stroke);
 157  4214
         g.setPaint(strokePaint);
 158  4214
         g.draw(shape);
 159  
 
 160  4214
         g.dispose();
 161  
 
 162  4214
         if (IconState.ACTIVE == state)
 163  
         {
 164  602
             return IconBundleUtils.makeActive(image);
 165  
         }
 166  3612
         else if (IconState.MOUSEOVER == state)
 167  
         {
 168  602
             return IconBundleUtils.makeMouseover(image);
 169  
         }
 170  3010
         else if (IconState.SELECTED == state)
 171  
         {
 172  602
             Color selectionColor = UIManager.getColor("List.selectionBackground");
 173  602
             return IconBundleUtils.makeSelected(image, selectionColor);
 174  
         }
 175  2408
         else if (IconState.SELECTED_MOUSEOVER == state)
 176  
         {
 177  602
             Color selectionColor = UIManager.getColor("List.selectionBackground");
 178  602
             return IconBundleUtils.makeSelectedMouseover(image, selectionColor);
 179  
         }
 180  1806
         else if (IconState.DRAGGING == state)
 181  
         {
 182  602
             return IconBundleUtils.makeDragging(image);
 183  
         }
 184  1204
         else if (IconState.DISABLED == state)
 185  
         {
 186  602
             return IconBundleUtils.makeDisabled(image);
 187  
         }
 188  
         else
 189  
         {
 190  602
             return image;
 191  
         }
 192  
     }
 193  
 }