package nu.fw.jeti.images; import java.io.*; import java.net.MalformedURLException; import java.net.URL; import java.util.*; import javax.swing.Icon; import javax.swing.ImageIcon; import nu.fw.jeti.jabber.JIDStatus; import nu.fw.jeti.jabber.elements.Presence; public class StatusIcons { private static Map statusIcons; public StatusIcons() { if( statusIcons == null ) { statusIcons = new HashMap(); String urlString = getClass().getResource("StatusIcons.class").toString(); try { urlString = urlString.substring(0,urlString.lastIndexOf("StatusIcons.class")); readImages(new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("status.cfg"))),new URL(urlString)); readImages(new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("images.cfg"))),new URL(urlString)); } catch(MalformedURLException e) { e.printStackTrace(); } catch(NullPointerException e) { System.err.println("nu/fw/jeti/images/Status.cfg or images.cfg missing"); } } } public static ImageIcon getOfflineIcon() { return getStatusIcon(Presence.UNAVAILABLE ,"default"); } public static ImageIcon getStatusIcon(int show) { return getStatusIcon(show,""); } /** verander * calls getImageIcon(show) whith the default imageset * @param show * @return ImageIcon */ public static ImageIcon getImageIcon(String show) { return getStatusIcon(show,"images"); } /** * returns a status icon * @param show one of chat,available,dnd,away,xa or unavailable * @param type which image set * @return ImageIcon */ public static ImageIcon getStatusIcon(int show,String type) { switch (show) { case Presence.FREE_FOR_CHAT: return getStatusIcon("status/chat",type); case Presence.AWAY: return getStatusIcon("status/away",type); case Presence.XA: return getStatusIcon("status/xa",type); case Presence.DND: return getStatusIcon("status/dnd",type); case Presence.UNAVAILABLE: return getStatusIcon("status/offline",type); case Presence.INVISIBLE: return getStatusIcon("status/invisible",type); case Presence.NONE: return getStatusIcon("status/ask",type); default: return getStatusIcon("status/online",type); } } public static ImageIcon getStatusIcon(JIDStatus jidStatus) { return getStatusIcon(jidStatus.getShow(),jidStatus.getType()); } private static ImageIcon getStatusIcon(String show,String type) { if ( !statusIcons.containsKey(type) ) type = "default"; ImageIcon icon = (ImageIcon)((Map)statusIcons.get(type)).get(show); if (icon == null) icon = (ImageIcon)((Map)statusIcons.get("default")).get(show); return icon; } private void readImages(BufferedReader data,URL file) { try { Map map = new HashMap(); statusIcons.put(data.readLine(),map); while(true) { readSmilie(data,file,map); } } catch(EOFException e) { //e.printStackTrace(); } catch (IOException e2) { //e2.printStackTrace(); } } private void readSmilie(BufferedReader data,URL file,Map current) throws IOException { StringBuffer buffer = new StringBuffer(); while( true ) { int in2 = data.read(); if( in2 == -1) throw new EOFException(); char ca =(char)in2; if ( ca == ' ') { current.put(new String(buffer),readImage(data,file)); return; } buffer.append(ca); } } private Icon readImage(BufferedReader data,URL file) throws IOException { return new ImageIcon( new URL(file + data.readLine()) ); } } /* * Overrides for emacs * Local variables: * tab-width: 4 * End: */