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