source: branches/2.2/jabberit_messenger/java_source/src/nu/fw/jeti/images/StatusIcons.java @ 3102

Revision 3102, 3.5 KB checked in by amuller, 14 years ago (diff)

Ticket #986 - Efetuado merge para o Branch 2.2( atualizacao do modulo)

  • Property svn:executable set to *
Line 
1package nu.fw.jeti.images;
2
3import java.io.*;
4import java.net.MalformedURLException;
5import java.net.URL;
6import java.util.*;
7
8import javax.swing.Icon;
9import javax.swing.ImageIcon;
10
11import nu.fw.jeti.jabber.JIDStatus;
12import nu.fw.jeti.jabber.elements.Presence;
13
14public 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 */
Note: See TracBrowser for help on using the repository browser.