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.JLabel;
12
import javax.swing.JPanel;
13
import javax.swing.JScrollPane;
14
 
15
import org.openconcerto.modules.extensionbuilder.Extension;
16
import org.openconcerto.sql.model.SQLTable;
17
import org.openconcerto.ui.DefaultGridBagConstraints;
18
import org.openconcerto.ui.DefaultListModel;
19
import org.openconcerto.ui.JLabelBold;
20
import org.openconcerto.utils.Tuple2;
21
 
22
public class TableModifyPanel extends JPanel {
23
    private final Extension extension;
24
 
25
    // TODO: tooltip sur un champs pour indiquer quels modules l'utilisent
26
 
27
    public TableModifyPanel(SQLTable t, final TableDescritor desc, final Extension extension, final TableModifyLeftPanel leftPanel) {
28
        this.extension = extension;
29
        this.setLayout(new GridBagLayout());
30
 
31
        GridBagConstraints c = new DefaultGridBagConstraints();
32
        c.weightx = 1;
33
        this.add(new JLabelBold("Table " + desc.getName()), c);
34
 
35
        c.weightx = 1;
36
        c.gridy++;
37
        final Tuple2<JPanel, GridBagConstraints> l = createEditorList(desc);
38
        this.add(l.get0(), c);
39
        c.gridy++;
40
        c.fill = GridBagConstraints.NONE;
41
        c.anchor = GridBagConstraints.WEST;
42
        JButton buttonAdd = new JButton("Ajouter un champs");
43
 
44
        this.add(buttonAdd, c);
45
        c.gridy++;
46
        if (t != null) {
47
            this.add(new JLabel("Structure actuelle dans la base de données"), c);
48
 
49
            c.gridy++;
86 ilm 50
            TableModifyInfoPanel info = new TableModifyInfoPanel(extension, t, desc, leftPanel);
74 ilm 51
            c.weighty = 1;
52
            c.fill = GridBagConstraints.BOTH;
53
            this.add(new JScrollPane(info), c);
54
        } else {
55
            final JPanel spacer = new JPanel();
56
            spacer.setOpaque(false);
57
            c.weighty = 1;
58
 
59
            this.add(spacer, c);
60
        }
61
        buttonAdd.addActionListener(new ActionListener() {
62
 
63
            @Override
64
            public void actionPerformed(ActionEvent e) {
65
                FieldDescriptor f = new FieldDescriptor(desc.getName(), "field" + ((l.get0().getComponentCount() / 2) + 1), "string", "", "200", "");
66
                desc.add(f);
67
                GridBagConstraints c1 = l.get1();
68
                addField(desc, l.get0(), c1, f);
69
                l.get0().revalidate();
70
                extension.setChanged();
71
            }
72
        });
73
    }
74
 
75
    private Tuple2<JPanel, GridBagConstraints> createEditorList(final TableDescritor desc) {
76
        JPanel p = new JPanel();
77
        p.setLayout(new GridBagLayout());
78
        GridBagConstraints c = new DefaultGridBagConstraints();
79
        List<FieldDescriptor> fields = desc.getFields();
80
        for (final FieldDescriptor field : fields) {
81
            addField(desc, p, c, field);
82
        }
83
        Tuple2<JPanel, GridBagConstraints> result = Tuple2.create(p, c);
84
        return result;
85
 
86
    }
87
 
88
    private void addField(final TableDescritor desc, final JPanel p, GridBagConstraints c, final FieldDescriptor field) {
89
        c.weightx = 1;
90
        c.gridx = 0;
181 ilm 91
        final FieldDescriptorEditor editor = new FieldDescriptorEditor(this.extension, field);
74 ilm 92
        p.add(editor, c);
93
 
94
        c.gridx++;
95
        c.weightx = 0;
96
        final JButton close = new JButton(new ImageIcon(DefaultListModel.class.getResource("close_popup.png")));
97
 
98
        p.add(close, c);
99
        close.addActionListener(new ActionListener() {
100
 
101
            @Override
102
            public void actionPerformed(ActionEvent e) {
103
                desc.remove(field);
104
                p.remove(editor);
105
                p.remove(close);
106
                p.revalidate();
181 ilm 107
                TableModifyPanel.this.extension.setChanged();
74 ilm 108
            }
109
        });
110
        c.gridy++;
111
    }
112
 
113
}