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.menu.mainmenu;
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.ArrayList;
14
import java.util.Enumeration;
15
import java.util.List;
16
 
17
import javax.swing.JButton;
18
import javax.swing.JCheckBox;
19
import javax.swing.JComponent;
20
import javax.swing.JLabel;
21
import javax.swing.JPanel;
22
import javax.swing.JScrollPane;
23
import javax.swing.JTree;
24
import javax.swing.SwingUtilities;
25
import javax.swing.event.TreeModelEvent;
26
import javax.swing.event.TreeModelListener;
27
import javax.swing.event.TreeSelectionEvent;
28
import javax.swing.event.TreeSelectionListener;
29
import javax.swing.tree.DefaultMutableTreeNode;
30
import javax.swing.tree.DefaultTreeCellRenderer;
31
import javax.swing.tree.TreePath;
32
 
33
import org.openconcerto.modules.extensionbuilder.AbstractSplittedPanel;
34
import org.openconcerto.modules.extensionbuilder.Extension;
35
import org.openconcerto.modules.extensionbuilder.component.ActivableMutableTreeNode;
36
import org.openconcerto.ui.group.Group;
37
import org.openconcerto.ui.group.Item;
38
import org.openconcerto.ui.group.LayoutHints;
39
import org.openconcerto.ui.tree.ReorderableJTree;
40
 
