001    /*
002     * Copyright (C) 2003 Adam Olsen
003     * 
004     * This program is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU General Public License as published by the Free Software
006     * Foundation; either version 1, or (at your option) any later version.
007     * 
008     * This program is distributed in the hope that it will be useful, but WITHOUT
009     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
010     * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
011     * details.
012     * 
013     * You should have received a copy of the GNU General Public License along with
014     * this program; if not, write to the Free Software Foundation, Inc., 675 Mass
015     * Ave, Cambridge, MA 02139, USA.
016     */
017    
018    package com.valhalla.jbother;
019    
020    import java.io.File;
021    import java.util.Hashtable;
022    
023    import javax.swing.ImageIcon;
024    
025    import org.jivesoftware.smack.packet.Presence;
026    
027    import com.valhalla.gui.Standard;
028    import com.valhalla.jbother.jabber.SelfStatuses;
029    import com.valhalla.settings.Settings;
030    
031    public class StatusIconCache {
032        private static Hashtable statusIconCache = new Hashtable();
033    
034        public static ImageIcon getStatusIcon(Presence.Mode mode) {
035            String statusShortcut = (mode != null) ? SelfStatuses.getInstance()
036                    .getStatus(mode).getShortcut() : "offline";
037            if (!statusIconCache.containsKey(statusShortcut))
038                statusIconCache
039                        .put(statusShortcut, fetchStatusIcon(statusShortcut));
040            return (ImageIcon) statusIconCache.get(statusShortcut);
041        }
042    
043        public static ImageIcon fetchStatusIcon(String statusShortcut) {
044            if (Settings.getInstance().getProperty("statusTheme") == null)
045                Settings.getInstance().setProperty("statusTheme", "default");
046            char slash = File.separatorChar;
047    
048            String secondPathPart = "statusicons" + slash
049                    + Settings.getInstance().getProperty("statusTheme") + slash
050                    + statusShortcut + ".png";
051            String userPath = JBother.settingsDir + slash + "themes" + slash
052                    + secondPathPart;
053            ImageIcon result = null;
054            if (new File(userPath).exists()) {
055                result = new ImageIcon(userPath);
056            } else {
057                // this was changed because items in JAR files are ALWAYS found with
058                // a /, not separatorChar
059                result = Standard.getIcon("imagethemes/statusicons/"
060                        + Settings.getInstance().getProperty("statusTheme") + '/'
061                        + statusShortcut + ".png");
062            }
063            return result;
064        }
065    
066        public static void clearStatusIconCache() {
067            statusIconCache.clear();
068        }
069    }