在基础模型更改后更新JFace TreeViewer [英] Updating the JFace TreeViewer after the underlying model changes

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

问题描述

我的视图中有一个树查看器,该树查看器从标准Ecore编辑器中监听EMF模型,并使用它做进一步的工作.我已经注册了一个选择侦听器,该侦听器将检查所选元素是否为树查看器需要作为输入的类型.因此,问题在于,如果模型中有任何更改(例如,向现有元素中添加新元素或新信息等),则树查看器仅在用户更改选择(即单击任何模型元素等)时显示更改后的模型.

I have a tree viewer in my view, which listens to EMF models from the standard Ecore editor and does further things with it. I have already registered a selection listener, which checks whether the selected elements are the types the tree viewer needs as input. So the problem is that if there any changes in the model (e.g. adding a new element or new information to an existing element etc.) the tree viewer shows the changed model only if the user changes the selection, i.e. clicks to any model element etc.

但是我需要做的是,如果基础模型发生更改,树查看器将直接收到通知,并且还可以显示新的模型元素,而不必单击模型以进行监听.

But what I need to do is, that the tree viewer gets directly notified if the underlying model changes and shows the new model element too without being have to click on the model to listen it.

我找到了以下蚀角文章( https://www .eclipse.org/articles/Article-TreeViewer/TreeViewerArticle.htm#inputChanged ),从响应更改"看来,inputChanged()和refresh()方法可能是我正在寻找的解决方案,是不是吗?

I have found the following eclipse corner article ( https://www.eclipse.org/articles/Article-TreeViewer/TreeViewerArticle.htm#inputChanged ) and from "Responding th change" it seems that the inputChanged() and refresh() methods might be the solution I am looking for, isn't it?

我仍然想知道是否有一种更简单的方法,而不必更改模型代码,而仅通过更改UI代码?谢谢!

Still I was wondering if there is maybe an easier way to do this without being have to change the model code, but only by making changes in the UI code? Thanks!

推荐答案

您可以调用TreeViewer refresh()方法来获取它以刷新模型中的整个树,或者refresh(Object)来刷新树以开始于给定的模型对象.

You can call the TreeViewer refresh() method to get it to refresh the whole tree from the model, or refresh(Object) to refresh the tree starting at the given model object.

如果树结构没有更改,则可以调用update(Object)来更新单个对象的显示.

If the tree structure has not changed you can call update(Object) to just update the display of a single object.

在模型树中添加和删除对象时,还有addremove方法.

There are also add and remove methods for when you add and remove objects from the model tree.

某些方法也具有Object []变体,因此您可以一次修改多个对象.

Some of the methods also have Object [] variants so you can modify several objects at once.

更新:

您的模型应支持生成模型更改的事件,内容提供商可以收听该事件.您可以在内容提供者inputChanged方法中设置此侦听器,并在dispose方法中将其删除.收到模型更改事件后,请使用各种TreeViewer方法来更新树.

Your model should support generating a model changed event which the content provider can listen to. You would set up this listener in the content provider inputChanged method and remove it in the dispose method. When model change events are received use the various TreeViewer methods to update the tree.

如何使用所有这些示例是Eclipse视图,该视图显示工作空间中的文件(例如Navigator视图).这些内容提供者使用工作空间资源更改侦听器(IResourceChangeListener)来通知工作空间更改,并使用事件中的信息调用我上面列出的方法来更新树.

An example of how all this is used are the Eclipse views which show the files in the workspace (such as the Navigator view). The content provider for these uses the workspace resource change listener (IResourceChangeListener) to be notified of changes to the workspace and using the information in the event calls the methods I listed above to update the tree.

更新2: 从org.eclipse.ui.views.tasklist.TaskListContentProvider

class TaskListContentProvider 
      implements IStructuredContentProvider, IResourceChangeListener 
{
  private TableViewer viewer;

  private IResource input;

  ... other methods ....

  public void dispose() {
    if (input != null) {
        input.getWorkspace().removeResourceChangeListener(this);
        input = null;
    }
  }


  public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
    if (input != null) {
        input.getWorkspace().removeResourceChangeListener(this);
    }

    input = (IResource) newInput;

    if (input != null) {
        input.getWorkspace().addResourceChangeListener(this, IResourceChangeEvent.POST_CHANGE);
    }

    viewer = (TableViewer) viewer;
  }

  public void resourceChanged(IResourceChangeEvent event) {

    ... use resource change event to update viewer
  }
}

这篇关于在基础模型更改后更新JFace TreeViewer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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