在Eclipse RCP项目的Part中更新表视图的正确方法是什么? [英] What is the right way to update a table view in a Part in a Eclipse RCP project?

查看:101
本文介绍了在Eclipse RCP项目的Part中更新表视图的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我的RCP项目中有一个 ItemListPart 类,并且其中有一个要显示的表,如下所示:

Say I have a ItemListPart class in my RCP project, and there is a table to be displayed in it as following:

import java.util.List;

import javax.annotation.PostConstruct;

import org.eclipse.e4.ui.di.Focus;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

public class ItemListPart {
    private Table table;

    @PostConstruct 
    public void createControls(Composite parent){
      //I wish the ItemList could be changed from outside the ItemListPart class.
      List<Item> ItemList = getItemList();

      parent.setLayout(new GridLayout(2, false));

      table = new Table(parent, SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION);
      table.setLinesVisible(true);
      table.setHeaderVisible(true);
      GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
      data.heightHint = 200;
      table.setLayoutData(data);

      String[] titles = { "Item Name", "Description"};
      for (int i = 0; i < titles.length; i++) {
          TableColumn column = new TableColumn(table, SWT.NONE);
          column.setText(titles[i]);
          table.getColumn(i).pack();
      }


      for(Item it:ItemList){
          TableItem item = new TableItem(table, SWT.NONE);
          item.setText(0, it.getName());
          item.setText(1, it.getDesc());
      }

      for (int i=0; i<titles.length; i++) {
          table.getColumn (i).pack ();
      }
    }

    @Focus
    public void onFocus() {
        table.setFocus();
    }
}

这里, ItemList< Item> 是一个列表,希望可以从 ItemListPart 类外部进行更改,并且每当 ItemList< Item>中的数据被更改时,更改后,可以根据更新的数据自动刷新表格视图.实现这个目标的正确方法是什么?谢谢.

Here the ItemList<Item> is a List which I wish could be changed from outside the ItemListPart class, and whenever the data in ItemList<Item> changed, the table view could be auto refreshed according to the updated data. What is the right way to achieve this goal? Thanks.

推荐答案

没有一种正确"的方法.

There is no one 'correct' way.

一种方法是使用事件代理,让视图部分侦听更新列表的代码中的事件.

One way is to use the event broker and have your view part listen for events from the code which updates the list.

在更新列表的代码中发布事件:

In the code which updates the list post an event:

@Inject
IEventBroker eventBroker;

...

eventBroker.post("/list/updated", Collections.EMPTY_MAP);

在您的视图中收听更新事件:

In your view listen for the update event:

@Inject
@Optional
public void listUpdated(@UIEventTopic("/list/updated") Event event)
{
  // TODO handle the update
}

注意: Event org.osgi.service.event.Event .

您可能会发现使用 TableViewer 而不是 Table 会更容易,因为单独的内容提供程序使更新表以更改内容更容易.

You will probably find it easier to use TableViewer rather than Table as the separate content provider makes it much easier to update the table for content changes.

您还可以使用以下方式在事件中传递数据:

You can also pass data in the event using something like:

MyClass myClass = ....

eventBroker.post("/list/updated", myClass);


@Inject
@Optional
public void listUpdated(@UIEventTopic("/list/updated") MyClass myClass)

有关事件代理的更多信息,请此处

There is some more information on event brokers here

这篇关于在Eclipse RCP项目的Part中更新表视图的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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