1   package org.wcb.gui.util;
2   
3   import org.wcb.resources.Base64;
4   
5   import javax.swing.*;
6   import javax.imageio.stream.ImageInputStream;
7   import javax.imageio.ImageIO;
8   import javax.imageio.ImageReader;
9   import java.net.URL;
10  import java.net.MalformedURLException;
11  import java.awt.*;
12  import java.awt.image.BufferedImage;
13  import java.util.StringTokenizer;
14  import java.io.InputStream;
15  import java.io.IOException;
16  
17  /**
18   * <small>
19   * <p/>
20   * Copyright (c)  2006  wbogaardt.
21   * This library is free software; you can redistribute it and/or
22   * modify it under the terms of the GNU Lesser General Public
23   * License as published by the Free Software Foundation; either
24   * version 2.1 of the License, or (at your option) any later version.
25   *
26   * This library is distributed in the hope that it will be useful,
27   * but WITHOUT ANY WARRANTY; without even the implied warranty of
28   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
29   * Lesser General Public License for more details.
30   *
31   * You should have received a copy of the GNU Lesser General Public
32   * License along with this library; if not, write to the Free Software
33   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
34   * <p/>
35   * $File:  $ <br>
36   * $Change:  $ submitted by $Author: wbogaardt $ at $DateTime: Sep 15, 2006 8:31:46 AM $ <br>
37   * </small>
38   *
39   * @author wbogaardt
40   *
41   * This utility class allows loading of various types of images via various resources
42   * those resources can be jar files, or file system locations.
43   */
44  
45  public class UIHelper {
46  
47  
48      /**
49       * Helps center a frame to the center of the user's screen.
50       * @param frame the frame to center
51       */
52      public static void centerApplicationToScreen(JFrame frame) {
53          Dimension scr = Toolkit.getDefaultToolkit().getScreenSize();
54          Dimension frm = frame.getSize();
55          int x = (scr.width - frm.width )/2;
56          int y = (scr.height - frm.height)/2;
57          frame.setLocation(x,y);
58      }
59  
60      /**
61       * Helps center a dialog box to the center of the user's screen.
62       * @param dialog the dialog box to center
63       */
64      public static void centerDialogToScreen(JDialog dialog) {
65          Dimension scr = Toolkit.getDefaultToolkit().getScreenSize();
66          Dimension  me = dialog.getSize();
67          int x = (scr.width - me.width )/2;
68          int y = (scr.height - me.height)/2;
69          dialog.setLocation(x,y);
70      }
71  
72      /**
73       * Loads an image from the class loader, or jar file and returns
74       * the image as an ImageIcon. A sample would be "org/mypackage/resource/myimage.jpg"
75       * @param resource A string location for the resource such as "org/package/imagename.gif"
76       * @return  ImageIcon that can be used as a label icon.
77       */
78      public static ImageIcon getIcon(String resource) {
79          URL imageURL = UIHelper.class.getClassLoader().getResource(resource);
80          return new ImageIcon(imageURL);
81      }
82  
83      /**
84       * Get an image from a base 64 encoded string.
85       * @param base64
86       * @return image
87       */
88      public static ImageIcon getIconBase64(String base64) {
89          return new ImageIcon(Base64.decode(base64));
90      }
91  
92      public static Image loadImageFromFile(String fileName) {
93          BufferedImage image;
94          StringTokenizer tokenizer = new StringTokenizer(fileName, ".");
95          tokenizer.nextToken();
96          String suffix = tokenizer.nextToken();
97  
98          InputStream resourceStream = UIHelper.class.getResourceAsStream(fileName);
99          ImageInputStream imageStream;
100         try {
101             imageStream = ImageIO.createImageInputStream(resourceStream);
102         } catch (IOException e) {
103             return null;
104         }
105 
106         ImageReader imageReader = ImageIO.getImageReadersBySuffix(suffix).next();
107         imageReader.setInput(imageStream);
108         try {
109             image = imageReader.read(0);
110         } catch (IOException e) {
111             return null;
112         }
113 
114         return image;
115     }
116 
117     /**
118      * Loads an image resources as a buffered image resource, which is usable
119      * under Java2d implementations to draw on components.
120      * @param fileName Resource string to the image file example "org/package/myimage.jpg"
121      * @return A BufferedImage for drawing the image on a component.
122      */
123     public static Image loadImage(String fileName) {
124         BufferedImage image;
125         try {
126             URL imageURL = UIHelper.class.getClassLoader().getResource(fileName);
127             image = ImageIO.read(imageURL);
128         } catch (MalformedURLException mlur) {
129             return null;
130         } catch (IOException ioe) {
131             return null;
132         }
133         return image;
134     }
135 }