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