41
public class MainMenuGroupEditor extends AbstractSplittedPanel {
42
 
43
    private MenuItemTreeModel newModel;
44
    private JTree tree;
45
 
46
    public MainMenuGroupEditor(Extension extension) {
47
        super(extension);
48
        fillModel();
49
    }
50
 
51
    public void fillModel() {
181 ilm 52
        this.newModel.fillFromDescriptor(this.extension);
53
        this.tree.setModel(this.newModel);
74 ilm 54
        expand();
55
 
56
    }
57
 
58
    private void expand() {
181 ilm 59
        this.tree.expandRow(0);
74 ilm 60
 
61
        final List<MenuDescriptor> m = new ArrayList<MenuDescriptor>();
181 ilm 62
        m.addAll(this.extension.getCreateMenuList());
63
        m.addAll(this.extension.getRemoveMenuList());
74 ilm 64
        for (MenuDescriptor menuDescriptor : m) {
181 ilm 65
            final DefaultMutableTreeNode root = (DefaultMutableTreeNode) this.tree.getModel().getRoot();
74 ilm 66
            @SuppressWarnings("unchecked")
67
            final Enumeration<DefaultMutableTreeNode> e = root.depthFirstEnumeration();
68
            while (e.hasMoreElements()) {
69
                final DefaultMutableTreeNode node = e.nextElement();
70
                final Object userObject = node.getUserObject();
71
                if (userObject != null) {
72
                    final String nodeLabel = ((Item) userObject).getId();
73
                    if (nodeLabel != null && nodeLabel.equals(menuDescriptor.getId())) {
74
                        final TreePath path = new TreePath(((DefaultMutableTreeNode) node.getParent()).getPath());
181 ilm 75
                        this.tree.expandPath(path);
74 ilm 76
                    }
77
                }
78
            }
79
        }
80
    }
81
 
82
    @Override
83
    public JComponent createLeftComponent() {
84
        final JPanel panel = new JPanel();
85
        panel.setLayout(new GridBagLayout());
86
        final GridBagConstraints c = new GridBagConstraints();
87
        c.fill = GridBagConstraints.BOTH;
88
        c.gridx = 0;
89
        c.gridy = 0;
90
        c.weightx = 1;
91
        c.weighty = 0;
92
        c.insets = new Insets(2, 2, 2, 0);
93
 
94
        panel.add(new JLabel("Menus"), c);
181 ilm 95
        this.newModel = new MenuItemTreeModel();
96
        this.tree = new ReorderableJTree();
97
        this.tree.setModel(this.newModel);
98
        this.tree.setRootVisible(false);
99
        this.tree.setShowsRootHandles(true);
74 ilm 100
 
101
        final DefaultTreeCellRenderer treeRenderer = new DefaultTreeCellRenderer() {
102
            @Override
103
            public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
104
                final ActivableMutableTreeNode tr = (ActivableMutableTreeNode) value;
105
                if (tr.getUserObject() instanceof Item) {
106
                    final String id = ((Item) tr.getUserObject()).getId();
107
                    value = id;
108
                }
109
                final JLabel r = (JLabel) super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
110
                if (tr.getUserObject() instanceof Item) {
111
                    final String id = ((Item) tr.getUserObject()).getId();
181 ilm 112
                    if (MainMenuGroupEditor.this.extension.getCreateMenuItemFromId(id) != null) {
74 ilm 113
                        r.setForeground(new Color(50, 80, 150));
114
                    }
115
                }
116
                if (!tr.isActive()) {
117
                    r.setForeground(Color.LIGHT_GRAY);
118
                }
181 ilm 119
                if (sel) {
120
                    r.setForeground(Color.WHITE);
121
                }
74 ilm 122
                return r;
123
            }
124
        };
125
 
126
        treeRenderer.setLeafIcon(null);
181 ilm 127
        this.tree.setCellRenderer(treeRenderer);
128
        final JScrollPane comp2 = new JScrollPane(this.tree);
74 ilm 129
        comp2.setMinimumSize(new Dimension(250, 150));
130
        comp2.setPreferredSize(new Dimension(250, 150));
131
        c.weighty = 1;
132
        c.gridy++;
133
        c.insets = new Insets(0, 0, 0, 0);
134
        panel.add(comp2, c);
135
 
136
        c.gridy++;
137
        c.weighty = 0;
138
        c.fill = GridBagConstraints.NONE;
139
        c.anchor = GridBagConstraints.SOUTHWEST;
140
        c.insets = new Insets(2, 2, 2, 0);
141
        final JButton addGroupButton = new JButton("Ajouter un menu");
142
        panel.add(addGroupButton, c);
143
        c.gridy++;
144
        final JButton addItemButton = new JButton("Ajouter une action");
145
        panel.add(addItemButton, c);
146
        c.gridy++;
147
        final JButton removeButton = new JButton("Supprimer ");
148
        removeButton.setEnabled(false);
149
        panel.add(removeButton, c);
150
 
151
        c.gridy++;
152
        final JCheckBox hideCheckbox = new JCheckBox("Afficher les champs masqués");
153
        hideCheckbox.setSelected(true);
154
        panel.add(hideCheckbox, c);
155
 
156
        // Listeners
157
        addGroupButton.addActionListener(new ActionListener() {
158
 
159
            @Override
160
            public void actionPerformed(ActionEvent e) {
161
                addNewGroup();
162
            }
163
 
164
        });
165
        addItemButton.addActionListener(new ActionListener() {
166
 
167
            @Override
168
            public void actionPerformed(ActionEvent e) {
169
                addNewItem();
170
            }
171
 
172
        });
173
        removeButton.addActionListener(new ActionListener() {
174
            @Override
175
            public void actionPerformed(ActionEvent e) {
181 ilm 176
                final TreePath selectionPath = MainMenuGroupEditor.this.tree.getSelectionPath();
74 ilm 177
                if (selectionPath != null) {
178
                    DefaultMutableTreeNode node = (DefaultMutableTreeNode) selectionPath.getLastPathComponent();
179
                    if (node.getUserObject() != null) {
180
                        String idToDelete = ((Item) node.getUserObject()).getId();
181 ilm 181
                        MainMenuGroupEditor.this.extension.removeCreateMenuForId(idToDelete);
182
                        MainMenuGroupEditor.this.extension.setChanged();
74 ilm 183
                        fillModel();
184
                    }
185
 
186
                }
187
            }
188
        });
189
        hideCheckbox.addActionListener(new ActionListener() {
190
 
191
            @Override
192
            public void actionPerformed(ActionEvent e) {
181 ilm 193
                MainMenuGroupEditor.this.newModel.setShowAll(hideCheckbox.isSelected());
194
                MainMenuGroupEditor.this.newModel.fillFromDescriptor(MainMenuGroupEditor.this.extension);
74 ilm 195
            }
196
 
197
        });
181 ilm 198
        this.tree.addTreeSelectionListener(new TreeSelectionListener() {
74 ilm 199
 
200
            @Override
201
            public void valueChanged(TreeSelectionEvent e) {
181 ilm 202
                final TreePath selectionPath = MainMenuGroupEditor.this.tree.getSelectionPath();
74 ilm 203
                if (selectionPath != null) {
204
                    DefaultMutableTreeNode node = (DefaultMutableTreeNode) selectionPath.getLastPathComponent();
205
                    if (node.getUserObject() != null) {
206
                        String selectedId = ((Item) node.getUserObject()).getId();
181 ilm 207
                        removeButton.setEnabled(MainMenuGroupEditor.this.extension.getCreateMenuItemFromId(selectedId) != null);
74 ilm 208
                    }
209
                } else {
210
                    removeButton.setEnabled(false);
211
                }
212
 
213
            }
214
        });
181 ilm 215
        this.tree.addMouseListener(new MouseAdapter() {
74 ilm 216
            @Override
217
            public void mouseClicked(MouseEvent e) {
218
                if (e.getClickCount() > 1) {
181 ilm 219
                    MainMenuGroupEditor.this.newModel.toggleActive(MainMenuGroupEditor.this.tree.getSelectionPath());
74 ilm 220
                }
221
            }
222
 
223
            @Override
224
            public void mousePressed(MouseEvent e) {
181 ilm 225
                final TreePath selectionPath = MainMenuGroupEditor.this.tree.getSelectionPath();
74 ilm 226
                if (selectionPath == null) {
227
                    setRightPanel(new JPanel());
228
                } else {
229
                    Item i = (Item) ((DefaultMutableTreeNode) selectionPath.getLastPathComponent()).getUserObject();
181 ilm 230
                    setRightPanel(new MenuItemEditor(MainMenuGroupEditor.this.newModel, i, MainMenuGroupEditor.this.extension));
74 ilm 231
                }
232
            }
233
        });
181 ilm 234
        this.tree.getModel().addTreeModelListener(new TreeModelListener() {
74 ilm 235
 
236
            @Override
237
            public void treeStructureChanged(TreeModelEvent e) {
238
                // Postpone expand because default behaviour is collapsing tree
239
                SwingUtilities.invokeLater(new Runnable() {
240
                    @Override
241
                    public void run() {
242
                        expand();
243
                    }
244
                });
245
            }
246
 
247
            @Override
248
            public void treeNodesRemoved(TreeModelEvent e) {
249
            }
250
 
251
            @Override
252
            public void treeNodesInserted(TreeModelEvent e) {
253
            }
254
 
255
            @Override
256
            public void treeNodesChanged(TreeModelEvent e) {
257
            }
258
        });
259
        return panel;
260
    }
