aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/impl/gui/main/contactlist/GroupRightButtonMenu.java
blob: 8c45425e6e398c5ff594d43dd5ea16e5431c67f5 (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
/*
 * 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 net.java.sip.communicator.impl.gui.*;
import net.java.sip.communicator.impl.gui.event.*;
import net.java.sip.communicator.impl.gui.main.*;
import net.java.sip.communicator.impl.gui.utils.*;
import net.java.sip.communicator.service.contactlist.*;
import net.java.sip.communicator.service.gui.*;
import net.java.sip.communicator.service.gui.Container;
import net.java.sip.communicator.util.*;
import net.java.sip.communicator.util.skin.*;
import net.java.sip.communicator.util.swing.*;

import org.osgi.framework.*;

/**
 * The GroupRightButtonMenu is the menu, opened when user clicks with the right
 * mouse button on a group in the contact list. Through this menu the user could
 * add a contact to a group.
 *
 * @author Yana Stamcheva
 * @author Adam Netocny
 */
public class GroupRightButtonMenu
    extends SIPCommPopupMenu
    implements  ActionListener,
                PluginComponentListener,
                Skinnable
{
    private final Logger logger = Logger.getLogger(GroupRightButtonMenu.class);

    private final JMenuItem addContactItem
        = new JMenuItem(GuiActivator.getResources().getI18NString(
            "service.gui.ADD_CONTACT") + "...");

    private final JMenuItem removeGroupItem
        = new JMenuItem(GuiActivator.getResources().getI18NString(
            "service.gui.REMOVE_GROUP"));

    private final JMenuItem renameGroupItem
        = new JMenuItem(GuiActivator.getResources().getI18NString(
            "service.gui.RENAME_GROUP"));

    private final MetaContactGroup group;

    private final MainFrame mainFrame;

    /**
     * Creates an instance of GroupRightButtonMenu.
     * 
     * @param mainFrame The parent <tt>MainFrame</tt> window.
     * @param group The <tt>MetaContactGroup</tt> for which the menu is opened.
     */
    public GroupRightButtonMenu(MainFrame mainFrame, MetaContactGroup group)
    {
        this.group = group;
        this.mainFrame = mainFrame;

        if (!ConfigurationManager.isAddContactDisabled())
            this.add(addContactItem);

        this.addSeparator();

        this.add(renameGroupItem);
        this.add(removeGroupItem);

        this.addContactItem.setMnemonic(GuiActivator.getResources()
            .getI18nMnemonic("service.gui.ADD_CONTACT"));

        this.renameGroupItem.setMnemonic(GuiActivator.getResources()
            .getI18nMnemonic("service.gui.RENAME_GROUP"));

        this.removeGroupItem.setMnemonic(GuiActivator.getResources()
            .getI18nMnemonic("service.gui.REMOVE_GROUP"));

        this.addContactItem.addActionListener(this);
        this.renameGroupItem.addActionListener(this);
        this.removeGroupItem.addActionListener(this);

        loadSkin();

        this.initPluginComponents();
    }

    /**
     * Initializes all plugin components.
     */
    private void initPluginComponents()
    {
        // Search for plugin components registered through the OSGI bundle
        // context.
        ServiceReference[] serRefs = null;

        String osgiFilter =
            "(" + Container.CONTAINER_ID + "="
                + Container.CONTAINER_GROUP_RIGHT_BUTTON_MENU.getID() + ")";

        try
        {
            serRefs =
                GuiActivator.bundleContext.getServiceReferences(
                    PluginComponent.class.getName(), osgiFilter);
        }
        catch (InvalidSyntaxException exc)
        {
            logger.error("Could not obtain plugin reference.", exc);
        }

        if (serRefs != null)
        {
            for (int i = 0; i < serRefs.length; i++)
            {
                PluginComponent component =
                    (PluginComponent) GuiActivator.bundleContext
                        .getService(serRefs[i]);
                ;

                component.setCurrentContactGroup(group);

                this.add((Component) component.getComponent());

                this.repaint();
            }
        }

        GuiActivator.getUIService().addPluginComponentListener(this);
    }

    /**
     * Handles the <tt>ActionEvent</tt>. The chosen menu item should correspond
     * to an account, where the new contact will be added. We obtain here the
     * protocol provider corresponding to the chosen account and show the
     * dialog, where the user could add the contact.
     * 
     * @param e the <tt>ActionEvent</tt> that notified us
     */
    public void actionPerformed(ActionEvent e)
    {
        JMenuItem item = (JMenuItem) e.getSource();

        if (item.equals(removeGroupItem))
        {
            if (group != null)
                MetaContactListManager.removeMetaContactGroup(group);
        }
        else if (item.equals(renameGroupItem))
        {

            RenameGroupDialog dialog = new RenameGroupDialog(mainFrame, group);

            dialog.setLocation(
                Toolkit.getDefaultToolkit().getScreenSize().width / 2 - 200,
                Toolkit.getDefaultToolkit().getScreenSize().height / 2 - 50);

            dialog.setVisible(true);

            dialog.requestFocusInFiled();
        }
        else if (item.equals(addContactItem))
        {
            AddContactDialog dialog
                = new AddContactDialog(mainFrame);

            dialog.setSelectedGroup(group);

            dialog.setVisible(true);
        }
    }

    /**
     * Indicates that a plugin component has been added to this container.
     * 
     * @param event the <tt>PluginComponentEvent</tt> that notified us
     */
    /**
     * Indicates that a new plugin component has been added. Adds it to this
     * container if it belongs to it.
     * 
     * @param event the <tt>PluginComponentEvent</tt> that notified us
     */
    public void pluginComponentAdded(PluginComponentEvent event)
    {
        PluginComponent c = event.getPluginComponent();

        if (!c.getContainer().equals(
            Container.CONTAINER_GROUP_RIGHT_BUTTON_MENU))
            return;

        this.add((Component) c.getComponent());

        c.setCurrentContactGroup(group);

        this.repaint();
    }

    /**
     * Indicates that a new plugin component has been removed. Removes it to
     * from this container if it belongs to it.
     * 
     * @param event the <tt>PluginComponentEvent</tt> that notified us
     */
    public void pluginComponentRemoved(PluginComponentEvent event)
    {
        PluginComponent c = event.getPluginComponent();

        if (c.getContainer()
            .equals(Container.CONTAINER_GROUP_RIGHT_BUTTON_MENU))
        {
            this.remove((Component) c.getComponent());
        }
    }

    /**
     * Reloads label icons.
     */
    public void loadSkin()
    {
        addContactItem.setIcon(new ImageIcon(ImageLoader
            .getImage(ImageLoader.ADD_CONTACT_16x16_ICON)));

        removeGroupItem.setIcon(new ImageIcon(ImageLoader
            .getImage(ImageLoader.DELETE_16x16_ICON)));

        renameGroupItem.setIcon(new ImageIcon(ImageLoader
            .getImage(ImageLoader.RENAME_16x16_ICON)));
    }
}