OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev Author Line No. Line
74 ilm 1
package org.openconcerto.modules.extensionbuilder.component;
2
 
3
import java.awt.GridBagConstraints;
4
import java.awt.GridBagLayout;
5
import java.awt.event.ActionEvent;
6
import java.awt.event.ActionListener;
7
 
8
import javax.swing.JButton;
9
import javax.swing.JComboBox;
10
import javax.swing.JFrame;
11
import javax.swing.JLabel;
12
import javax.swing.JOptionPane;
13
import javax.swing.JPanel;
14
import javax.swing.SwingConstants;
15
import javax.swing.event.ChangeEvent;
16
import javax.swing.event.ChangeListener;
17
 
18
import org.openconcerto.erp.config.ComptaPropsConfiguration;
19
import org.openconcerto.erp.config.Log;
20
import org.openconcerto.modules.extensionbuilder.Extension;
153 ilm 21
import org.openconcerto.modules.extensionbuilder.ExtensionGroupSQLComponent;
74 ilm 22
import org.openconcerto.modules.extensionbuilder.list.AllTablesComboBoxModel;
23
import org.openconcerto.sql.element.GroupSQLComponent;
24
import org.openconcerto.sql.element.SQLElement;
25
import org.openconcerto.sql.model.SQLTable;
139 ilm 26
import org.openconcerto.sql.request.RowItemDesc;
74 ilm 27
import org.openconcerto.sql.view.EditPanel;
28
import org.openconcerto.sql.view.EditPanel.EditMode;
29
import org.openconcerto.ui.DefaultGridBagConstraints;
30
import org.openconcerto.ui.FrameUtil;
31
import org.openconcerto.ui.JLabelBold;
32
import org.openconcerto.ui.group.Group;
33
 
34
public class ComponentCreatePanel extends JPanel {
35
 
36
    private GroupEditor panel;
37
    private JFrame previewFrame;
38
    private Group oldGroup;
39
 
40
    public ComponentCreatePanel(final ComponentDescritor n, Extension extension) {
41
        this.setLayout(new GridBagLayout());
42
        GridBagConstraints c = new DefaultGridBagConstraints();
43
        c.gridwidth = 2;
44
 
45
        this.add(new JLabelBold(n.getId()), c);
46
        c.gridy++;
47
        c.gridwidth = 1;
48
        c.fill = GridBagConstraints.HORIZONTAL;
49
        this.add(new JLabel("Table principale", SwingConstants.RIGHT), c);
50
        c.gridx++;
51
        c.fill = GridBagConstraints.NONE;
52
        final JComboBox comboTable = new JComboBox(new AllTablesComboBoxModel(extension));
53
        this.add(comboTable, c);
54
        c.gridx = 0;
55
        c.gridy++;
56
        c.weightx = 1;
57
        c.weighty = 1;
58
        c.gridwidth = 2;
59
        c.fill = GridBagConstraints.BOTH;
60
 
61
        panel = new GroupEditor(n, extension);
62
        final String mainTable = n.getTable();
63
        if (mainTable == null && comboTable.getModel().getSize() > 0) {
64
            comboTable.setSelectedIndex(0);
65
            panel.setMainTable((String) comboTable.getModel().getElementAt(0));
66
        } else {
67
            comboTable.setSelectedItem(mainTable);
68
            panel.setMainTable(mainTable);
69
        }
70
        this.add(panel, c);
71
 
72
        final JButton previewButton = new JButton("Prévisualiser");
73
        c.gridy++;
74
        c.weighty = 0;
75
        c.fill = GridBagConstraints.NONE;
76
        c.anchor = GridBagConstraints.EAST;
77
        this.add(previewButton, c);
78
 
79
        // Listeners
80
 
81
        comboTable.addActionListener(new ActionListener() {
82
 
83
            @Override
84
            public void actionPerformed(ActionEvent e) {
85
                panel.setMainTable((String) comboTable.getSelectedItem());
86
 
87
            }
88
        });
89
        previewButton.addActionListener(new ActionListener() {
90
 
91
            @Override
92
            public void actionPerformed(ActionEvent e) {
93
 
94
                final SQLTable t = ComptaPropsConfiguration.getInstanceCompta().getRootSociete().getTable(n.getTable());
95
                if (t == null) {
96
                    JOptionPane.showMessageDialog(ComponentCreatePanel.this, "La table doit être créée avant de pouvoir prévisualiser.");
97
                    return;
98
                }
99
                final Group group = panel.getFilteredGroup();
100
 
101
                final SQLElement element = ComptaPropsConfiguration.getInstanceCompta().getDirectory().getElement(t);
102
                if (element == null) {
103
                    Log.get().warning("No element for table: " + t.getName());
104
                }
153 ilm 105
                final GroupSQLComponent gComponent = new ExtensionGroupSQLComponent(element, group);
74 ilm 106
                oldGroup = group;
107
                if (previewFrame == null || !previewFrame.isVisible()) {
108
                    previewFrame = new JFrame();
109
                    previewFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
110
                    previewFrame.setTitle("Preview: " + group.getId());
111
                }
112
                final EditPanel panel = new EditPanel(gComponent, EditMode.CREATION);
113
                previewFrame.setContentPane(panel);
114
                previewFrame.pack();
115
                if (!previewFrame.isVisible()) {
116
                    FrameUtil.show(previewFrame);
117
                }
118
 
119
            };
120
 
121
        });
122
        n.addGroupChangeListener(new ChangeListener() {
123
 
124
            @Override
125
            public void stateChanged(ChangeEvent e) {
126
                if (previewFrame == null || !previewFrame.isVisible()) {
127
                    return;
128
                }
129
                final Group group = panel.getFilteredGroup();
130
                if (group.equalsDesc(oldGroup)) {
131
                    // Avoid refresh when group doesn't change
132
                    return;
133
                }
134
                oldGroup = group;
135
                final SQLTable t = ComptaPropsConfiguration.getInstanceCompta().getRootSociete().getTable(n.getTable());
136
                if (t == null) {
137
                    return;
138
                }
139
                final SQLElement element = ComptaPropsConfiguration.getInstanceCompta().getDirectory().getElement(t);
153 ilm 140
                final GroupSQLComponent gComponent = new ExtensionGroupSQLComponent(element, group);
74 ilm 141
                previewFrame.setContentPane(new EditPanel(gComponent, EditMode.CREATION));
142
                previewFrame.pack();
143
                if (!previewFrame.isVisible()) {
144
                    FrameUtil.show(previewFrame);
145
                }
146
 
147
            }
148
        });
149
    }
150
}