aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/plugin/contactinfo/ContactInfoContactPanel.java
blob: a90e56e1b3e053f6d76d7a05b7ee589d1c9098b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
 * Jitsi, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Distributable under LGPL license. See terms of license at gnu.org.
 */
package net.java.sip.communicator.plugin.contactinfo;

import java.awt.*;
import java.util.*;

import javax.swing.*;
import javax.swing.event.*;

import net.java.sip.communicator.plugin.desktoputil.*;
import net.java.sip.communicator.service.protocol.*;

/**
 * The left side panel of ContactInfoDialog. Display all associated subcontacts
 * and their respective protocols in a JList. If a user is selected, the
 * ContactInfoDetailsPanel will be updated to the current contact.
 *
 * @author Adam Goldstein
 * @author Yana Stamcheva
 */
public class ContactInfoContactPanel
    extends TransparentPanel
{
    /**
     * Serial version UID.
     */
    private static final long serialVersionUID = 0L;

    /**
     * The list of all subcontacts related to the selected contact.
     */
    private JList contactList = new JList();

    /**
     * The scroll pane containing the list of all sub contacts of a selected
     * contact.
     */
    private JScrollPane contactScrollPane = new JScrollPane();

    private DefaultListModel contactListModel = new DefaultListModel();

    /**
     * The parent dialog that makes the connection between the contacts and
     * the details panel.
     */
    private ContactInfoDialog contactInfoDialog;

    /**
     * Create a panel with a list of all sub-contacts associated with the
     * contact that was originally selected. Whenever a sub-contact is picked,
     * notifies the protocolPanel of the change and it will update the displayed
     * details.
     *
     * @param contacts the list of contacts
     * @param dialog the contact info dialog
     */
    public ContactInfoContactPanel( Iterator<Contact> contacts,
                                    ContactInfoDialog dialog)
    {
        super(new BorderLayout());

        this.contactInfoDialog = dialog;

        this.setBorder(BorderFactory.createCompoundBorder(BorderFactory
            .createTitledBorder(Resources.getString("service.gui.CONTACTS")),
            BorderFactory.createEmptyBorder(5, 5, 5, 5)));

        this.contactList.setOpaque(false);
        this.contactList.setModel(contactListModel);
        this.contactList.setCellRenderer(new ContactPanelCellRenderer());
        this.contactList.addListSelectionListener(new ListSelectionListener()
        {
            public void valueChanged(ListSelectionEvent e)
            {
                // When the user release the mouse button and completes the
                // selection, getValueIsAdjusting() becomes false
                if (!e.getValueIsAdjusting())
                {
                    JList list = (JList) e.getSource();

                    Contact selectedContact
                        = (Contact) list.getSelectedValue();

                    contactInfoDialog.loadContactDetails(selectedContact);
                }
            }
        });

        boolean isFirstIter = true;
        while (contacts.hasNext())
        {
            Contact contact = contacts.next();

            this.contactListModel.addElement(contact);

            if (isFirstIter)
            {
                isFirstIter = false;
                contactInfoDialog.loadContactDetails(contact);
                contactList.setSelectedIndex(0);
            }
        }

        this.contactScrollPane.setPreferredSize(new Dimension(100, 200));
        this.contactScrollPane.getViewport().add(contactList);
        this.add(contactScrollPane);
    }

    /**
     * A cell renderer that allows both text and icons in our contactList.
     */
    private static class ContactPanelCellRenderer
        extends DefaultListCellRenderer
    {
        /**
         * Serial version UID.
         */
        private static final long serialVersionUID = 0L;

        private boolean isSelected;

        private Color blueGreyBorderColor = new Color(131, 149, 178);

        private Color selectedColor = new Color(209, 212, 225);

        public ContactPanelCellRenderer()
        {
            this.setOpaque(false);
        }

        /**
         * Renders a <tt>Contact</tt> object in a JList, by visualizing
         * the contact name and the protocol icon.
         *
         * @param list the rendered JList
         * @param value the object to be rendered
         * @param index the index of the object in the list
         * @param isSelected indicates if the rendered object is selected
         * @param cellHasFocus indicates if the rendered object is in a focused
         * cell
         */
        @Override
        public Component getListCellRendererComponent(  JList list,
                                                        Object value,
                                                        int index,
                                                        boolean isSelected,
                                                        boolean cellHasFocus)
        {
            super.getListCellRendererComponent(list, value, index, isSelected,
                cellHasFocus);

            this.isSelected = isSelected;

            Contact contact = (Contact) value;

            this.setIcon(new ImageIcon(contact.getProtocolProvider()
                .getProtocolIcon().getIcon(ProtocolIcon.ICON_SIZE_16x16)));
            this.setText(((Contact) value).getDisplayName());

            return this;
        }

        /**
         * Paint a round blue border and background when a cell is selected.
         */
        @Override
        public void paintComponent(Graphics g)
        {
            if (this.isSelected)
            {
                Graphics2D g2 = (Graphics2D) g.create();

                try
                {
                    AntialiasingManager.activateAntialiasing(g2);

                    int width = getWidth();
                    int height = getHeight();

                    g2.setColor(selectedColor);
                    g2.fillRoundRect(1, 0, width, height, 7, 7);

                    g2.setColor(blueGreyBorderColor);
                    g2.setStroke(new BasicStroke(1.5f));
                    g2.drawRoundRect(1, 0, width - 2, height - 1, 7, 7);
                }
                finally
                {
                    g2.dispose();
                }
            }

            super.paintComponent(g);
        }
    }
}