[1014] | 1 | package nu.fw.jeti.images; |
---|
| 2 | |
---|
| 3 | import java.io.*; |
---|
| 4 | import java.net.MalformedURLException; |
---|
| 5 | import java.net.URL; |
---|
| 6 | import java.util.*; |
---|
| 7 | |
---|
| 8 | import javax.swing.Icon; |
---|
| 9 | import javax.swing.ImageIcon; |
---|
| 10 | |
---|
| 11 | import nu.fw.jeti.jabber.JIDStatus; |
---|
| 12 | import nu.fw.jeti.jabber.elements.Presence; |
---|
| 13 | |
---|
| 14 | public class StatusIcons |
---|
| 15 | { |
---|
| 16 | private static Map statusIcons; |
---|
| 17 | |
---|
[1091] | 18 | public StatusIcons() |
---|
[1014] | 19 | { |
---|
| 20 | if( statusIcons == null ) |
---|
| 21 | { |
---|
[1091] | 22 | statusIcons = new HashMap(); |
---|
| 23 | |
---|
| 24 | String urlString = getClass().getResource("StatusIcons.class").toString(); |
---|
| 25 | try |
---|
[1014] | 26 | { |
---|
[1091] | 27 | urlString = urlString.substring(0,urlString.lastIndexOf("StatusIcons.class")); |
---|
| 28 | readImages(new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("status.cfg"))),new URL(urlString)); |
---|
| 29 | readImages(new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("images.cfg"))),new URL(urlString)); |
---|
| 30 | } |
---|
| 31 | catch(MalformedURLException e) |
---|
| 32 | { |
---|
[1014] | 33 | e.printStackTrace(); |
---|
| 34 | } |
---|
[1091] | 35 | catch(NullPointerException e) |
---|
[1014] | 36 | { |
---|
[1091] | 37 | System.err.println("nu/fw/jeti/images/Status.cfg or images.cfg missing"); |
---|
[1014] | 38 | } |
---|
| 39 | } |
---|
| 40 | } |
---|
| 41 | |
---|
[1091] | 42 | public static ImageIcon getOfflineIcon() |
---|
[1014] | 43 | { |
---|
[1091] | 44 | return getStatusIcon(Presence.UNAVAILABLE ,"default"); |
---|
[1014] | 45 | } |
---|
| 46 | |
---|
[1091] | 47 | public static ImageIcon getStatusIcon(int show) |
---|
[1014] | 48 | { |
---|
[1091] | 49 | return getStatusIcon(show,""); |
---|
[1014] | 50 | } |
---|
| 51 | |
---|
| 52 | /** verander |
---|
| 53 | * calls getImageIcon(show) whith the default imageset |
---|
| 54 | * @param show |
---|
| 55 | * @return ImageIcon |
---|
| 56 | */ |
---|
[1091] | 57 | public static ImageIcon getImageIcon(String show) |
---|
| 58 | { |
---|
| 59 | return getStatusIcon(show,"images"); |
---|
| 60 | } |
---|
[1014] | 61 | |
---|
| 62 | /** |
---|
| 63 | * returns a status icon |
---|
| 64 | * @param show one of chat,available,dnd,away,xa or unavailable |
---|
| 65 | * @param type which image set |
---|
| 66 | * @return ImageIcon |
---|
| 67 | */ |
---|
[1091] | 68 | public static ImageIcon getStatusIcon(int show,String type) |
---|
[1014] | 69 | { |
---|
| 70 | switch (show) |
---|
| 71 | { |
---|
| 72 | case Presence.FREE_FOR_CHAT: return getStatusIcon("status/chat",type); |
---|
| 73 | case Presence.AWAY: return getStatusIcon("status/away",type); |
---|
| 74 | case Presence.XA: return getStatusIcon("status/xa",type); |
---|
| 75 | case Presence.DND: return getStatusIcon("status/dnd",type); |
---|
| 76 | case Presence.UNAVAILABLE: return getStatusIcon("status/offline",type); |
---|
| 77 | case Presence.INVISIBLE: return getStatusIcon("status/invisible",type); |
---|
| 78 | case Presence.NONE: return getStatusIcon("status/ask",type); |
---|
| 79 | default: return getStatusIcon("status/online",type); |
---|
| 80 | } |
---|
| 81 | } |
---|
| 82 | |
---|
[1091] | 83 | public static ImageIcon getStatusIcon(JIDStatus jidStatus) |
---|
[1014] | 84 | { |
---|
| 85 | return getStatusIcon(jidStatus.getShow(),jidStatus.getType()); |
---|
| 86 | } |
---|
| 87 | |
---|
[1091] | 88 | private static ImageIcon getStatusIcon(String show,String type) |
---|
[1014] | 89 | { |
---|
[1091] | 90 | |
---|
| 91 | if ( !statusIcons.containsKey(type) ) |
---|
| 92 | type = "default"; |
---|
| 93 | |
---|
[1014] | 94 | ImageIcon icon = (ImageIcon)((Map)statusIcons.get(type)).get(show); |
---|
[1091] | 95 | |
---|
| 96 | if (icon == null) |
---|
| 97 | icon = (ImageIcon)((Map)statusIcons.get("default")).get(show); |
---|
| 98 | |
---|
[1014] | 99 | return icon; |
---|
| 100 | } |
---|
| 101 | |
---|
[1091] | 102 | private void readImages(BufferedReader data,URL file) |
---|
[1014] | 103 | { |
---|
| 104 | try |
---|
| 105 | { |
---|
| 106 | Map map = new HashMap(); |
---|
| 107 | statusIcons.put(data.readLine(),map); |
---|
| 108 | while(true) |
---|
| 109 | { |
---|
| 110 | readSmilie(data,file,map); |
---|
| 111 | } |
---|
| 112 | } |
---|
| 113 | catch(EOFException e) |
---|
| 114 | { |
---|
[1165] | 115 | //e.printStackTrace(); |
---|
[1014] | 116 | } |
---|
| 117 | catch (IOException e2) |
---|
| 118 | { |
---|
[1165] | 119 | //e2.printStackTrace(); |
---|
[1014] | 120 | } |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | private void readSmilie(BufferedReader data,URL file,Map current) throws IOException |
---|
| 124 | { |
---|
[1091] | 125 | StringBuffer buffer = new StringBuffer(); |
---|
| 126 | |
---|
| 127 | while( true ) |
---|
[1014] | 128 | { |
---|
| 129 | int in2 = data.read(); |
---|
[1091] | 130 | if( in2 == -1) throw new EOFException(); |
---|
[1014] | 131 | char ca =(char)in2; |
---|
[1091] | 132 | if ( ca == ' ') |
---|
[1014] | 133 | { |
---|
| 134 | current.put(new String(buffer),readImage(data,file)); |
---|
| 135 | return; |
---|
| 136 | } |
---|
| 137 | buffer.append(ca); |
---|
| 138 | } |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | private Icon readImage(BufferedReader data,URL file) throws IOException |
---|
| 142 | { |
---|
[1091] | 143 | return new ImageIcon( new URL(file + data.readLine()) ); |
---|
[1014] | 144 | } |
---|
| 145 | } |
---|
[1091] | 146 | |
---|
[1014] | 147 | /* |
---|
| 148 | * Overrides for emacs |
---|
| 149 | * Local variables: |
---|
| 150 | * tab-width: 4 |
---|
| 151 | * End: |
---|
[1165] | 152 | */ |
---|