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.util.ArrayList;
4
import java.util.List;
5
 
6
import javax.swing.event.ChangeEvent;
7
import javax.swing.event.ChangeListener;
8
 
9
import org.openconcerto.ui.group.Group;
10
import org.openconcerto.ui.group.Item;
11
import org.openconcerto.ui.group.LayoutHints;
12
 
13
public class GroupDescritor {
14
 
15
    private String id;
16
 
17
    private Group group;
18
    private List<ChangeListener> groupChangeListener = new ArrayList<ChangeListener>();
19
 
20
    public GroupDescritor(String id) {
21
        this.id = id;
22
        this.group = new Group(id);
23
    }
24
 
25
    public String getId() {
181 ilm 26
        return this.id;
74 ilm 27
    }
28
 
29
    public void setId(String id) {
30
        this.id = id;
31
    }
32
 
33
    public Group getGroup() {
181 ilm 34
        return this.group;
74 ilm 35
    }
36
 
37
    @Override
38
    public String toString() {
39
        return this.getId();
40
    }
41
 
42
    public boolean containsGroupId(String gId) {
181 ilm 43
        return containsGroup(this.group, gId);
74 ilm 44
    }
45
 
46
    private boolean containsGroup(Item item, String gId) {
47
        if (item.getId().equals(gId)) {
48
            return true;
49
        }
50
        if (item instanceof Group) {
51
            Group group = (Group) item;
52
            final int size = group.getSize();
53
            for (int i = 0; i < size; i++) {
54
                boolean b = containsGroup(group.getItem(i), gId);
55
                if (b) {
56
                    return true;
57
                }
58
            }
59
        }
60
        return false;
61
    }
62
 
63
    public Item getItemFromId(String id) {
181 ilm 64
        return getItemFromId(this.group, id);
74 ilm 65
    }
66
 
67
    private Item getItemFromId(Item item, String gId) {
68
        if (item.getId().equals(gId)) {
69
            return item;
70
        }
71
        if (item instanceof Group) {
72
            Group group = (Group) item;
73
            final int size = group.getSize();
74
            for (int i = 0; i < size; i++) {
75
                Item b = getItemFromId(group.getItem(i), gId);
76
                if (b != null) {
77
                    return b;
78
                }
79
            }
80
        }
81
        return null;
82
    }
83
 
84
    public void removeGroup(Item gr) {
85
        remove(this.group, gr.getId());
86
 
87
    }
88
 
89
    private void remove(Item item, String gId) {
90
        if (item instanceof Group) {
91
            Group group2 = (Group) item;
92
            group2.remove(gId);
93
        }
94
    }
95
 
96
    public void updateGroupFrom(GroupTreeModel model) {
97
        this.group = new Group(this.getId());
98
        walk(model, this.group, model.getRoot());
99
        this.group = (Group) this.group.getItem(0);
100
 
101
    }
102
 
103
    protected void walk(GroupTreeModel model, Group gr, Object o) {
104
        int cc = model.getChildCount(o);
105
        for (int i = 0; i < cc; i++) {
106
            ActivableMutableTreeNode child = (ActivableMutableTreeNode) model.getChild(o, i);
107
            if (child.isActive()) {
108
                final Item userObject = (Item) child.getUserObject();
109
                if (userObject instanceof Group) {
110
                    final Group item = new Group(userObject.getId());
111
                    item.setLocalHint(new LayoutHints(userObject.getLocalHint()));
112
                    gr.add(item);
113
                    walk(model, item, child);
114
                } else {
115
                    final Item item = new Item(userObject.getId());
116
                    item.setLocalHint(new LayoutHints(userObject.getLocalHint()));
117
                    gr.add(item);
118
                }
119
 
120
            }
121
        }
122
    }
123
 
124
    public void addGroupChangeListener(ChangeListener changeListener) {
125
        this.groupChangeListener.add(changeListener);
126
 
127
    }
128
 
129
    public void fireGroupChanged() {
130
        for (ChangeListener l : this.groupChangeListener) {
131
            l.stateChanged(new ChangeEvent(this));
132
        }
133
    }
134
 
135
}