source: trunk/jabberit_messenger/java_source/src/nu/fw/jeti/images/StatusIcons.java @ 1014

Revision 1014, 8.5 KB checked in by alexandrecorreia, 15 years ago (diff)

Ticket #552 - Inclusão do projeto Java referente ao applet do módulo.

Line 
1/*
2 *      Jeti, a Java Jabber client, Copyright (C) 2001 E.S. de Boer 
3 *
4 *  This program is free software; you can redistribute it and/or modify
5 *  it under the terms of the GNU General Public License as published by
6 *  the Free Software Foundation; either version 2 of the License, or
7 *  (at your option) any later version.
8 *
9 *  This program is distributed in the hope that it will be useful,
10 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
11 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 *      GNU General Public License for more details.
13 *
14 *  You should have received a copy of the GNU General Public License
15 *  along with this program; if not, write to the Free Software
16 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17 *
18 *      For questions, comments etc,
19 *      use the website at http://jeti.jabberstudio.org
20 *  or mail/IM me at jeti@jabber.org
21 */
22
23package nu.fw.jeti.images;
24
25import java.io.*;
26import java.net.MalformedURLException;
27import java.net.URL;
28import java.util.*;
29
30import javax.swing.Icon;
31import javax.swing.ImageIcon;
32import javax.xml.parsers.FactoryConfigurationError;
33import javax.xml.parsers.ParserConfigurationException;
34import javax.xml.parsers.SAXParser;
35import javax.xml.parsers.SAXParserFactory;
36
37import nu.fw.jeti.backend.Start;
38import nu.fw.jeti.jabber.JIDStatus;
39import nu.fw.jeti.jabber.elements.Presence;
40import nu.fw.jeti.util.Preferences;
41
42import org.xml.sax.SAXException;
43
44/**
45 * @author E.S. de Boer
46 */
47
48public class StatusIcons
49{
50        private static Map statusIcons;
51        private SAXParser parser;
52
53        public StatusIcons(SAXParser parser)
54    {
55                if( statusIcons == null )
56                {
57                        try{
58                                new Icons(parser,"rostericons");
59                        }catch(IOException e)
60                        {
61                                e.printStackTrace();
62                        }
63                        statusIcons = new HashMap();
64                        loadDefaultPics();
65                        //loadWebstartPics();
66                        List iconList = Preferences.getPlugable("rostericons");
67                        for (Iterator i = iconList.iterator();i.hasNext();)
68                        {
69                                Object[] temp = (Object[])i.next();
70                                if(((Boolean)temp[1]).booleanValue())
71                                {
72                                        loadRosterIcon((String)temp[4],(String)temp[3]);
73                                }
74                        }
75                }
76        }
77       
78        public StatusIcons(){}
79       
80        private void loadParser()
81        {
82                try{parser = SAXParserFactory.newInstance().newSAXParser();}
83                catch (FactoryConfigurationError ex){ex.printStackTrace();}
84                catch (SAXException ex){ex.printStackTrace();}
85                catch (ParserConfigurationException ex){ex.printStackTrace();}
86        }
87       
88        protected void reloadRosterIcon(String name,String type)
89        {
90                if(!statusIcons.containsKey(type))loadRosterIcon(name, type);
91        }
92       
93        protected void loadRosterIcon(String filename,String type)
94        {
95                String name2 = filename.substring(0, filename.lastIndexOf("."));
96                InputStream stream = null;
97                URL url = null;
98                try
99                {
100                        url = getClass().getResource("/" + name2 + "/");
101                        if(url==null) url =  new URL ("jar:" + Start.dataURL +
102                                        "plugins/rostericons/" + filename +"!/" + name2 + "/");
103                        stream =  new URL(url,"icondef.xml").openStream();
104                }
105                catch (IOException ex)
106                {
107                        //ex.printStackTrace();
108                }
109                if(stream==null)
110                {
111                        System.out.println(filename + " not found");
112                        return;
113                }
114                try
115                {
116                        if(parser == null) loadParser();
117                        parser.parse(stream ,new RosterIconsHandler(url,statusIcons,type));
118                }
119                catch (SAXException ex)
120                {
121                        ex.getException().printStackTrace();
122                }
123                catch (IOException ex)
124                {
125                        ex.printStackTrace();
126                }
127        }
128
129        protected void unloadRosterIcon(String name)
130        {
131                statusIcons.remove(name);
132        }
133       
134
135        static public ImageIcon getOfflineIcon()
136        {
137                return getStatusIcon(Presence.UNAVAILABLE ,"default");
138        }
139       
140        static public ImageIcon getStatusIcon(int show){return getStatusIcon(show,"");}
141
142        /** verander
143         * calls getImageIcon(show) whith the default imageset
144         * @param show
145         * @return ImageIcon
146         */
147        static public ImageIcon getImageIcon(String show){return getStatusIcon(show,"images");}
148
149
150        /**
151         * calls getStatusIcon(show,type) whith the default imageset
152         * @param show
153         * @return ImageIcon
154         */
155        //static public ImageIcon getImageIcon(int image){return getStatusIcon(image,"images");}
156
157        //TODO change all calls with jidstatus
158        /**
159         * returns a status icon
160         * @param show one of chat,available,dnd,away,xa or unavailable
161         * @param type which image set
162         * @return ImageIcon
163         */
164        static public ImageIcon getStatusIcon(int show,String type)
165        {
166                switch (show)
167                {
168                        case Presence.FREE_FOR_CHAT: return  getStatusIcon("status/chat",type);
169                        case Presence.AWAY: return  getStatusIcon("status/away",type);
170                        case Presence.XA: return  getStatusIcon("status/xa",type);
171                        case Presence.DND: return  getStatusIcon("status/dnd",type);
172                        case Presence.UNAVAILABLE: return  getStatusIcon("status/offline",type);
173                        case Presence.INVISIBLE: return  getStatusIcon("status/invisible",type);
174                        case Presence.NONE: return  getStatusIcon("status/ask",type);
175                        default: return getStatusIcon("status/online",type);
176                }       
177        }
178       
179        static public ImageIcon getStatusIcon(JIDStatus jidStatus)
180        {
181                return getStatusIcon(jidStatus.getShow(),jidStatus.getType());
182        }
183       
184        static private ImageIcon getStatusIcon(String show,String type)
185        {
186                if(!statusIcons.containsKey(type)) type = "default";
187                ImageIcon icon = (ImageIcon)((Map)statusIcons.get(type)).get(show);
188                if (icon == null) icon = (ImageIcon)((Map)statusIcons.get("default")).get(show);
189                return icon;
190        }
191
192        public void loadDefaultPics()
193        {
194                // load default smilies
195                // readSmilies(new BufferedReader(new InputStreamReader(PlgEmoticons.class.getResourceAsStream("emoticons/msn/info.cfg"))),null);
196                // Now, search for and get the URL for this class.
197                String urlString = getClass().getResource("StatusIcons.class").toString();
198                // String urlString = url.toString();
199                // System.out.println(url);
200                try
201                {
202                        urlString = urlString.substring(0,urlString.lastIndexOf("StatusIcons.class"));
203                        readImages(new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("status.cfg"))),new URL(urlString));
204                        readImages(new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("images.cfg"))),new URL(urlString));
205                }catch(MalformedURLException e){e.printStackTrace(); }
206                catch(NullPointerException e){System.err.println("nu/fw/jeti/images/Status.cfg or images.cfg missing");}
207        }
208       
209       
210//      public static ImageIcon getImage(String location)
211//      {
212//              //get url for images
213//              URL url = null;
214//              try
215//              {
216//                      if (Start.remoteURL != null)
217//                      {
218//
219//                              url = new URL("jar:" + Start.remoteURL + location);
220//                      }
221//                      else url = new URL("jar:" + Start.localURL + location);
222//              }
223//              catch (MalformedURLException e)
224//              {
225//                      e.printStackTrace();
226//              }
227//              return new ImageIcon(url);
228//      }
229       
230
231        public void readImages(BufferedReader data,URL file)
232        {
233            try
234                {
235                        Map map = new HashMap();
236                        statusIcons.put(data.readLine(),map);
237                        while(true)
238                        {
239                                readSmilie(data,file,map);
240                        }
241                }
242                catch(EOFException e)
243                {
244                    //System.out.println(smilies);
245                }
246                catch (IOException e2)
247                {
248                    e2.printStackTrace();
249                }
250        }
251
252        private void readSmilie(BufferedReader data,URL file,Map current) throws IOException
253        {
254                StringBuffer buffer=new StringBuffer();
255                while(true)
256                {
257                        int in2 = data.read();
258                        if(in2 ==-1) throw new EOFException();
259                        char ca =(char)in2;
260                        if(ca ==' ')
261                        {
262                                current.put(new String(buffer),readImage(data,file));
263                                return;
264                        }
265                        buffer.append(ca);
266                }
267        }
268
269        private Icon readImage(BufferedReader data,URL file) throws IOException
270        {
271                //Icon icon=null;
272                java.net.URL picURL=null;
273                String resource = data.readLine();
274                picURL =new URL(file + resource);
275                //System.out.println(picURL);
276                //if(picURL ==null) System.err.println(resource +" not found");
277                return new ImageIcon(picURL);
278        }
279/*     
280        private void loadWebstartPics()
281        {
282                try
283                {
284                        Enumeration m=getClass().getClassLoader().getResources("info.cfg");
285                        for(Enumeration s =m;s.hasMoreElements();)
286                        {
287                                try
288                                {
289                                        //URL urlJar = new URL("jar:"+url+"!/");
290                                        URL urlJar = (URL )s.nextElement();
291                                        //JarFile jarFile = new JarFile(  ""  );
292                                        //BufferedReader data =new BufferedReader(new InputStreamReader(jarFile.getInputStream(jarFile.getEntry("info.cfg"))));
293
294                                        BufferedReader data = null;
295                                        data =new BufferedReader(new InputStreamReader(urlJar.openStream()));
296                                        String url = urlJar.toString();
297                                        url = url.substring(0,url.length()-8);
298                                        //System.out.println(url);
299                                        readImages(data,new URL(url));
300                                }
301                                catch(IOException e)
302                                {
303                                        //System.out.println(currentFile.getName() + " is not a valid jar file");
304                                        System.err.println(e.toString());
305                                }
306                        }
307                       
308                } catch (IOException e)
309                {
310                        // TODO Auto-generated catch block
311                        e.printStackTrace();
312                }
313               
314        }
315*/
316}
317/*
318 * Overrides for emacs
319 * Local variables:
320 * tab-width: 4
321 * End:
322 */
Note: See TracBrowser for help on using the repository browser.