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 javax.swing.tree.DefaultMutableTreeNode;
4
import javax.swing.tree.DefaultTreeModel;
5
import javax.swing.tree.TreePath;
6
 
7
import org.openconcerto.modules.extensionbuilder.table.ForbiddenFieldName;
8
import org.openconcerto.ui.group.Group;
9
import org.openconcerto.ui.group.Item;
10
 
11
public class GroupTreeModel extends DefaultTreeModel {
12
 
13
    private boolean showAll = true;
14
    private GroupDescritor componentDescriptor;
15
 
16
    public GroupTreeModel() {
17
        super(null, false);
18
 
19
    }
20
 
21
    public void setShowAll(boolean b) {
181 ilm 22
        if (b != this.showAll) {
74 ilm 23
            this.showAll = b;
24
            System.err.println("GroupTreeModel.setShowAll()" + b);
25
            reload();
26
        }
27
    }
28
 
29
    public void fillFromGroup(GroupDescritor n, Group tableGroup) {
30
        if (n == null) {
31
            throw new IllegalArgumentException("null GroupDescriptor");
32
        }
33
        this.componentDescriptor = n;
34
        final ActivableMutableTreeNode root = new ActivableMutableTreeNode(null);
35
        root.setActive(true);
36
        if (tableGroup == null) {
37
            this.setRoot(root);
38
            return;
39
        }
40
        System.err.println("Desc:");
41
        System.err.println(n.getGroup().printTree());
42
        System.err.println("Table:");
43
        System.err.println(tableGroup.printTree());
44
 
45
        addToTreeNode(root, n.getGroup(), n, 0);
46
        // Add from tableGroup
47
        // Group filteredTableGroup = new Group(tableGroup.getId());
48
        // filteredTableGroup = Group.copy(tableGroup, filteredTableGroup);
49
        for (int i = 0; i < tableGroup.getSize(); i++) {
50
            Item item = tableGroup.getItem(i);
51
            final String id = item.getId();
52
            if (!n.getGroup().contains(id) && ForbiddenFieldName.isAllowed(id) && !id.equals("ID")) {
53
                addToTreeNode((DefaultMutableTreeNode) root.getFirstChild(), item, n, 0);
54
            }
55
        }
56
 
57
        // /////////
58
        this.setRoot(root);
59
    }
60
 
61
    void addToTreeNode(DefaultMutableTreeNode node, Item item, GroupDescritor n, int depth) {
62
        System.err.println("GroupTreeModel.addToTreeNode():" + node + " item:" + item + " Desc:" + n + " Depth:" + depth);
63
 
64
        if (depth > 50) {
65
            return;
66
        }
67
        depth++;
68
        final ActivableMutableTreeNode newChild = new ActivableMutableTreeNode(item);
69
 
70
        newChild.setActive(n.containsGroupId(item.getId()));
181 ilm 71
        if (this.showAll || newChild.isActive()) {
74 ilm 72
            node.add(newChild);
73
        }
74
        if (item instanceof Group) {
75
            final Group gr = (Group) item;
76
            final int childCount = gr.getSize();
77
            for (int i = 0; i < childCount; i++) {
78
                final Item it = gr.getItem(i);
79
                addToTreeNode(newChild, it, n, depth);
80
            }
81
            newChild.setAllowsChildren(true);
82
        } else {
83
            newChild.setAllowsChildren(false);
84
        }
85
    }
86
 
87
    public void toggleActive(TreePath selectionPath) {
88
        if (selectionPath == null) {
89
            return;
90
        }
91
        final ActivableMutableTreeNode n = (ActivableMutableTreeNode) selectionPath.getLastPathComponent();
92
 
93
        final Item item = (Item) n.getUserObject();
94
        if (item instanceof Group) {
95
            // A Group is always active
96
            return;
97
        }
98
        n.setActive(!n.isActive());
99
        if (n.isActive()) {
100
            this.componentDescriptor.updateGroupFrom(this);
101
        } else {
102
            this.componentDescriptor.removeGroup(item);
103
        }
104
        reload(n);
181 ilm 105
        this.componentDescriptor.fireGroupChanged();
74 ilm 106
    }
107
 
108
    @Override
109
    protected void fireTreeNodesInserted(Object source, Object[] path, int[] childIndices, Object[] children) {
110
        // To update preview while reordering
111
        super.fireTreeNodesInserted(source, path, childIndices, children);
181 ilm 112
        this.componentDescriptor.fireGroupChanged();
74 ilm 113
    }
114
 
115
    @Override
116
    public boolean isLeaf(Object node) {
117
        final ActivableMutableTreeNode n = (ActivableMutableTreeNode) node;
118
        if (n.getUserObject() == null)
119
            return super.isLeaf(node);
120
        return !(n.getUserObject() instanceof Group);
121
    }
122
}