OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 180 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
67 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
182 ilm 4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
67 ilm 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
 
83 ilm 16
import java.util.Collection;
17
import java.util.Collections;
180 ilm 18
import java.util.LinkedList;
19
import java.util.List;
20
import java.util.concurrent.atomic.AtomicReference;
21
import java.util.function.Consumer;
83 ilm 22
 
73 ilm 23
import net.jcip.annotations.GuardedBy;
67 ilm 24
 
25
public class Item {
73 ilm 26
 
27
    public static Item copy(final Item item, final Group newParent) {
28
        return item instanceof Group ? Group.copy((Group) item, newParent) : new Item(item, newParent);
29
    }
30
 
67 ilm 31
    private String id;
73 ilm 32
    @GuardedBy("this")
33
    private boolean frozen;
34
    private Group parent;
35
    private LayoutHints localHint;
67 ilm 36
 
73 ilm 37
    public Item(final String id) {
38
        this(id, null);
67 ilm 39
    }
40
 
73 ilm 41
    public Item(final String id, final LayoutHints hint) {
67 ilm 42
        this.id = id.trim();
73 ilm 43
        this.frozen = false;
44
        this.setLocalHint(hint);
67 ilm 45
    }
46
 
73 ilm 47
    public Item(final Item item, final Group newParent) {
48
        this.id = item.id;
49
        // no need to copy if frozen
50
        this.frozen = false;
51
        this.parent = newParent;
52
        this.setLocalHint(item.localHint);
67 ilm 53
    }
54
 
73 ilm 55
    public final String getId() {
56
        return this.id;
57
    }
58
 
59
    public final void setId(final String id) {
60
        checkFrozen("setId");
67 ilm 61
        this.id = id;
62
    }
63
 
73 ilm 64
    public final synchronized boolean isFrozen() {
65
        return this.frozen;
66
    }
67
 
68
    protected final void checkFrozen(String op) {
69
        if (this.isFrozen())
70
            throw new IllegalStateException("Frozen cannot " + op);
71
    }
72
 
180 ilm 73
    public synchronized final void freeze() {
74
        if (this.getParent() != null)
75
            throw new IllegalStateException("Can only freeze from the root down");
76
        this._freeze();
77
    }
78
 
79
    protected synchronized void _freeze() {
73 ilm 80
        this.frozen = true;
81
    }
82
 
83
    public final Item getChildFromID(final String id) {
84
        return this.getDescFromID(id, 1);
85
    }
86
 
83 ilm 87
    /**
88
     * Get all descendant leaves, including this.
89
     *
90
     * @return the descendant non-group.
91
     */
92
    public Collection<Item> getDescendantItems() {
93
        return Collections.singletonList(this);
94
    }
95
 
96
    protected void getDescendantItems(final Collection<Item> res) {
97
        res.add(this);
98
    }
99
 
73 ilm 100
    public final Item getDescFromID(final String id) {
101
        return this.getDescFromID(id, -1);
102
    }
103
 
104
    public Item getDescFromID(final String id, final int maxLevel) {
105
        return this.getId().equals(id) ? this : null;
106
    }
107
 
67 ilm 108
    public Group getParent() {
73 ilm 109
        return this.parent;
67 ilm 110
    }
111
 
73 ilm 112
    final void setParent(Group parent) {
113
        checkFrozen("setParent");
114
        this.parent = parent;
115
    }
116
 
67 ilm 117
    public LayoutHints getLocalHint() {
73 ilm 118
        return this.localHint;
67 ilm 119
    }
120
 
73 ilm 121
    public void setLocalHint(final LayoutHints localHint) {
122
        checkFrozen("setLocalHint");
123
        this.localHint = new LayoutHints(localHint == null ? LayoutHints.DEFAULT_FIELD_HINTS : localHint);
67 ilm 124
    }
125
 
180 ilm 126
    public final void applyUntilRoot(final Consumer<Item> cons) {
73 ilm 127
        Item current = this;
180 ilm 128
        while (current != null) {
129
            cons.accept(current);
73 ilm 130
            current = current.getParent();
67 ilm 131
        }
132
    }
133
 
180 ilm 134
    public final List<String> getAbsolutePath() {
135
        final LinkedList<String> res = new LinkedList<>();
136
        this.applyUntilRoot((item) -> res.addFirst(item.getId()));
137
        return res;
138
    }
139
 
140
    public final Group getRoot() {
141
        final AtomicReference<Item> current = new AtomicReference<>();
142
        this.applyUntilRoot((item) -> current.set(item));
143
        return current.get() instanceof Group ? (Group) current.get() : null;
144
    }
145
 
73 ilm 146
    protected void printTree(final StringBuilder builder, final int localOrder, final int level) {
67 ilm 147
        for (int i = 0; i < level - 1; i++) {
148
            builder.append("  ");
149
        }
150
 
151
        builder.append("+-- ");
73 ilm 152
        builder.append(localOrder + " " + this.getId() + " [" + this.localHint + "]\n");
67 ilm 153
    }
154
 
73 ilm 155
    protected int printColumns(final StringBuilder builder, final int width, int x, final int localOrder, final int level) {
156
        if (this.localHint.largeWidth() && x > 0) {
67 ilm 157
            builder.append("\n");
158
            x = 0;
159
        }
160
        // print a leaf
161
        builder.append(" (" + x + ")");
73 ilm 162
        builder.append(localOrder + " " + this.getId() + "[" + this.localHint + "]");
67 ilm 163
 
73 ilm 164
        x++;
165
        if (x >= width || this.localHint.largeWidth()) {
67 ilm 166
            builder.append("\n");
167
            x = 0;
168
        }
169
        return x;
170
    }
171
 
172
    @Override
173
    public String toString() {
73 ilm 174
        return this.getClass().getSimpleName() + " '" + this.getId() + "'";
67 ilm 175
    }
73 ilm 176
 
177
    public boolean equalsDesc(Object obj) {
178
        if (this == obj)
179
            return true;
180
        if (obj == null)
181
            return false;
182
        if (getClass() != obj.getClass())
183
            return false;
184
        final Item other = (Item) obj;
185
        return this.id.equals(other.id) && this.localHint.equals(other.localHint);
186
    }
67 ilm 187
}