Peduz
Peduz

Reputation: 25

Set widget to TreeItem on gwt

I have to implements a tree on gwt. I'm implementing an object that extends Widget to set it on a TreeItem.

The problem is that if I set on a TreeItem this object (that have only Strings and Integers fields) I can't see anymore the tree.

Where is the problem? I can't see it.

private Tree frontEndTree;

private void buildTree(HashMap<Long, Categoria> categoriaMap) {
    for(Categoria categoria : categoriaMap.values()) {

        if(categoria.getLevel() != 1) continue;

        TreeItem firstLevel = //this.frontEndTree.addTextItem(categoria.getDescription());
        this.frontEndTree.addTextItem(categoria.getDescription());
        ItemTree itemTree = new ItemTree(categoria);
        firstLevel.setWidget((Widget)itemTree);
        this.visitaAlbero(firstLevel, categoria.getDescendants());
    }
} 
private void visitaAlbero(TreeItem parentItem, ArrayList<Categoria> categorie) {
    if(categorie == null) return;
    if(categorie.isEmpty()) return;

    TreeItem node = null;
    for(Categoria item : categorie) {
        if(node == null) {
            node = parentItem.addTextItem(item.getDescription());
            node.setWidget(new ItemTree(item));
            visitaAlbero(node, item.getDescendants());
            continue;
        }
        node.addTextItem(item.getDescription());
        node.setWidget(new ItemTree(item));
        visitaAlbero(node, item.getDescendants());
    }
}



/**
 * Inner class che gestisce il widget principale
 * del treeitem 
 * 
 * @author Giuseppe Pedullà
 *
 */
public class ItemTree extends Widget implements Serializable, IsWidget {

    /**
     * 
     */
    private static final long serialVersionUID = 5347332077472260376L;

    private Long id;

    private String description;

    private Integer level;

    public ItemTree(Categoria categoria) {
        this.id = categoria.getId();
        this.description = categoria.getDescription();
        this.level = categoria.getLevel();
    }
    public String getDescription() {
        String valueID = "";
        if(id < 10) {
            valueID = "00" + id;
        } else if(id < 100) {
            valueID = "0" + id;
        } else {
            valueID = "" + id;
        }
        return valueID + " - " + description;
    }

    //GETTERS
    public Long getId() {
        return id;
    }
    public Integer getLevel() {
        return level;
    }
    @Override
    public String toString() {
        return description;
    }   
} 

Upvotes: 1

Views: 223

Answers (1)

Adam
Adam

Reputation: 5599

The problem is in your ItemTree class. It extends Widget but does not set any element for that widget. At least it lacks asWidget method. It could be for example like this:

@Override
public Widget asWidget() {
    return new Label(getDescription());
}

And you could use it like this:

firstLevel.setWidget(itemTree.asWidget());

The easier method is to extend Composite instead of Widget. You would only need to call initWidget method in the constructor:

public class ItemTree extends Composite implements Serializable, IsWidget {

    ...

    public ItemTree(Categoria categoria) {
        this.id = categoria.getId();
        this.description = categoria.getDescription();
        this.level = categoria.getLevel();
        initWidget(new Label(getDescription()));    // <- !!!
    }

    ...
}

You can initWidget with whatever Widget you want - Label is the simplest working example - I have successfully tested it.

Upvotes: 1

Related Questions