OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev Author Line No. Line
74 ilm 1
package org.openconcerto.modules.extensionbuilder.table;
2
 
3
import java.awt.GridBagConstraints;
4
import java.awt.GridBagLayout;
5
import java.awt.event.ActionEvent;
6
import java.awt.event.ActionListener;
7
import java.util.List;
8
 
9
import javax.swing.ImageIcon;
10
import javax.swing.JButton;
11
import javax.swing.JPanel;
12
 
13
import org.openconcerto.modules.extensionbuilder.Extension;
14
import org.openconcerto.ui.DefaultGridBagConstraints;
15
import org.openconcerto.ui.DefaultListModel;
16
import org.openconcerto.ui.JLabelBold;
17
import org.openconcerto.utils.Tuple2;
18
 
19
public class TableCreatePanel extends JPanel {
20
    private final Extension extension;
21
 
22
    public TableCreatePanel(final TableDescritor desc, final Extension extension) {
23
        this.extension = extension;
24
        this.setLayout(new GridBagLayout());
25
        GridBagConstraints c = new DefaultGridBagConstraints();
26
        this.add(new JLabelBold("Table " + desc.getName()), c);
27
        c.weightx = 1;
28
        c.gridy++;
29
        final Tuple2<JPanel, GridBagConstraints> l = createEditorList(desc);
30
        this.add(l.get0(), c);
31
        c.gridy++;
32
        c.fill = GridBagConstraints.NONE;
33
        c.anchor = GridBagConstraints.WEST;
34
        JButton buttonAdd = new JButton("Ajouter un champs");
35
 
36
        this.add(buttonAdd, c);
37
        c.gridy++;
38
        JPanel spacer = new JPanel();
39
        c.weighty = 1;
40
        this.add(spacer, c);
41
        buttonAdd.addActionListener(new ActionListener() {
42
 
43
            @Override
44
            public void actionPerformed(ActionEvent e) {
45
                FieldDescriptor f = new FieldDescriptor(desc.getName(), "field" + ((l.get0().getComponentCount() / 2) + 1), "string", "", "200", "");
46
                GridBagConstraints c1 = l.get1();
47
                desc.add(f);
48
                addField(desc, l.get0(), c1, f);
49
                l.get0().revalidate();
50
                extension.setChanged();
51
            }
52
        });
53
 
54
    }
55
 
56
    Tuple2<JPanel, GridBagConstraints> createEditorList(final TableDescritor desc) {
57
        JPanel p = new JPanel();
58
        p.setLayout(new GridBagLayout());
59
        GridBagConstraints c = new DefaultGridBagConstraints();
60
        List<FieldDescriptor> fields = desc.getFields();
61
        for (final FieldDescriptor field : fields) {
62
            addField(desc, p, c, field);
63
        }
64
        Tuple2<JPanel, GridBagConstraints> result = Tuple2.create(p, c);
65
        return result;
66
 
67
    }
68
 
69
    private void addField(final TableDescritor desc, final JPanel p, GridBagConstraints c, final FieldDescriptor field) {
70
 
71
        c.weightx = 1;
72
        c.gridx = 0;
181 ilm 73
        final FieldDescriptorEditor editor = new FieldDescriptorEditor(this.extension, field);
74 ilm 74
        p.add(editor, c);
75
 
76
        c.gridx++;
77
        c.weightx = 0;
78
        final JButton close = new JButton(new ImageIcon(DefaultListModel.class.getResource("close_popup.png")));
79
 
80
        p.add(close, c);
81
        close.addActionListener(new ActionListener() {
82
 
83
            @Override
84
            public void actionPerformed(ActionEvent e) {
85
                desc.remove(field);
86
                p.remove(editor);
87
                p.remove(close);
88
                p.revalidate();
181 ilm 89
                TableCreatePanel.this.extension.setChanged();
74 ilm 90
            }
91
        });
92
        c.gridy++;
93
    }
94
 
95
}