001    /**
002     * iFish - An iRiver iHP jukebox database creation tool
003     *
004     * Copyright (C) 2009 Richard "Shred" Körber
005     *   http://ifish.shredzone.org
006     *
007     * This program is free software: you can redistribute it and/or modify
008     * it under the terms of the GNU General Public License as published by
009     * the Free Software Foundation, either version 3 of the License, or
010     * (at your option) any later version.
011     *
012     * This program is distributed in the hope that it will be useful,
013     * but WITHOUT ANY WARRANTY; without even the implied warranty of
014     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
015     * GNU General Public License for more details.
016     *
017     * You should have received a copy of the GNU General Public License
018     * along with this program.  If not, see <http://www.gnu.org/licenses/>.
019     */
020    package net.shredzone.ifish.pool;
021    
022    import java.awt.Image;
023    import java.io.*;
024    import java.lang.ref.SoftReference;
025    import java.util.*;
026    import javax.swing.*;
027    
028    /**
029     * Serves images from a common image pool. You just need to pass the name
030     * of the picture, and get the Image as result.
031     * <p>
032     * The ImgPool implements a weak caching mechanism. If you need several
033     * instances of the same image, you can just call <code>get()</code> multiple
034     * times and always get the same instance. The image is internally cached
035     * until the last reference has been discarded.
036     *
037     * @author  Richard Körber &lt;dev@shredzone.de&gt;
038     * @version $Id: ImgPool.java 291 2009-04-28 21:29:27Z shred $
039     */
040    public final class ImgPool {
041    
042      private static HashMap<String, SoftReference<ImageIcon>> cache = new HashMap<String, SoftReference<ImageIcon>>();
043    
044      /**
045       * Get an ImageIcon by its name.
046       *
047       * @param   name          Image name
048       * @return  ImageIcon or null if not found
049       */
050      public static ImageIcon get( String name ) {
051        // First check if there is a cache entry, because the GC could have
052        // sweeped it already.
053    
054        SoftReference<ImageIcon> ref = cache.get(name);
055        ImageIcon result = (ref!=null ? ref.get() : null);
056        if(result==null) {
057          try {
058            result = new ImageIcon( ImgPool.class.getResource(name) );
059            cache.put(name, new SoftReference<ImageIcon>(result));
060          }catch( Exception ex ) {}
061        }
062        return result;
063      }
064    
065      /**
066       * Get an ImageIcon by its name, and scales it to the given dimensions.
067       * Note that scaled images will <em>not</em> be cached.
068       *
069       * @param   name          Image name
070       * @param   width         Image width
071       * @param   height        Image height
072       * @return  ImageIcon or null if not found
073       */
074      public static ImageIcon getScaled( String name, int width, int height ) {
075        //--- Get original icon ---
076        ImageIcon iconFull = get( name );
077        if( iconFull==null ) return null;     // was not found
078        
079        //--- Scale ---
080        Image img = iconFull.getImage();
081        img = img.getScaledInstance( width, height, Image.SCALE_SMOOTH );
082        
083        //--- Return new ImageIcon ---
084        return new ImageIcon( img );
085      }
086    
087    }