aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/impl/gui/main/chatroomslist/ChatRoomTableUI.java
blob: ace2d378262c882e00707f7c74a84eec0ec376c7 (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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
 * SIP Communicator, 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 javax.swing.event.*;
import javax.swing.table.*;

import net.java.sip.communicator.service.protocol.*;
import net.java.sip.communicator.util.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.*;
import net.java.sip.communicator.impl.gui.utils.*;

/**
 * The <tt>ChatRoomTableUI</tt> is the panel that contains the
 * <tt>ChatRoomsList</tt>.
 *
 * @author Damian Minkov
 */
public class ChatRoomTableUI
    extends SCScrollPane
    implements MouseListener
{
    /**
     * The table with available rooms.
     */
    private JTable chatRoomList = new JTable();

    /**
     * The model of the table with the available rooms.
     */
    private ChatRoomTableModel chatRoomsTableModel = null;

    /**
     * Creates the scroll panel containing the chat rooms list.
     * 
     * @param parentDialog Currently not used
     */
    public ChatRoomTableUI(ChatRoomTableDialog parentDialog)
    {
        this.initChatRoomList();

        this.setViewportView(chatRoomList);
        this.setHorizontalScrollBarPolicy(
            JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    }

    /**
     * Initializes the chat rooms list interface.
     */
    private void initChatRoomList()
    {
        this.chatRoomsTableModel = new ChatRoomTableModel(chatRoomList);

        this.chatRoomList.addMouseListener(this);

        this.chatRoomList.setDefaultRenderer(ProtocolProviderService.class,
            new ProtocolProviderTableCellRenderer());
        this.chatRoomList.setDefaultRenderer(ChatRoomWrapper.class,
            new ChatRoomTableCellRenderer());

        this.chatRoomList.setOpaque(false);
        this.chatRoomList.setModel(chatRoomsTableModel);

        ConferenceChatManager confChatManager
            = GuiActivator.getUIService().getConferenceChatManager();

        confChatManager.addChatRoomListChangeListener(chatRoomsTableModel);

//        this.chatRoomList.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
//        this.chatRoomList.getColumnModel().getColumn(0).setMinWidth(250);
//        this.chatRoomList.getColumnModel().getColumn(1).setMinWidth(250);
//        this.chatRoomList.getColumnModel().getColumn(2).setPreferredWidth(50);
    }

    /**
     * Removes the room that is currently selected.
     */
    void removeSelectedRoom()
    {
        ChatRoomWrapper chatRoomWrapper = 
            chatRoomsTableModel.getValueAt(chatRoomList.getSelectedRow());

        ConferenceChatManager conferenceManager = GuiActivator.getUIService()
            .getConferenceChatManager();
        conferenceManager.removeChatRoom(chatRoomWrapper);
    }

    /**
     * Clears any selected room.
     */
    void clearSelection()
    {
        this.chatRoomList.clearSelection();
    }

    /**
     * Adds listener for selection changes.
     * @param l
     */
    void addSelectionListener(ListSelectionListener l)
    {
        this.chatRoomList.getSelectionModel().addListSelectionListener(l);
    }

    /**
     * Returns the currently selected room.
     * @return the currently selected room.
     */
    ChatRoomWrapper getSelectedRoom()
    {
        return chatRoomsTableModel.getValueAt(chatRoomList.getSelectedRow());
    }

    /**
     * Opens the currently selected chat.
     */
    void openChatForSelection()
    {
        Object selectedValue = this.chatRoomsTableModel.getValueAt(
            this.chatRoomList.getSelectedRow());

        ChatRoomWrapper chatRoomWrapper;
        if (selectedValue instanceof ChatRoomWrapper)
            chatRoomWrapper = (ChatRoomWrapper) selectedValue;
        else
            return;

        if(!chatRoomWrapper.getChatRoom().isJoined())
        {
            GuiActivator.getUIService().getConferenceChatManager()
                .joinChatRoom(chatRoomWrapper);
        }

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

        chatWindowManager.openChat(chatPanel, true);
    }

    /**
     * Listens for double clicks to open the chat room.
     * @param e
     */
    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()))
        {
            int ix = this.chatRoomList.rowAtPoint(e.getPoint());

            if(ix != -1)
            {
                this.chatRoomList.setRowSelectionInterval(ix, ix);
            }
        }

        Object o = this.chatRoomsTableModel.getValueAt(
            this.chatRoomList.getSelectedRow());
        
        Point selectedCellPoint = e.getPoint();

        SwingUtilities.convertPointToScreen(selectedCellPoint, chatRoomList);

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

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

            rightButtonMenu.setInvoker(this);
            rightButtonMenu.setLocation(selectedCellPoint);
            rightButtonMenu.setVisible(true);
        }
    }

    /**
     * Renders the chat room with an icon corresponding its status in the table.
     */
    private class ChatRoomTableCellRenderer
        extends JLabel implements TableCellRenderer
    {
        /**
         * Creates the renderer.
         */
        public ChatRoomTableCellRenderer()
        {
            this.setHorizontalAlignment(JLabel.LEFT);
            this.setOpaque(true);
            this.setBorder(BorderFactory.createEmptyBorder(0, 8, 0, 0));
        }

        public Component getTableCellRendererComponent(
            JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column)
        {
            ChatRoomWrapper chatRoom = (ChatRoomWrapper)value;
            

            this.setText(chatRoom.getChatRoomName());

            Image chatRoomImage = ImageLoader
                .getImage(ImageLoader.CHAT_ROOM_16x16_ICON);

            if(chatRoom.getChatRoom() == null ||
                !chatRoom.getChatRoom().isJoined())
            {
                chatRoomImage
                    = LightGrayFilter.createDisabledImage(chatRoomImage);
            }

            this.setIcon(new ImageIcon(chatRoomImage));

            this.setFont(this.getFont().deriveFont(Font.PLAIN));

            if(isSelected)
                this.setBackground(table.getSelectionBackground());
            else
                this.setBackground(UIManager.getColor("Table.background"));

            return this;
        }
    }

    /**
     * Renders in the table the account with its protocol icon, which
     * is corresponding the current status of the protocol.
     */
    private class ProtocolProviderTableCellRenderer
        extends JLabel implements TableCellRenderer
    {
        /**
         * Creates the Renderer.
         */
        public ProtocolProviderTableCellRenderer()
        {
            this.setHorizontalAlignment(JLabel.LEFT);
            this.setOpaque(true);
            this.setBorder(BorderFactory.createEmptyBorder(0, 8, 0, 0));
        }

        public Component getTableCellRendererComponent(
            JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column)
        {
            ProtocolProviderService pps = (ProtocolProviderService)value;
            OperationSetPresence presence
                = pps.getOperationSet(OperationSetPresence.class);

            this.setText(pps.getAccountID().getDisplayName());

            byte[] protocolStatusImage = presence.getPresenceStatus().getStatusIcon();

            if(protocolStatusImage != null)
            {
                this.setIcon(new ImageIcon(protocolStatusImage));
            }
            else
            {
                this.setIcon(null);
            }

            if(isSelected)
                this.setBackground(table.getSelectionBackground());
            else
                this.setBackground(UIManager.getColor("Table.background"));

            return this;
        }        
    }
}