aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/impl/gui/utils/PluginContainer.java
blob: 5d5135ff1d5d741e3570ab81826b88c7b3a082c3 (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
/*
 * 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.utils;

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

import javax.swing.*;

import org.osgi.framework.*;

import net.java.sip.communicator.impl.gui.*;
import net.java.sip.communicator.impl.gui.event.*;
import net.java.sip.communicator.service.gui.*;
import net.java.sip.communicator.service.gui.Container;
import net.java.sip.communicator.util.*;

/**
 * Provides capabilities to a specific <code>JComponent</code> to contain
 * <code>PluginComponent</code>s, track when they are added and removed.
 * 
 * @author Lubomir Marinov
 */
public class PluginContainer
    implements PluginComponentListener
{
    private static final Logger logger
        = Logger.getLogger(PluginContainer.class);

    /**
     * The <code>JComponent</code> which contains the components of the
     * <code>PluginComponent</code>s managed by this instance.
     */
    private final JComponent container;

    /**
     * The container id of the <code>PluginComponent</code> managed by this
     * instance.
     */
    private final Container containerId;

    /**
     * The list of <code>PluginComponent</code> instances which have their
     * components added to this <code>PluginContainer</code>.
     */
    private final java.util.List<PluginComponent> pluginComponents
        = new LinkedList<PluginComponent>();

    /**
     * Initializes a new <code>PluginContainer</code> instance which is to
     * provide capabilities to a specific <code>JComponent</code> container with
     * a specific <code>Container</code> id to contain
     * <code>PluginComponent</code> and track when they are added and removed.
     * 
     * @param container
     *            the <code>JComponent</code> container the new instance is to
     *            provide its capabilities to
     * @param containerId
     *            the <code>Container</code> id of the specified
     *            <code>container</code>
     */
    public PluginContainer(JComponent container, Container containerId)
    {
        this.container = container;
        this.containerId = containerId;

        initPluginComponents();
    }

    /**
     * Adds a specific <code>Component</code> to a specific
     * <code>JComponent</code> container. Allows extenders to apply custom logic
     * to the exact placement of the specified <code>Component</code> in the
     * specified container.
     * 
     * @param component
     *            the <code>Component</code> to be added to the specified
     *            <code>JComponent</code> container
     * @param container
     *            the <code>JComponent</code> container to add the specified
     *            <code>Component</code> to
     */
    protected void addComponentToContainer(
        Component component,
        JComponent container)
    {
        container.add(component);
    }

    /**
     * Adds the component of a specific <code>PluginComponent</code> to this
     * <code>JMenuBar</code>.
     * 
     * @param c
     *            the <code>PluginComponent</code> which is to have its
     *            component added to this <code>PluginContainer</code>
     */
    private void addPluginComponent(PluginComponent c)
    {
        if (pluginComponents.contains(c))
            return;

        addComponentToContainer((Component) c.getComponent(), container);
        pluginComponents.add(c);

        container.revalidate();
        container.repaint();
    }

    public Iterable<PluginComponent> getPluginComponents()
    {
        return pluginComponents;
    }

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

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

        if (serRefs != null)
        {
            for (ServiceReference serRef : serRefs)
            {
                PluginComponent component
                    = (PluginComponent)
                        GuiActivator.bundleContext.getService(serRef);

                addPluginComponent(component);
            }
        }

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

    /**
     * Runs clean-up for associated resources which need explicit disposal (e.g.
     * listeners keeping this instance alive because they were added to the
     * model which operationally outlives this instance).
     */
    public void dispose()
    {
        GuiActivator.getUIService().removePluginComponentListener(this);

        /*
         * Explicitly remove the components of the PluginComponent instances
         * because the latter are registered with OSGi and are thus global.
         */
        for (PluginComponent pluginComponent : pluginComponents)
            container.remove((Component) pluginComponent.getComponent());
        pluginComponents.clear();
    }

    /*
     * Implements
     * PluginComponentListener#pluginComponentAdded(PluginComponentEvent).
     */
    public void pluginComponentAdded(PluginComponentEvent event)
    {
        PluginComponent c = event.getPluginComponent();

        if (c.getContainer().equals(containerId))
            addPluginComponent(c);
    }

    /*
     * Implements
     * PluginComponentListener#pluginComponentRemoved(PluginComponentEvent).
     */
    public void pluginComponentRemoved(PluginComponentEvent event)
    {
        PluginComponent c = event.getPluginComponent();

        if (c.getContainer().equals(containerId))
            removePluginComponent(c);
    }

    /**
     * Removes the component of a specific <code>PluginComponent</code> from
     * this <code>PluginContainer</code>.
     * 
     * @param c
     *            the <code>PluginComponent</code> which is to have its
     *            component removed from this <code>PluginContainer</code>
     */
    private void removePluginComponent(PluginComponent c)
    {
        container.remove((Component) c.getComponent());
        pluginComponents.remove(c);
    }
}