View Javadoc

1   /*
2    * Copyright (c) 2003-2008 by Cosylab d. d.
3    *
4    * This file is part of CosyBeans-Common.
5    *
6    * CosyBeans-Common is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * CosyBeans-Common is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with CosyBeans-Common.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  
20  package com.cosylab.gui.components.util;
21  
22  import java.awt.Image;
23  import java.awt.image.ImageProducer;
24  import java.io.File;
25  import java.net.URL;
26  import java.util.HashMap;
27  
28  import javax.swing.Icon;
29  import javax.swing.ImageIcon;
30  
31  
32  /**
33   * IconHelper acts as an interface to the all icons currently in classpath.
34   * IconHelper is a singleton class that maintains internal cache to all
35   * accessed icons. This class may be used to minimize the memory consumption
36   * where application uses static icons. If icons are changed independently,
37   * this class should not be used.
38   *
39   * @author <a href="mailto:ales.pucelj@cosylab.com">Ales Pucelj</a>
40   * @version $id$
41   */
42  public final class IconHelper {
43  	
44  	public static String ICONS_NAVIGATION_DOWN16= "icons/navigation/Down16.gif";
45  	public static String ICONS_NAVIGATION_DOWN24= "icons/navigation/Down24.gif";
46  	public static String ICONS_NAVIGATION_MINUS16= "icons/navigation/Minus16.gif";
47  	public static String ICONS_NAVIGATION_PLUS16= "icons/navigation/Plus16.gif";
48  	public static String ICONS_NAVIGATION_UP16= "icons/navigation/Up16.gif";
49  	public static String ICONS_NAVIGATION_UP24= "icons/navigation/Up24.gif";
50  	public static String ICONS_GENERAL_DELETE16= "icons/general/Delete16.gif";
51  	public static String ICONS_GENERAL_DELETE24= "icons/general/Delete24.gif";
52  	public static String ICONS_GENERAL_REMOVE16= "icons/general/Remove16.gif";
53  	public static String ICONS_GENERAL_REMOVE24= "icons/general/Remove24.gif";
54  	
55  	
56      /** cache for icons. */
57      private static HashMap<String, Icon> cache;
58  
59      /**
60       * Protected constructor for creation of singleton class.
61       */
62      private IconHelper() {
63          super();
64      }
65  
66      /**
67       * Returns icon from the cache. If icon cannot be found in cache, return
68       * value will be <code>null</code>.
69       *
70       * @param name String
71       * @return Icon instance of an icon or null if not found
72       */
73      private static synchronized Icon getCachedIcon(String name) {
74      	if (cache==null) {
75      		return null;
76      	}
77          if (cache.containsKey(name)) {
78              return cache.get(name);
79          }
80  
81          return null;
82      }
83  
84      /**
85       * Puts icon in the cache. If an icon with the specified name has already been stored,
86       * current icon will be overwritten.
87       *
88       * @param name
89       * @param icon
90       */
91      private static synchronized void putCachedIcon(String name, Icon icon) {
92      	if (cache==null) {
93      		cache = new HashMap<String, Icon>();
94      	}
95          cache.put(name, icon);
96      }
97  
98      /**
99       * Attempts to load the icon from the resources in classpath.
100      * If the icon cannot be found or other error occurs, the return value is null.
101      *
102      * @param resource String
103      * @param cl ClassLoader
104      * @return Icon icon instance or null
105      */
106     private static final Icon loadResource(String resource, ClassLoader cl) {
107         try {
108         	URL url = cl.getResource(resource);
109         	if (!(url.getContent() instanceof ImageProducer)) return null;
110             return new ImageIcon(url);
111         } catch (Exception e) {
112             return null;
113         }
114     }
115 
116     /**
117      * Returns instance of the icon by loading it using the <code>
118      * ClassLoader</code> instance provided. Icons are cached. If an icon with
119      * the same name is requested several times, <code>createIcon</code> will
120      * return the same instance each time. If icons will be manipulated
121      * individually, do not use this method to load icons. If icon is not found
122      * in the classpath, return value will be null.
123      *
124      * @param name String
125      * @param cl ClassLoader Instance of class loader to use
126      * @return Icon Instance of Icon or null if icon could not be found.
127      */
128     public static Icon createIcon(String name, ClassLoader cl) {
129         Icon icon = getCachedIcon(name);
130 
131         if (icon == null) {
132             icon = loadResource(name, cl);
133 
134             if (icon != null) {
135                 putCachedIcon(name, icon);
136             } else {
137             	return createIcon(new File(name));
138             }
139         }
140 
141         return icon;
142     }
143 
144     /**
145      * Returns instance of the icon. Icons are cached. If an icon with the same
146      * name is requested several times, <code>createIcon</code> will return the
147      * same instance each time. If icons will be manipulated individually, do
148      * not use this method to load icons. If icon is not found in the classpath,
149      * or the file with that name does not exist on the disk return value will be null.
150      *
151      * @param name String
152      * @return Icon Instance of Icon or null if icon could not be found.
153      */
154     public static Icon createIcon(String name) {
155         return createIcon(name, IconHelper.class.getClassLoader());
156     }
157 
158     /**
159      * Creates a new image loaded from the provided image file name.
160      * This class' class loader is used to access resources.
161      * 
162      * @param name the filename of the image
163      * @return Image
164      */
165     public static Image createImage(String name) {
166         return createImage(name, IconHelper.class.getClassLoader());
167     }
168 
169     /**
170      * Creates a new image loaded from the provided image file. The image
171      * is loaded using the given class loader. 
172      * 
173      * @param name the filename of the image
174      * @param cl classloader to be used for accessing resources
175      * @return Image
176      */
177     public static Image createImage(String name, ClassLoader cl) {
178         ImageIcon icon = ((ImageIcon) createIcon(name, cl));
179 
180         if (icon == null) {
181             return null;
182         }
183 
184         return icon.getImage();
185     }
186     
187     /**
188      * Creates an Icon by reading the image stored in the given file. This file
189      * can be anywhere on the computer as long as it is accessible by the current user
190      * rights (the icon file does not need to be added to classpath).
191      * 
192      * @param file the image file
193      * @return icon
194      */
195     public static Icon createIcon(File file) {
196     	if (file == null) return null;
197     	Icon icon = getCachedIcon(file.getAbsolutePath());
198     	if (icon == null) {
199     		icon = new ImageIcon(file.getAbsolutePath());
200    			putCachedIcon(file.getAbsolutePath(), icon);
201     	}
202     	return icon;
203     }
204 }