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 | |
---|
18 | public StatusIcons() |
---|
19 | { |
---|
20 | if( statusIcons == null ) |
---|
21 | { |
---|
22 | statusIcons = new HashMap(); |
---|
23 | |
---|
24 | String urlString = getClass().getResource("StatusIcons.class").toString(); |
---|
25 | try |
---|
26 | { |
---|
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 | { |
---|
33 | e.printStackTrace(); |
---|
34 | } |
---|
35 | catch(NullPointerException e) |
---|
36 | { |
---|
37 | System.err.println("nu/fw/jeti/images/Status.cfg or images.cfg missing"); |
---|
38 | } |
---|
39 | } |
---|
40 | } |
---|
41 | |
---|
42 | public static ImageIcon getOfflineIcon() |
---|
43 | { |
---|
44 | return getStatusIcon(Presence.UNAVAILABLE ,"default"); |
---|
45 | } |
---|
46 | |
---|
47 | public static ImageIcon getStatusIcon(int show) |
---|
48 | { |
---|
49 | return getStatusIcon(show,""); |
---|
50 | } |
---|
51 | |
---|
52 | /** verander |
---|
53 | * calls getImageIcon(show) whith the default imageset |
---|
54 | * @param show |
---|
55 | * @return ImageIcon |
---|
56 | */ |
---|
57 | public static ImageIcon getImageIcon(String show) |
---|
58 | { |
---|
59 | return getStatusIcon(show,"images"); |
---|
60 | } |
---|
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 | */ |
---|
68 | public static ImageIcon getStatusIcon(int show,String type) |
---|
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 | |
---|
83 | public static ImageIcon getStatusIcon(JIDStatus jidStatus) |
---|
84 | { |
---|
85 | return getStatusIcon(jidStatus.getShow(),jidStatus.getType()); |
---|
86 | } |
---|
87 | |
---|
88 | private static ImageIcon getStatusIcon(String show,String type) |
---|
89 | { |
---|
90 | |
---|
91 | if ( !statusIcons.containsKey(type) ) |
---|
92 | type = "default"; |
---|
93 | |
---|
94 | ImageIcon icon = (ImageIcon)((Map)statusIcons.get(type)).get(show); |
---|
95 | |
---|
96 | if (icon == null) |
---|
97 | icon = (ImageIcon)((Map)statusIcons.get("default")).get(show); |
---|
98 | |
---|
99 | return icon; |
---|
100 | } |
---|
101 | |
---|
102 | private void readImages(BufferedReader data,URL file) |
---|
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 | { |
---|
115 | //e.printStackTrace(); |
---|
116 | } |
---|
117 | catch (IOException e2) |
---|
118 | { |
---|
119 | //e2.printStackTrace(); |
---|
120 | } |
---|
121 | } |
---|
122 | |
---|
123 | private void readSmilie(BufferedReader data,URL file,Map current) throws IOException |
---|
124 | { |
---|
125 | StringBuffer buffer = new StringBuffer(); |
---|
126 | |
---|
127 | while( true ) |
---|
128 | { |
---|
129 | int in2 = data.read(); |
---|
130 | if( in2 == -1) throw new EOFException(); |
---|
131 | char ca =(char)in2; |
---|
132 | if ( ca == ' ') |
---|
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 | { |
---|
143 | return new ImageIcon( new URL(file + data.readLine()) ); |
---|
144 | } |
---|
145 | } |
---|
146 | |
---|
147 | /* |
---|
148 | * Overrides for emacs |
---|
149 | * Local variables: |
---|
150 | * tab-width: 4 |
---|
151 | * End: |
---|
152 | */ |
---|