模型更改时是否更新Eclipse JFace Treeviewer? [英] Updating Eclipse JFace Treeviewer when model changes?

查看:78
本文介绍了模型更改时是否更新Eclipse JFace Treeviewer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用TreeViewer开发RCP应用程序.虽然有很多文章可以解释如何向查看器添加编辑支持(以及如何在模型中更新视图的更改),但是当底层模型发生更改时,我发现没有太多更新Treeview的内容.简而言之,我的问题:

I am developing a RCP application with a TreeViewer. While there are good number of articles to explain how to add editing support to the Viewer (and how changes in view are updated in the model), I don't find much for updating the Treeview when the underlaying model changes. my question in short:

TreeView ---->模型更新------有很多示例

TreeView ----> Model updation ------ there are lots of examples

模型---->更新Treeview -----这是我的问题

Model ----> Treeview updation ----- this is my question

这是我尝试过的,并且有效.请发表评论

This is what I tried and it works. comments please

viewer.getTree().addKeyListener(new KeyListener(){

    @Override
    public void keyPressed(KeyEvent e) {

    }

    @Override
    public void keyReleased(KeyEvent e) {

        if(e.keyCode==SWT.F3){
            System.out.println("F3 pressed... new element will be added");
            TreeParent root = (TreeParent) viewer.getInput();
            TreeParent activityRoot = (TreeParent) root.getChildren()[0];
            activityRoot.addChild(new TreeObject("NEW_ACTIVITY"));
            //viewer.update(root, null);
            viewer.refresh();
        }

    }

    });

推荐答案

数据模型由您的content provider提供,TreeViewer不提供任何更改此数据的方法-您必须自己编写代码.更改模型后,可以使用以下方法将更改告知TreeViewer:

The data model is provided by your content provider, TreeViewer does not provide any means of changing this data - you must do that it your own code. When you have changed to model you can use the following methods to tell the TreeViewer about the change:

如果您刚刚更改了树用途中单个项目需要显示的内容

If you have just changed what needs to be shown for a single item in the tree use

TreeViewer.update(object, null);

以更新树中的该项目.还有一个数组版本可以更新多个对象.

to get that item in the tree updated. There is also an array version of this to update multiple objects.

如果您在树中添加或删除了对象,请使用

If you have added or removed objects in the tree use

TreeViewer.refresh();

重建整个树或

TreeViewer.refresh(object);

要刷新树的一部分,请从object开始.

to refresh the part of the tree start at object.

要告诉树上有关添加和删除对象的信息

To tell the tree about adding and removing objects there are

TreeViewer.add(parent, object);
TreeViewer.remove(object);

还有这些的数组变体.

为帮助TreeViewer查找对象调用

To help the TreeViewer find the objects call

TreeViewer.setUseHashlookup(true);

(必须在TreeViewer.setInput之前调用).由于此操作使用哈希表,因此对象应具有明智的hashCodeequals方法.您还可以使用TreeViewer.setComparer指定其他类来进行哈希码和比较.

(must be called before TreeViewer.setInput). Since this uses a hash table the objects should have sensible hashCode and equals methods. You can also use TreeViewer.setComparer to specify a different class to do the hash code and comparison.

这篇关于模型更改时是否更新Eclipse JFace Treeviewer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