001    /*
002     Copyright (C) 2003 Adam Olsen
003    
004     This program is free software; you can redistribute it and/or modify
005     it under the terms of the GNU General Public License as published by
006     the Free Software Foundation; either version 1, or (at your option)
007     any later version.
008    
009     This program is distributed in the hope that it will be useful,
010     but WITHOUT ANY WARRANTY; without even the implied warranty of
011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012     GNU General Public License for more details.
013    
014     You should have received a copy of the GNU General Public License
015     along with this program; if not, write to the Free Software
016     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
017     */
018    
019    package com.valhalla.jbother.groupchat;
020    
021    import java.awt.*;
022    import java.net.URL;
023    import java.util.Locale;
024    import java.util.ResourceBundle;
025    
026    import javax.swing.ImageIcon;
027    import javax.swing.JLabel;
028    import javax.swing.JList;
029    import javax.swing.ListCellRenderer;
030    
031    import com.valhalla.jbother.StatusIconCache;
032    import com.valhalla.jbother.jabber.MUCBuddyStatus;
033    import com.valhalla.settings.Settings;
034    
035    /**
036     * Renders the JList that represents the nickname list in a groupchat
037     *
038     * @author Adam Olsen
039     * @version 1.0
040     */
041    public class NickListRenderer extends JLabel implements ListCellRenderer {
042        private ResourceBundle resources = ResourceBundle.getBundle(
043                "JBotherBundle", Locale.getDefault());
044    
045        private ChatRoomPanel window;
046    
047        /**
048         * Sets up the renderer
049         *
050         * @param window
051         *            the window that this nicklist belongs to
052         */
053        public NickListRenderer(ChatRoomPanel window) {
054            this.window = window;
055            setOpaque(true);
056        }
057    
058        /**
059         * @see ListCellRenderer
060         */
061        public Component getListCellRendererComponent(JList list, Object value,
062                int index, boolean isSelected, boolean cellHasFocus) {
063    
064            String name = ((String)value).replaceAll( "^aa_aa\\d{4} ", "" );
065            MUCBuddyStatus buddy = window.getBuddyStatus(name);
066    
067            String nick = "";
068            String room = "";
069    
070            int a = buddy.getUser().indexOf("/");
071            if (a > -1) {
072                room = buddy.getUser().substring(0, a);
073                nick = buddy.getUser().substring(a + 1);
074            } else { //just in case
075                nick = buddy.getUser();
076                room = buddy.getUser();
077            }
078    
079            if (Settings.getInstance().getProperty("statusTheme") == null)
080                Settings.getInstance().setProperty("statusTheme", "default");
081            ImageIcon icon = StatusIconCache.getStatusIcon(buddy.getPresence(buddy
082                    .getHighestResource()));
083            if (icon != null)
084                setIcon(icon);
085    
086            URL light = getClass().getClassLoader().getResource(
087                    "images/lightbulb.png");
088            StringBuffer tooltip = new StringBuffer();
089                    tooltip.append( "<html><table border='0'><tr><td valign='top'><img src='" )
090                    .append( light.toString() )
091                    .append( "'></td><td>" )
092                    .append( "<b><font size='+1'>")
093                    .append( nick )
094                    .append( "</font></b>" )
095                    .append( "<table border='0' cellpadding='2' cellspacing='2'>" );
096    
097            // role and affiliation information
098            String role = buddy.getRole();
099            if (role == null || role.equals(""))
100                role = "none";
101            String affiliation = buddy.getAffiliation();
102            if (affiliation == null || affiliation.equals(""))
103                affiliation = "none";
104            if (buddy.getJid() != null)
105                tooltip.append( "<tr><td><b>JID:</b></td><td>" ).append( buddy.getJid() )
106                        .append( "</td></tr>\n" );
107            tooltip.append( "<tr><td><b>Role:</b></td><td>" ).append( role ).append( "</td></tr>\n" );
108            tooltip.append( "<tr><td><b>Affiliation:</b></td><td>" ).append( affiliation )
109                    .append( "</td></tr>\n" );
110    
111            String statusMessage = buddy.getStatusMessage(buddy
112                    .getHighestResource());
113            if (statusMessage != null && !statusMessage.equals("")) {
114                tooltip.append( "<tr><td><b>" ).append( this.resources.getString("status") )
115                        .append( ":</b></td><td>" ).append( statusMessage ).append( "</td></tr>" );
116            }
117    
118            String using = buddy.getVersionInfo();
119            if (using != null) {
120                tooltip.append( "<tr><td nowrap><b>" ).append( this.resources.getString("using") )
121                        .append( ":</b></td><td nowrap>" ).append( using ).append( "</td></tr>" );
122            }
123    
124            tooltip.append( "</table></td></tr></table></html>" );
125    
126            setToolTipText(tooltip.toString());
127    
128            URL type = getClass().getClassLoader().getResource(
129                    "imagethemes/muc/default/" + buddy.getRole() + ".png");
130            if (getClass().getClassLoader().getResource(
131                    "imagethemes/muc/default/" + buddy.getAffiliation() + ".png") != null) {
132                type = getClass().getClassLoader().getResource(
133                        "imagethemes/muc/default/" + buddy.getAffiliation()
134                                + ".png");
135            }
136    
137            if (type == null) {
138                type = getClass().getClassLoader().getResource(
139                        "imagethemes/muc/default/blank.png");
140            }
141    
142            if (type != null) {
143                nick = "<html><img src='" + type.toString() + "'>&nbsp;&nbsp;"
144                        + nick + "</html>";
145            }
146    
147            setText(nick);
148    
149            setBackground(isSelected ? list.getSelectionBackground() : Color.WHITE);
150            setForeground(isSelected ? list.getSelectionForeground() : list
151                    .getForeground());
152            list.validate();
153    
154            return this;
155        }
156    }