OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 86 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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