OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 74 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
74 ilm 1
package org.openconcerto.modules.extensionbuilder.list;
2
 
3
import java.awt.Dimension;
4
import java.awt.GridBagConstraints;
5
import java.awt.GridBagLayout;
6
import java.awt.Insets;
7
import java.awt.event.ActionEvent;
8
import java.awt.event.ActionListener;
9
 
10
import javax.swing.JButton;
11
import javax.swing.JLabel;
12
import javax.swing.JList;
13
import javax.swing.JPanel;
14
import javax.swing.JScrollPane;
15
import javax.swing.ListModel;
16
import javax.swing.ListSelectionModel;
17
import javax.swing.event.ListSelectionEvent;
18
import javax.swing.event.ListSelectionListener;
19
 
20
public abstract class EditableListPanel extends JPanel {
21
 
22
    protected JList list;
23
    protected ListModel dataModel;
24
    private JButton removeButton = null;
25
    private JButton renameButton = null;
26
 
27
    public EditableListPanel(final ListModel dataModel, String title, String addLabel) {
28
        this(dataModel, title, addLabel, true, true);
29
    }
30
 
31
    public EditableListPanel(final ListModel dataModel, String title, String addLabel, boolean editable, boolean canRename) {
32
        this.dataModel = dataModel;
33
        this.setOpaque(false);
34
 
35
        this.setLayout(new GridBagLayout());
36
        final GridBagConstraints c = new GridBagConstraints();
37
        c.fill = GridBagConstraints.BOTH;
38
        c.gridx = 0;
39
        c.gridy = 0;
40
        c.weightx = 1;
41
        c.weighty = 0;
42
        c.insets = new Insets(2, 2, 2, 0);
43
        if (title != null) {
44
            this.add(new JLabel(title), c);
45
        }
181 ilm 46
        this.list = new JList(dataModel);
47
        this.list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
48
        final JScrollPane comp2 = new JScrollPane(this.list);
74 ilm 49
        comp2.setMinimumSize(new Dimension(150, 150));
50
        comp2.setPreferredSize(new Dimension(150, 150));
51
        c.weighty = 1;
52
        c.gridy++;
53
        c.insets = new Insets(0, 0, 0, 0);
54
        this.add(comp2, c);
55
 
56
        if (editable) {
57
 
58
            c.gridy++;
59
            c.weighty = 0;
60
            c.fill = GridBagConstraints.NONE;
61
            c.anchor = GridBagConstraints.SOUTHWEST;
62
            c.insets = new Insets(2, 2, 2, 0);
63
            final JButton addButton = new JButton(addLabel);
64
            this.add(addButton, c);
65
            c.gridy++;
66
            if (canRename) {
181 ilm 67
                this.renameButton = new JButton("Renommer");
68
                this.add(this.renameButton, c);
69
                this.renameButton.setEnabled(false);
70
                this.renameButton.addActionListener(new ActionListener() {
74 ilm 71
 
72
                    @Override
73
                    public void actionPerformed(ActionEvent e) {
181 ilm 74
                        if (EditableListPanel.this.list.getSelectedValue() != null) {
75
                            renameItem(EditableListPanel.this.list.getSelectedValue());
74 ilm 76
                        }
77
                    }
78
 
79
                });
80
                c.gridy++;
81
            }
82
 
181 ilm 83
            this.removeButton = new JButton("Supprimer");
84
            this.removeButton.setEnabled(false);
85
            this.add(this.removeButton, c);
74 ilm 86
 
87
            // init
88
 
89
            addButton.addActionListener(new ActionListener() {
90
 
91
                @Override
92
                public void actionPerformed(ActionEvent e) {
93
                    addNewItem();
94
                    if (dataModel.getSize() > 0) {
95
                        selectItem(dataModel.getElementAt(dataModel.getSize() - 1));
96
                    }
97
                }
98
 
99
            });
100
 
181 ilm 101
            this.removeButton.addActionListener(new ActionListener() {
74 ilm 102
                @Override
103
                public void actionPerformed(ActionEvent e) {
181 ilm 104
                    if (EditableListPanel.this.list.getSelectedValue() != null) {
105
                        removeItem(EditableListPanel.this.list.getSelectedValue());
74 ilm 106
                    }
107
                }
108
            });
109
        }
181 ilm 110
        this.list.addListSelectionListener(new ListSelectionListener() {
74 ilm 111
 
112
            @Override
113
            public void valueChanged(ListSelectionEvent e) {
181 ilm 114
                final Object selectedValue = EditableListPanel.this.list.getSelectedValue();
115
                if (EditableListPanel.this.removeButton != null) {
116
                    EditableListPanel.this.removeButton.setEnabled(selectedValue != null);
74 ilm 117
                }
181 ilm 118
                if (EditableListPanel.this.renameButton != null) {
119
                    EditableListPanel.this.renameButton.setEnabled(selectedValue != null);
74 ilm 120
                }
121
                if (!e.getValueIsAdjusting() && selectedValue != null) {
122
 
123
                    itemSelected(selectedValue);
124
 
125
                }
126
                if (selectedValue == null) {
127
                    itemSelected(null);
128
                }
129
 
130
            }
131
        });
132
 
133
    }
134
 
135
    /**
136
     * Select an item in the list
137
     * */
138
    public void selectItem(Object item) {
181 ilm 139
        this.list.setSelectedValue(item, true);
74 ilm 140
    }
141
 
142
    /**
143
     * Called when the user click "add"
144
     * */
145
    public abstract void addNewItem();
146
 
147
    /**
148
     * Called when the user click "rename"
149
     * */
150
    public abstract void renameItem(Object item);
151
 
152
    /**
153
     * Called when the user click "remove"
154
     * */
155
    public abstract void removeItem(Object item);
156
 
157
    /**
158
     * Called when the user select an item in the list
159
     *
160
     * @param item the item, null if all the items are deselected
161
     * */
162
    public abstract void itemSelected(Object item);
163
 
164
    public void reload() {
181 ilm 165
        this.list.invalidate();
166
        this.list.repaint();
74 ilm 167
    }
168
}