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