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.translation.menu;
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.MouseAdapter;
10
import java.awt.event.MouseEvent;
11
 
12
import javax.swing.JComponent;
13
import javax.swing.JLabel;
14
import javax.swing.JPanel;
15
import javax.swing.JScrollPane;
16
import javax.swing.JTree;
17
import javax.swing.tree.DefaultMutableTreeNode;
18
import javax.swing.tree.DefaultTreeCellRenderer;
19
import javax.swing.tree.TreePath;
20
 
21
import org.openconcerto.modules.extensionbuilder.AbstractSplittedPanel;
22
import org.openconcerto.modules.extensionbuilder.Extension;
23
import org.openconcerto.modules.extensionbuilder.component.ActivableMutableTreeNode;
24
import org.openconcerto.modules.extensionbuilder.menu.mainmenu.MenuItemTreeModel;
25
import org.openconcerto.ui.group.Group;
26
import org.openconcerto.ui.group.Item;
27
 
28
public class MenuTranslationPanel extends AbstractSplittedPanel {
29
 
30
    private MenuItemTreeModel newModel;
31
    private JTree tree;
32
 
33
    public MenuTranslationPanel(Extension extension) {
34
        super(extension);
35
        fillModel();
36
    }
37
 
38
    public void fillModel() {
181 ilm 39
        this.newModel.fillFromDescriptor(this.extension);
40
        this.tree.setModel(this.newModel);
41
        this.tree.expandRow(0);
74 ilm 42
    }
43
 
44
    @Override
45
    public JComponent createLeftComponent() {
46
        final JPanel panel = new JPanel();
47
        panel.setLayout(new GridBagLayout());
48
        final GridBagConstraints c = new GridBagConstraints();
49
        c.fill = GridBagConstraints.BOTH;
50
        c.gridx = 0;
51
        c.gridy = 0;
52
        c.weightx = 1;
53
        c.weighty = 0;
54
        c.insets = new Insets(2, 2, 2, 0);
55
 
56
        panel.add(new JLabel("Menus"), c);
181 ilm 57
        this.newModel = new MenuItemTreeModel();
58
        this.tree = new JTree() {
74 ilm 59
            @Override
60
            public String convertValueToText(Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
61
                final Object userObject = ((DefaultMutableTreeNode) value).getUserObject();
62
                if (userObject == null) {
63
                    return "null";
64
                }
65
                if (userObject instanceof Group) {
66
                    Group d = (Group) userObject;
67
                    return " " + d.getId();
68
                }
69
                return userObject.toString();
70
            }
71
        };
181 ilm 72
        this.tree.setModel(this.newModel);
73
        this.tree.setRootVisible(false);
74
        this.tree.setShowsRootHandles(true);
75
        this.tree.expandRow(0);
74 ilm 76
        final DefaultTreeCellRenderer treeRenderer = new DefaultTreeCellRenderer() {
77
            @Override
78
            public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
79
 
80
                final JLabel r = (JLabel) super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
81
                final ActivableMutableTreeNode tr = (ActivableMutableTreeNode) value;
82
                if (!tr.isActive()) {
83
                    r.setForeground(Color.LIGHT_GRAY);
84
                }
85
                if (tr.getUserObject() instanceof Item) {
86
                    r.setText(((Item) tr.getUserObject()).getId());
87
                }
181 ilm 88
                if (sel) {
89
                    r.setForeground(Color.WHITE);
90
                }
74 ilm 91
                return r;
92
            }
93
        };
94
 
95
        treeRenderer.setLeafIcon(null);
181 ilm 96
        this.tree.setCellRenderer(treeRenderer);
97
        final JScrollPane comp2 = new JScrollPane(this.tree);
74 ilm 98
        comp2.setMinimumSize(new Dimension(250, 150));
99
        comp2.setPreferredSize(new Dimension(250, 150));
100
        c.weighty = 1;
101
        c.gridy++;
102
        c.insets = new Insets(0, 0, 0, 0);
103
        panel.add(comp2, c);
104
 
105
        // init
106
 
181 ilm 107
        this.tree.addMouseListener(new MouseAdapter() {
74 ilm 108
 
109
            @Override
110
            public void mousePressed(MouseEvent e) {
181 ilm 111
                final TreePath selectionPath = MenuTranslationPanel.this.tree.getSelectionPath();
74 ilm 112
                if (selectionPath == null) {
113
                    setRightPanel(new JPanel());
114
                } else {
115
                    Item i = (Item) ((DefaultMutableTreeNode) selectionPath.getLastPathComponent()).getUserObject();
181 ilm 116
                    setRightPanel(new MenuTranslationItemEditor(i, MenuTranslationPanel.this.extension));
74 ilm 117
                }
118
            }
119
        });
120
        return panel;
121
    }
122
 
123
}