aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/plugin/chatconfig/replacement/ReplacementConfigPanel.java
blob: f46a1e59d7ec6b49544f2d478d70fcea5de1688b (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
/*
 * 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.plugin.chatconfig.replacement;

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

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

import net.java.sip.communicator.impl.replacement.smiley.*;
import net.java.sip.communicator.plugin.chatconfig.*;
import net.java.sip.communicator.service.configuration.*;
import net.java.sip.communicator.service.replacement.*;
import net.java.sip.communicator.util.swing.*;

/**
 * The <tt>ConfigurationForm</tt> that would be added in the chat configuration
 * window.
 * 
 * @author Purvesh Sahoo
 */
public class ReplacementConfigPanel
    extends TransparentPanel
{
    /**
     * Checkbox to enable/disable smiley replacement.
     */
    private JCheckBox enableSmiley;

    /**
     * Checkbox to enable/disable replacements other than smileys.
     */
    private JCheckBox enableReplacement;

    /**
     * Jtable to list all the available replacement sources.
     */
    private JTable table;

    /**
     * Create an instance of Replacement Config
     */
    public ReplacementConfigPanel()
    {
        super(new BorderLayout());

        add(ChatConfigActivator
            .createConfigSectionComponent(ChatConfigActivator.getResources()
                .getI18NString("plugin.chatconfig.replacement.TITLE")),
            BorderLayout.WEST);
        add(createMainPanel());

        initValues();
    }

    /**
     * Init the main panel.
     * 
     * @return the created component
     */
    private Component createMainPanel()
    {
        JPanel mainPanel = new TransparentPanel(new BorderLayout());

        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));

        enableSmiley =
            new SIPCommCheckBox(ChatConfigActivator.getResources()
                .getI18NString(
                    "plugin.chatconfig.replacement.ENABLE_SMILEY_STATUS"));

        mainPanel.add(enableSmiley, BorderLayout.WEST);

        enableSmiley.addActionListener(new ActionListener()
        {

            public void actionPerformed(ActionEvent e)
            {
                saveData();
            }
        });

        mainPanel.add(Box.createVerticalStrut(10));

        enableReplacement =
            new SIPCommCheckBox(ChatConfigActivator.getResources()
                .getI18NString(
                    "plugin.chatconfig.replacement.ENABLE_REPLACEMENT_STATUS"));

        mainPanel.add(enableReplacement, BorderLayout.WEST);

        enableReplacement.addActionListener(new ActionListener()
        {

            public void actionPerformed(ActionEvent e)
            {
                saveData();
                table.revalidate();
                table.repaint();
            }
        });

        // the Jtable to list all the available sources
        table = new JTable();
        table.setShowGrid(false);
        table.setTableHeader(null);

        table.setOpaque(true);
        table.setBackground(Color.white);
  
        JScrollPane tablePane = new JScrollPane(table);
        tablePane.setOpaque(false);
        tablePane.setPreferredSize(new Dimension(mainPanel.getWidth(), 150));
        tablePane.setAlignmentX(LEFT_ALIGNMENT);

        JPanel container = new TransparentPanel(new BorderLayout());
        container.setPreferredSize(new Dimension(mainPanel.getWidth(), 200));
        container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));

        JLabel label =
            new JLabel(ChatConfigActivator.getResources().getI18NString(
                "plugin.chatconfig.replacement.REPLACEMENT_SOURCES"));
        label.setDisplayedMnemonic(ChatConfigActivator.getResources()
            .getI18nMnemonic(
                "plugin.chatconfig.replacement.REPLACEMENT_SOURCES"));
        label.setLabelFor(table);

        container.add(label);
        container.add(Box.createRigidArea(new Dimension(0, 5)));
        container.add(tablePane, BorderLayout.EAST);

        table.setModel(new ReplacementConfigurationTableModel(
            ReplacementService.sourceList));

        table.getSelectionModel().addListSelectionListener(
            new ListSelectionListener()
            {
                public void valueChanged(ListSelectionEvent e)
                {
                    if (e.getValueIsAdjusting())
                        return;
                    if (table.getSelectedRow() != -1)
                    {
                        boolean isEnabled =
                            (Boolean) table.getValueAt(table.getSelectedRow(),
                                0);

                        if (isEnabled)
                        {
                            enableReplacement.setSelected(true);
                        }
                    }
                }
            });

        TableColumnModel tableColumnModel = table.getColumnModel();
        TableColumn tableColumn = tableColumnModel.getColumn(0);
        tableColumn.setMaxWidth(tableColumn.getMinWidth());
        table.setDefaultRenderer(table.getColumnClass(1),
            new FixedTableCellRenderer());

        mainPanel.add(Box.createVerticalStrut(10));
        mainPanel.add(container, BorderLayout.WEST);

        return mainPanel;
    }

    /**
     * Init the values of the widgets
     */
    private void initValues()
    {
        ConfigurationService configService =
            ChatConfigActivator.getConfigurationService();

        boolean e =
            configService.getBoolean(ReplacementProperty
                .getPropertyName(ReplacementServiceSmileyImpl.SMILEY_SOURCE),
                true);
        this.enableSmiley.setSelected(e);

        e =
            configService.getBoolean(ReplacementProperty.REPLACEMENT_ENABLE,
                true);
        this.enableReplacement.setSelected(e);

        this.table.setEnabled(e);
    }

    /**
     * Save data in the configuration file
     */
    private void saveData()
    {
        ConfigurationService configService =
            ChatConfigActivator.getConfigurationService();

        configService.setProperty(ReplacementProperty
            .getPropertyName(ReplacementServiceSmileyImpl.SMILEY_SOURCE),
            Boolean.toString(enableSmiley.isSelected()));

        configService.setProperty(ReplacementProperty.REPLACEMENT_ENABLE,
            Boolean.toString(enableReplacement.isSelected()));

        boolean e = enableReplacement.isSelected();
        table.getSelectionModel().clearSelection();
        table.setEnabled(e);
    }

    /**
     * Renderer for text column in the table.
     */
    private static class FixedTableCellRenderer
        extends DefaultTableCellRenderer
    {
        public Component getTableCellRendererComponent(JTable table, Object value,
            boolean selected, boolean focused, int row, int column)
        {
            setEnabled(table == null || table.isEnabled());

            super.getTableCellRendererComponent(table, value, selected, focused,
                row, column);

            return this;
        }
    }
}