001    /* JAPI - (Yet anothr (hopefully) useful) Java API
002     *
003     * Copyright (C) 2004-2006 Christian Hujer
004     *
005     * This program is free software; you can redistribute it and/or
006     * modify it under the terms of the GNU General Public License as
007     * published by the Free Software Foundation; either version 2 of the
008     * License, or (at your option) any later version.
009     *
010     * This program is distributed in the hope that it will be useful, but
011     * WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013     * General Public License for more details.
014     *
015     * You should have received a copy of the GNU General Public License
016     * along with this program; if not, write to the Free Software
017     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
018     * 02111-1307, USA.
019     */
020    
021    package net.sf.japi.swing;
022    
023    import java.net.URL;
024    import java.util.Map;
025    import java.util.WeakHashMap;
026    import javax.swing.Icon;
027    import javax.swing.ImageIcon;
028    
029    /** Class to handle icons.
030     * Default size is 16.
031     * Instances must have an associated ClassLoader, otherwise several methods will not work properly but throw a NullPointerException instead.
032     * So if you do not provide a ClassLoader, be sure the class you provide has one, or if you use the no-arg constructor resp. the default instance, be
033     * sure the IconManager class itself was loaded with some ClassLoader other than <code>null</code>.
034     * @author <a href="mailto:Christian.Hujer@itcqis.com">Christian Hujer</a>
035     * @todo this class should be refactored into a more generic version and accessible through ActionFactory?
036     */
037    public final class IconManager {
038    
039        /** Version Information. */
040        public static final String version = "$Revision: 1.4 $";
041    
042        /** The default IconManager. */
043        private static final IconManager defaultManager = new IconManager();
044    
045        /** ClassLoader to get icons from, must not be null. */
046        private final ClassLoader classLoader;
047    
048        /** The available sizes provided by this IconManager. */
049        private final int[] availableSizes = { 16, 24 };
050    
051        /** The icon cache.
052         * Key: short name for icon, which is likely to be used as a relative file name.
053         * Value: Icon
054         */
055        private final Map<String,Icon> smallCache = new WeakHashMap<String,Icon>();
056    
057        /** Get the default IconManager.
058         * The ClassLoader in use is the classloader IconManager was loaded with, whatever classloader that was.
059         * @return default IconManaager
060         */
061        public static IconManager getDefaultIconManager() {
062            return defaultManager;
063        }
064    
065        /** Create a IconManager.
066         * Uses the IconManager's class loader.
067         * Only use this if you want to be independent of the global icon size settings.
068         * The recommended way to get a default IconManager instance is {#getDefaultIconManager()}.
069         */
070        public IconManager() {
071            this(IconManager.class.getClassLoader());
072        }
073    
074        /** Create an IconManager.
075         * @param clazz Class to get ClassLoader for IconManager
076         */
077        public IconManager(final Class<?> clazz) {
078            this.classLoader = clazz.getClassLoader();
079        }
080    
081        /** Create an IconManager.
082         * @param cl ClassLoader to create IconManager for
083         */
084        public IconManager(final ClassLoader cl) {
085            this.classLoader = cl;
086        }
087    
088        /** Return the available sizes for icons.
089         * @return available icon sizes
090         */
091        public int[] getAvailableSizes() {
092            return availableSizes.clone();
093        }
094    
095        /** Load an icon.
096         * @param s icon name, like "general/About" or "navigation/Forward"
097         * @return Icon for <var>s</var>
098         */
099        public Icon getIcon(final String s) {
100            Icon icon = smallCache.get(s);
101            if (icon == null) {
102                final String path = getPathFor(s);
103                final URL url = classLoader.getResource(path);
104                if (url == null) { return null; }
105                icon = new ImageIcon(url);
106                //icon = new ImageIcon(classLoader.getResource(getPathFor(s)));
107                smallCache.put(s, icon);
108            }
109            return icon;
110        }
111    
112        /** Get the path for an icon.
113         * @param s icon name
114         * @return path for s
115         */
116        private static String getPathFor(final String s) {
117            final StringBuilder name = new StringBuilder(); // 1.5
118            name.append("icons/");
119            name.append(s);
120            //name.append(Integer.toString(size));
121            name.append(".gif");
122            return name.toString();
123        }
124    
125    } // class IconManager