OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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