aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/impl/gui/main/contactlist/DefaultTreeContactList.java
blob: b795c48d62c1d1911c11c459131df6188c8fdd0f (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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
 * 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.impl.gui.main.contactlist;

import java.awt.*;
import java.awt.event.*;

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

import net.java.sip.communicator.impl.gui.*;
import net.java.sip.communicator.impl.gui.lookandfeel.*;
import net.java.sip.communicator.impl.gui.main.*;
import net.java.sip.communicator.impl.gui.main.chat.*;
import net.java.sip.communicator.impl.gui.main.contactlist.contactsource.*;
import net.java.sip.communicator.impl.gui.utils.*;
import net.java.sip.communicator.service.contactlist.*;
import net.java.sip.communicator.util.skin.*;

/**
 * DeafultContactlist used to display <code>JList</code>s with contacts.
 *
 * @author Damian Minkov
 * @author Yana Stamcheva
 * @author Lubomir Marinov
 */
public class DefaultTreeContactList
    extends JTree
    implements Skinnable
{
    private static final long serialVersionUID = 0L;

    /**
     * Class id key used in UIDefaults.
     */
    private static final String uiClassID =
        DefaultTreeContactList.class.getName() +  "TreeUI";

    /**
     * Adds the ui class to UIDefaults.
     */
    static
    {
        UIManager.getDefaults().put(uiClassID,
            SIPCommTreeUI.class.getName());
    }

    /**
     * The cached selection event.
     */
    private TreeSelectionEvent myCachedSelectionEvent;

    /**
     * The tree cell renderer.
     */
    private ContactListTreeCellRenderer renderer;

    /**
     * Creates an instance of <tt>DefaultContactList</tt>.
     */
    public DefaultTreeContactList()
    {
        this.setBackground(Color.WHITE);
        this.setDragEnabled(true);
        this.setTransferHandler(new ContactListTransferHandler(this));
        this.getSelectionModel().
            setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);

        renderer = new ContactListTreeCellRenderer();
        this.setCellRenderer(renderer);

        ToolTipManager.sharedInstance().registerComponent(this);

        // By default 2 successive clicks are need to begin dragging.
        // Workaround provided by simon@tardell.se on 29-DEC-2002 for bug 4521075
        // http://bugs.sun.com/bugdatabase/view_bug.do;jsessionid=a13e98ab2364524506eb91505565?bug_id=4521075
        // "Drag gesture in JAVA different from Windows". The bug is also noticed
        // on Mac Leopard.
        addMouseListener(new MouseAdapter()
        {
            public void mouseReleased(MouseEvent e)
            {
                if(myCachedSelectionEvent == null)
                    return;

                DefaultTreeContactList.super
                    .fireValueChanged(myCachedSelectionEvent);

                myCachedSelectionEvent = null;
            }
        });
    }

    /**
     * Returns the currently selected object in the contact list. If there's
     * no selection, returns null.
     *
     * @return the currently selected object
     */
    public Object getSelectedValue()
    {
        TreePath selectionPath = getSelectionPath();

        if (selectionPath != null)
            return selectionPath.getLastPathComponent();

        return null;
    }

    /**
     * Checks if the given contact is currently active.
     * Dummy method used and overridden from classes extending this
     * functionality such as ContactList.
     *
     * @param contact the <tt>MetaContact</tt> to verify
     * @return TRUE if the given <tt>MetaContact</tt> is active, FALSE -
     * otherwise
     */
    public boolean isContactActive(UIContact contact)
    {
        return false;
    }

    /**
     * Checks if the given contact is currently active.
     * Dummy method used and overridden from classes extending this
     * functionality such as ContactList.
     *
     * @param metaContact the <tt>MetaContact</tt> to verify
     * @return TRUE if the given <tt>MetaContact</tt> is active, FALSE -
     * otherwise
     */
    public boolean isContactActive(MetaContact metaContact)
    {
        UIContact uiContact;
        synchronized (metaContact)
        {
            uiContact = MetaContactListSource.getUIContact(metaContact);
        }

        if (uiContact != null)
            return isContactActive(uiContact);
        return false;
    }

    /**
     * Creates a customized tooltip for this contact list.
     *
     * @return The customized tooltip.
     */
    public JToolTip createToolTip()
    {
        Point currentMouseLocation = MouseInfo.getPointerInfo().getLocation();

        SwingUtilities.convertPointFromScreen(currentMouseLocation, this);

        TreePath path = this.getClosestPathForLocation(
            currentMouseLocation.x, currentMouseLocation.y);
        Object element = path.getLastPathComponent();

        MainFrame mainWindow = GuiActivator.getUIService().getMainFrame();

        ExtendedTooltip tip = null;
        if (element instanceof ContactNode)
        {
            UIContact contact
                = ((ContactNode) element).getContactDescriptor();

            tip = contact.getToolTip();
            if (tip == null)
            {
                tip = new ExtendedTooltip(mainWindow, true);
                tip.setTitle(contact.getDisplayName());
            }
        }
        else if (element instanceof GroupNode)
        {
            UIGroup group
                = ((GroupNode) element).getGroupDescriptor();

            tip = new ExtendedTooltip(mainWindow, true);
            tip.setTitle(group.getDisplayName());
        }
        else if (element instanceof ChatContact<?>)
        {
            ChatContact<?> chatContact = (ChatContact<?>) element;

            ImageIcon avatarImage = chatContact.getAvatar();

            tip = new ExtendedTooltip(mainWindow, true);
            if (avatarImage != null)
                tip.setImage(avatarImage);

            tip.setTitle(chatContact.getName());
        }

        tip.setComponent(this);

        return tip;
    }

    /**
     * Gets the <tt>String</tt> to be used as the tool tip text for the mouse
     * location given by a specific <tt>MouseEvent</tt>.
     * <tt>DefaultTreeContactList</tt> only overrides in order to return a
     * different <tt>String</tt> each time in order to make
     * <tt>TooltipManager</tt> change the tool tip over the different nodes of
     * this <tt>JTree</tt>.
     *
     * @param event the <tt>MouseEvent</tt> which gives the mouse location to
     * get the tool tip text for
     * @return the <tt>String</tt> to be used as the tool tip text for the mouse
     * location given by the specified <tt>MouseEvent</tt>
     */
    @Override
    public String getToolTipText(MouseEvent event)
    {
        TreePath path = getClosestPathForLocation(event.getX(), event.getY());

        if (path != null)
        {
            Object element = path.getLastPathComponent();

            /*
             * Since it does not seem obvious how to get a unique String ID for
             * element even after converting to ContactNode, GroupNode or
             * ChatContact, just make up a String which is very likely to be
             * different for the different nodes in this JTree.
             */
            return
                "className= "
                    + element.getClass().getName()
                    + ", hashCode= "
                    + element.hashCode()
                    + ", toString= "
                    + element.toString();
        }
        return null;
    }

    /**
     * Reloads renderer resources for this tree.
     */
    public void loadSkin()
    {
        renderer.loadSkin();
    }

    /**
     * Returns the name of the L&F class that renders this component.
     *
     * @return the string "TreeUI"
     * @see JComponent#getUIClassID
     * @see UIDefaults#getUI
     */
    public String getUIClassID()
    {
        return uiClassID;
    }
}