74 |
ilm |
1 |
package org.openconcerto.modules.extensionbuilder.component;
|
|
|
2 |
|
|
|
3 |
import java.awt.Color;
|
|
|
4 |
import java.awt.Component;
|
|
|
5 |
import java.awt.Dimension;
|
|
|
6 |
import java.awt.GridBagConstraints;
|
|
|
7 |
import java.awt.GridBagLayout;
|
|
|
8 |
import java.awt.Insets;
|
|
|
9 |
import java.awt.event.ActionEvent;
|
|
|
10 |
import java.awt.event.ActionListener;
|
|
|
11 |
import java.awt.event.MouseAdapter;
|
|
|
12 |
import java.awt.event.MouseEvent;
|
|
|
13 |
import java.util.List;
|
|
|
14 |
|
|
|
15 |
import javax.swing.JButton;
|
|
|
16 |
import javax.swing.JCheckBox;
|
|
|
17 |
import javax.swing.JComponent;
|
|
|
18 |
import javax.swing.JLabel;
|
|
|
19 |
import javax.swing.JPanel;
|
|
|
20 |
import javax.swing.JScrollPane;
|
|
|
21 |
import javax.swing.JTree;
|
|
|
22 |
import javax.swing.event.TreeSelectionEvent;
|
|
|
23 |
import javax.swing.event.TreeSelectionListener;
|
|
|
24 |
import javax.swing.tree.DefaultMutableTreeNode;
|
|
|
25 |
import javax.swing.tree.DefaultTreeCellRenderer;
|
|
|
26 |
import javax.swing.tree.TreePath;
|
|
|
27 |
|
|
|
28 |
import org.openconcerto.erp.config.ComptaPropsConfiguration;
|
|
|
29 |
import org.openconcerto.modules.extensionbuilder.AbstractSplittedPanel;
|
|
|
30 |
import org.openconcerto.modules.extensionbuilder.Extension;
|
|
|
31 |
import org.openconcerto.sql.model.SQLField;
|
|
|
32 |
import org.openconcerto.sql.model.SQLTable;
|
|
|
33 |
import org.openconcerto.ui.JLabelBold;
|
|
|
34 |
import org.openconcerto.ui.group.Group;
|
|
|
35 |
import org.openconcerto.ui.group.Item;
|
|
|
36 |
import org.openconcerto.ui.group.LayoutHints;
|
|
|
37 |
import org.openconcerto.ui.tree.ReorderableJTree;
|
|
|
38 |
|
|
|
39 |
public class GroupEditor extends AbstractSplittedPanel {
|
|
|
40 |
|
|
|
41 |
private ComponentDescritor n;
|
|
|
42 |
private ItemTreeModel newModel;
|
|
|
43 |
private JTree tree;
|
|
|
44 |
private Group tableGroup;
|
|
|
45 |
|
|
|
46 |
public GroupEditor(ComponentDescritor n, Extension extension) {
|
|
|
47 |
super(extension);
|
|
|
48 |
this.n = n;
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
@Override
|
|
|
52 |
public JComponent createLeftComponent() {
|
|
|
53 |
final JPanel panel = new JPanel();
|
|
|
54 |
panel.setLayout(new GridBagLayout());
|
|
|
55 |
final GridBagConstraints c = new GridBagConstraints();
|
|
|
56 |
c.fill = GridBagConstraints.BOTH;
|
|
|
57 |
c.gridx = 0;
|
|
|
58 |
c.gridy = 0;
|
|
|
59 |
c.weightx = 1;
|
|
|
60 |
c.weighty = 0;
|
|
|
61 |
c.insets = new Insets(2, 2, 2, 0);
|
|
|
62 |
|
|
|
63 |
panel.add(new JLabelBold("Champs et groupes"), c);
|
|
|
64 |
newModel = new ItemTreeModel();
|
|
|
65 |
tree = new ReorderableJTree() {
|
|
|
66 |
@Override
|
|
|
67 |
public String convertValueToText(Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
|
|
|
68 |
final Object userObject = ((DefaultMutableTreeNode) value).getUserObject();
|
|
|
69 |
if (userObject == null) {
|
|
|
70 |
return "null";
|
|
|
71 |
}
|
|
|
72 |
if (userObject instanceof Group) {
|
|
|
73 |
Group d = (Group) userObject;
|
|
|
74 |
return " " + d.getId();
|
|
|
75 |
}
|
|
|
76 |
return userObject.toString();
|
|
|
77 |
}
|
|
|
78 |
};
|
|
|
79 |
tree.setModel(newModel);
|
|
|
80 |
tree.setRootVisible(false);
|
|
|
81 |
tree.setShowsRootHandles(true);
|
|
|
82 |
tree.expandRow(0);
|
|
|
83 |
final DefaultTreeCellRenderer treeRenderer = new DefaultTreeCellRenderer() {
|
|
|
84 |
@Override
|
|
|
85 |
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
|
|
|
86 |
|
|
|
87 |
final JLabel r = (JLabel) super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
|
|
|
88 |
final ActivableMutableTreeNode tr = (ActivableMutableTreeNode) value;
|
|
|
89 |
if (!tr.isActive()) {
|
|
|
90 |
r.setForeground(Color.LIGHT_GRAY);
|
|
|
91 |
}
|
|
|
92 |
if (tr.getUserObject() instanceof Item) {
|
|
|
93 |
r.setText(((Item) tr.getUserObject()).getId());
|
|
|
94 |
}
|
|
|
95 |
return r;
|
|
|
96 |
}
|
|
|
97 |
};
|
|
|
98 |
|
|
|
99 |
treeRenderer.setLeafIcon(null);
|
|
|
100 |
tree.setCellRenderer(treeRenderer);
|
|
|
101 |
final JScrollPane comp2 = new JScrollPane(tree);
|
|
|
102 |
comp2.setMinimumSize(new Dimension(250, 150));
|
|
|
103 |
comp2.setPreferredSize(new Dimension(250, 150));
|
|
|
104 |
c.weighty = 1;
|
|
|
105 |
c.gridy++;
|
|
|
106 |
c.insets = new Insets(0, 0, 0, 0);
|
|
|
107 |
panel.add(comp2, c);
|
|
|
108 |
|
|
|
109 |
c.gridy++;
|
|
|
110 |
c.weighty = 0;
|
|
|
111 |
c.fill = GridBagConstraints.NONE;
|
|
|
112 |
c.anchor = GridBagConstraints.SOUTHWEST;
|
|
|
113 |
c.insets = new Insets(2, 2, 2, 0);
|
|
|
114 |
final JButton addGroupButton = new JButton("Ajouter un groupe");
|
|
|
115 |
panel.add(addGroupButton, c);
|
|
|
116 |
c.gridy++;
|
|
|
117 |
final JButton showHideButton = new JButton("Afficher / Masquer");
|
|
|
118 |
showHideButton.setEnabled(false);
|
|
|
119 |
panel.add(showHideButton, c);
|
|
|
120 |
|
|
|
121 |
c.gridy++;
|
|
|
122 |
final JCheckBox hideCheckbox = new JCheckBox("Afficher les champs masqués");
|
|
|
123 |
hideCheckbox.setSelected(true);
|
|
|
124 |
|
|
|
125 |
panel.add(hideCheckbox, c);
|
|
|
126 |
|
|
|
127 |
// init
|
|
|
128 |
|
|
|
129 |
addGroupButton.addActionListener(new ActionListener() {
|
|
|
130 |
|
|
|
131 |
@Override
|
|
|
132 |
public void actionPerformed(ActionEvent e) {
|
|
|
133 |
addNewGroup();
|
|
|
134 |
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
});
|
|
|
138 |
|
|
|
139 |
showHideButton.addActionListener(new ActionListener() {
|
|
|
140 |
@Override
|
|
|
141 |
public void actionPerformed(ActionEvent e) {
|
|
|
142 |
newModel.toggleActive(tree.getSelectionPath());
|
|
|
143 |
}
|
|
|
144 |
});
|
|
|
145 |
|
|
|
146 |
hideCheckbox.addActionListener(new ActionListener() {
|
|
|
147 |
|
|
|
148 |
@Override
|
|
|
149 |
public void actionPerformed(ActionEvent e) {
|
|
|
150 |
newModel.setShowAll(hideCheckbox.isSelected());
|
|
|
151 |
setMainTable(n.getTable());
|
|
|
152 |
setRightPanel(new JPanel());
|
|
|
153 |
}
|
|
|
154 |
});
|
|
|
155 |
|
|
|
156 |
tree.addTreeSelectionListener(new TreeSelectionListener() {
|
|
|
157 |
|
|
|
158 |
@Override
|
|
|
159 |
public void valueChanged(TreeSelectionEvent e) {
|
|
|
160 |
final Object selectedValue = tree.getSelectionPath();
|
|
|
161 |
showHideButton.setEnabled((selectedValue != null));
|
|
|
162 |
}
|
|
|
163 |
});
|
|
|
164 |
tree.addMouseListener(new MouseAdapter() {
|
|
|
165 |
@Override
|
|
|
166 |
public void mouseClicked(MouseEvent e) {
|
|
|
167 |
if (e.getClickCount() > 1) {
|
|
|
168 |
newModel.toggleActive(tree.getSelectionPath());
|
|
|
169 |
}
|
|
|
170 |
}
|
|
|
171 |
|
|
|
172 |
@Override
|
|
|
173 |
public void mousePressed(MouseEvent e) {
|
|
|
174 |
final TreePath selectionPath = tree.getSelectionPath();
|
|
|
175 |
if (selectionPath == null) {
|
|
|
176 |
setRightPanel(new JPanel());
|
|
|
177 |
} else {
|
|
|
178 |
Item i = (Item) ((DefaultMutableTreeNode) selectionPath.getLastPathComponent()).getUserObject();
|
|
|
179 |
setRightPanel(new ItemEditor(i, n));
|
|
|
180 |
}
|
|
|
181 |
}
|
|
|
182 |
});
|
|
|
183 |
return panel;
|
|
|
184 |
}
|
|
|
185 |
|
|
|
186 |
protected void addNewGroup() {
|
|
|
187 |
final DefaultMutableTreeNode root = (DefaultMutableTreeNode) ((DefaultMutableTreeNode) (tree.getModel().getRoot())).getFirstChild();
|
|
|
188 |
DefaultMutableTreeNode node = root;
|
|
|
189 |
if (node.getChildCount() > 0) {
|
|
|
190 |
node = (DefaultMutableTreeNode) node.getFirstChild();
|
|
|
191 |
}
|
|
|
192 |
if (tree.getSelectionPath() != null) {
|
|
|
193 |
node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
|
|
|
194 |
}
|
|
|
195 |
if (node != root) {
|
|
|
196 |
DefaultMutableTreeNode newNode = new ActivableMutableTreeNode(new Group("group" + node.getParent().getChildCount() + 1));
|
|
|
197 |
final DefaultMutableTreeNode parent = (DefaultMutableTreeNode) node.getParent();
|
|
|
198 |
parent.insert(newNode, parent.getIndex(node));
|
|
|
199 |
newModel.reload();
|
|
|
200 |
tree.setSelectionPath(new TreePath(newModel.getPathToRoot(newNode)));
|
|
|
201 |
}
|
|
|
202 |
|
|
|
203 |
}
|
|
|
204 |
|
|
|
205 |
public void setMainTable(String table) {
|
|
|
206 |
n.setTable(table);
|
|
|
207 |
|
|
|
208 |
initGroupFromTable(extension.getAllKnownFieldName(table));
|
|
|
209 |
newModel.fillFromGroup(n, this.tableGroup);
|
|
|
210 |
|
|
|
211 |
tree.expandRow(0);
|
|
|
212 |
}
|
|
|
213 |
|
|
|
214 |
public void initGroupFromTable(List<String> fields) {
|
|
|
215 |
System.out.println("GroupEditor.initGroupFromTable()");
|
|
|
216 |
System.out.println("GroupEditor.initGroupFromTable Component group");
|
|
|
217 |
|
|
|
218 |
this.tableGroup = new Group(n.getId());
|
|
|
219 |
for (String field : fields) {
|
|
|
220 |
Item i = n.getItemFromId(field);
|
|
|
221 |
Item newItem = new Item(field);
|
|
|
222 |
|
|
|
223 |
if (i != null) {
|
|
|
224 |
System.out.println("GroupEditor.initGroupFromTable() searching found: " + i + ":" + i.getLocalHint());
|
|
|
225 |
newItem.setLocalHint(new LayoutHints(i.getLocalHint()));
|
|
|
226 |
}
|
|
|
227 |
this.tableGroup.add(newItem);
|
|
|
228 |
}
|
|
|
229 |
System.out.println("GroupEditor.initGroupFromTable Table group");
|
|
|
230 |
|
|
|
231 |
}
|
|
|
232 |
|
|
|
233 |
public Group getFilteredGroup() {
|
|
|
234 |
// Parcours du Tree
|
|
|
235 |
Group filteredGroup = new Group(n.getId());
|
|
|
236 |
if (n.getTable() == null) {
|
|
|
237 |
throw new IllegalStateException("Not table defined for " + n);
|
|
|
238 |
}
|
|
|
239 |
walk(newModel, filteredGroup, newModel.getRoot());
|
|
|
240 |
filteredGroup = (Group) filteredGroup.getItem(0);
|
|
|
241 |
|
|
|
242 |
return filteredGroup;
|
|
|
243 |
}
|
|
|
244 |
|
|
|
245 |
protected void walk(ItemTreeModel model, Group gr, Object o) {
|
|
|
246 |
int cc = model.getChildCount(o);
|
|
|
247 |
for (int i = 0; i < cc; i++) {
|
|
|
248 |
ActivableMutableTreeNode child = (ActivableMutableTreeNode) model.getChild(o, i);
|
|
|
249 |
if (child.isActive()) {
|
|
|
250 |
final Item userObject = (Item) child.getUserObject();
|
|
|
251 |
if (userObject instanceof Group) {
|
|
|
252 |
final Group item = new Group(userObject.getId());
|
|
|
253 |
item.setLocalHint(new LayoutHints(userObject.getLocalHint()));
|
|
|
254 |
gr.add(item);
|
|
|
255 |
walk(model, item, child);
|
|
|
256 |
} else {
|
|
|
257 |
final Item item = new Item(userObject.getId());
|
|
|
258 |
item.setLocalHint(new LayoutHints(userObject.getLocalHint()));
|
|
|
259 |
final SQLTable table = ComptaPropsConfiguration.getInstanceCompta().getRootSociete().getTable(n.getTable());
|
|
|
260 |
if (table.contains(userObject.getId())) {
|
|
|
261 |
SQLField field = table.getField(userObject.getId());
|
|
|
262 |
if (!field.isPrimaryKey() && !field.getName().endsWith("ORDRE") && !field.getName().endsWith("ARCHIVE")) {
|
|
|
263 |
gr.add(item);
|
|
|
264 |
}
|
|
|
265 |
}
|
|
|
266 |
}
|
|
|
267 |
|
|
|
268 |
}
|
|
|
269 |
}
|
|
|
270 |
}
|
|
|
271 |
|
|
|
272 |
}
|