View Javadoc

1   /*
2   
3       dsh-iconbundle-tools  Command line icon bundle tools.
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.tools;
25  
26  import java.io.File;
27  
28  import java.awt.Color;
29  import java.awt.Graphics2D;
30  
31  import java.awt.image.BufferedImage;
32  
33  import java.util.Iterator;
34  
35  import javax.imageio.ImageIO;
36  
37  import org.dishevelled.iconbundle.IconSize;
38  import org.dishevelled.iconbundle.IconState;
39  import org.dishevelled.iconbundle.IconBundle;
40  import org.dishevelled.iconbundle.IconTextDirection;
41  
42  /**
43   * Show all variants runnable.
44   *
45   * @author  Michael Heuer
46   */
47  public final class ShowAllVariants
48      implements Runnable
49  {
50      /** File name. */
51      private String fileName;
52  
53      /** True to draw borders. */
54      private boolean drawBorders;
55  
56      /** Icon bundle. */
57      private IconBundle iconBundle;
58  
59  
60      /**
61       * Create a new show all variants runnable with the
62       * specified icon bundle.
63       *
64       * @param iconBundle icon bundle
65       * @param fileName file name
66       * @param drawBorders true to draw borders
67       */
68      public ShowAllVariants(final IconBundle iconBundle, final String fileName, final boolean drawBorders)
69      {
70          this.iconBundle = iconBundle;
71          this.fileName = fileName;
72          this.drawBorders = drawBorders;
73      }
74  
75  
76      /** @see Runnable */
77      public void run()
78      {
79          int w = 0;
80          int h = 0;
81  
82          int maxHeight = 0;
83          int horizontalGap = 4;
84          int verticalGap = 4;
85  
86          for (Iterator i = IconSize.VALUES.iterator(); i.hasNext(); )
87          {
88              IconSize size = (IconSize) i.next();
89              w += size.getWidth();
90              w += horizontalGap;
91  
92              if (size.getHeight() > maxHeight)
93              {
94                  maxHeight = size.getHeight();
95              }
96          }
97  
98          w *= IconTextDirection.VALUES.size();
99          h = IconState.VALUES.size() * (maxHeight + verticalGap);
100 
101         BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
102         Graphics2D g = image.createGraphics();
103 
104         int x = 0;
105         int y = 0;
106         maxHeight = 0;
107         for (Iterator i = IconState.VALUES.iterator(); i.hasNext(); )
108         {
109             IconState state = (IconState) i.next();
110             for (Iterator j = IconSize.VALUES.iterator(); j.hasNext(); )
111             {
112                 IconSize size = (IconSize) j.next();
113                 for (Iterator k = IconTextDirection.VALUES.iterator(); k.hasNext(); )
114                 {
115                     IconTextDirection direction = (IconTextDirection) k.next();
116 
117                     g.drawImage(iconBundle.getImage(null, direction, state, size),
118                                 x, y, Color.WHITE, null);
119 
120                     if (drawBorders)
121                     {
122                         g.setPaint(Color.BLACK);
123                         g.drawRect(x, y, size.getWidth(), size.getHeight());
124                     }
125 
126                     x += size.getWidth();
127                     x += horizontalGap;
128 
129                     if (size.getHeight() > maxHeight)
130                     {
131                         maxHeight = size.getHeight();
132                     }
133                 }
134             }
135             x = 0;
136             y += maxHeight;
137             y += verticalGap;
138             maxHeight = 0;
139         }
140 
141         try
142         {
143             ImageIO.write(image, "png", new File(fileName));
144         }
145         catch (Exception e)
146         {
147             // ignore
148         }
149     }
150 }