OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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