aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/plugin/pluginmanager/PluginManagerPanel.java
blob: bc59d42981d877b4a198e93fc0b2c44022fb0176 (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
/*
 * 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.plugin.pluginmanager;

import java.awt.*;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;

import net.java.sip.communicator.plugin.desktoputil.*;

import org.osgi.framework.*;

/**
 * @author Yana Stamcheva
 */
public class PluginManagerPanel
    extends TransparentPanel
{
    /**
     * Serial version UID.
     */
    private static final long serialVersionUID = 0L;

    private final JTable pluginTable = new JTable();

    private final PluginTableModel tableModel = new PluginTableModel();

    private final ManageButtonsPanel buttonsPanel;

    private JCheckBox showSysBundlesCheckBox = new SIPCommCheckBox(
        Resources.getString("plugin.pluginmanager.SHOW_SYSTEM_BUNDLES"));

    /**
     * Creates an instance of <tt>PluginManagerPanel</tt>.
     */
    public PluginManagerPanel()
    {
        super(new BorderLayout());
        JScrollPane pluginListScrollPane = new JScrollPane();

        pluginTable.setModel(tableModel);

        TableColumn col = pluginTable.getColumnModel().getColumn(0);
        col.setCellRenderer(new PluginListCellRenderer());

        PluginListSelectionListener selectionListener =
            new PluginListSelectionListener();

        pluginTable.getSelectionModel().addListSelectionListener(
            selectionListener);
        pluginTable.getColumnModel().getSelectionModel()
            .addListSelectionListener(selectionListener);

        pluginTable.setRowHeight(48);

        pluginTable.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));

        pluginTable.setTableHeader(null);

        buttonsPanel = new ManageButtonsPanel(pluginTable);

        this.add(pluginListScrollPane, BorderLayout.CENTER);

        this.add(buttonsPanel, BorderLayout.EAST);

        this.initSystemBundleCheckBox();

        pluginListScrollPane.getViewport().add(pluginTable);

        pluginListScrollPane.getVerticalScrollBar().setUnitIncrement(30);

        PluginManagerActivator.bundleContext
            .addBundleListener(new PluginListBundleListener());
    }

    /**
     * Initializes the check box used to show or hide system bundles from the
     * list.
     */
    private void initSystemBundleCheckBox()
    {
        //Obtains previously saved value for the showSystemBundles check box.
        String showSystemBundlesProp = PluginManagerActivator
            .getConfigurationService().getString(
            "net.java.sip.communicator.plugin.pluginManager.showSystemBundles");

        if(showSystemBundlesProp != null)
        {
            boolean isShowSystemBundles
                = new Boolean(showSystemBundlesProp).booleanValue();

            this.showSysBundlesCheckBox.setSelected(isShowSystemBundles);

            ((PluginTableModel)pluginTable.getModel())
                .setShowSystemBundles(isShowSystemBundles);
        }

        this.showSysBundlesCheckBox
            .addChangeListener(new ShowSystemBundlesChangeListener());

        JPanel checkBoxPanel
            = new TransparentPanel(new FlowLayout(FlowLayout.LEFT));
        checkBoxPanel.add(showSysBundlesCheckBox);

        this.add(checkBoxPanel, BorderLayout.SOUTH);
    }

    /**
     * Listens for events triggered when a selection is made in the plugin list.
     */
    private class PluginListSelectionListener
        implements ListSelectionListener
    {
        public void valueChanged(ListSelectionEvent e)
        {
            int selectedRow = pluginTable.getSelectedRow();

            if (selectedRow == -1)
                return;

            Bundle selectedBundle =
                (Bundle) pluginTable.getValueAt(selectedRow, 0);


            if(PluginManagerActivator.isSystemBundle(selectedBundle))
            {
                buttonsPanel.enableUninstallButton(false);
                buttonsPanel.enableDeactivateButton(false);

                if (selectedBundle.getState() != Bundle.ACTIVE)
                {
                    buttonsPanel.enableActivateButton(true);
                }
                else
                {
                    buttonsPanel.enableActivateButton(false);
                }
            }
            else
            {
                buttonsPanel.enableUninstallButton(true);

                if (selectedBundle.getState() != Bundle.ACTIVE)
                {
                    buttonsPanel.enableActivateButton(true);
                    buttonsPanel.enableDeactivateButton(false);
                }
                else
                {
                    buttonsPanel.enableActivateButton(false);
                    buttonsPanel.enableDeactivateButton(true);
                }
            }

            // every bundle can be updated
            buttonsPanel.enableUpdateButton(true);
        }
    }

    /**
     * Listens for <tt>BundleEvents</tt> triggered by the bundle context.
     */
    private class PluginListBundleListener
        implements BundleListener
    {
        public void bundleChanged(final BundleEvent event)
        {
            if(!SwingUtilities.isEventDispatchThread())
            {
                SwingUtilities.invokeLater(new Runnable()
                {
                    public void run()
                    {
                        bundleChanged(event);
                    }
                });
                return;
            }

            tableModel.update();

            if (event.getType() == BundleEvent.INSTALLED)
            {
                pluginTable.scrollRectToVisible(new Rectangle(0, pluginTable
                    .getHeight(), 1, pluginTable.getHeight()));
            }
        }
    }


    /**
     * Adds all system bundles to the bundles list when the check box is
     * selected and removes them when user deselect it.
     */
    private class ShowSystemBundlesChangeListener implements ChangeListener
    {
        private boolean currentValue = false;

        public ShowSystemBundlesChangeListener()
        {
            currentValue = showSysBundlesCheckBox.isSelected();
        }

        public void stateChanged(ChangeEvent e)
        {
            if (currentValue == showSysBundlesCheckBox.isSelected())
            {
                return;
            }
            currentValue = showSysBundlesCheckBox.isSelected();
            //Save the current value of the showSystemBundles check box.
            PluginManagerActivator.getConfigurationService().setProperty(
                "net.java.sip.communicator.plugin.pluginManager.showSystemBundles",
                new Boolean(showSysBundlesCheckBox.isSelected()));

            PluginTableModel tableModel
                = (PluginTableModel)pluginTable.getModel();

            tableModel.setShowSystemBundles(showSysBundlesCheckBox.isSelected());

            tableModel.update();

            // as this changes the selection to none, make the buttons
            // at defautl state
            buttonsPanel.defaultButtonState();
        }
    }
}