OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 74 | 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.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);
181 ilm 64
        this.newModel = new ItemTreeModel();
65
        this.tree = new ReorderableJTree() {
74 ilm 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
        };
181 ilm 79
        this.tree.setModel(this.newModel);
80
        this.tree.setRootVisible(false);
81
        this.tree.setShowsRootHandles(true);
82
        this.tree.expandRow(0);
74 ilm 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);
181 ilm 100
        this.tree.setCellRenderer(treeRenderer);
101
        final JScrollPane comp2 = new JScrollPane(this.tree);
74 ilm 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) {
181 ilm 142
                GroupEditor.this.newModel.toggleActive(GroupEditor.this.tree.getSelectionPath());
74 ilm 143
            }
144
        });
145
 
146
        hideCheckbox.addActionListener(new ActionListener() {
147
 
148
            @Override
149
            public void actionPerformed(ActionEvent e) {
181 ilm 150
                GroupEditor.this.newModel.setShowAll(hideCheckbox.isSelected());
151
                setMainTable(GroupEditor.this.n.getTable());
74 ilm 152
                setRightPanel(new JPanel());
153
            }
154
        });
155
 
181 ilm 156
        this.tree.addTreeSelectionListener(new TreeSelectionListener() {
74 ilm 157
 
158
            @Override
159
            public void valueChanged(TreeSelectionEvent e) {
181 ilm 160
                final Object selectedValue = GroupEditor.this.tree.getSelectionPath();
74 ilm 161
                showHideButton.setEnabled((selectedValue != null));
162
            }
163
        });
181 ilm 164
        this.tree.addMouseListener(new MouseAdapter() {
74 ilm 165
            @Override
166
            public void mouseClicked(MouseEvent e) {
167
                if (e.getClickCount() > 1) {
181 ilm 168
                    GroupEditor.this.newModel.toggleActive(GroupEditor.this.tree.getSelectionPath());
74 ilm 169
                }
170
            }
171
 
172
            @Override
173
            public void mousePressed(MouseEvent e) {
181 ilm 174
                final TreePath selectionPath = GroupEditor.this.tree.getSelectionPath();
74 ilm 175
                if (selectionPath == null) {
176
                    setRightPanel(new JPanel());
177
                } else {
178
                    Item i = (Item) ((DefaultMutableTreeNode) selectionPath.getLastPathComponent()).getUserObject();
181 ilm 179
                    setRightPanel(new ItemEditor(i, GroupEditor.this.n));
74 ilm 180
                }
181
            }
182
        });
183
        return panel;
184
    }
185
 
186
    protected void addNewGroup() {
181 ilm 187
        final DefaultMutableTreeNode root = (DefaultMutableTreeNode) ((DefaultMutableTreeNode) (this.tree.getModel().getRoot())).getFirstChild();
74 ilm 188
        DefaultMutableTreeNode node = root;
189
        if (node.getChildCount() > 0) {
190
            node = (DefaultMutableTreeNode) node.getFirstChild();
191
        }
181 ilm 192
        if (this.tree.getSelectionPath() != null) {
193
            node = (DefaultMutableTreeNode) this.tree.getLastSelectedPathComponent();
74 ilm 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));
181 ilm 199
            this.newModel.reload();
200
            this.tree.setSelectionPath(new TreePath(this.newModel.getPathToRoot(newNode)));
74 ilm 201
        }
202
 
203
    }
204
 
205
    public void setMainTable(String table) {
181 ilm 206
        this.n.setTable(table);
74 ilm 207
 
181 ilm 208
        initGroupFromTable(this.extension.getAllKnownFieldName(table));
209
        this.newModel.fillFromGroup(this.n, this.tableGroup);
74 ilm 210
 
181 ilm 211
        this.tree.expandRow(0);
74 ilm 212
    }
213
 
214
    public void initGroupFromTable(List<String> fields) {
215
        System.out.println("GroupEditor.initGroupFromTable()");
216
        System.out.println("GroupEditor.initGroupFromTable Component group");
217
 
181 ilm 218
        this.tableGroup = new Group(this.n.getId());
74 ilm 219
        for (String field : fields) {
181 ilm 220
            Item i = this.n.getItemFromId(field);
74 ilm 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
181 ilm 235
        Group filteredGroup = new Group(this.n.getId());
236
        if (this.n.getTable() == null) {
237
            throw new IllegalStateException("Not table defined for " + this.n);
74 ilm 238
        }
181 ilm 239
        walk(this.newModel, filteredGroup, this.newModel.getRoot());
74 ilm 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()));
181 ilm 259
                    final SQLTable table = ComptaPropsConfiguration.getInstanceCompta().getRootSociete().getTable(this.n.getTable());
74 ilm 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
}