aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/plugin/propertieseditor/ButtonsPanel.java
blob: 62c8f847e786a3de6abf71cb71553ffda9501422 (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
/*
 * 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.propertieseditor;

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

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

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

import org.jitsi.service.configuration.*;
import org.jitsi.service.resources.*;

/**
 * The panel containing all buttons for the <tt>PropertiesEditorPanel</tt>.
 * 
 * @author Marin Dzhigarov
 */
public class ButtonsPanel 
    extends TransparentPanel 
    implements ActionListener
{
    /**
     * Serial version UID.
     */
    private static final long serialVersionUID = 1L;

    private final ConfigurationService confService 
        = PropertiesEditorActivator.getConfigurationService();

    /**
     * The delete button.
     */
    private final JButton deleteButton;

    /**
     * The new button.
     */
    private final JButton newButton;

    /**
     * The props table.
     */
    private final JTable propsTable;

    /**
     * Creates an instance of <tt>ButtonsPanel</tt>.
     * @param propsTable the table containing all properties.
     * @param searchBox the search box panel containing the search box text 
     * field.
     */
    public ButtonsPanel(JTable propsTable, SearchField searchField)
    {
        this.propsTable = propsTable;

        ResourceManagementService r
            = PropertiesEditorActivator.getResourceManagementService();

        newButton = new JButton(r.getI18NString("service.gui.NEW"));
        deleteButton = new JButton(r.getI18NString("service.gui.DELETE"));
        newButton.setOpaque(false);
        deleteButton.setOpaque(false);

        JPanel buttonsPanel = new TransparentPanel(new GridLayout(0, 1, 8, 8));

        buttonsPanel.add(newButton);
        buttonsPanel.add(deleteButton);

        setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
        setLayout(new BorderLayout());
        add(buttonsPanel, BorderLayout.NORTH);

        newButton.addActionListener(this);
        deleteButton.addActionListener(this);

        //default as nothing is selected
        defaultButtonState();
    }

    /**
     * Default state of buttons, as nothing is selected
     */
    public void defaultButtonState()
    {
        enableDeleteButton(false);
    }

    /**
     * Enable or disable the delete button.
     *
     * @param enable TRUE - to enable the delete button, FALSE - to disable it
     */
    public void enableDeleteButton(boolean enable)
    {
        this.deleteButton.setEnabled(enable);
    }

    /**
     * Performs corresponding actions, when a button is pressed.
     * @param e the <tt>ActionEvent</tt> that notified us
     */
    public void actionPerformed(ActionEvent e)
    {
        JButton sourceButton = (JButton) e.getSource();

        if (sourceButton.equals(newButton))
        {
            NewPropertyDialog dialog = new NewPropertyDialog();
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

            dialog.pack();
            dialog.setLocation(
                    (screenSize.width - dialog.getWidth()) / 2,
                    (screenSize.height - dialog.getHeight()) / 2);

            dialog.setVisible(true);
        }
        else if (sourceButton.equals(deleteButton))
        {
            int viewRow = propsTable.getSelectedRow();
            int modelRow =  propsTable.convertRowIndexToModel(viewRow);
            String selectedProperty
                = (String) propsTable.getModel().getValueAt(modelRow, 0);

            confService.removeProperty(selectedProperty);
            ((DefaultTableModel) propsTable.getModel()).removeRow(modelRow);
            propsTable.clearSelection();
            defaultButtonState();
        }
    }
}