ZK内部类树属性不可读

时间:2014-01-24 17:38:07

标签: java ajax properties tree zk

我遇到与此问题几乎相同的问题: Property not readable on a inner class in a TreeModel

但同样的解决方案似乎不适用。

我有一棵树,当我实现我的实体和我的TreeModel时,他们工作正常,在我的.zul文件中,我的“数据”属性在树状单元格中找不到。

以下是一些代码:

- 结构实现,运行@AfterCompose:

 private void montarEstrutura(){

    Unidade ub1 = new Unidade();
    ub1.setParent(null);
    ub1.setNumero("NBase");
    ub1.setDescricao("DBase");

    Unidade ub2 = new Unidade();
    ub2.setParent(ub1);
    ub2.setNumero("N002");
    ub2.setDescricao("D002");

        Unidade ub4 = new Unidade();
        ub4.setParent(ub2);
        ub4.setNumero("N004");
        ub4.setDescricao("D004");

            Unidade ub6 = new Unidade();
            ub6.setParent(ub4);
            ub6.setNumero("N006");
            ub6.setDescricao("D006");

            Unidade ub7 = new Unidade();
            ub7.setParent(ub4);
            ub7.setNumero("N007");
            ub7.setDescricao("D007");

            Unidade ub8 = new Unidade();
            ub8.setParent(ub4);
            ub8.setNumero("N008");
            ub8.setDescricao("D008");

        Unidade ub5 = new Unidade();
        ub5.setParent(ub2);
        ub5.setNumero("N005");
        ub5.setDescricao("D005");

    Unidade ub3 = new Unidade();
    ub3.setParent(ub1);
    ub3.setNumero("N003");
    ub3.setDescricao("D003");


    List<Unidade> lub3 = new ArrayList<Unidade>();
    lub3.add(ub6);
    lub3.add(ub7);
    lub3.add(ub8);

    ub4.setUnidades(lub3);

    List<Unidade> lub2 = new ArrayList<Unidade>();
    lub2.add(ub4);
    lub2.add(ub5);
    ub2.setUnidades(lub2);

    List<Unidade> lub1 = new ArrayList<Unidade>();
    lub1.add(ub2);
    lub1.add(ub3);
    ub1.setUnidades(lub1);

    unidadeTreeModel = new UnidadeTreeModel(ub1);
    tree.setModel(unidadeTreeModel);

}

- 实体很长,所以我会削减它:

@Entity
@Audited
public class Unidade extends AbstractSuseTools{

private String numero;
private String descricao;

    @OneToMany
private List<Unidade> unidades = new LinkedList<Unidade>();

    public String getNumero() {
    return numero;
}

public void setNumero(String numero) {
    this.numero = numero;
}

public String getDescricao() {
    return descricao;
}

public void setDescricao(String descricao) {
    this.descricao = descricao;
}
public List<Unidade> getUnidades() {
    return unidades;
}

public void setUnidades(List<Unidade> unidades) {
    this.unidades = unidades;
}
}

这是.zul文件,它不读取实体的属性:

<div apply="org.zkoss.bind.BindComposer"
     viewModel="@id('vm') @init('br.com.simplesmenteuse.viewmodel.basico.OrganizacaoViewModel')"    >
   <tree id="tree" model="@load(vm.unidadeTreeModel)" selectedItem="@bind(vm.unidadeSelected)" >
        <treecols>
            <treecol width="100px" label="Editar" />
            <treecol width="100px" label="Excluir" />
            <treecol hflex="4" label="Número" />
            <treecol hflex="1" label="Nome" />
        </treecols>
        <template name="model">
            <treeitem>
                <treerow>
                    <treecell label="Editar"/>
                    <treecell label="Excluir"/>
                    <treecell label="@load(each.numero)"/>
                    <treecell label="@load(each.descricao)"/>
                </treerow>
            </treeitem>
        </template>
    </tree>

我的TreeModel:

    public class UnidadeTreeModel extends AbstractTreeModel<Unidade> {
    /**
     *
     */
    private static final long serialVersionUID = 3854683349236034878L;

    public UnidadeTreeModel(Unidade unidade){
        super(unidade);
    }

    public boolean isLeaf(Unidade node) {
        return getLevel(node.toString()) >= 4; //at most 4 levels;
    }
    public Unidade getChild(Unidade parent, int index) {
        if(((Unidade)parent).getUnidades() != null && ((Unidade)parent).getUnidades().size() > index)
            return ((Unidade)parent).getUnidades().get(index);
        else
            return null;
    }
    public int getChildCount(Unidade parent) {
        return (((Unidade)parent).getUnidades() != null)?((Unidade)parent).getUnidades().size():0;
    }
    public int getIndexOfChild(Unidade parent, Unidade child) {
        for (int i  = 0; i<parent.getUnidades().size();i++) {
            if (child.equals(parent.getUnidades().get(i))) { return i;}
        }
        return -1; //or throw exception what you want.
    }
    private int getLevel(String data) {
        for (int i = -1, level = 0;; ++level)
            if ((i = data.indexOf('.', i + 1)) < 0)
                return level;
    }

};

错误:

  

现在它没有显示任何错误,只是没有得到对象的子级别。

我真的不知道这一切是由树完成的,所以我无法理解为什么它不可读。

2 个答案:

答案 0 :(得分:2)

只需删除zul中的这些数据:

 <treecell label="@load(each.numero)"/>
   <treecell label="@load(each.nome)"/>

答案 1 :(得分:1)

这是正常的(因为你没有实现toString)。

现在问题出在您的模型中(这是测试)。 我想改变的第一件事是:

public class UnidadeTreeModel extends AbstractTreeModel<Object> {

public class UnidadeTreeModel extends AbstractTreeModel<Unidade> {

这将帮助您完成所有的铸件并获得更好的清洁代码。 您可以使用模型的新代码编辑问题吗?这应该有助于我更快地检查代码。

Oke我确实读了你的代码。 我有一个问题:

public int getIndexOfChild(Object parent, Object child) {
    String data = (String)child;
    int i = data.lastIndexOf('.');
    return Integer.parseInt(data.substring(i + 1));
}

你在那做什么?字符串数据将保留adres(因为您没有toString)

现在尝试将其更改为:

 public int getIndexOfChild(Unidade parent, Unidade child) {
    for (int i  = 0; i<parent.getUnidades().size();i++) {
        if (child.equals(parent.getUnidades().get(i))) { return i;}
    }
    return -1; //or throw exception what you want.
}

对于isLeaf(我不知道第4级怎么样,但也许你可以用root检查?)

public boolean isLeaf(Unidade node) {
    return getRoot().equals(node);    
}

更新最新问题:

这用于在按下按钮后展开树:

private void doCollapseExpandAll(Component component, boolean aufklappen) {
    if (component instanceof Treeitem) {
        Treeitem treeitem = (Treeitem) component;
        treeitem.setOpen(aufklappen);
    }
    Collection<?> com = component.getChildren();
    if (com != null) {
        for (Iterator<?> iterator = com.iterator(); iterator.hasNext();) {
            doCollapseExpandAll((Component) iterator.next(), aufklappen);

        }
    }
}

public void fullyExpandTree(Component root){
    doCollapseExpandAll(root, true);
}

现在你不必一直走,只计数到4.我知道你足够聪明,可以更新代码;)。

Greetz chill。