OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 74 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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