aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/impl/gui/main/chatroomslist/ChatRoomListUI.java
blob: 4ca0420881ba5f0c2d92fe003b76f15d5e5bfcf8 (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
/*
 * 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.chatroomslist;

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

import javax.swing.*;

import net.java.sip.communicator.impl.gui.*;
import net.java.sip.communicator.impl.gui.customcontrols.*;
import net.java.sip.communicator.impl.gui.main.chat.*;
import net.java.sip.communicator.impl.gui.main.chat.conference.*;

/**
 * The <tt>ChatRoomsListPanel</tt> is the panel that contains the
 * <tt>ChatRoomsList</tt>.
 *
 * @author Yana Stamcheva
 * @author Lubomir Marinov
 */
public class ChatRoomListUI
    extends SCScrollPane
    implements  MouseListener,
                ChatRoomListChangeListener,
                AdHocChatRoomListChangeListener
{
    private final JList chatRoomList = new JList();

    private final ChatRoomListModel chatRoomsListModel
        = new ChatRoomListModel();

    private final JPanel treePanel = new JPanel(new BorderLayout());

    /**
     * Creates the scroll panel containing the chat rooms list.
     * 
     * @param parentDialog Currently not used
     */
    public ChatRoomListUI(ChatRoomListDialog parentDialog)
    {
        ConferenceChatManager confChatManager
            = GuiActivator.getUIService().getConferenceChatManager();

        confChatManager.addChatRoomListChangeListener(this);
        confChatManager.addAdHocChatRoomListChangeListener(this);

        this.treePanel.add(chatRoomList, BorderLayout.NORTH);

        this.setViewportView(treePanel);

        this.setHorizontalScrollBarPolicy(
            JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

        this.getActionMap().put("runChat", new ChatRoomsListPanelEnterAction());

        InputMap imap = this.getInputMap(
                JComponent.WHEN_IN_FOCUSED_WINDOW);

        imap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "runChat");

        this.setPreferredSize(new Dimension(200, 450));
        this.setMinimumSize(new Dimension(80, 200));

        this.initChatRoomList();
    }

    /**
     * Initializes the chat rooms list interface.
     */
    private void initChatRoomList()
    {
        this.chatRoomList.setCursor(
            Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));

        this.chatRoomList.setOpaque(false);

        this.chatRoomList.setModel(chatRoomsListModel);
        this.chatRoomList.setCellRenderer(new ChatRoomsListCellRenderer());

        this.chatRoomList.addMouseListener(this);

    }

    private void openChatForSelection()
    {
        Object selectedValue = chatRoomList.getSelectedValue();
        ChatRoomWrapper chatRoomWrapper;

        if (selectedValue instanceof ChatRoomProviderWrapper)
            chatRoomWrapper
                = ((ChatRoomProviderWrapper) selectedValue)
                        .getSystemRoomWrapper();
        else if (selectedValue instanceof ChatRoomWrapper)
            chatRoomWrapper = (ChatRoomWrapper) selectedValue;
        else
            return;

        ChatWindowManager chatWindowManager
            = GuiActivator.getUIService().getChatWindowManager();
        ChatPanel chatPanel
            = chatWindowManager.getMultiChat(chatRoomWrapper, true);

        chatWindowManager.openChat(chatPanel, true);
    }
    /**
     * Opens chat window when the selected value is a MetaContact and opens a
     * group when the selected value is a MetaContactGroup.
     */
    private class ChatRoomsListPanelEnterAction
        extends AbstractAction
    {
        public void actionPerformed(ActionEvent e)
        {
            openChatForSelection();
        }
    }

    public void mouseClicked(MouseEvent e)
    {
        if (((e.getModifiers() & InputEvent.BUTTON1_MASK) != 0)
                && (e.getClickCount() > 1))
            openChatForSelection();
    }

    public void mouseEntered(MouseEvent e)
    {}

    public void mouseExited(MouseEvent e)
    {}
    
    public void mouseReleased(MouseEvent e)
    {}

    /**
     * A chat room was selected. Opens the chat room in the chat window.
     *
     * @param e the <tt>MouseEvent</tt> instance containing details of
     * the event that has just occurred.
     */
    public void mousePressed(MouseEvent e)
    {
        //Select the object under the right button click.
        if ((e.getModifiers() & InputEvent.BUTTON2_MASK) != 0
            || (e.getModifiers() & InputEvent.BUTTON3_MASK) != 0
            || (e.isControlDown() && !e.isMetaDown()))
        {
            this.chatRoomList.setSelectedIndex(
                chatRoomList.locationToIndex(e.getPoint()));
        }

        Object o = chatRoomList.getSelectedValue();

        Point selectedCellPoint
            = chatRoomList.indexToLocation(chatRoomList.getSelectedIndex());

        SwingUtilities.convertPointToScreen(selectedCellPoint, chatRoomList);

        if ((e.getModifiers() & InputEvent.BUTTON3_MASK) != 0)
        {
            JPopupMenu rightButtonMenu;

            if(o instanceof ChatRoomProviderWrapper)
                rightButtonMenu
                    = new ChatRoomServerRightButtonMenu(
                            (ChatRoomProviderWrapper) o);
            else if (o instanceof ChatRoomWrapper)
                rightButtonMenu
                    = new ChatRoomRightButtonMenu((ChatRoomWrapper) o);
            else
                return;

            rightButtonMenu.setInvoker(this);
            rightButtonMenu
                .setLocation(selectedCellPoint.x, selectedCellPoint.y + 20);
            rightButtonMenu.setVisible(true);
        }
    }

    /**
     * Refreshes the chat room's list when a modification in the model has
     * occurred.
     */
    public void contentChanged(ChatRoomListChangeEvent evt)
    {
        ChatRoomWrapper chatRoomWrapper = evt.getSourceChatRoom();

        if (evt.getEventID() == ChatRoomListChangeEvent.CHAT_ROOM_ADDED)
        {
            int index = chatRoomsListModel.indexOf(chatRoomWrapper);

            if (index != -1)
                chatRoomsListModel.contentAdded(index, index);
        }
        else if (evt.getEventID() == ChatRoomListChangeEvent.CHAT_ROOM_REMOVED)
        {
            int groupIndex = chatRoomsListModel.indexOf(
                chatRoomWrapper.getParentProvider());

            int listSize = chatRoomsListModel.getSize();

            if (groupIndex != -1 && listSize > 0)
            {
                chatRoomsListModel.contentChanged(groupIndex, listSize - 1);
                chatRoomsListModel.contentRemoved(listSize, listSize);
            }
        }
        else if (evt.getEventID() == ChatRoomListChangeEvent.CHAT_ROOM_CHANGED)
        {
            int index = chatRoomsListModel.indexOf(chatRoomWrapper);

            chatRoomsListModel.contentChanged(index, index);
        }
    }

    /**
     * Updates the chat room list model when notified of a change in the chat
     * room list.
     */
    public void contentChanged(AdHocChatRoomListChangeEvent evt)
    {
        AdHocChatRoomWrapper chatRoomWrapper = evt.getSourceAdHocChatRoom();

        if (evt.getEventID()
                == AdHocChatRoomListChangeEvent.AD_HOC_CHAT_ROOM_ADDED)
        {
            int index = chatRoomsListModel.indexOf(chatRoomWrapper);

            if (index != -1)
                chatRoomsListModel.contentAdded(index, index);
        }
        else if (evt.getEventID()
                == AdHocChatRoomListChangeEvent.AD_HOC_CHAT_ROOM_REMOVED)
        {
            int groupIndex = chatRoomsListModel.indexOf(
                chatRoomWrapper.getParentProvider());

            int listSize = chatRoomsListModel.getSize();

            if (groupIndex != -1 && listSize > 0)
            {
                chatRoomsListModel.contentChanged(groupIndex, listSize - 1);
                chatRoomsListModel.contentRemoved(listSize, listSize);
            }
        }
        else if (evt.getEventID()
                == AdHocChatRoomListChangeEvent.AD_HOC_CHAT_ROOM_CHANGED)
        {
            int index = chatRoomsListModel.indexOf(chatRoomWrapper);

            chatRoomsListModel.contentChanged(index, index);
        }
    }
}