261
 
262
    protected void addNewGroup() {
181 ilm 263
        final DefaultMutableTreeNode root = (DefaultMutableTreeNode) ((DefaultMutableTreeNode) (this.tree.getModel().getRoot())).getFirstChild();
74 ilm 264
        DefaultMutableTreeNode node = root;
265
        if (node.getChildCount() > 0) {
266
            node = (DefaultMutableTreeNode) node.getFirstChild();
267
        }
181 ilm 268
        if (this.tree.getSelectionPath() != null) {
269
            node = (DefaultMutableTreeNode) this.tree.getLastSelectedPathComponent();
74 ilm 270
        }
271
        if (node != root) {
272
            final String newGroupId = "group" + node.getParent().getChildCount() + 1;
273
            DefaultMutableTreeNode newNode = new ActivableMutableTreeNode(new Group(newGroupId));
274
            final DefaultMutableTreeNode parent = (DefaultMutableTreeNode) node.getParent();
275
            parent.insert(newNode, parent.getIndex(node));
276
 
277
            final MenuDescriptor desc = new MenuDescriptor(newGroupId);
278
            desc.setType(MenuDescriptor.GROUP);
279
            desc.setInsertInMenu(((Item) parent.getUserObject()).getId());
181 ilm 280
            this.extension.addCreateMenu(desc);
281
            this.extension.setChanged();
74 ilm 282
 
181 ilm 283
            this.newModel.reload();
284
            this.tree.setSelectionPath(new TreePath(this.newModel.getPathToRoot(newNode)));
74 ilm 285
        }
181 ilm 286
        this.extension.setChanged();
74 ilm 287
    }
288
 
289
    protected void addNewItem() {
181 ilm 290
        final DefaultMutableTreeNode root = (DefaultMutableTreeNode) ((DefaultMutableTreeNode) (this.tree.getModel().getRoot())).getFirstChild();
74 ilm 291
        DefaultMutableTreeNode node = root;
292
        if (node.getChildCount() > 0) {
293
            node = (DefaultMutableTreeNode) node.getFirstChild();
294
        }
181 ilm 295
        if (this.tree.getSelectionPath() != null) {
296
            node = (DefaultMutableTreeNode) this.tree.getLastSelectedPathComponent();
74 ilm 297
        }
298
        if (node != root) {
299
            final String newActionId = "action" + node.getParent().getChildCount() + 1;
300
            DefaultMutableTreeNode newNode = new ActivableMutableTreeNode(new Item(newActionId));
301
            final DefaultMutableTreeNode parent = (DefaultMutableTreeNode) node.getParent();
302
            parent.insert(newNode, parent.getIndex(node));
303
            final MenuDescriptor desc = new MenuDescriptor(newActionId);
304
            desc.setType(MenuDescriptor.CREATE);
305
            desc.setInsertInMenu(((Item) parent.getUserObject()).getId());
181 ilm 306
            this.extension.addCreateMenu(desc);
307
            this.extension.setChanged();
308
            this.newModel.reload();
309
            this.tree.setSelectionPath(new TreePath(this.newModel.getPathToRoot(newNode)));
74 ilm 310
        }
311
 
312
    }
313
 
314
    // public Group getFilteredGroup() {
315
    // // Parcours du Tree
316
    // Group filteredGroup = new Group(n.getId());
317
    //
318
    // walk(newModel, filteredGroup, newModel.getRoot());
319
    // filteredGroup = (Group) filteredGroup.getItem(0);
320
    // filteredGroup.dumpTwoColumn();
321
    // return filteredGroup;
322
    // }
323
 
324
    protected void walk(MenuItemTreeModel model, Group gr, Object o) {
325
        int cc = model.getChildCount(o);
326
        for (int i = 0; i < cc; i++) {
327
            ActivableMutableTreeNode child = (ActivableMutableTreeNode) model.getChild(o, i);
328
            if (child.isActive()) {
329
                final Item userObject = (Item) child.getUserObject();
330
                if (userObject instanceof Group) {
331
                    final Group item = new Group(userObject.getId());
332
                    item.setLocalHint(new LayoutHints(userObject.getLocalHint()));
333
                    gr.add(item);
334
                    walk(model, item, child);
335
                } else {
336
                    final Item item = new Item(userObject.getId());
337
                    item.setLocalHint(new LayoutHints(userObject.getLocalHint()));
338
                    gr.add(item);
339
                }
340
 
341
            }
342
        }
343
    }
344
 
345
}