Gwt CellTree isLeaf() 问题

发布于 2024-11-30 20:05:02 字数 2294 浏览 4 评论 0原文

我正在尝试按照基本的 CellTree gwt 示例显示类别树。

我所坚持的是确定类别的“叶子”条件。

当一个类别没有子项时,它就“是一片叶子”,对吧?所以,这是我的类别(我使用 Objectify 进行 appengine 持久性):

@Entity
public class Categoria implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    Long id;

    String nome;    
    Key<Categoria> parent;

    public Categoria() { }

    public Categoria(String nome) {
        super();
        this.nome = nome;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public Key<Categoria> getParent() {
        return parent;
    }

    public void setParent(Key<Categoria> parent) {
        this.parent = parent;
    }
}

我的 TreeViewModel 基于 AsyncDataProvider (我从外部传递):

public class CategorieTreeViewModel implements TreeViewModel {

    private AbstractDataProvider<Categoria> dataProvider;

    public CategorieTreeViewModel(AbstractDataProvider<Categoria> dataProvider) {
        this.dataProvider = dataProvider;
    }

    @Override
    public <T> NodeInfo<?> getNodeInfo(T value) {
        return new DefaultNodeInfo<Categoria>(dataProvider, new CategoriaCell());
    }

    @Override
    public boolean isLeaf(Object value) {
        return false;
    }
}

所以这里是:

dataProvider = new AsyncDataProvider<Categoria>() {         
            @Override
            protected void onRangeChanged(HasData<Categoria> display) {
                updateTree();
            }
        };

private void updateTree() {
        rpcService.getCategorie(new AsyncCallback<Categoria[]>() {
            @Override
            public void onSuccess(Categoria[] result) {
                dataProvider.updateRowCount(result.length, true);
                dataProvider.updateRowData(0, Arrays.asList(result));
            }
            @Override
            public void onFailure(Throwable caught) {
                Window.alert(caught.toString());
            }
        });
    }

问题是:因为我没有“叶子属性”我的类别 bean,我怎么知道它是否有孩子?显然是进行了查询,但是 isLeaf 方法需要同步返回,我该如何进行 rpc 调用?

或者我可以在 getCategorie() 调用中检索“叶子”信息,在运行时填充属性,但这可能是一个性能问题。

我能做些什么?

I'm trying to display a tree of Categories, following the basic CellTree gwt examples.

What I am stuck at is determining the "leaf" condition of a Category.

A Category "is-a-leaf" when it hasn't children, right? So, here's my Category (I am using Objectify for appengine persistence):

@Entity
public class Categoria implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    Long id;

    String nome;    
    Key<Categoria> parent;

    public Categoria() { }

    public Categoria(String nome) {
        super();
        this.nome = nome;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public Key<Categoria> getParent() {
        return parent;
    }

    public void setParent(Key<Categoria> parent) {
        this.parent = parent;
    }
}

My TreeViewModel is based on AsyncDataProvider (which I pass from outside):

public class CategorieTreeViewModel implements TreeViewModel {

    private AbstractDataProvider<Categoria> dataProvider;

    public CategorieTreeViewModel(AbstractDataProvider<Categoria> dataProvider) {
        this.dataProvider = dataProvider;
    }

    @Override
    public <T> NodeInfo<?> getNodeInfo(T value) {
        return new DefaultNodeInfo<Categoria>(dataProvider, new CategoriaCell());
    }

    @Override
    public boolean isLeaf(Object value) {
        return false;
    }
}

So here it is:

dataProvider = new AsyncDataProvider<Categoria>() {         
            @Override
            protected void onRangeChanged(HasData<Categoria> display) {
                updateTree();
            }
        };

private void updateTree() {
        rpcService.getCategorie(new AsyncCallback<Categoria[]>() {
            @Override
            public void onSuccess(Categoria[] result) {
                dataProvider.updateRowCount(result.length, true);
                dataProvider.updateRowData(0, Arrays.asList(result));
            }
            @Override
            public void onFailure(Throwable caught) {
                Window.alert(caught.toString());
            }
        });
    }

The question is: since I don't have a "leaf property" on my Category bean, how can I know if it has children or not? By doing a query obviously, but the isLeaf method needs to return synchronously, how can I make my rpc call?

Or I can retrieve that "leaf" information in the getCategorie() call, filling the property at runtime, but this could be a performance problem.

What can I do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无人接听 2024-12-07 20:05:02

我会向 Category 类添加一个瞬态属性,即一个布尔值 isLeaf,然后在 setParent 方法中,您可以将父级的 isLeaf 属性设置为false(因为如果 this 对象有 that 的父对象,则 that 对象不是叶子)。使属性成为瞬态意味着它不会被持久化,因此您不必担心数据模型中是否存在该字段。

编辑:这是我对 Categoria 类的 setParent 方法进行编码的方式...

public void setParent(Key<Categoria> parent) {
    this.parent = parent;
    parent.setIsLeaf(false);
}

这样,一旦您建立了 Categoria 节点的模型,他们每个人都知道它是否是一片叶子。这是有效的,因为如果 parentthis 作为子项,则 parent 不可能是叶子。将 isLeaf 属性默认为 true,只需检查其属性即可知道任何给定的 Categoria 是否为叶子。

I would add a transient property to the Categoria class, a boolean isLeaf, then inside the setParent method, you could set the parent's isLeaf property to false (because if this object has a parent of that, then that object is not a leaf). Making the property transient means it won't be persisted, so you don't have to worry about having that field in your data model.

EDIT: Here is how I would code the Categoria class's setParent method...

public void setParent(Key<Categoria> parent) {
    this.parent = parent;
    parent.setIsLeaf(false);
}

That way, once you have built up your model of Categoria nodes, each one of them knows whether it is a leaf or not. This works because if parent has this as a child, parent can't possibly be a leaf. Default the isLeaf property to true and you'll know if any given Categoria is a leaf just by checking it's property.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文