Dépôt officiel du code source de l'ERP OpenConcerto
Rev 180 | Blame | Compare with Previous | Last modification | View Log | RSS feed
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
*
* The contents of this file are subject to the terms of the GNU General Public License Version 3
* only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
* copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each file.
*/
package org.openconcerto.ui.group;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import net.jcip.annotations.GuardedBy;
public class Item {
public static Item copy(final Item item, final Group newParent) {
return item instanceof Group ? Group.copy((Group) item, newParent) : new Item(item, newParent);
}
private String id;
@GuardedBy("this")
private boolean frozen;
private Group parent;
private LayoutHints localHint;
public Item(final String id) {
this(id, null);
}
public Item(final String id, final LayoutHints hint) {
this.id = id.trim();
this.frozen = false;
this.setLocalHint(hint);
}
public Item(final Item item, final Group newParent) {
this.id = item.id;
// no need to copy if frozen
this.frozen = false;
this.parent = newParent;
this.setLocalHint(item.localHint);
}
public final String getId() {
return this.id;
}
public final void setId(final String id) {
checkFrozen("setId");
this.id = id;
}
public final synchronized boolean isFrozen() {
return this.frozen;
}
protected final void checkFrozen(String op) {
if (this.isFrozen())
throw new IllegalStateException("Frozen cannot " + op);
}
public synchronized final void freeze() {
if (this.getParent() != null)
throw new IllegalStateException("Can only freeze from the root down");
this._freeze();
}
protected synchronized void _freeze() {
this.frozen = true;
}
public final Item getChildFromID(final String id) {
return this.getDescFromID(id, 1);
}
/**
* Get all descendant leaves, including this.
*
* @return the descendant non-group.
*/
public Collection<Item> getDescendantItems() {
return Collections.singletonList(this);
}
protected void getDescendantItems(final Collection<Item> res) {
res.add(this);
}
public final Item getDescFromID(final String id) {
return this.getDescFromID(id, -1);
}
public Item getDescFromID(final String id, final int maxLevel) {
return this.getId().equals(id) ? this : null;
}
public Group getParent() {
return this.parent;
}
final void setParent(Group parent) {
checkFrozen("setParent");
this.parent = parent;
}
public LayoutHints getLocalHint() {
return this.localHint;
}
public void setLocalHint(final LayoutHints localHint) {
checkFrozen("setLocalHint");
this.localHint = new LayoutHints(localHint == null ? LayoutHints.DEFAULT_FIELD_HINTS : localHint);
}
public final void applyUntilRoot(final Consumer<Item> cons) {
Item current = this;
while (current != null) {
cons.accept(current);
current = current.getParent();
}
}
public final List<String> getAbsolutePath() {
final LinkedList<String> res = new LinkedList<>();
this.applyUntilRoot((item) -> res.addFirst(item.getId()));
return res;
}
public final Group getRoot() {
final AtomicReference<Item> current = new AtomicReference<>();
this.applyUntilRoot((item) -> current.set(item));
return current.get() instanceof Group ? (Group) current.get() : null;
}
protected void printTree(final StringBuilder builder, final int localOrder, final int level) {
for (int i = 0; i < level - 1; i++) {
builder.append(" ");
}
builder.append("+-- ");
builder.append(localOrder + " " + this.getId() + " [" + this.localHint + "]\n");
}
protected int printColumns(final StringBuilder builder, final int width, int x, final int localOrder, final int level) {
if (this.localHint.largeWidth() && x > 0) {
builder.append("\n");
x = 0;
}
// print a leaf
builder.append(" (" + x + ")");
builder.append(localOrder + " " + this.getId() + "[" + this.localHint + "]");
x++;
if (x >= width || this.localHint.largeWidth()) {
builder.append("\n");
x = 0;
}
return x;
}
@Override
public String toString() {
return this.getClass().getSimpleName() + " '" + this.getId() + "'";
}
public boolean equalsDesc(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final Item other = (Item) obj;
return this.id.equals(other.id) && this.localHint.equals(other.localHint);
}
}