OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 94 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
41 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.ui.group;
15
 
67 ilm 16
import org.openconcerto.utils.Tuple2;
17
 
41 ilm 18
import java.util.ArrayList;
83 ilm 19
import java.util.Collection;
41 ilm 20
import java.util.Collections;
21
import java.util.Comparator;
22
import java.util.List;
23
 
67 ilm 24
public class Group extends Item {
41 ilm 25
 
73 ilm 26
    public static Group copy(final Group g, final Group newParent) {
27
        return new Group(g, newParent);
41 ilm 28
    }
29
 
73 ilm 30
    private static final Comparator<Tuple2<Item, Integer>> COMPARATOR = new Comparator<Tuple2<Item, Integer>>() {
31
        @Override
32
        public int compare(final Tuple2<Item, Integer> o1, final Tuple2<Item, Integer> o2) {
33
            int c = o1.get1().compareTo(o2.get1());
34
            if (c == 0) {
35
                c = o1.get0().getId().compareTo(o2.get0().getId());
36
            }
37
            return c;
38
        }
39
    };
41 ilm 40
 
73 ilm 41
    private int order;
42
    private final List<Tuple2<Item, Integer>> list;
41 ilm 43
 
156 ilm 44
    private String tabId;
45
 
73 ilm 46
    public Group(final String id) {
47
        this(id, LayoutHints.DEFAULT_GROUP_HINTS);
41 ilm 48
    }
49
 
73 ilm 50
    public Group(final String id, final LayoutHints hint) {
51
        super(id, hint);
52
        this.order = 100;
53
        this.list = new ArrayList<Tuple2<Item, Integer>>();
41 ilm 54
    }
55
 
73 ilm 56
    public Group(final Group g, final Group newParent) {
57
        super(g, newParent);
58
        this.order = g.order;
59
        this.list = new ArrayList<Tuple2<Item, Integer>>(g.list.size());
60
        for (final Tuple2<Item, Integer> t : g.list) {
61
            final Item copy = Item.copy(t.get0(), this);
62
            this.list.add(new Tuple2<Item, Integer>(copy, t.get1()));
63
        }
41 ilm 64
    }
65
 
67 ilm 66
    @Override
73 ilm 67
    public synchronized void freeze() {
68
        super.freeze();
69
        for (final Tuple2<Item, Integer> child : this.list) {
70
            child.get0().freeze();
41 ilm 71
        }
72
    }
73
 
73 ilm 74
    public Item addItem(final String string) {
75
        final Item res = new Item(string);
76
        this.add(res);
77
        return res;
78
    }
41 ilm 79
 
73 ilm 80
    public Item addItem(final String string, final LayoutHints hint) {
81
        final Item res = new Item(string, hint);
82
        this.add(res);
83
        return res;
41 ilm 84
    }
85
 
73 ilm 86
    public void add(final Item item) {
87
        add(item, this.order);
41 ilm 88
    }
89
 
73 ilm 90
    public void add(final Item item, final int order) {
91
        checkFrozen("add");
92
        item.setParent(this);
93
        this.list.add(new Tuple2<Item, Integer>(item, order));
94
        Collections.sort(this.list, COMPARATOR);
95
        if (this.order <= order) {
96
            this.order = (order / 100) * 100 + 100;
41 ilm 97
        }
98
    }
99
 
73 ilm 100
    public final int getSize() {
41 ilm 101
        return this.list.size();
102
    }
103
 
73 ilm 104
    public final boolean isEmpty() {
41 ilm 105
        return this.list.isEmpty();
106
    }
107
 
73 ilm 108
    public Item getItem(final int index) {
67 ilm 109
        return this.list.get(index).get0();
41 ilm 110
    }
111
 
73 ilm 112
    public Integer getOrder(final int index) {
113
        return this.list.get(index).get1();
41 ilm 114
    }
115
 
73 ilm 116
    public boolean contains(final String id) {
117
        return getDescFromID(id) != null;
41 ilm 118
    }
119
 
94 ilm 120
    public final Collection<Group> getDescendantGroups() {
121
        final List<Group> res = new ArrayList<Group>();
122
        this.getDescendantGroups(res);
123
        return res;
124
    }
125
 
126
    protected void getDescendantGroups(final Collection<Group> res) {
127
        for (final Tuple2<Item, Integer> t : this.list) {
128
            if (t.get0() instanceof Group) {
129
                Group g = (Group) t.get0();
130
                res.add(g);
131
                g.getDescendantGroups(res);
132
            }
133
        }
134
    }
135
 
67 ilm 136
    @Override
83 ilm 137
    public final Collection<Item> getDescendantItems() {
138
        final List<Item> res = new ArrayList<Item>();
139
        this.getDescendantItems(res);
140
        return res;
141
    }
142
 
143
    @Override
144
    protected void getDescendantItems(final Collection<Item> res) {
145
        for (final Tuple2<Item, Integer> t : this.list) {
146
            t.get0().getDescendantItems(res);
147
        }
148
    }
149
 
150
    @Override
73 ilm 151
    public Item getDescFromID(final String id, final int maxLevel) {
152
        final Item res = super.getDescFromID(id, maxLevel);
153
        if (res != null || maxLevel == 0)
154
            return res;
155
        final int size = this.getSize();
156
        final int nextLevel = maxLevel < 0 ? maxLevel : maxLevel - 1;
157
        for (int i = 0; i < size; i++) {
158
            final Item desc = this.getItem(i).getDescFromID(id, nextLevel);
159
            if (desc != null) {
160
                return desc;
161
            }
162
        }
163
        return null;
67 ilm 164
    }
165
 
73 ilm 166
    /**
167
     * Get a descendant group.
168
     *
169
     * @param path a list of IDs.
170
     * @param create <code>true</code> if missing descendant should be created.
171
     * @return the descendant, or <code>null</code> if <code>create</code> is <code>false</code> and
172
     *         the descendant is missing.
173
     */
174
    public final Group followPath(final List<String> path, final boolean create) {
175
        final int size = path.size();
176
        Group g = this;
177
        for (int i = 0; i < size; i++) {
178
            final String id = path.get(i);
179
            final Item child = g.getChildFromID(id);
180
            if (child instanceof Group) {
181
                g = (Group) child;
182
            } else if (child != null) {
183
                throw new IllegalStateException("ID exists but isn't a group : " + child);
184
            } else if (create) {
185
                final Group ng = new Group(id);
186
                g.add(ng);
187
                g = ng;
188
            } else {
189
                return null;
67 ilm 190
            }
191
        }
73 ilm 192
        return g;
67 ilm 193
    }
194
 
73 ilm 195
    public void remove(final String itemId) {
67 ilm 196
        this.remove(this, itemId);
197
    }
198
 
73 ilm 199
    private void remove(final Group group, final String id) {
200
        checkFrozen("remove");
67 ilm 201
        final int size = group.getSize();
202
        for (int i = 0; i < size; i++) {
203
            final Item b = group.getItem(i);
73 ilm 204
            if (b.getId().equals(id)) {
205
                this.list.remove(i);
67 ilm 206
                return;
207
            }
208
            if (b instanceof Group) {
209
                remove((Group) b, id);
210
            }
211
        }
212
 
213
    }
214
 
73 ilm 215
    public Integer getOrder(final String id) {
67 ilm 216
        final int size = this.getSize();
217
        for (int i = 0; i < size; i++) {
73 ilm 218
            final Tuple2<Item, Integer> b = this.list.get(i);
67 ilm 219
            if (b.get0().getId().equals(id)) {
220
                return b.get1();
221
            }
222
        }
223
        return null;
224
    }
225
 
73 ilm 226
    public int getIndex(final String id) {
67 ilm 227
        final int size = this.getSize();
228
        for (int i = 0; i < size; i++) {
73 ilm 229
            final Tuple2<Item, Integer> b = this.list.get(i);
67 ilm 230
            if (b.get0().getId().equals(id)) {
231
                return i;
232
            }
233
        }
234
        return -1;
235
    }
236
 
73 ilm 237
    public String printTree() {
238
        final StringBuilder b = new StringBuilder();
239
        printTree(b, 0, 1);
240
        return b.toString();
241
    }
242
 
243
    @Override
244
    protected void printTree(final StringBuilder builder, final int localOrder, final int level) {
245
        for (int i = 0; i < level - 1; i++) {
246
            builder.append("  ");
247
        }
248
        builder.append("+-+ ");
249
        builder.append(localOrder + " " + this.getId() + " [" + this.getLocalHint() + "]\n");
250
        for (final Tuple2<Item, Integer> tuple : this.list) {
251
            tuple.get0().printTree(builder, tuple.get1(), level + 1);
252
        }
253
    }
254
 
255
    public String printTwoColumns() {
256
        final StringBuilder b = new StringBuilder("==== Group " + this.getId() + " ====\n");
257
        printColumns(b, 2, 0, 0, 1);
258
        return b.toString();
259
    }
260
 
261
    @Override
262
    protected int printColumns(final StringBuilder builder, final int width, int x, final int localOrder, final int level) {
263
        if (this.getLocalHint().isSeparated()) {
264
            x = 0;
265
            builder.append(" -------\n");
266
        }
267
        if (isEmpty()) {
268
            // print a leaf
269
            x = super.printColumns(builder, width, x, localOrder, level);
270
        } else {
271
            // Subgroup
272
            for (final Tuple2<Item, Integer> tuple : this.list) {
273
                final Item subGroup = tuple.get0();
274
                final Integer subGroupOrder = tuple.get1();
275
                x = subGroup.printColumns(builder, width, x, subGroupOrder, level + 1);
276
            }
277
        }
278
        return x;
279
    }
280
 
281
    @Override
282
    public boolean equalsDesc(Object obj) {
283
        if (this == obj)
284
            return true;
285
        if (!super.equalsDesc(obj))
286
            return false;
287
        if (getClass() != obj.getClass())
288
            return false;
289
        final Group other = (Group) obj;
290
        final int size = this.list.size();
291
        if (size != other.list.size())
292
            return false;
293
        for (int i = 0; i < size; i++) {
294
            final Tuple2<Item, Integer> thisChild = this.list.get(i);
295
            final Tuple2<Item, Integer> oChild = other.list.get(i);
296
            if (!thisChild.get1().equals(oChild.get1()) || !thisChild.get0().equalsDesc(oChild.get0()))
297
                return false;
298
        }
299
        return true;
300
    }
156 ilm 301
 
302
    public void setTabId(String tabId) {
303
        this.tabId = tabId;
304
    }
305
 
306
    public String getTabId() {
307
        return this.tabId;
308
    }
41 ilm 309
}